Skip to content

Commit a7fe935

Browse files
committed
new utilitarian function
1 parent 8fb7e2a commit a7fe935

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

plotpy/tests/unit/utils.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from qwt import QwtPlotItem
1010

1111
from plotpy.interfaces.items import ICurveItemType, IItemType
12+
from plotpy.plot.base import BasePlot
1213
from plotpy.tests import vistools as ptv
1314
from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend
1415
from plotpy.tools import CommandTool, InteractiveTool
@@ -26,6 +27,29 @@
2627
ToolT = TypeVar("ToolT", bound=Union[InteractiveTool, CommandTool])
2728

2829

30+
def rel_pos_to_canvas_pos(
31+
canvas: BasePlot, relative_xy: tuple[float, float]
32+
) -> tuple[QC.QPointF, QC.QPointF]:
33+
"""Computes and return the relative position on the canvas and the corresponding
34+
abolute position in the window.
35+
36+
Args:
37+
canvas: Plot canvas
38+
relative_xy: Relative position on the canvas (0 < xy <1)
39+
40+
Returns:
41+
Tuple of the relative position on the canvas and the corresponding absolute
42+
position in the window.
43+
"""
44+
size = canvas.size()
45+
pos_x, pos_y = (
46+
relative_xy[0] * size.width(),
47+
relative_xy[1] * size.height(),
48+
)
49+
canvas_pos = QC.QPointF(pos_x, pos_y)
50+
return canvas_pos, QC.QPointF(canvas.mapToGlobal(canvas_pos.toPoint()))
51+
52+
2953
def keyboard_event(
3054
win: PlotDialog | PlotWindow,
3155
qapp: QW.QApplication,
@@ -70,22 +94,14 @@ def mouse_event_at_relative_plot_pos(
7094
None
7195
"""
7296
plot = win.manager.get_plot()
73-
74-
plot = win.manager.get_plot()
75-
canvas: QW.QWidget = plot.canvas() # type: ignore
76-
size = canvas.size()
77-
pos_x, pos_y = (
78-
relative_xy[0] * size.width(),
79-
relative_xy[1] * size.height(),
80-
)
81-
canvas_pos = QC.QPointF(pos_x, pos_y)
82-
glob_pos = QC.QPointF(canvas.mapToGlobal(canvas_pos.toPoint()))
97+
canvas = plot.canvas()
98+
canvas_pos, glob_pos = rel_pos_to_canvas_pos(plot, relative_xy)
8399

84100
for type_ in click_types:
85101
mouse_event = QG.QMouseEvent(
86102
type_,
87103
canvas_pos,
88-
glob_pos,
104+
# glob_pos,
89105
QC.Qt.MouseButton.LeftButton,
90106
QC.Qt.MouseButton.LeftButton,
91107
mod,

0 commit comments

Comments
 (0)