11
11
12
12
from contextlib import ExitStack
13
13
import copy
14
+ import enum
14
15
import itertools
15
16
from numbers import Integral , Number
16
17
@@ -3149,6 +3150,13 @@ def onselect(eclick: MouseEvent, erelease: MouseEvent)
3149
3150
"""
3150
3151
3151
3152
3153
+ class _RectangleSelectorState (enum .Enum ):
3154
+ ROTATE = enum .auto ()
3155
+ MOVE = enum .auto ()
3156
+ RESIZE = enum .auto ()
3157
+ CREATE = enum .auto ()
3158
+
3159
+
3152
3160
@_docstring .Substitution (_RECTANGLESELECTOR_PARAMETERS_DOCSTRING .replace (
3153
3161
'__ARTIST_NAME__' , 'rectangle' ))
3154
3162
class RectangleSelector (_SelectorWidget ):
@@ -3283,18 +3291,17 @@ def _press(self, event):
3283
3291
self ._set_aspect_ratio_correction ()
3284
3292
3285
3293
match self ._get_action ():
3286
- case "rotate" :
3294
+ case _RectangleSelectorState . ROTATE :
3287
3295
# TODO: set to a rotate cursor if possible?
3288
3296
pass
3289
- case "move" :
3297
+ case _RectangleSelectorState . MOVE :
3290
3298
self ._set_cursor (backend_tools .cursors .MOVE )
3291
- case "resize" :
3299
+ case _RectangleSelectorState . RESIZE :
3292
3300
# TODO: set to a resize cursor if possible?
3293
3301
pass
3294
- case "create" :
3302
+ case _RectangleSelectorState . CREATE :
3295
3303
self ._set_cursor (backend_tools .cursors .SELECT_REGION )
3296
3304
3297
-
3298
3305
return False
3299
3306
3300
3307
def _release (self , event ):
@@ -3346,18 +3353,15 @@ def _release(self, event):
3346
3353
return False
3347
3354
3348
3355
def _get_action (self ):
3349
- """
3350
- Return one of "rotate", "move", "resize", "create"
3351
- """
3352
3356
state = self ._state
3353
3357
if 'rotate' in state and self ._active_handle in self ._corner_order :
3354
- return 'rotate'
3358
+ return _RectangleSelectorState . ROTATE
3355
3359
elif self ._active_handle == 'C' :
3356
- return 'move'
3360
+ return _RectangleSelectorState . MOVE
3357
3361
elif self ._active_handle :
3358
- return 'resize'
3362
+ return _RectangleSelectorState . RESIZE
3359
3363
3360
- return 'create'
3364
+ return _RectangleSelectorState . CREATE
3361
3365
3362
3366
3363
3367
def _onmove (self , event ):
@@ -3377,7 +3381,7 @@ def _onmove(self, event):
3377
3381
action = self ._get_action ()
3378
3382
3379
3383
xdata , ydata = self ._get_data_coords (event )
3380
- if action == "resize" :
3384
+ if action == _RectangleSelectorState . RESIZE :
3381
3385
inv_tr = self ._get_rotation_transform ().inverted ()
3382
3386
xdata , ydata = inv_tr .transform ([xdata , ydata ])
3383
3387
eventpress .xdata , eventpress .ydata = inv_tr .transform (
@@ -3397,7 +3401,7 @@ def _onmove(self, event):
3397
3401
3398
3402
x0 , x1 , y0 , y1 = self ._extents_on_press
3399
3403
# rotate an existing shape
3400
- if action == "rotate" :
3404
+ if action == _RectangleSelectorState . ROTATE :
3401
3405
# calculate angle abc
3402
3406
a = (eventpress .xdata , eventpress .ydata )
3403
3407
b = self .center
@@ -3406,7 +3410,7 @@ def _onmove(self, event):
3406
3410
np .arctan2 (a [1 ]- b [1 ], a [0 ]- b [0 ]))
3407
3411
self .rotation = np .rad2deg (self ._rotation_on_press + angle )
3408
3412
3409
- elif action == "resize" :
3413
+ elif action == _RectangleSelectorState . RESIZE :
3410
3414
size_on_press = [x1 - x0 , y1 - y0 ]
3411
3415
center = (x0 + size_on_press [0 ] / 2 , y0 + size_on_press [1 ] / 2 )
3412
3416
@@ -3457,7 +3461,7 @@ def _onmove(self, event):
3457
3461
sign = np .sign (xdata - x0 )
3458
3462
x1 = x0 + sign * abs (y1 - y0 ) * self ._aspect_ratio_correction
3459
3463
3460
- elif action == "move" :
3464
+ elif action == _RectangleSelectorState . MOVE :
3461
3465
x0 , x1 , y0 , y1 = self ._extents_on_press
3462
3466
dx = xdata - eventpress .xdata
3463
3467
dy = ydata - eventpress .ydata
0 commit comments