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

Skip to content

Commit ee76451

Browse files
committed
Add ExpandableBase
1 parent e300707 commit ee76451

File tree

4 files changed

+66
-57
lines changed

4 files changed

+66
-57
lines changed

examples/user_interfaces/gui_elements.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
topbar = manager.backend.Toolbar(tool_mgr)
2323

2424
# Add it to the figure window, we can place it north, east, west and south
25-
manager.window.add_element(topbar, False, 'north')
25+
manager.window.add_element(topbar, 'north')
2626

2727
# Remove some tools from the main toolbar and add them to the
2828
# new sidebar
@@ -39,7 +39,7 @@
3939
sidebar = manager.backend.Toolbar(tool_mgr)
4040

4141
# add the sidebar to the new window
42-
win.add_element(sidebar, False, 'west')
42+
win.add_element(sidebar, 'west')
4343

4444
# Add some tools to the new sidebar
4545
for tool in ('home', 'back', 'forward', 'zoom', 'pan'):

lib/matplotlib/backend_bases.py

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,61 @@ def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
16561656
self.key = key
16571657

16581658

1659-
class FigureCanvasBase(object):
1659+
class ExpandableBase(object):
1660+
"""
1661+
Base class for GUI elements that can expand to fill the area given to them
1662+
by the encapsulating container (e.g. the main window).
1663+
1664+
At the moment this class does not do anything apart from mark such classes,
1665+
but this may well change at a later date, PRs welcome.
1666+
"""
1667+
pass
1668+
1669+
class FlowBase(object):
1670+
"""
1671+
Base mixin class for all GUI elements that can flow, aka laid out in
1672+
different directions.
1673+
1674+
The MPL window class deals with the manipulation of this mixin, so users
1675+
don't actually need to interact with this class.
1676+
1677+
Classes the implement this class must override the _update_flow method.
1678+
"""
1679+
flow_types = ['horizontal', 'vertical']
1680+
1681+
def __init__(self, flow='horizontal', flow_locked=False, **kwargs):
1682+
super(FlowBase, self).__init__(**kwargs)
1683+
self.flow_locked = flow_locked
1684+
self.flow = flow
1685+
1686+
@property
1687+
def flow(self):
1688+
"""
1689+
The direction of flow, one of the strings in `flow_type`.
1690+
"""
1691+
return FlowBase.flow_types[self._flow]
1692+
1693+
@flow.setter
1694+
def flow(self, flow):
1695+
if self.flow_locked:
1696+
return
1697+
1698+
try:
1699+
self._flow = FlowBase.flow_types.index(flow)
1700+
except ValueError:
1701+
raise ValueError('Flow (%s), not in list %s' % (flow, FlowBase.flow_types))
1702+
1703+
self._update_flow()
1704+
1705+
def _update_flow(self):
1706+
"""
1707+
Classes that extend FlowBase must override this method.
1708+
You can use the internal property self._flow whereby
1709+
flow_types[self._flow] gives the current flow.
1710+
"""
1711+
raise NotImplementedError
1712+
1713+
class FigureCanvasBase(ExpandableBase):
16601714
"""
16611715
The canvas the figure renders into.
16621716
@@ -2717,7 +2771,7 @@ def set_window_title(self, title):
27172771
"""
27182772
pass
27192773

2720-
def add_element(self, element, expand, place):
2774+
def add_element(self, element, place):
27212775
""" Adds a gui widget to the window.
27222776
This has no effect for non-GUI backends and properties only apply
27232777
to those backends that support them, or have a suitable workaround.
@@ -2726,9 +2780,6 @@ def add_element(self, element, expand, place):
27262780
----------
27272781
element : A gui element.
27282782
The element to add to the window
2729-
expand : bool
2730-
Whether the element should auto expand to fill as much space within
2731-
the window as possible.
27322783
place : string
27332784
The location to place the element, either compass points north,
27342785
east, south, west, or center.
@@ -3479,51 +3530,6 @@ def remove_toolitem(self, name):
34793530
raise NotImplementedError
34803531

34813532

3482-
class FlowBase(object):
3483-
"""
3484-
Base mixin class for all GUI elements that can flow, aka laid out in
3485-
different directions.
3486-
3487-
The MPL window class deals with the manipulation of this mixin, so users
3488-
don't actually need to interact with this class.
3489-
3490-
Classes the implement this class must override the _update_flow method.
3491-
"""
3492-
flow_types = ['horizontal', 'vertical']
3493-
3494-
def __init__(self, flow='horizontal', flow_locked=False, **kwargs):
3495-
super(FlowBase, self).__init__(**kwargs)
3496-
self.flow_locked = flow_locked
3497-
self.flow = flow
3498-
3499-
@property
3500-
def flow(self):
3501-
"""
3502-
The direction of flow, one of the strings in `flow_type`.
3503-
"""
3504-
return FlowBase.flow_types[self._flow]
3505-
3506-
@flow.setter
3507-
def flow(self, flow):
3508-
if self.flow_locked:
3509-
return
3510-
3511-
try:
3512-
self._flow = FlowBase.flow_types.index(flow)
3513-
except ValueError:
3514-
raise ValueError('Flow (%s), not in list %s' % (flow, FlowBase.flow_types))
3515-
3516-
self._update_flow()
3517-
3518-
def _update_flow(self):
3519-
"""
3520-
Classes that extend FlowBase must override this method.
3521-
You can use the internal property self._flow whereby
3522-
flow_types[self._flow] gives the current flow.
3523-
"""
3524-
raise NotImplementedError
3525-
3526-
35273533
class ToolbarBase(ToolContainerBase, FlowBase):
35283534
pass
35293535

lib/matplotlib/backend_managers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self, figure, num, **kwargs):
8787
w = int(self.figure.bbox.width)
8888
h = int(self.figure.bbox.height)
8989

90-
self.window.add_element(self.figure.canvas, True, 'center')
90+
self.window.add_element(self.figure.canvas, 'center')
9191

9292
self.toolmanager = ToolManager(self.figure)
9393
self.toolbar = self._get_toolbar()
@@ -96,10 +96,10 @@ def __init__(self, figure, num, **kwargs):
9696
if self.toolbar:
9797
tools.add_tools_to_container(self.toolbar)
9898
self.statusbar = self._backend.Statusbar(self.toolmanager)
99-
h += self.window.add_element(self.statusbar, False, 'south')
99+
h += self.window.add_element(self.statusbar, 'south')
100100

101101
if self.toolbar is not None:
102-
h += self.window.add_element(self.toolbar, False, 'south')
102+
h += self.window.add_element(self.toolbar, 'south')
103103

104104
self.window.set_default_size(w, h)
105105
self._full_screen_flag = False

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3030
from matplotlib._pylab_helpers import Gcf
3131
from matplotlib.backend_bases import (RendererBase, GraphicsContextBase,
3232
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors,
33-
TimerBase, WindowBase, MainLoopBase)
33+
TimerBase, WindowBase, MainLoopBase, ExpandableBase)
3434
from matplotlib.backend_bases import ShowBase, ToolbarBase, StatusbarBase
3535
from matplotlib.backend_managers import ToolManager
3636
from matplotlib import backend_tools
@@ -426,7 +426,7 @@ def _setup_box(self, name, orientation, grow, parent):
426426
self._layout[parent].pack_start(self._layout[name], grow, grow, 0)
427427
self._layout[name].show()
428428

429-
def add_element(self, element, expand, place):
429+
def add_element(self, element, place):
430430
element.show()
431431

432432
# Get the flow of the element (the opposite of the container)
@@ -440,6 +440,9 @@ def add_element(self, element, expand, place):
440440
except AttributeError:
441441
pass
442442

443+
# Determine if this element should fill all the space given to it
444+
expand = isinstance(element, ExpandableBase)
445+
443446
if place in ['north', 'west', 'center']:
444447
self._layout[place].pack_start(element, expand, expand, 0)
445448
self._layout[place].pack_start(separator, False, False, 0)

0 commit comments

Comments
 (0)