8000 removing view positions singleton · matplotlib/matplotlib@411e6e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 411e6e2

Browse files
committed
removing view positions singleton
1 parent 0665890 commit 411e6e2

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3546,6 +3546,16 @@ def get_tools(self):
35463546
'keymap': keys}
35473547
return d
35483548

3549+
def get_tool(self, name):
3550+
"""Return the tool object
3551+
3552+
Parameters:
3553+
-----------
3554+
name: String
3555+
Name of the tool
3556+
"""
3557+
return self._tools[name]
3558+
35493559
def _set_cursor(self, canvasevent):
35503560
"""Sets the current cursor in ToolSetCursor"""
35513561

lib/matplotlib/backend_tools.py

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -336,77 +336,84 @@ def trigger(self, sender, event, data=None):
336336
ax.figure.canvas.draw()
337337

338338

339-
class ViewsPositions(object):
340-
"""Auxiliary class to handle changes in views and positions"""
339+
class ToolViewsPositions(ToolBase):
340+
"""Auxiliary Tool to handle changes in views and positions
341+
342+
This tool is accessed by navigation.manipulate_tool
343+
This tool is used by all the tools that need to access the record of
344+
views and positions of the figure
345+
- Zoom
346+
- Pan
347+
- Home
348+
- Back
349+
- Forward
350+
"""
341351

342-
views = WeakKeyDictionary()
343-
"""Record of views with Figure objects as keys"""
352+
def __init__(self, *args, **kwargs):
353+
self.views = WeakKeyDictionary()
354+
self.positions = WeakKeyDictionary()
355+
ToolBase.__init__(self, *args, **kwargs)
344356

345-
positions = WeakKeyDictionary()
346-
"""Record of positions with Figure objects as keys"""
357+
def set_figure(self, figure):
358+
ToolBase.set_figure(self, figure)
347359

348-
@classmethod
349-
def add_figure(cls, figure):
350-
"""Add a figure to the list of figures handled by this class"""
351-
if figure not in cls.views:
352-
cls.views[figure] = cbook.Stack()
353-
cls.positions[figure] = cbook.Stack()
360+
def add_figure(self):
361+
"""Add the current figure to the stack of views and positions"""
362+
if self.figure not in self.views:
363+
self.views[self.figure] = cbook.Stack()
364+
self.positions[self.figure] = cbook.Stack()
354365
# Define Home
355-
cls.push_current(figure)
366+
self.push_current()
356367
# Adding the clear method as axobserver, removes this burden from
357368
# the backend
358-
figure.add_axobserver(cls.clear)
369+
self.figure.add_axobserver(self.clear)
359370

360-
@classmethod
361-
def clear(cls, figure):
371+
def clear(self, figure):
362372
"""Reset the axes stack"""
363-
if figure in cls.views:
364-
cls.views[figure].clear()
365-
cls.positions[figure].clear()
373+
if figure in self.views:
374+
self.views[figure].clear()
375+
self.positions[figure].clear()
366376

367-
@classmethod
368-
def update_view(cls, figure):
377+
def update_view(self):
369378
"""Update the viewlim and position from the view and
370379
position stack for each axes
371380
"""
372381

373-
lims = cls.views[figure]()
382+
lims = self.views[self.figure]()
374383
if lims is None:
375384
return
376-
pos = cls.positions[figure]()
385+
pos = self.positions[self.figure]()
377386
if pos is None:
378387
return
379-
for i, a in enumerate(figure.get_axes()):
388+
for i, a in enumerate(self.figure.get_axes()):
380389
xmin, xmax, ymin, ymax = lims[i]
381390
a.set_xlim((xmin, xmax))
382391
a.set_ylim((ymin, ymax))
383392
# Restore both the original and modified positions
384393
a.set_position(pos[i][0], 'original')
385394
a.set_position(pos[i][1], 'active')
386395

387-
figure.canvas.draw_idle()
396+
self.figure.canvas.draw_idle()
388397

389-
@classmethod
390-
def push_current(cls, figure):
398+
def push_current(self):
391399
"""push the current view limits and position onto the stack"""
392400

393401
lims = []
394402
pos = []
395-
for a in figure.get_axes():
403+
for a in self.figure.get_axes():
396404
xmin, xmax = a.get_xlim()
397405
ymin, ymax = a.get_ylim()
398406
lims.append((xmin, xmax, ymin, ymax))
399407
# Store both the original and modified positions
400408
pos.append((
401409
a.get_position(True).frozen(),
402410
a.get_position().frozen()))
403-
cls.views[figure].push(lims)
404-
cls.positions[figure].push(pos)
411+
self.views[self.figure].push(lims)
412+
self.positions[self.figure].push(pos)
405413

406-
@classmethod
407-
def refresh_locators(cls, figure):
414+
def refresh_locators(self):
408415
"""Redraw the canvases, update the locators"""
409-
for a in figure.get_axes():
416+
for a in self.figure.get_axes():
410417
xaxis = getattr(a, 'xaxis', None)
411418
yaxis = getattr(a, 'yaxis', None)
412419
zaxis = getattr(a, 'zaxis', None)
@@ -423,37 +430,30 @@ def refresh_locators(cls, figure):
423430

424431
for loc in locators:
425432
loc.refresh()
426-
figure.canvas.draw_idle()
433+
self.figure.canvas.draw_idle()
427434

428-
@classmethod
429-
def home(cls, figure):
430-
cls.views[figure].home()
431-
cls.positions[figure].home()
435+
def home(self):
436+
self.views[self.figure].home()
437+
self.positions[self.figure].home()
432438

433-
@classmethod
434-
def back(cls, figure):
435-
cls.views[figure].back()
436-
cls.positions[figure].back()
439+
def back(self):
440+
self.views[self.figure].back()
441+
self.positions[self.figure].back()
437442

438-
@classmethod
439-
def forward(cls, figure):
440-
cls.views[figure].forward()
441-
cls.positions[figure].forward()
443+
def forward(self):
444+
self.views[self.figure].forward()
445+
self.positions[self.figure].forward()
442446

443447

444448
class ViewsPositionsBase(ToolBase):
445449
"""Base class for ToolHome, ToolBack and ToolForward"""
446450

447451
_on_trigger = None
448452

449-
def __init__(self, *args, **kwargs):
450-
ToolBase.__init__(self, *args, **kwargs)
451-
self.viewspos = ViewsPositions()
452-
453453
def trigger(self, sender, event, data=None):
454-
self.viewspos.add_figure(self.figure)
455-
getattr(self.viewspos, self._on_trigger)(self.figure)
456-
self.viewspos.update_view(self.figure)
454+
self.navigation.get_tool('viewpos').add_figure()
455+
getattr(self.navigation.get_tool('viewpos'), self._on_trigger)()
456+
self.navigation.get_tool('viewpos').update_view()
457457

458458

459459
class ToolHome(ViewsPositionsBase):
@@ -499,15 +499,13 @@ class SaveFigureBase(ToolBase):
499499

500500

501501
class ZoomPanBase(ToolToggleBase):
502-
# Base class to group common functionality between zoom and pan
503-
# Not of much use for other tools, so not documented
502+
"""Base class for Zoom and Pan tools"""
504503
def __init__(self, *args):
505504
ToolToggleBase.__init__(self, *args)
506505
self._button_pressed = None
507506
self._xypress = None
508507
self._idPress = None
509508
self._idRelease = None
510-
self.viewspos = ViewsPositions()
511509

512510
def enable(self, event):
513511
self.figure.canvas.widgetlock(self)
@@ -523,7 +521,7 @@ def disable(self, event):
523521
self.figure.canvas.mpl_disconnect(self._idRelease)
524522

525523
def trigger(self, sender, event, data=None):
526-
self.viewspos.add_figure(self.figure)
524+
self.navigation.get_tool('viewpos').add_figure()
527525
ToolToggleBase.trigger(self, sender, event, data)
528526

529527

@@ -543,7 +541,7 @@ def _cancel_action(self):
543541
for zoom_id in self._ids_zoom:
544542
self.figure.canvas.mpl_disconnect(zoom_id)
545543
self.navigation.tool_trigger_event('rubberband', self)
546-
self.viewspos.refresh_locators(self.figure)
544+
self.navigation.get_tool('viewpos').refresh_locators()
547545
self._xypress = None
548546
self._button_pressed = None
549547
self._ids_zoom = []
@@ -611,8 +609,6 @@ def _mouse_move(self, event):
611609
x1, y1, x2, y2 = a.bbox.extents
612610
x, lastx = x1, x2
613611

614-
# self.navigation.draw_rubberband(event, self, x, y, lastx, lasty)
615-
# data = {'x': x, 'y': y, 'lastx': lastx, 'lasty': lasty}
616612
self.navigation.tool_trigger_event('rubberband',
617613
self,
618614
data=(x, y, lastx, lasty))
@@ -736,7 +732,7 @@ def _release(self, event):
736732
a.set_ylim((ry1, ry2))
737733

738734
self._zoom_mode = None
739-
self.viewspos.push_current(self.figure)
735+
self.navigation.get_tool('viewpos').push_current()
740736
self._cancel_action()
741737

742738

@@ -757,7 +753,7 @@ def _cancel_action(self):
757753
self._xypress = []
758754
self.figure.canvas.mpl_disconnect(self._idDrag)
759755
self.navigation.messagelock.release(self)
760-
self.viewspos.refresh_locators(self.figure)
756+
self.navigation.get_tool('viewpos').refresh_locators()
761757

762758
def _press(self, event):
763759
if event.button == 1:
@@ -794,7 +790,7 @@ def _release(self, event):
794790
self._cancel_action()
795791
return
796792

797-
self.viewspos.push_current(self.figure)
793+
self.navigation.get_tool('viewpos').push_current()
798794
self._cancel_action()
799795

800796
def _mouse_move(self, event):
@@ -842,6 +838,7 @@ def _mouse_move(self, event):
842838
(ToolXScale, 'xscale'),
843839
(ToolYScale, 'yscale'),
844840
(ToolCursorPosition, 'position'),
841+
(ToolViewsPositions, 'viewpos'),
845842
('ToolSetCursor', 'cursor'),
846843
('ToolRubberband', 'rubberband')]]]
847844

0 commit comments

Comments
 (0)
0