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
tk backend
  • Loading branch information
fariza committed Apr 7, 2015
commit 50567298326effe8ae3ae72a5a1d27988d374629
3 changes: 2 additions & 1 deletion examples/user_interfaces/navigation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import matplotlib
matplotlib.use('GTK3Cairo')
# matplotlib.use('GTK3Cairo')
matplotlib.use('TkAGG')
matplotlib.rcParams['toolbar'] = 'navigation'
Copy link
Member

Choose a reason for hiding this comment

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

Since these are examples, I would suggest getting rid of extraneous commented out code, and also highlight important lines of code such as this one. For example, is it important that it gets called before importing pyplot? What is it for? Perhaps a short docstring at the top of this example would help explain its purpose/goal that it is demonstrating?

import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolBase
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/backend_bases.py
6D47
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,9 @@ def add_tool(self, tool):
"""

tool_cls = self._get_cls_to_instantiate(tool)
if tool_cls is False:
Copy link
Member

Choose a reason for hiding this comment

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

probably should do a # noqa here to keep pep8 from complaining. Might also need a comment explaining why to keep future devs from "fixing" this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I don't see the pep8 error here.

Copy link
Member

Choose a reason for hiding this comment

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

I think this should be if not tool_cls ?

Wouldn't be simpler to just have self._get_cls_to_instantiate raise if it fails? If you want to do something else in that case then just try...execpt and if you want to die (which I am assuming is the default case) you don't have to do anything.

I suspect @WeatherGod also has a pyflake/pylint turned on.

Copy link
Member

Choose a reason for hiding this comment

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

also, I am reading and commenting top-down. Sorry if some of these seem dumb in light of code I have not read 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.

done @tacaswell change, but still not see the pep8 error

Copy link
Member

Choose a reason for hiding this comment

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

Different versions of pep8? I find myself curious to learn about this as Travis doesn't complain.

warnings.warn('Impossible to find class for %s' % str(tool))
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 raise an exception? The function failed to achieve its goal and no new tool was added.

return
name = tool_cls.name

if name is None:
Expand All @@ -3442,9 +3445,11 @@ def add_tool(self, tool):
self._keys[k] = name

if self.toolbar and tool_cls.position is not None:
# TODO: better search for images, they are not always in the
# datapath
basedir = os.path.join(rcParams['datapath'], 'images')
if tool_cls.image is not None:
fname = os.path.join(basedir, tool_cls.image + '.png')
fname = os.path.join(basedir, tool_cls.image)
else:
fname = None
toggle = issubclass(tool_cls, tools.ToolToggleBase)
Expand Down
14 changes: 7 additions & 7 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class ToolHome(ToolBase):

description = 'Reset original view'
name = 'Home'
image = 'home'
image = 'home.png'
keymap = rcParams['keymap.home']
position = -1

Expand All @@ -312,7 +312,7 @@ class ToolBack(ToolBase):

description = 'Back to previous view'
name = 'Back'
image = 'back'
image = 'back.png'
keymap = rcParams['keymap.back']
position = -1

Expand All @@ -328,7 +328,7 @@ class ToolForward(ToolBase):

description = 'Forward to next view'
name = 'Forward'
image = 'forward'
image = 'forward.png'
keymap = rcParams['keymap.forward']
position = -1

Expand All @@ -344,7 +344,7 @@ class ConfigureSubplotsBase(ToolPersistentBase):

description = 'Configure subplots'
name = 'Subplots'
image = 'subplots'
image = 'subplots.png'
position = -1


Expand All @@ -353,7 +353,7 @@ class SaveFigureBase(ToolBase):

description = 'Save the figure'
name = 'Save'
image = 'filesave'
image = 'filesave.png'
position = -1
keymap = rcParams['keymap.save']

Expand All @@ -363,7 +363,7 @@ class ToolZoom(ToolToggleBase):

description = 'Zoom to rectangle'
name = 'Zoom'
image = 'zoom_to_rect'
image = 'zoom_to_rect.png'
position = -1
keymap = rcParams['keymap.zoom']
cursor = cursors.SELECT_REGION
Expand Down Expand Up @@ -597,7 +597,7 @@ class ToolPan(ToolToggleBase):
keymap = rcParams['keymap.pan']
name = 'Pan'
description = 'Pan axes with left mouse, zoom with right'
image = 'move'
image = 'move.png'
position = -1
cursor = cursors.MOVE

Expand Down
194 changes: 192 additions & 2 deletions lib/matplotlib/backends/backend_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from matplotlib.backend_bases import RendererBase, GraphicsContextBase
from matplotlib.backend_bases import FigureManagerBase, FigureCanvasBase
from matplotlib.backend_bases import NavigationToolbar2, cursors, TimerBase
from matplotlib.backend_bases import ShowBase
from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase
from matplotlib._pylab_helpers import Gcf

from matplotlib.figure import Figure
Expand Down Expand Up @@ -541,9 +542,23 @@ def __init__(self, canvas, num, window):

def notify_axes_change(fig):
'this will be called whenever the current axes is changed'
if self.toolbar != None: self.toolbar.update()
if self.navigation is not None:
self.navigation.update()
elif self.toolbar is not None:
self.toolbar.update()
self.canvas.figure.add_axobserver(notify_axes_change)

def _get_toolbar(self, canvas):
if matplotlib.rcParams['toolbar'] == 'toolbar2':
toolbar = NavigationToolbar2TkAgg(canvas, self.window)
elif matplotlib.rcParams['toolbar'] == 'navigation':
self.navigation = NavigationTk(canvas, ToolbarTk)
toolbar = self.navigation.toolbar
else:
self.navigation = NavigationTk(canvas, None)
toolbar = None
return toolbar

def resize(self, width, height=None):
# before 09-12-22, the resize method takes a single *event*
# parameter. On the other hand, the resize method of other
Expand Down Expand Up @@ -871,5 +886,180 @@ def hidetip(self):
if tw:
tw.destroy()


class NavigationTk(NavigationBase):
def __init__(self, *args, **kwargs):
NavigationBase.__init__(self, *args, **kwargs)

def set_cursor(self, cursor):
self.canvas.manager.window.configure(cursor=cursord[cursor])

def draw_rubberband(self, event, caller, x0, y0, x1, y1):
if not self.canvas.widgetlock.available(caller):
return
height = self.canvas.figure.bbox.height
y0 = height - y0
y1 = height - y1
try:
self.lastrect
except AttributeError:
pass
else:
self.canvas._tkcanvas.delete(self.lastrect)
self.lastrect = self.canvas._tkcanvas.create_rectangle(x0, y0, x1, y1)

def remove_rubberband(self, event, caller):
try:
self.lastrect
except AttributeError:
pass
else:
self.canvas._tkcanvas.delete(self.lastrect)
del self.lastrect


class ToolbarTk(ToolbarBase, Tk.Frame):
def __init__(self, manager):
ToolbarBase.__init__(self, manager)
xmin, xmax = self.manager.canvas.figure.bbox.intervalx
height, width = 50, xmax - xmin
Tk.Frame.__init__(self, master=self.manager.window,
width=int(width), height=int(height),
borderwidth=2)
self._toolitems = {}
self._add_message()

def _add_toolitem(self, name, tooltip_text, image_file, position,
toggle):

button = self._Button(name, image_file, toggle)
if tooltip_text is not None:
ToolTip.createToolTip(button, tooltip_text)
self._toolitems[name] = button

def _Button(self, text, file, toggle):
if file is not None:
img_file = os.path.join(rcParams['datapath'], 'images', file)
im = Tk.PhotoImage(master=self, file=img_file)
else:
im = None

if not toggle:
b = Tk.Button(
master=self, text=text, padx=2, pady=2, image=im,
command=lambda: self._button_click(text))
else:
b = Tk.Checkbutton(master=self, text=text, padx=2, pady=2,
image=im, indicatoron=False,
command=lambda: self._button_click(text))
b._ntimage = im
b.pack(side=Tk.LEFT)
return b

def _button_click(self, name):
self.manager.navigation._toolbar_callback(name)

def _toggle(self, name, callback=False):
if name not in self._toolitems:
self.set_message('%s Not in toolbar' % name)
return
self._toolitems[name].toggle()
if callback:
self._button_click(name)

def _add_message(self):
self.message = Tk.StringVar(master=self)
self._message_label = Tk.Label(master=self, textvariable=self.message)
self._message_label.pack(side=Tk.RIGHT)
self.pack(side=Tk.BOTTOM, fill=Tk.X)

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

def _remove_toolitem(self, name):
self._toolitems[name].pack_forget()
del self._toolitems[name]

def set_toolitem_visibility(self, name, visible):
pass


class SaveFigureTk(SaveFigureBase):
def trigger(self, *args):
from six.moves import tkinter_tkfiledialog, tkinter_messagebox
filetypes = self.figure.canvas.get_supported_filetypes().copy()
default_filetype = self.figure.canvas.get_default_filetype()

# Tk doesn't provide a way to choose a default filetype,
# so we just have to put it first
default_filetype_name = filetypes[default_filetype]
del filetypes[default_filetype]

sorted_filetypes = list(six.iteritems(filetypes))
sorted_filetypes.sort()
sorted_filetypes.insert(0, (default_filetype, default_filetype_name))

tk_filetypes = [
(name, '*.%s' % ext) for (ext, name) in sorted_filetypes]

# adding a default extension seems to break the
# asksaveasfilename dialog when you choose various save types
# from the dropdown. Passing in the empty string seems to
# work - JDH!
#defaultextension = self.figure.canvas.get_default_filetype()
defaultextension = ''
initialdir = rcParams.get('savefig.directory', '')
initialdir = os.path.expanduser(initialdir)
initialfile = self.figure.canvas.get_default_filename()
fname = tkinter_tkfiledialog.asksaveasfilename(
master=self.figure.canvas.manager.window,
title='Save the figure',
filetypes=tk_filetypes,
defaultextension=defaultextension,
initialdir=initialdir,
initialfile=initialfile,
)

if fname == "" or fname == ():
return
else:
if initialdir == '':
# explicitly missing key or empty str signals to use cwd
rcParams['savefig.directory'] = initialdir
else:
# save dir for next time
rcParams['savefig.directory'] = os.path.dirname(
six.text_type(fname))
try:
# This method will handle the delegation to the correct type
self.figure.canvas.print_figure(fname)
except Exception as e:
tkinter_messagebox.showerror("Error saving file", str(e))


class ConfigureSubplotsTk(ConfigureSubplotsBase):
def __init__(self, *args, **kwargs):
ConfigureSubplotsBase.__init__(self, *args, **kwargs)
toolfig = Figure(figsize=(6, 3))
self.window = Tk.Tk()

canvas = FigureCanvasTkAgg(toolfig, master=self.window)
toolfig.subplots_adjust(top=0.9)
_tool = SubplotTool(self.figure, toolfig)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.window.protocol("WM_DELETE_WINDOW", self.destroy)

def trigger(self, event):
self.window.lift()

def destroy(self, *args, **kwargs):
self.unregister()
self.window.destroy()


SaveFigure = SaveFigureTk
ConfigureSubplots = ConfigureSubplotsTk

FigureCanvas = FigureCanvasTkAgg
FigureManager = FigureManagerTkAgg
0