8000 simulate click · matplotlib/matplotlib@d1a9de4 · GitHub
[go: up one dir, main page]

Skip to content

Commit d1a9de4

Browse files
committed
simulate click
1 parent 622cb95 commit d1a9de4

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,16 +3308,17 @@ def _get_toolbar(self, toolbar, canvas):
33083308

33093309
@property
33103310
def active_toggle(self):
3311-
"""Get the tooggled Tool"""
3311+
"""Toggled Tool
3312+
3313+
**string** : Currently toggled tool, or None
3314+
"""
33123315
return self._toggled
33133316

3314-
def get_instances(self):
3315-
"""Get the active tools instgances
3317+
@property
3318+
def instances(self):
3319+
"""Active tools instances
33163320
3317-
Returns
3318-
----------
3319-
A dictionary with the active instances that are registered with
3320-
Navigation
3321+
**dictionary** : Contains the active instances that are registered
33213322
"""
33223323
return self._instances
33233324

@@ -3331,7 +3332,7 @@ def get_tool_keymap(self, name):
33313332
33323333
Returns
33333334
----------
3334-
Keymap : list of keys associated with the Tool
3335+
list : list of keys associated with the Tool
33353336
"""
33363337
keys = [k for k, i in self._keys.items() if i == name]
33373338
return keys
@@ -3363,6 +3364,11 @@ def set_tool_keymap(self, name, *keys):
33633364
def unregister(self, name):
33643365
"""Unregister the tool from the active instances
33653366
3367+
Parameters
3368+
----------
3369+
name : string
3370+
Name of the tool to unregister
3371+
33663372
Notes
33673373
-----
33683374
This method is used by `PersistentTools` to remove the reference kept
@@ -3372,8 +3378,7 @@ def unregister(self, name):
33723378
destroy if it is a graphical Tool.
33733379
33743380
If called, next time the `Tool` is used it will be reinstantiated
3375-
instead
3376-
of using the existing instance.
3381+
instead of using the existing instance.
33773382
"""
33783383
if self._toggled == name:
33793384
self._handle_toggle(name, from_toolbar=False)
@@ -3450,24 +3455,21 @@ def _get_cls_to_instantiate(self, callback_class):
34503455

34513456
return callback_class
34523457

3453-
def _key_press(self, event):
3454-
if event.key is None:
3455-
return
3458+
def click_tool(self, name):
3459+
"""Simulate a click on a tool
34563460
3457-
#some tools may need to capture keypress, but they need to be toggle
3458-
if self._toggled:
3459-
instance = self._get_instance(self._toggled)
3460-
if self.keypresslock.isowner(instance):
3461-
instance.key_press(event)
3462-
return
3461+
This is a convenient method to programatically click on
3462+
Tools
3463+
"""
3464+
self._tool_activate(name, None, False)
34633465

3464-
name = self._keys.get(event.key, None)
3465-
if name is None:
3466-
return
3466+
def _tool_activate(self, name, event, from_toolbar):
3467+
if name not in self._tools:
3468+
raise AttributeError('%s not in Tools' % name)
34673469

34683470
tool = self._tools[name]
34693471
if tool.toggle:
3470-
self._handle_toggle(name, event=event)
3472+
self._handle_toggle(name, event=event, from_toolbar=from_toolbar)
34713473
elif tool.persistent:
34723474
instance = self._get_instance(name)
34733475
instance.activate(event)
@@ -3476,6 +3478,20 @@ def _key_press(self, event):
34763478
#instantiated and forgotten (reminds me an exgirlfriend?)
34773479
tool(self.canvas.figure, event)
34783480

3481+
def _key_press(self, event):
3482+
if event.key is None:
3483+
return
3484+
3485+
#some tools may need to capture keypress, but they need to be toggle
3486+
if self._toggled:
3487+
instance = self._get_instance(self._toggled)
3488+
if self.keypresslock.isowner(instance):
3489+
instance.key_press(event)
3490+
return
3491+
3492+
name = self._keys.get(event.key, None)
3493+
self._tool_activate(name, event, False)
3494+
34793495
def _get_instance(self, name):
34803496
if name not in self._instances:
34813497
instance = self._tools[name](self.canvas.figure)
@@ -3496,14 +3512,7 @@ def _toolbar_callback(self, name):
34963512
Name of the tool that was activated (click) by the user using the
34973513
toolbar
34983514
"""
3499-
tool = self._tools[name]
3500-
if tool.toggle:
3501-
self._handle_toggle(name, from_toolbar=True)
3502-
elif tool.persistent:
3503-
instance = self._get_instance(name)
3504-
instance.activate(None)
3505-
else:
3506-
tool(self.canvas.figure, None)
3515+
self._tool_activate(name, None, True)
35073516

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

lib/matplotlib/backend_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def deactivate(self, event=None):
175175
"""Deactivate the toggle tool
176176
177177
This method is called when the tool is deactivated (second click on the
178-
toolbar button) or when another toogle tool from the same `navigation` is
179-
activated
178+
toolbar button) or when another toogle tool from the same `navigation`
179+
is activated
180180
"""
181181
pass
182182

0 commit comments

Comments
 (0)
0