8000 MEP22: Navigation by events by fariza · Pull Request #3652 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

MEP22: Navigation by events #3652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 69 commits into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
8cceed4
navigation and toolbar coexistence
fariza Jan 23, 2014
3118a5a
mod keypress in figuremanager
fariza Jan 23, 2014
b4d5fcf
helper methods in toolbar and navigation
fariza Jan 24, 2014
1e8af47
Adding doc to base methods
fariza Jan 24, 2014
622cb95
property for active_toggle
fariza Jan 26, 2014
d1a9de4
simulate click
fariza Jan 27, 2014
3f89d52
activate renamed to trigger
fariza Jan 28, 2014
4f3c10b
toggle tools using enable/disable from its trigger method
fariza Jan 29, 2014
6065daa
simplifying _handle_toggle
fariza Jan 29, 2014
f6a2f19
reducing number of locks
fariza Jan 29, 2014
05db3b6
changing toggle and persistent attributes for issubclass
fariza Feb 4, 2014
c08fe56
bug in combined key press
fariza Feb 4, 2014
b207a72
untoggle zoom and pan from keypress while toggled
fariza Feb 4, 2014
9266447
classmethods for default tools modification
fariza Feb 6, 2014
a53419a
adding zaxis and some pep8
fariza May 1, 2014
704c717
removing legacy method dynamic update
fariza May 6, 2014
5056729
tk backend
fariza May 6, 2014
e6a4e1e
example working with Tk
fariza May 6, 2014
8942c47
duplicate code in keymap tool initialization
fariza Jul 24, 2014
022de6f
grammar corrections
fariza Jul 24, 2014
2c9a195
moving views and positions to tools
fariza Jul 24, 2014
cafe668
The views positions mixin automatically adds the clear as axobserver
fariza Jul 25, 2014
224f745
bug when navigation was not defined
fariza Jul 25, 2014
94c711e
Small refactor so that we first initiate the Navigation (ToolManager)…
OceanWolf Jul 28, 2014
67257e7
Moved default_tool initilisation to FigureManagerBase and cleaned.
OceanWolf Jul 29, 2014
ffa65d6
Temporary fix to backends
OceanWolf Jul 29, 2014
6739ee0
removing persistent tools
fariza Sep 3, 2014
d18206f
removing unregister
fariza Sep 4, 2014
34a52c8
change cursor inmediately after toggle
fariza Sep 5, 2014
c2da483
removing intoolbar
fariza Oct 15, 2014
44a9b0e
events working
fariza Oct 16, 2014
a2ed47f
using pydispatch
fariza Oct 17, 2014
0665890
using navigation as signal handler
fariza Oct 20, 2014
411e6e2
removing view positions singleton
fariza Oct 20, 2014
d484ebd
moving set_cursor completely out of navigation
fariza Oct 27, 2014
75bf97b
removing unused event class
fariza Nov 10, 2014
6cc040b
underscore in tool_trigger-xxx
fariza Nov 10, 2014
0ff5997
adding radio_group for toggle tools
fariza Nov 14, 2014
af6734f
scroll to zoom in zoom/pan tools
fariza Nov 28, 2014
78513d2
remove ToolAddedEvent incorporating the functionality into toolevent
fariza Dec 5, 2014
377ff54
eliminating repeated loop
fariza Jan 5, 2015
7dbbf58
replace draw by draw_idle in tools
fariza Jan 21, 2015
dd66b57
rename mpl_connect
fariza Jan 21, 2015
67a414f
cleaning navigation and toolbar dependencies
fariza Feb 4, 2015
e415d8d
Made NavigationBase.get_tool() more useful.
OceanWolf Feb 11, 2015
1213086
Refactored Toolbar out of NavigationBase
OceanWolf Feb 12, 2015
ba61dec
Some short cuts for adding tools
OceanWolf Feb 16, 2015
9f2ee2b
Lots of fixes
OceanWolf Feb 18, 2015
9da2b13
Rename ToolbarBase -> ToolContainerBase
OceanWolf Feb 18, 2015
110253f
Statusbar
OceanWolf Feb 20, 2015
e2804ea
tool group position
fariza Feb 26, 2015
9a64b7e
docstrings and small corrections by WeatherGod
fariza Mar 23, 2015
64f947f
tkbackend updated
fariza Mar 31, 2015
e8cd5d5
tacaswell comments aprl 1
fariza Apr 1, 2015
4bbcf4e
renaming tool_trigger_event
fariza Apr 1, 2015
73a2661
add_tools moved out of base classes
fariza Apr 1, 2015
1b83628
figure.setter in tools
fariza Apr 1, 2015
e4edd23
rename tools to default_tools to avoid confusion
fariza Apr 1, 2015
d4ac2fb
docstring helper add_tools methods
fariza Apr 1, 2015
a7640ef
rename navigation to toolmanager
fariza Apr 2, 2015
48a6971
tkagg updated for toolmanager and tool groups
fariza Apr 2, 2015
8dafe09
doc and minor code organization
fariza Apr 2, 2015
a0695d0
whats new
fariza Apr 3, 2015
328b169
missing object from class declaration
fariza Apr 3, 2015
aac4744
remove comments and docstrings
fariza Apr 3, 2015
f09b9ef
import with original name backend_tools
fariza Apr 3, 2015
def3a52
rename 2 -> to, example without gtk only code
fariza Apr 7, 2015
9ee7e25
zoom pan buttons order
fariza Apr 7, 2015
5eae4e1
matplotlib.rcParams['toolbar'] == 'None' starts toolmanager but not t…
fariza Apr 7, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Lots of fixes
  • Loading branch information
OceanWolf authored and fariza committed Apr 7, 2015
commit 9f2ee2b8cc5642fb79505a1da7bbf51a8dd9b43c
2 changes: 1 addition & 1 deletion examples/user_interfaces/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def trigger(self, *args, **kwargs):
fig.canvas.manager.navigation.add_tool('List', ListTools)
if matplotlib.rcParams['backend'] == 'GTK3Cairo':
fig.canvas.manager.navigation.add_tool('copy', CopyToolGTK3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with making this a GTK3Cairo-only example. Makes it a little bit simpler


fig.canvas.manager.toolbar.add_tool('zoom', 'foo')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is 'foo' an existing tool?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple answer it ain't we have here 'zoom' as the tool, and 'foo' as the group it gets placed in.

# Uncomment to remove the forward button
# fig.canvas.manager.navigation.remove_tool('forward')

Expand Down
56 changes: 32 additions & 24 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3413,7 +3413,7 @@ def add_tools(self, tools):
for tool, name in tools:
self.add_tool(name, tool)

def add_tool(self, name, tool):
def add_tool(self, name, tool, *args, **kwargs):
"""Add tool to `NavigationBase`

Add a tool to the tools controlled by Navigation
Expand All @@ -3428,6 +3428,10 @@ def add_tool(self, name, tool):
Name of the tool, treated as the ID, has to be unique
tool : string or `matplotlib.backend_tools.ToolBase` derived class
Reference to find the class of the Tool to be added

Notes
-----
args and kwargs get passed directly to the tools constructor.
"""

tool_cls = self._get_cls_to_instantiate(tool)
Expand All @@ -3440,12 +3444,7 @@ def add_tool(self, name, tool):
'not added')
return

if isinstance(tool_cls, type):
self._tools[name] = tool_cls(self, name)
else:
tool_cls.set_navigation(self)
tool.name = name
self._tools[name] = tool_cls
self._tools[name] = tool_cls(self, name, *args, **kwargs)

if tool_cls.keymap is not None:
self.set_tool_keymap(name, tool_cls.keymap)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be self._tools[name].keymap ? Might as well allow for per-instance key binding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one who makes the sorting (by keymap) of the keypress events is NavigationBase.
It is easier to handleNavigationBase._keys dictionnary than looping throught all the tools finding their keymaps when an event occurs.
What we can do, if you insist in per-instance key binding, is to modify set_tool_keymap to set the tool keymap attribute at the same time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? per-instance key binding happens, this just sets the default keymap.

@fariza I think you should rename ToolBase.keymap to ToolBase.default_keymap as I think that would make it a lot clearer!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite follow this yet..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OceanWolf agreed and done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell set_tool_keymap(name, keymap) does per-instance key binding, it sets the keymap of the instance identified by name to the specified keymap. Here it takes the default keymap (a class level property) and binds it to the instance. Does that make it clearer for you?

Expand All @@ -3460,7 +3459,6 @@ def add_tool(self, name, tool):
self._toggled.setdefault(tool_cls.radio_group, None)

self._tool_added_event(self._tools[name])

return self._tools[name]

def _tool_added_event(self, tool):
Expand Down Expand Up @@ -3583,7 +3581,7 @@ def get_tool(self, name):
name : String, ToolBase
Name of the tool, or the tool itself
"""
if isinstance(name, tools.ToolBase):
if isinstance(name, tools.ToolBase) and tool.name in self._tools:
return name
if name not in self._tools:
warnings.warn("%s is not a tool controlled by Navigation" % name)
Expand Down Expand Up @@ -3612,15 +3610,12 @@ def _message_cbk(self, event):
"""Captures the 'tool_message_event' to set the message on the toolbar"""
self.set_message(event.message)

def _tool_triggered_cbk(self, event):
def _tool_toggled_cbk(self, event):
"""Captures the 'tool-trigger-toolname
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "tool_trigger_[name]" to be consistent with documentation elsewhere.


This only gets used for toggled tools
"""
if event.sender is self:
return

self.toggle_toolitem(event.tool.name)
self.toggle_toolitem(event.tool.name, event.tool.toggled)

def add_tools(self, tools):
""" Add multiple tools to `Navigation`
Expand All @@ -3639,25 +3634,38 @@ def add_tools(self, tools):
for position, tool in enumerate(grouptools):
self.add_tool(tool, group, position)

def add_tool(self, tool, group, position):
"""Adds a tool to the toolbar"""
def add_tool(self, tool, group, position=-1, name=None, **kwargs):
"""Adds a tool to the toolbar

Parameters
----------
tool : string, tool
The name or the type of tool to add.
group : string
The name of the group to add this tool to.
position : int
the relative position within the group to place this tool.
name : string (optional)
If given, and the above fails, we use this to create a new tool of
type given by tool, and use this as the name of the tool.
"""
t = self.navigation.get_tool(tool)
if t is None:
if isinstance(tool, (list, tuple)):
t = self.navigation.add_tool(tool[0], tool[1])
elif isinstance(tool, ToolBase):
t = self.navigation.add_tool(tool.name, tool)
else:
warning.warn('Cannot add tool %s'%tool)
return
if isinstance(tool, type):
tool = tool.__class__
if name is not None:
t = self.navigation.add_tool(name, tool, **kwargs)
if t is None:
warning.warn('Cannot add tool %s'%tool)
return
tool = t
image = self._get_image_filename(tool.image)
toggle = getattr(tool, 'toggled', None) is not None
self.add_toolitem(tool.name, group, position, image,
tool.description, toggle)
if toggle:
self.navigation.nav_connect('tool_trigger_%s' % tool.name,
self._tool_triggered_cbk)
self._tool_toggled_cbk)

def _remove_tool_cbk(self, event):
"""Captures the 'tool_removed_event' signal and removes the tool"""
Expand Down
3 changes: 0 additions & 3 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ class ToolBase(object):
def __init__(self, navigation, name):
self._name = name
self.figure = None
self.set_navigation(navigation)

def set_navigation(self, navigation):
self.navigation = navigation
self.set_figure(navigation.canvas.figure)

Expand Down
22 changes: 9 additions & 13 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ def __init__(self, navigation):
self.pack_start(self._toolbar, False, False, 0)
self._toolbar.show_all()
self._toolitems = {}
self._signals = {}
self._setup_message_area()

def _setup_message_area(self):
Expand All @@ -788,9 +787,6 @@ def _setup_message_area(self):

def add_toolitem(self, name, group, position, image_file, description,
toggle):
if group is None:
return

if toggle:
tbutton = Gtk.ToggleToolButton()
else:
Expand All @@ -809,29 +805,29 @@ def add_toolitem(self, name, group, position, image_file, description,
signal = tbutton.connect('clicked', self._call_tool, name)
tbutton.set_tooltip_text(description)
tbutton.show_all()
self._toolitems[name] = tbutton
self._signals[name] = signal
self._toolitems.setdefault(name, [])
self._toolitems[name].append((tbutton, signal))

def _call_tool(self, btn, name):
self.trigger_tool(name)

def set_message(self, s):
self.message.set_label(s)

def toggle_toolitem(self, name):
def toggle_toolitem(self, name, toggled):
if name not in self._toolitems:
return

status = self._toolitems[name].get_active()
self._toolitems[name].handler_block(self._signals[name])
self._toolitems[name].set_active(not status)
self._toolitems[name].handler_unblock(self._signals[name])
for toolitem, signal in self._toolitems[name]:
toolitem.handler_block(signal)
toolitem.set_active(toggled)
toolitem.handler_unblock(signal)

def remove_toolitem(self, name):
if name not in self._toolitems:
self.set_message('%s Not in toolbar' % name)
return
self._toolbar.remove(self._toolitems[name])
for toolitem, signal in self._toolitems[name]:
self._toolbar.remove(toolitem)
del self._toolitems[name]

def add_separator(self, pos=-1):
Expand Down
0