Skip to content

Commit 9651705

Browse files
committed
renammed and slightly improved annotion tools
1 parent 35741fa commit 9651705

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

plotpy/tests/unit/test_shape_tools.py renamed to plotpy/tests/unit/test_annotation_tools.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import numpy as np
66
import pytest
77
import qtpy.QtCore as QC
8-
from guidata.qthelpers import exec_dialog, qt_app_context
8+
from guidata.qthelpers import exec_dialog, execenv, qt_app_context
99

10-
from plotpy.interfaces.items import IShapeItemType
10+
from plotpy.interfaces.items import IBasePlotItem, IShapeItemType
11+
from plotpy.items.image.base import BaseImageItem
1112
from plotpy.tests import vistools as ptv
1213
from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend
1314
from plotpy.tools import (
@@ -22,24 +23,27 @@
2223
CrossSectionTool,
2324
EllipseTool,
2425
ImageStatsTool,
26+
InteractiveTool,
27+
LabelTool,
2528
ObliqueRectangleTool,
2629
PointTool,
2730
RectangleTool,
2831
SegmentTool,
2932
SelectTool,
3033
SnapshotTool,
3134
)
35+
from plotpy.tools.label import LabelTool
3236

3337
if TYPE_CHECKING:
3438
from plotpy.plot.plotwidget import PlotWindow
3539
from plotpy.tools.base import RectangularActionTool
3640

37-
from plotpy.tests.unit.utils import drag_mouse
41+
from plotpy.tests.unit.utils import drag_mouse, keyboard_event, undo_redo
3842

3943
P0 = QC.QPointF(10, 10)
4044
P1 = QC.QPointF(100, 100)
4145

42-
TOOLS = (
46+
TOOLS: tuple[type[InteractiveTool], ...] = (
4347
AnnotatedCircleTool,
4448
AnnotatedEllipseTool,
4549
AnnotatedObliqueRectangleTool,
@@ -56,12 +60,13 @@
5660
PointTool,
5761
RectangleTool,
5862
SegmentTool,
63+
LabelTool,
5964
)
6065

6166
# guitest: show
6267

6368

64-
def create_window(tool_classes: tuple[type[RectangularActionTool], ...]) -> PlotWindow:
69+
def create_window(tool_classes: tuple[type[InteractiveTool], ...]) -> PlotWindow:
6570
"""Create a window with the given tools. The plot contains a curve, an image and a
6671
legend.
6772
@@ -72,6 +77,9 @@ def create_window(tool_classes: tuple[type[RectangularActionTool], ...]) -> Plot
7277
PlotWindow: The window containing the tools.
7378
"""
7479
items = make_curve_image_legend()
80+
img: BaseImageItem = items[0]
81+
img.set_rotatable(True)
82+
img.set_movable(True)
7583
win = ptv.show_items(
7684
items, wintitle="Unit tests for RectangularActionTools", auto_tools=True
7785
)
@@ -84,7 +92,7 @@ def create_window(tool_classes: tuple[type[RectangularActionTool], ...]) -> Plot
8492
return win
8593

8694

87-
def _test_annotation_tools(tool_classes: tuple[type[RectangularActionTool], ...]):
95+
def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
8896
"""Generic test for annotation tool. Simulates a mouse drag on the plot and checks
8997
that the tool is activated and deactivated correctly."""
9098
with qt_app_context(exec_loop=False) as qapp:
@@ -98,27 +106,34 @@ def _test_annotation_tools(tool_classes: tuple[type[RectangularActionTool], ...]
98106
tool.activate()
99107
x_path = np.linspace(0, 0.5, 100)
100108
y_path = np.linspace(0, 0.5, 100)
101-
drag_mouse(win, qapp, x_path, y_path)
109+
with execenv.context(accept_dialogs=True):
110+
drag_mouse(win, qapp, x_path, y_path)
102111
if hasattr(tool_class, "SWITCH_TO_DEFAULT_TOOL"):
103112
assert win.manager.get_default_tool() == default_tool
104-
plot.select_some_items(plot.get_items(item_type=IShapeItemType))
113+
plot.unselect_all()
114+
shape_items: list[IBasePlotItem] = plot.get_items(item_type=IShapeItemType)
115+
plot.select_some_items(shape_items)
105116
select_tool = win.manager.get_tool(SelectTool)
106117
assert select_tool is not None
107118
select_tool.activate()
119+
108120
drag_mouse(win, qapp, np.linspace(0.2, 0.5, 10), np.linspace(0.2, 0.5, 10))
121+
122+
undo_redo(win, qapp)
123+
109124
exec_dialog(win)
110125

111126

112127
@pytest.mark.parametrize("tool", TOOLS)
113-
def test_tool(tool: type[RectangularActionTool]) -> None:
128+
def test_tool(tool: type[InteractiveTool]) -> None:
114129
"""Test a single tool."""
115130
_test_annotation_tools((tool,))
116131

117132

118-
def test_all_tools():
133+
def _test_all_tools():
119134
"""Test all tools."""
120135
_test_annotation_tools(TOOLS)
121136

122137

123138
if __name__ == "__main__":
124-
test_all_tools()
139+
_test_all_tools()

0 commit comments

Comments
 (0)