Skip to content

Commit a776feb

Browse files
committed
Fix: reimplement edit_plot_parameters in ItemListWidget to support non-selectable items
Fix #32
1 parent f382f40 commit a776feb

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

plotpy/panels/itemlist.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
from qtpy import QtWidgets as QW
2222

2323
from plotpy.config import _
24-
from plotpy.constants import ID_ITEMLIST
24+
from plotpy.constants import ID_ITEMLIST, PARAMETERS_TITLE_ICON
2525
from plotpy.interfaces import IPanel
2626
from plotpy.interfaces import items as itf
2727
from plotpy.panels.base import PanelWidget
28+
from plotpy.styles.base import ItemParameters
2829

2930
if TYPE_CHECKING:
3031
from qtpy.QtGui import QContextMenuEvent, QIcon
@@ -106,7 +107,35 @@ def setup_actions(self) -> None:
106107

107108
def edit_plot_parameters(self) -> None:
108109
"""Edit plot parameters"""
109-
self.plot.edit_plot_parameters("item")
110+
# In order to support non-selectable items, we have to reimplement the
111+
# `BasePlot.edit_plot_parameters` method. This is because we can select items
112+
# using the `PlotItemList` widget, but we can't select them using the plot
113+
# widget. Thus we can't rely on the `BasePlot.get_selected_items` method and
114+
# other methods that rely on it.
115+
#
116+
# This can be tested for example by uncommenting the line containing
117+
# `item.set_selectable(False)` in the `tests/items/test_transform.py` file.
118+
119+
# === Reimplementing the `BasePlot.edit_plot_parameters`:
120+
sel_items = self.get_selected_items()
121+
multiselection = len(sel_items) > 1
122+
itemparams = ItemParameters(multiselection=multiselection)
123+
# === === Reimplementing the `BasePlot.get_plot_parameters`:
124+
for item in sel_items:
125+
item.get_item_parameters(itemparams)
126+
sel_items[0].get_item_parameters(itemparams)
127+
Param = self.plot.get_axesparam_class(sel_items[0])
128+
axesparam = Param(
129+
title=_("Axes"),
130+
icon="lin_lin.png",
131+
comment=_("Axes associated to selected item"),
132+
)
133+
axesparam.update_param(sel_items[0])
134+
itemparams.add("AxesParam", self.plot, axesparam)
135+
# === ===
136+
title, icon = PARAMETERS_TITLE_ICON["item"]
137+
itemparams.edit(self.plot, title, icon)
138+
# ===
110139

111140
def __is_selection_contiguous(self) -> bool:
112141
"""Check if selected items are contiguous"""

plotpy/plot/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,9 @@ def set_item_parameters(self, itemparams: ItemParameters) -> None:
18971897
dataset = itemparams.get("AxesParam")
18981898
if dataset is not None:
18991899
active_item = self.get_active_item()
1900-
dataset.update_item(active_item)
1900+
if active_item is not None:
1901+
# active_item may be None when dealing with non-selectable items only
1902+
dataset.update_item(active_item)
19011903

19021904
def edit_plot_parameters(self, key: str) -> None:
19031905
"""

plotpy/tests/items/test_transform.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def make_items(N: int) -> list[TrImageItem]:
133133
maxy = max(maxy, h)
134134
w = item.boundingRect().width()
135135
item.set_transform(x, y, 0.0)
136+
# item.set_selectable(False)
136137
return items
137138

138139

@@ -205,7 +206,8 @@ def test_transform(N: int, assemble_images: bool) -> None:
205206
items,
206207
wintitle="Transform test ({}x{} images)".format(N, N),
207208
plot_type="image",
208-
show_itemlist=False,
209+
show_itemlist=True,
210+
winsize=(1000, 600),
209211
)
210212
if assemble_images:
211213
build_image(items)

0 commit comments

Comments
 (0)