Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e6e252

Browse files
committed
Improve flow handling and make it a lot more generic
1 parent 7edaf5a commit 8e6e252

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

examples/user_interfaces/gui_elements.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
# Create a new toolbar
2222
topbar = manager.backend.Toolbar(tool_mgr)
23-
# The options are north, east, west and south
23+
24+
# Add it to the figure window, we can place it north, east, west and south
2425
manager.window.add_element(topbar, False, 'north')
2526

2627
# Remove some tools from the main toolbar and add them to the
@@ -33,17 +34,17 @@
3334

3435
# Add a new window
3536
win = manager.backend.Window('Extra tools')
37+
3638
# create a sidebar for the new window
3739
sidebar = manager.backend.Toolbar(tool_mgr)
38-
# set the direction of the sidebar
39-
# the options are horizontal and vertical
40-
sidebar.set_flow('vertical')
40+
4141
# add the sidebar to the new window
4242
win.add_element(sidebar, False, 'west')
4343

4444
# Add some tools to the new sidebar
4545
for tool in ('home', 'back', 'forward', 'zoom', 'pan'):
4646
sidebar.add_tool(tool, None)
47+
4748
# show the new window
4849
win.show()
4950

lib/matplotlib/backend_bases.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,6 +3479,38 @@ def remove_toolitem(self, name):
34793479
raise NotImplementedError
34803480

34813481

3482+
class FlowBase(object):
3483+
flow_types = ['horizontal', 'vertical']
3484+
3485+
def __init__(self, flow='horizontal', flow_locked=False, **kwargs):
3486+
super(FlowBase, self).__init__(**kwargs)
3487+
self.flow_locked = flow_locked
3488+
self.flow = flow
3489+
3490+
@property
3491+
def flow(self):
3492+
return FlowBase.flow_types[self._flow]
3493+
3494+
@flow.setter
3495+
def flow(self, flow):
3496+
if self.flow_locked:
3497+
return
3498+
3499+
try:
3500+
self._flow = FlowBase.flow_types.index(flow)
3501+
except ValueError:
3502+
raise ValueError('Flow (%s), not in list %s' % (flow, FlowBase.flow_types))
3503+
3504+
self._update_flow()
3505+
3506+
def _update_flow(self):
3507+
raise NotImplementedError
3508+
3509+
3510+
class ToolbarBase(ToolContainerBase, FlowBase):
3511+
pass
3512+
3513+
34823514
class StatusbarBase(object):
34833515
"""Base class for the statusbar"""
34843516
def __init__(self, toolmanager, **kwargs):

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3131
from matplotlib.backend_bases import (RendererBase, GraphicsContextBase,
3232
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors,
3333
TimerBase, WindowBase, MainLoopBase)
34-
from matplotlib.backend_bases import (ShowBase, ToolContainerBase,
35-
StatusbarBase)
34+
from matplotlib.backend_bases import ShowBase, ToolbarBase, StatusbarBase
3635
from matplotlib.backend_managers import ToolManager
3736
from matplotlib import backend_tools
3837

@@ -387,7 +386,6 @@ def stop_event_loop(self):
387386

388387

389388
_flow = [Gtk.Orientation.HORIZONTAL, Gtk.Orientation.VERTICAL]
390-
flow_types = ['horizontal', 'vertical']
391389

392390

393391
class WindowGTK3(WindowBase, Gtk.Window):
@@ -431,9 +429,17 @@ def _setup_box(self, name, orientation, grow, parent):
431429
def add_element(self, element, expand, place):
432430
element.show()
433431

434-
flow = _flow[not _flow.index(self._layout[place].get_orientation())]
432+
# Get the flow of the element (the opposite of the container)
433+
flow_index = not _flow.index(self._layout[place].get_orientation())
434+
flow = _flow[flow_index]
435435
separator = Gtk.Separator(orientation=flow)
436436
separator.show()
437+
438+
try:
439+
element.flow = element.flow_types[flow_index]
440+
except AttributeError:
441+
pass
442+
437443
if place in ['north', 'west', 'center']:
438444
self._layout[place].pack_start(element, expand, expand, 0)
439445
self._layout[place].pack_start(separator, False, False, 0)
@@ -827,11 +833,10 @@ def draw_rubberband(self, x0, y0, x1, y1):
827833
self.ctx.stroke()
828834

829835

830-
class ToolbarGTK3(ToolContainerBase, Gtk.Box):
831-
def __init__(self, toolmanager, flow='horizontal', **kwargs):
832-
super(ToolbarGTK3, self).__init__(toolmanager=toolmanager, **kwargs)
836+
class ToolbarGTK3(ToolbarBase, Gtk.Box):
837+
def __init__(self, toolmanager, **kwargs):
833838
self._toolarea = Gtk.Box()
834-
self.set_flow(flow)
839+
super(ToolbarGTK3, self).__init__(toolmanager=toolmanager, **kwargs)
835840

836841
self.pack_start(self._toolarea, False, False, 0)
837842
self._toolarea.show_all()
@@ -894,11 +899,7 @@ def remove_toolitem(self, name):
894899
self._groups[group].remove(toolitem)
895900
del self._toolitems[name]
896901

897-
def set_flow(self, flow):
898-
try:
899-
self._flow = flow_types.index(flow)
900-
except ValueError:
901-
raise ValueError('Flow (%s), not in list %s' % (flow, flow_types))
902+
def _update_flow(self):
902903
self.set_property("orientation", _flow[not self._flow])
903904
self._toolarea.set_property('orientation', _flow[self._flow])
904905
for item in self._toolarea:

0 commit comments

Comments
 (0)