Add `PlotItemOverlay.get_axes()`

Enables retrieving all "named axes" on a particular "side" of the
overlayed plot items. This is useful for calculating how much space
needs to be allocated for the axes before the view box area starts.
windows_fixes_yo
Tyler Goodlet 2022-02-06 12:15:05 -05:00
parent e8d7709358
commit 0271841412
1 changed files with 33 additions and 16 deletions
piker/ui

View File

@ -103,11 +103,6 @@ class ComposedGridLayout:
dict[str, AxisItem], dict[str, AxisItem],
] = {} ] = {}
self._axes2pi: dict[
AxisItem,
dict[str, PlotItem],
] = {}
# TODO: better name? # TODO: better name?
# construct surrounding layouts for placing outer axes and # construct surrounding layouts for placing outer axes and
# their legends and title labels. # their legends and title labels.
@ -158,8 +153,8 @@ class ComposedGridLayout:
for name, axis_info in plotitem.axes.items(): for name, axis_info in plotitem.axes.items():
axis = axis_info['item'] axis = axis_info['item']
# register this plot's (maybe re-placed) axes for lookup. # register this plot's (maybe re-placed) axes for lookup.
self._pi2axes.setdefault(index, {})[name] = axis # print(f'inserting {name}:{axis} to index {index}')
self._axes2pi.setdefault(index, {})[name] = plotitem self._pi2axes.setdefault(name, {})[index] = axis
# enter plot into list for index tracking # enter plot into list for index tracking
self.items.insert(index, plotitem) self.items.insert(index, plotitem)
@ -213,11 +208,12 @@ class ComposedGridLayout:
# invert insert index for layouts which are # invert insert index for layouts which are
# not-left-to-right, top-to-bottom insert oriented # not-left-to-right, top-to-bottom insert oriented
insert_index = index
if name in ('top', 'left'): if name in ('top', 'left'):
index = min(len(axes) - index, 0) insert_index = min(len(axes) - index, 0)
assert index >= 0 assert insert_index >= 0
linlayout.insertItem(index, axis) linlayout.insertItem(insert_index, axis)
axes.insert(index, axis) axes.insert(index, axis)
self._register_item(index, plotitem) self._register_item(index, plotitem)
@ -243,13 +239,15 @@ class ComposedGridLayout:
plot: PlotItem, plot: PlotItem,
name: str, name: str,
) -> AxisItem: ) -> Optional[AxisItem]:
''' '''
Retrieve the named axis for overlayed ``plot``. Retrieve the named axis for overlayed ``plot`` or ``None``
if axis for that name is not shown.
''' '''
index = self.items.index(plot) index = self.items.index(plot)
return self._pi2axes[index][name] named = self._pi2axes[name]
return named.get(index)
def pop( def pop(
self, self,
@ -341,7 +339,7 @@ def mk_relay_method(
# halt/short circuit the graphicscene loop). Further the # halt/short circuit the graphicscene loop). Further the
# surrounding handler for this signal must be allowed to execute # surrounding handler for this signal must be allowed to execute
# and get processed by **this consumer**. # and get processed by **this consumer**.
print(f'{vb.name} rx relayed from {relayed_from.name}') # print(f'{vb.name} rx relayed from {relayed_from.name}')
ev.ignore() ev.ignore()
return slot( return slot(
@ -351,7 +349,7 @@ def mk_relay_method(
) )
if axis is not None: if axis is not None:
print(f'{vb.name} handling axis event:\n{str(ev)}') # print(f'{vb.name} handling axis event:\n{str(ev)}')
ev.accept() ev.accept()
return slot( return slot(
vb, vb,
@ -490,7 +488,6 @@ class PlotItemOverlay:
vb.setZValue(1000) # XXX: critical for scene layering/relaying vb.setZValue(1000) # XXX: critical for scene layering/relaying
self.overlays: list[PlotItem] = [] self.overlays: list[PlotItem] = []
from piker.ui._overlay import ComposedGridLayout
self.layout = ComposedGridLayout( self.layout = ComposedGridLayout(
root_plotitem, root_plotitem,
root_plotitem.layout, root_plotitem.layout,
@ -613,6 +610,26 @@ class PlotItemOverlay:
''' '''
return self.layout.get_axis(plot, name) return self.layout.get_axis(plot, name)
def get_axes(
self,
name: str,
) -> list[AxisItem]:
'''
Retrieve all axes for all plots with ``name: str``.
If a particular overlay doesn't have a displayed named axis
then it is not delivered in the returned ``list``.
'''
axes = []
for plot in self.overlays:
axis = self.layout.get_axis(plot, name)
if axis:
axes.append(axis)
return axes
# TODO: i guess we need this if you want to detach existing plots # TODO: i guess we need this if you want to detach existing plots
# dynamically? XXX: untested as of now. # dynamically? XXX: untested as of now.
def _disconnect_all( def _disconnect_all(