Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/silx/gui/plot/LegendsWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,37 +127,37 @@ def __init__(
def setPlotWidget(self, plot: PlotWidget | None):
previousPlot = None if self._plotRef is None else self._plotRef()
if previousPlot is not None:
previousPlot.sigItemAdded.disconnect(self._onItemAdded)
previousPlot.sigItemRemoved.disconnect(self._onItemRemoved)

self._clear()
Comment thread
abmajith marked this conversation as resolved.
previousPlot.sigItemAdded.disconnect(self.addItem)
previousPlot.sigItemRemoved.disconnect(self.removeItem)
for item in previousPlot.getItems():
self.removeItem(item)

if plot is None:
self._plotRef = None
else:
plot.sigItemAdded.connect(self._onItemAdded)
plot.sigItemRemoved.connect(self._onItemRemoved)
plot.sigItemAdded.connect(self.addItem)
plot.sigItemRemoved.connect(self.removeItem)
self._plotRef = weakref.ref(plot)

for item in plot.getItems():
self._onItemAdded(item)
self.addItem(item)

def _clear(self):
def clear(self):
layout = self.layout()
while layout.count() > 1:
child = layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
self._itemWidgets.clear()

def _onItemAdded(self, item: items.Item):
def addItem(self, item: items.Item):
if item in self._itemWidgets:
return
widget = _LegendItemWidget(self, item)
self.layout().insertWidget(self.layout().count() - 1, widget)
self._itemWidgets[item] = widget

def _onItemRemoved(self, item: items.Item):
def removeItem(self, item: items.Item):
widget: qt.QWidget | None = self._itemWidgets.pop(item, None)
if widget is not None:
self.layout().removeWidget(widget)
Expand Down
72 changes: 66 additions & 6 deletions src/silx/gui/plot/test/test_legendswidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from silx.gui import qt
from silx.gui.plot import PlotWidget
from silx.gui.plot.LegendsWidget import LegendsWidget
from .. import items


@pytest.fixture
Expand All @@ -13,12 +14,12 @@ def plot_with_legend(qWidgetFactory):
return plot, widget


def test_initial_state(plot_with_legend):
plot, legend_widget = plot_with_legend
def test_initial_state(qWidgetFactory):
legend_widget = qWidgetFactory(LegendsWidget)
assert len(legend_widget._itemWidgets) == 0


def test_add_remove_items(qapp, plot_with_legend):
def test_auto_sync(qapp, plot_with_legend):
plot, legend_widget = plot_with_legend
plot.addCurve(numpy.arange(10), numpy.arange(10), legend="curve1")
qapp.processEvents()
Expand All @@ -34,8 +35,67 @@ def test_visibility_toggle(qapp, qapp_utils, plot_with_legend):
qapp.processEvents()
item = plot.getCurve("test_item")
item_widget = legend_widget._itemWidgets[item]
assert item.isVisible() is True
assert item.isVisible()
qapp_utils.mouseClick(item_widget, qt.Qt.LeftButton)
assert not item.isVisible()
qapp_utils.mouseClick(item_widget, qt.Qt.LeftButton)
assert item.isVisible() is False
assert item.isVisible()


def test_manual_add_same_item(qapp, plot_with_legend):
plot, legend_widget = plot_with_legend
plot.addCurve([0, 1], [0, 1], legend="c1")
qapp.processEvents()
assert len(legend_widget._itemWidgets) == 1
item = plot.getCurve("c1")
legend_widget.addItem(item)
qapp.processEvents()
assert len(legend_widget._itemWidgets) == 1


def test_manual_remove_item_and_then_from_plot(qapp, plot_with_legend):
plot, legend_widget = plot_with_legend
plot.addCurve([0, 1], [0, 1], legend="c1")
plot.addCurve([1, 1], [1, 1], legend="c2")
qapp.processEvents()
item = plot.getCurve("c1")
legend_widget.removeItem(item)
assert len(legend_widget._itemWidgets) == 1
assert plot.getCurve("c1") is not None
plot.remove("c1")
qapp.processEvents()
assert len(legend_widget._itemWidgets) == 1


def test_setPlotWidget_switch_cases(qapp, qWidgetFactory):
plot1 = qWidgetFactory(PlotWidget)
plot2 = qWidgetFactory(PlotWidget)
legend_widget = qWidgetFactory(LegendsWidget, plotWidget=plot1)
plot1.addCurve([0, 1], [0, 1], legend="p1_item")
orphan_item = items.Curve()
orphan_item.setName("orphan_item")
legend_widget.addItem(orphan_item)
qapp.processEvents()
assert len(legend_widget._itemWidgets) == 2
legend_widget.setPlotWidget(plot2)
qapp.processEvents()
assert "p1_item" not in [it.getName() for it in legend_widget._itemWidgets]
assert "orphan_item" in [it.getName() for it in legend_widget._itemWidgets]


def test_orphan_item_interaction(qapp, qapp_utils, qWidgetFactory):
legend_widget = qWidgetFactory(LegendsWidget)
orphan_item = items.Curve()
orphan_item.setName("orphan")
legend_widget.addItem(orphan_item)
qapp.processEvents()
item_widget = legend_widget._itemWidgets[orphan_item]
assert orphan_item.isVisible()
assert item_widget._label.isEnabled()
qapp_utils.mouseClick(item_widget, qt.Qt.LeftButton)
qapp.processEvents()
assert not orphan_item.isVisible()
assert not item_widget._label.isEnabled()
qapp_utils.mouseClick(item_widget, qt.Qt.LeftButton)
assert item.isVisible() is True
assert orphan_item.isVisible()
assert item_widget._label.isEnabled()