diff options
Diffstat (limited to 'silx/gui/plot/test/testPlotWidget.py')
-rwxr-xr-x | silx/gui/plot/test/testPlotWidget.py | 179 |
1 files changed, 177 insertions, 2 deletions
diff --git a/silx/gui/plot/test/testPlotWidget.py b/silx/gui/plot/test/testPlotWidget.py index f9d2281..b55260e 100755 --- a/silx/gui/plot/test/testPlotWidget.py +++ b/silx/gui/plot/test/testPlotWidget.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2016-2020 European Synchrotron Radiation Facility +# Copyright (c) 2016-2021 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -197,6 +197,21 @@ class TestPlotWidget(PlotWidgetTestCase, ParametricTestCase): self.assertTrue(numpy.all(numpy.equal(items[4].getPosition()[0], marker_x))) self.assertEqual(items[5].getType(), 'rectangle') + def testRemoveDiscardItem(self): + """Test removeItem and discardItem""" + self.plot.addCurve((1, 2, 3), (1, 2, 3)) + curve = self.plot.getItems()[0] + self.plot.removeItem(curve) + with self.assertRaises(ValueError): + self.plot.removeItem(curve) + + self.plot.addCurve((1, 2, 3), (1, 2, 3)) + curve = self.plot.getItems()[0] + result = self.plot.discardItem(curve) + self.assertTrue(result) + result = self.plot.discardItem(curve) + self.assertFalse(result) + def testBackGroundColors(self): self.plot.setVisible(True) self.qWaitForWindowExposed(self.plot) @@ -559,6 +574,11 @@ class TestPlotCurve(PlotWidgetTestCase): self.plot.addCurve(x=x, y=y, color='grey', legend='curve1', fill=True, baseline=list(range(0, 100, 1))) + def testPlotCurveComplexData(self): + """Test curve with complex data""" + data = numpy.arange(100.) + 1j + self.plot.addCurve(x=data, y=data, xerror=data, yerror=data) + class TestPlotHistogram(PlotWidgetTestCase): """Basic tests for add Histogram""" @@ -592,6 +612,13 @@ class TestPlotScatter(PlotWidgetTestCase, ParametricTestCase): self.plot.addScatter(x, y, value) self.plot.resetZoom() + def testScatterComplexData(self): + """Test scatter item with complex data""" + data = numpy.arange(100.) + 1j + self.plot.addScatter( + x=data, y=data, value=data, xerror=data, yerror=data) + self.plot.resetZoom() + def testScatterVisualization(self): self.plot.addScatter((0, 1, 0, 1), (0, 0, 2, 2), (0, 1, 2, 3)) self.plot.resetZoom() @@ -1857,6 +1884,153 @@ class TestPlotWidgetSwitchBackend(PlotWidgetTestCase): self.assertEqual(self.plot.getItems(), items) +class TestPlotWidgetSelection(PlotWidgetTestCase): + """Test PlotWidget.selection and active items handling""" + + def _checkSelection(self, selection, current=None, selected=()): + """Check current item and selected items.""" + self.assertIs(selection.getCurrentItem(), current) + self.assertEqual(selection.getSelectedItems(), selected) + + def testSyncWithActiveItems(self): + """Test update of PlotWidgetSelection according to active items""" + listener = SignalListener() + + selection = self.plot.selection() + selection.sigCurrentItemChanged.connect(listener) + self._checkSelection(selection) + + # Active item is current + self.plot.addImage(((0, 1), (2, 3)), legend='image') + image = self.plot.getActiveImage() + self.assertEqual(listener.callCount(), 1) + self._checkSelection(selection, image, (image,)) + + # No active = no current + self.plot.setActiveImage(None) + self.assertEqual(listener.callCount(), 2) + self._checkSelection(selection) + + # Active item is current + self.plot.setActiveImage('image') + self.assertEqual(listener.callCount(), 3) + self._checkSelection(selection, image, (image,)) + + # Mosted recently "actived" item is current + self.plot.addScatter((3, 2, 1), (0, 1, 2), (0, 1, 2), legend='scatter') + scatter = self.plot.getActiveScatter() + self.assertEqual(listener.callCount(), 4) + self._checkSelection(selection, scatter, (scatter, image)) + + # Previously mosted recently "actived" item is current + self.plot.setActiveScatter(None) + self.assertEqual(listener.callCount(), 5) + self._checkSelection(selection, image, (image,)) + + # Mosted recently "actived" item is current + self.plot.setActiveScatter('scatter') + self.assertEqual(listener.callCount(), 6) + self._checkSelection(selection, scatter, (scatter, image)) + + # No active = no current + self.plot.setActiveImage(None) + self.plot.setActiveScatter(None) + self.assertEqual(listener.callCount(), 7) + self._checkSelection(selection) + + # Mosted recently "actived" item is current + self.plot.setActiveScatter('scatter') + self.assertEqual(listener.callCount(), 8) + self.plot.setActiveImage('image') + self.assertEqual(listener.callCount(), 9) + self._checkSelection(selection, image, (image, scatter)) + + # Add a curve which is not active by default + self.plot.addCurve((0, 1, 2), (0, 1, 2), legend='curve') + curve = self.plot.getCurve('curve') + self.assertEqual(listener.callCount(), 9) + self._checkSelection(selection, image, (image, scatter)) + + # Mosted recently "actived" item is current + self.plot.setActiveCurve('curve') + self.assertEqual(listener.callCount(), 10) + self._checkSelection(selection, curve, (curve, image, scatter)) + + # Add a curve which is not active by default + self.plot.addCurve((0, 1, 2), (0, 1, 2), legend='curve2') + curve2 = self.plot.getCurve('curve2') + self.assertEqual(listener.callCount(), 10) + self._checkSelection(selection, curve, (curve, image, scatter)) + + # Mosted recently "actived" item is current, previous curve is removed + self.plot.setActiveCurve('curve2') + self.assertEqual(listener.callCount(), 11) + self._checkSelection(selection, curve2, (curve2, image, scatter)) + + # No items = no current + self.plot.clear() + self.assertEqual(listener.callCount(), 12) + self._checkSelection(selection) + + def testPlotWidgetWithItems(self): + """Test init of selection on a plot with items""" + self.plot.addImage(((0, 1), (2, 3)), legend='image') + self.plot.addScatter((3, 2, 1), (0, 1, 2), (0, 1, 2), legend='scatter') + self.plot.addCurve((0, 1, 2), (0, 1, 2), legend='curve') + self.plot.setActiveCurve('curve') + + selection = self.plot.selection() + self.assertIsNotNone(selection.getCurrentItem()) + selected = selection.getSelectedItems() + self.assertEqual(len(selected), 3) + self.assertIn(self.plot.getActiveCurve(), selected) + self.assertIn(self.plot.getActiveImage(), selected) + self.assertIn(self.plot.getActiveScatter(), selected) + + def testSetCurrentItem(self): + """Test setCurrentItem""" + # Add items to the plot + self.plot.addImage(((0, 1), (2, 3)), legend='image') + image = self.plot.getActiveImage() + self.plot.addScatter((3, 2, 1), (0, 1, 2), (0, 1, 2), legend='scatter') + scatter = self.plot.getActiveScatter() + self.plot.addCurve((0, 1, 2), (0, 1, 2), legend='curve') + self.plot.setActiveCurve('curve') + curve = self.plot.getActiveCurve() + + selection = self.plot.selection() + self.assertIsNotNone(selection.getCurrentItem()) + self.assertEqual(len(selection.getSelectedItems()), 3) + + # Set current to None reset all active items + selection.setCurrentItem(None) + self._checkSelection(selection) + self.assertIsNone(self.plot.getActiveCurve()) + self.assertIsNone(self.plot.getActiveImage()) + self.assertIsNone(self.plot.getActiveScatter()) + + # Set current to an item makes it active + selection.setCurrentItem(image) + self._checkSelection(selection, image, (image,)) + self.assertIsNone(self.plot.getActiveCurve()) + self.assertIs(self.plot.getActiveImage(), image) + self.assertIsNone(self.plot.getActiveScatter()) + + # Set current to an item makes it active and keeps other active + selection.setCurrentItem(curve) + self._checkSelection(selection, curve, (curve, image)) + self.assertIs(self.plot.getActiveCurve(), curve) + self.assertIs(self.plot.getActiveImage(), image) + self.assertIsNone(self.plot.getActiveScatter()) + + # Set current to an item makes it active and keeps other active + selection.setCurrentItem(scatter) + self._checkSelection(selection, scatter, (scatter, curve, image)) + self.assertIs(self.plot.getActiveCurve(), curve) + self.assertIs(self.plot.getActiveImage(), image) + self.assertIs(self.plot.getActiveScatter(), scatter) + + def suite(): testClasses = (TestPlotWidget, TestPlotImage, @@ -1870,7 +2044,8 @@ def suite(): TestPlotEmptyLog, TestPlotCurveLog, TestPlotImageLog, - TestPlotMarkerLog) + TestPlotMarkerLog, + TestPlotWidgetSelection) test_suite = unittest.TestSuite() |