|
4 | 4 | from unittest.mock import patch, Mock |
5 | 5 | import numpy as np |
6 | 6 |
|
7 | | -from AnyQt.QtCore import QRectF, Qt |
| 7 | +from AnyQt.QtCore import QRectF, QPointF, Qt |
8 | 8 | from AnyQt.QtGui import QColor, QTransform |
9 | 9 | from AnyQt.QtTest import QSignalSpy |
10 | | - |
11 | 10 | from pyqtgraph import mkPen, mkBrush |
12 | 11 |
|
13 | 12 | from orangewidget.tests.base import GuiTest |
@@ -1364,6 +1363,42 @@ def test_allow_aggregation(self): |
1364 | 1363 | graph.jitter_size = 0 |
1365 | 1364 | self.assertTrue(graph.allow_aggregation()) |
1366 | 1365 |
|
| 1366 | + @patch("AnyQt.QtWidgets.QToolTip.showText") |
| 1367 | + def test_help_event(self, _): |
| 1368 | + master = self.master |
| 1369 | + graph = self.graph |
| 1370 | + event = Mock() |
| 1371 | + |
| 1372 | + graph.scatterplot_item = None |
| 1373 | + self.assertFalse(graph.help_event(event)) |
| 1374 | + |
| 1375 | + scp = graph.scatterplot_item = Mock() |
| 1376 | + scp.mapFromScene = Mock() |
| 1377 | + scp.aggregatedPointsAt = Mock(return_value=[]) |
| 1378 | + master.get_aggregated_tooltip = Mock() |
| 1379 | + scp.pointsAt = Mock(return_value=[]) |
| 1380 | + master.get_tooltip = Mock() |
| 1381 | + |
| 1382 | + self.assertFalse(graph.help_event(event)) |
| 1383 | + |
| 1384 | + mv = [Mock(), Mock()] |
| 1385 | + scp.pointsAt = Mock(return_value=mv) |
| 1386 | + self.assertTrue(graph.help_event(event)) |
| 1387 | + master.get_aggregated_tooltip.assert_not_called() |
| 1388 | + master.get_tooltip.assert_called_with([mv[0].data.return_value, |
| 1389 | + mv[1].data.return_value]) |
| 1390 | + master.get_aggregated_tooltip.reset_mock() |
| 1391 | + scp.pointsAt.reset_mock() |
| 1392 | + master.get_tooltip.reset_mock() |
| 1393 | + |
| 1394 | + scp.aggregatedPointsAt = Mock(return_value=mv) |
| 1395 | + self.assertTrue(graph.help_event(event)) |
| 1396 | + master.get_tooltip.assert_not_called() |
| 1397 | + scp.pointsAt.assert_not_called() |
| 1398 | + master.get_aggregated_tooltip.assert_called_with([mv[0].data.return_value, |
| 1399 | + mv[1].data.return_value]) |
| 1400 | + |
| 1401 | + |
1367 | 1402 | def test_show_grid(self): |
1368 | 1403 | graph = self.graph |
1369 | 1404 | show_grid = self.graph.plot_widget.showGrid = Mock() |
@@ -1732,43 +1767,86 @@ def test_get_aggregate_points(self): |
1732 | 1767 | painter = Mock() |
1733 | 1768 | painter.transform = lambda: QTransform(1, 0, 0, 0, -1, 0, 0, 0, 1) |
1734 | 1769 |
|
1735 | | - self.assertEqual(scp._get_aggregated_points(painter), (None, None)) |
| 1770 | + self.assertIsNone(scp._get_aggregated_points(painter)) |
1736 | 1771 | np.testing.assert_equal(scp.data["visible"], np.ones(n)) |
1737 | 1772 |
|
1738 | 1773 | scp.setAggregation(True) |
1739 | | - pts, colors = scp._get_aggregated_points(painter) |
| 1774 | + scp._nonaggregated = True |
| 1775 | + colors = scp._get_aggregated_points(painter) |
| 1776 | + pts = scp._agg_coords |
1740 | 1777 | np.testing.assert_almost_equal(pts, [[ 1.16666667, -3.06666667]]) |
1741 | 1778 | self.assertEqual(colors, [{(0, 0, 0, 255): 2, (10, 10, 10, 255): 1}]) |
1742 | | - np.testing.assert_equal(scp.data["visible"], [0, 0, 0, 1, 1, 1, 1]) |
| 1779 | + np.testing.assert_equal(scp._nonaggregated, [0, 0, 0, 1, 1, 1, 1]) |
1743 | 1780 |
|
1744 | 1781 | scp._aggregation_threshold = 2 |
1745 | | - pts, colors = scp._get_aggregated_points(painter) |
| 1782 | + scp._nonaggregated = True |
| 1783 | + colors = scp._get_aggregated_points(painter) |
| 1784 | + pts = scp._agg_coords |
1746 | 1785 | np.testing.assert_almost_equal(pts, [[1.16666667, -3.06666667], |
1747 | 1786 | [24.25, -100]]) |
1748 | 1787 | self.assertEqual(colors, [ |
1749 | 1788 | {(0, 0, 0, 255): 2, (10, 10, 10, 255): 1}, |
1750 | 1789 | {(20, 20, 20, 255): 1, (40, 40, 40, 255): 1} |
1751 | 1790 | ]) |
1752 | | - np.testing.assert_equal(scp.data["visible"], [0, 0, 0, 0, 1, 0, 1]) |
| 1791 | + np.testing.assert_equal(scp._nonaggregated, [0, 0, 0, 0, 1, 0, 1]) |
1753 | 1792 | scp.data["visible"] = np.ones(n) |
1754 | 1793 |
|
1755 | 1794 | scp._aggregation_threshold = 4 |
1756 | | - self.assertEqual(scp._get_aggregated_points(painter), (None, None)) |
1757 | | - np.testing.assert_equal(scp.data["visible"], np.ones(n)) |
| 1795 | + scp._nonaggregated = True |
| 1796 | + self.assertIsNone(scp._get_aggregated_points(painter)) |
| 1797 | + np.testing.assert_equal(scp._nonaggregated, np.ones(n)) |
1758 | 1798 |
|
1759 | 1799 | def test_paint_aggregated_points(self): |
1760 | 1800 | scp = ScatterPlotItem() |
1761 | 1801 | painter = Mock() |
1762 | 1802 |
|
1763 | | - pts = [[1, 2], [3, 4]] |
| 1803 | + scp._agg_coordspts = [[1, 2], [3, 4]] |
1764 | 1804 | colors = [ |
1765 | 1805 | {(0, 0, 0, 255): 2, (10, 10, 10, 255): 1}, |
1766 | 1806 | {(20, 20, 20, 255): 3} |
1767 | 1807 | ] |
1768 | 1808 |
|
1769 | 1809 | # Well ... don't crash, OK? |
1770 | | - scp._paint_aggregated_points(painter, pts, colors) |
1771 | | - scp._paint_aggregated_points(painter, None, None) |
| 1810 | + scp._paint_aggregated_points(painter, colors) |
| 1811 | + scp._paint_aggregated_points(painter, None) |
| 1812 | + |
| 1813 | + def test_pointsAt(self): |
| 1814 | + x, y = np.array([[1, 3], [1, 3], [1.5, 3.2], |
| 1815 | + [24, 100], |
| 1816 | + [200, 1], |
| 1817 | + [24.5, 100], [200, 20]]).T |
| 1818 | + n = len(x) |
| 1819 | + scp = ScatterPlotItem(x=x, y=y) |
| 1820 | + scp.setBrush([mkBrush(QColor(0, 0, 0))] + |
| 1821 | + [mkBrush(QColor(10 * i, 10 * i, 10 * i)) for i in range(n - 1)]) |
| 1822 | + scp.setPointData(np.arange(n)) |
| 1823 | + scp._agg_size_default = 10 |
| 1824 | + scp._maskAt = Mock(return_value=np.arange(n - 1)) |
| 1825 | + painter = Mock() |
| 1826 | + painter.transform = lambda: QTransform(1, 0, 0, 0, -1, 0, 0, 0, 1) |
| 1827 | + scp.setAggregation(True) |
| 1828 | + scp._aggregation_threshold = 2 |
| 1829 | + |
| 1830 | + scp._nonaggregated = True |
| 1831 | + agg_colors = scp._get_aggregated_points(painter) |
| 1832 | + scp._paint_aggregated_points(painter, agg_colors) |
| 1833 | + |
| 1834 | + self.assertEqual( |
| 1835 | + {p.data() for p in scp.aggregatedPointsAt(QPointF(1.2, 1.3))}, {0, 1, 2}) |
| 1836 | + self.assertEqual( |
| 1837 | + len(scp.aggregatedPointsAt(QPointF(200, 1))), 0) |
| 1838 | + |
| 1839 | + def test_mask(*_, **__): |
| 1840 | + np.testing.assert_equal( |
| 1841 | + scp.data["visible"], |
| 1842 | + [False, False, False, False, True, False, True]) |
| 1843 | + |
| 1844 | + with patch( |
| 1845 | + "pyqtgraph.graphicsItems.ScatterPlotItem.ScatterPlotItem.pointsAt", |
| 1846 | + new=test_mask): |
| 1847 | + scp.pointsAt(QPointF(1.2, 1.3)) |
| 1848 | + np.testing.assert_equal(scp.data["visible"], True) |
| 1849 | + |
1772 | 1850 |
|
1773 | 1851 |
|
1774 | 1852 | if __name__ == "__main__": |
|
0 commit comments