Skip to content

Commit c5c4f0f

Browse files
committed
simulate click
1 parent 979875e commit c5c4f0f

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

lib/matplotlib/backend_bases.py

+40-31
Original file line numberDiff line numberDiff line change
@@ -3266,16 +3266,17 @@ def _get_toolbar(self, toolbar, canvas):
32663266

32673267
@property
32683268
def active_toggle(self):
3269-
"""Get the tooggled Tool"""
3269+
"""Toggled Tool
3270+
3271+
**string** : Currently toggled tool, or None
3272+
"""
32703273
return self._toggled
32713274

3272-
def get_instances(self):
3273-
"""Get the active tools instgances
3275+
@property
3276+
def instances(self):
3277+
"""Active tools instances
32743278
3275-
Returns
3276-
----------
3277-
A dictionary with the active instances that are registered with
3278-
Navigation
3279+
**dictionary** : Contains the active instances that are registered
32793280
"""
32803281
return self._instances
32813282

@@ -3289,7 +3290,7 @@ def get_tool_keymap(self, name):
32893290
32903291
Returns
32913292
----------
3292-
Keymap : list of keys associated with the Tool
3293+
list : list of keys associated with the Tool
32933294
"""
32943295
keys = [k for k, i in self._keys.items() if i == name]
32953296
return keys
@@ -3321,6 +3322,11 @@ def set_tool_keymap(self, name, *keys):
33213322
def unregister(self, name):
33223323
"""Unregister the tool from the active instances
33233324
3325+
Parameters
3326+
----------
3327+
name : string
3328+
Name of the tool to unregister
3329+
33243330
Notes
33253331
-----
33263332
This method is used by `PersistentTools` to remove the reference kept
@@ -3330,8 +3336,7 @@ def unregister(self, name):
33303336
destroy if it is a graphical Tool.
33313337
33323338
If called, next time the `Tool` is used it will be reinstantiated
3333-
instead
3334-
of using the existing instance.
3339+
instead of using the existing instance.
33353340
"""
33363341
if self._toggled == name:
33373342
self._handle_toggle(name, from_toolbar=False)
@@ -3408,24 +3413,21 @@ def _get_cls_to_instantiate(self, callback_class):
34083413

34093414
return callback_class
34103415

3411-
def _key_press(self, event):
3412-
if event.key is None:
3413-
return
3416+
def click_tool(self, name):
3417+
"""Simulate a click on a tool
34143418
3415-
#some tools may need to capture keypress, but they need to be toggle
3416-
if self._toggled:
3417-
instance = self._get_instance(self._toggled)
3418-
if self.keypresslock.isowner(instance):
3419-
instance.key_press(event)
3420-
return
3419+
This is a convenient method to programatically click on
3420+
Tools
3421+
"""
3422+
self._tool_activate(name, None, False)
34213423

3422-
name = self._keys.get(event.key, None)
3423-
if name is None:
3424-
return
3424+
def _tool_activate(self, name, event, from_toolbar):
3425+
if name not in self._tools:
3426+
raise AttributeError('%s not in Tools' % name)
34253427

34263428
tool = self._tools[name]
34273429
if tool.toggle:
3428-
self._handle_toggle(name, event=event)
3430+
self._handle_toggle(name, event=event, from_toolbar=from_toolbar)
34293431
elif tool.persistent:
34303432
instance = self._get_instance(name)
34313433
instance.activate(event)
@@ -3434,6 +3436,20 @@ def _key_press(self, event):
34343436
#instantiated and forgotten (reminds me an exgirlfriend?)
34353437
tool(self.canvas.figure, event)
34363438

3439+
def _key_press(self, event):
3440+
if event.key is None:
3441+
return
3442+
3443+
#some tools may need to capture keypress, but they need to be toggle
3444+
if self._toggled:
3445+
instance = self._get_instance(self._toggled)
3446+
if self.keypresslock.isowner(instance):
3447+
instance.key_press(event)
3448+
return
3449+
3450+
name = self._keys.get(event.key, None)
3451+
self._tool_activate(name, event, False)
3452+
34373453
def _get_instance(self, name):
34383454
if name not in self._instances:
34393455
instance = self._tools[name](self.canvas.figure)
@@ -3454,14 +3470,7 @@ def _toolbar_callback(self, name):
34543470
Name of the tool that was activated (click) by the user using the
34553471
toolbar
34563472
"""
3457-
tool = self._tools[name]
3458-
if tool.toggle:
3459-
self._handle_toggle(name, from_toolbar=True)
3460-
elif tool.persistent:
3461-
instance = self._get_instance(name)
3462-
instance.activate(None)
3463-
else:
3464-
tool(self.canvas.figure, None)
3473+
self._tool_activate(name, None, True)
34653474

34663475
def _handle_toggle(self, name, event=None, from_toolbar=False):
34673476
#toggle toolbar without callback

0 commit comments

Comments
 (0)