52
52
class DraggableLegend (DraggableOffsetBox ):
53
53
def __init__ (self , legend , use_blit = False , update = "loc" ):
54
54
"""
55
+ Wrapper around a `.Legend` to support mouse dragging.
56
+
55
57
Parameters
56
58
----------
57
- update : string
58
- If "loc", update *loc* parameter of legend upon finalizing.
59
- If "bbox", update *bbox_to_anchor* parameter.
59
+ legend : `.Legend`
60
+ The `.Legend` instance to wrap.
61
+ use_blit : bool, optional
62
+
63
+ update : ['loc' | 'bbox'], optional
64
+ If "loc", update the *loc* parameter of the legend upon finalizing.
65
+ If "bbox", update the *bbox_to_anchor* parameter.
60
66
"""
61
67
self .legend = legend
62
68
@@ -1096,6 +1102,41 @@ def _find_best_position(self, width, height, renderer, consider=None):
1096
1102
def contains (self , event ):
1097
1103
return self .legendPatch .contains (event )
1098
1104
1105
+ def set_draggable (self , state , use_blit = False , update = 'loc' ):
1106
+ """
1107
+ Enable or disable mouse dragging support of the legend.
1108
+
1109
+ Parameters
1110
+ ----------
1111
+ state : bool
1112
+ ``True`` / ``False`` enables / disables mouse dragging.
1113
+ use_blit : bool, optional
1114
+
1115
+ update_loc : ['loc' | 'bbox'], optional
1116
+ The legend parameter to be changed when dragged:
1117
+
1118
+ - 'loc': update the *loc* parameter of the legend
1119
+ - 'bbox': update the *bbox_to_anchor* parameter of the legend
1120
+
1121
+ Returns
1122
+ -------
1123
+ If *state* is ``True`` this returns the `~.DraggableLegend` helper
1124
+ instance. Otherwise this returns ``None``.
1125
+ """
1126
+ if state :
1127
+ if self ._draggable is None :
1128
+ self ._draggable = DraggableLegend (self ,
1129
+ use_blit ,
1130
+ update = update )
1131
+ else :
1132
+ if self ._draggable is not None :
1133
+ self ._draggable .disconnect ()
1134
+ self ._draggable = None
1135
+
1136
+ def get_draggable (self ):
1137
+ """Return ``True`` if the legend is draggable, ``False`` otherwise."""
1138
+ return self ._draggable is not None
1139
+
1099
1140
def draggable (self , state = None , use_blit = False , update = "loc" ):
1100
1141
"""
1101
1142
Set the draggable state -- if state is
@@ -1114,21 +1155,10 @@ def draggable(self, state=None, use_blit=False, update="loc"):
1114
1155
when dragged. If update is "loc", the *loc* parameter of the legend
1115
1156
is changed. If "bbox", the *bbox_to_anchor* parameter is changed.
1116
1157
"""
1117
- is_draggable = self ._draggable is not None
1118
-
1119
- # if state is None we'll toggle
1120
1158
if state is None :
1121
- state = not is_draggable
1159
+ state = not self . get_draggable () # toggle state
1122
1160
1123
- if state :
1124
- if self ._draggable is None :
1125
- self ._draggable = DraggableLegend (self ,
1126
- use_blit ,
1127
- update = update )
1128
- else :
1129
- if self ._draggable is not None :
1130
- self ._draggable .disconnect ()
1131
- self ._draggable = None
1161
+ self .set_draggable (state , use_blit , update )
1132
1162
1133
1163
return self ._draggable
1134
1164
0 commit comments