1111
1212from contextlib import ExitStack
1313import copy
14+ import enum
1415import itertools
1516from numbers import Integral , Number
1617
@@ -3149,6 +3150,13 @@ def onselect(eclick: MouseEvent, erelease: MouseEvent)
31493150 """
31503151
31513152
3153+ class _RectangleSelectorState (enum .Enum ):
3154+ ROTATE = enum .auto ()
3155+ MOVE = enum .auto ()
3156+ RESIZE = enum .auto ()
3157+ CREATE = enum .auto ()
3158+
3159+
31523160@_docstring .Substitution (_RECTANGLESELECTOR_PARAMETERS_DOCSTRING .replace (
31533161 '__ARTIST_NAME__' , 'rectangle' ))
31543162class RectangleSelector (_SelectorWidget ):
@@ -3283,18 +3291,17 @@ def _press(self, event):
32833291 self ._set_aspect_ratio_correction ()
32843292
32853293 match self ._get_action ():
3286- case "rotate" :
3294+ case _RectangleSelectorState . ROTATE :
32873295 # TODO: set to a rotate cursor if possible?
32883296 pass
3289- case "move" :
3297+ case _RectangleSelectorState . MOVE :
32903298 self ._set_cursor (backend_tools .cursors .MOVE )
3291- case "resize" :
3299+ case _RectangleSelectorState . RESIZE :
32923300 # TODO: set to a resize cursor if possible?
32933301 pass
3294- case "create" :
3302+ case _RectangleSelectorState . CREATE :
32953303 self ._set_cursor (backend_tools .cursors .SELECT_REGION )
32963304
3297-
32983305 return False
32993306
33003307 def _release (self , event ):
@@ -3346,18 +3353,15 @@ def _release(self, event):
33463353 return False
33473354
33483355 def _get_action (self ):
3349- """
3350- Return one of "rotate", "move", "resize", "create"
3351- """
33523356 state = self ._state
33533357 if 'rotate' in state and self ._active_handle in self ._corner_order :
3354- return 'rotate'
3358+ return _RectangleSelectorState . ROTATE
33553359 elif self ._active_handle == 'C' :
3356- return 'move'
3360+ return _RectangleSelectorState . MOVE
33573361 elif self ._active_handle :
3358- return 'resize'
3362+ return _RectangleSelectorState . RESIZE
33593363
3360- return 'create'
3364+ return _RectangleSelectorState . CREATE
33613365
33623366
33633367 def _onmove (self , event ):
@@ -3377,7 +3381,7 @@ def _onmove(self, event):
33773381 action = self ._get_action ()
33783382
33793383 xdata , ydata = self ._get_data_coords (event )
3380- if action == "resize" :
3384+ if action == _RectangleSelectorState . RESIZE :
33813385 inv_tr = self ._get_rotation_transform ().inverted ()
33823386 xdata , ydata = inv_tr .transform ([xdata , ydata ])
33833387 eventpress .xdata , eventpress .ydata = inv_tr .transform (
@@ -3397,7 +3401,7 @@ def _onmove(self, event):
33973401
33983402 x0 , x1 , y0 , y1 = self ._extents_on_press
33993403 # rotate an existing shape
3400- if action == "rotate" :
3404+ if action == _RectangleSelectorState . ROTATE :
34013405 # calculate angle abc
34023406 a = (eventpress .xdata , eventpress .ydata )
34033407 b = self .center
@@ -3406,7 +3410,7 @@ def _onmove(self, event):
34063410 np .arctan2 (a [1 ]- b [1 ], a [0 ]- b [0 ]))
34073411 self .rotation = np .rad2deg (self ._rotation_on_press + angle )
34083412
3409- elif action == "resize" :
3413+ elif action == _RectangleSelectorState . RESIZE :
34103414 size_on_press = [x1 - x0 , y1 - y0 ]
34113415 center = (x0 + size_on_press [0 ] / 2 , y0 + size_on_press [1 ] / 2 )
34123416
@@ -3457,7 +3461,7 @@ def _onmove(self, event):
34573461 sign = np .sign (xdata - x0 )
34583462 x1 = x0 + sign * abs (y1 - y0 ) * self ._aspect_ratio_correction
34593463
3460- elif action == "move" :
3464+ elif action == _RectangleSelectorState . MOVE :
34613465 x0 , x1 , y0 , y1 = self ._extents_on_press
34623466 dx = xdata - eventpress .xdata
34633467 dy = ydata - eventpress .ydata
0 commit comments