|
| 1 | +# guitest: show |
| 2 | + |
| 3 | +from typing import Callable |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import qtpy.QtCore as QC |
| 7 | +import qtpy.QtGui as QG |
| 8 | +import qtpy.QtWidgets as QW |
| 9 | +from guidata.qthelpers import exec_dialog, qt_app_context |
| 10 | +from numpy import linspace |
| 11 | + |
| 12 | +from plotpy.plot.plotwidget import PlotDialog |
| 13 | +from plotpy.tests.unit.test_point_tools import ( |
| 14 | + CLICK, |
| 15 | + create_window, |
| 16 | + drag_mouse, |
| 17 | + keyboard_event, |
| 18 | + mouse_event_at_relative_plot_pos, |
| 19 | +) |
| 20 | +from plotpy.tools import FreeFormTool, MultiLineTool, RectZoomTool |
| 21 | + |
| 22 | + |
| 23 | +def zoom( |
| 24 | + x_path: np.ndarray, y_path: np.ndarray, compare: Callable[[float, float], bool] |
| 25 | +): |
| 26 | + with qt_app_context(exec_loop=False) as qapp: |
| 27 | + win, tool = create_window(RectZoomTool) |
| 28 | + plot = win.manager.get_plot() |
| 29 | + initial_axis_limits = [ |
| 30 | + plot.get_axis_limits(axis) for axis in plot.get_active_axes() |
| 31 | + ] |
| 32 | + |
| 33 | + drag_mouse(win, qapp, x_path, y_path) |
| 34 | + |
| 35 | + final_axis_limits = [ |
| 36 | + plot.get_axis_limits(axis) for axis in plot.get_active_axes() |
| 37 | + ] |
| 38 | + |
| 39 | + assert all( |
| 40 | + map( |
| 41 | + lambda og, final: compare(og[1], final[1]), |
| 42 | + initial_axis_limits, |
| 43 | + final_axis_limits, |
| 44 | + ) |
| 45 | + ) |
| 46 | + exec_dialog(win) |
| 47 | + |
| 48 | + |
| 49 | +def test_rect_zoom_tool(): |
| 50 | + x_path = linspace(0, 0.5, 100) |
| 51 | + y_path = linspace(0, 0.5, 100) |
| 52 | + zoom(x_path, y_path, lambda og, final: final < og) |
| 53 | + |
| 54 | + |
| 55 | +def test_rect_unzoom_tool(): |
| 56 | + x_path = linspace(-0.5, 1.5, 100) |
| 57 | + y_path = linspace(-0.5, 1.5, 100) |
| 58 | + zoom(x_path, y_path, lambda og, final: final > og) |
| 59 | + |
| 60 | + |
| 61 | +def test_free_form_tool(): |
| 62 | + corners = np.array(((0.1, 0.1), (0.1, 0.8), (0.8, 0.8), (0.8, 0.1))) |
| 63 | + with qt_app_context(exec_loop=False) as qapp: |
| 64 | + win, tool = create_window(FreeFormTool) |
| 65 | + |
| 66 | + # drag_mouse(win, qapp, x_path, y_path) |
| 67 | + for x, y in corners: |
| 68 | + mouse_event_at_relative_plot_pos(win, qapp, (x, y), CLICK) |
| 69 | + |
| 70 | + assert tool.shape is not None |
| 71 | + |
| 72 | + assert tool.shape.get_points().shape == corners.shape |
| 73 | + |
| 74 | + exec_dialog(win) |
| 75 | + |
| 76 | + |
| 77 | +def test_multiline_tool(): |
| 78 | + n = 100 |
| 79 | + t = np.linspace(0, np.pi * 10, n) |
| 80 | + |
| 81 | + # Create x and y arrays |
| 82 | + x_arr = t * np.cos(t) / n + 0.5 |
| 83 | + y_arr = t * np.sin(t) / n + 0.5 |
| 84 | + |
| 85 | + with qt_app_context(exec_loop=False) as qapp: |
| 86 | + win, tool = create_window(MultiLineTool) |
| 87 | + |
| 88 | + # drag_mouse(win, qapp, x_path, y_path) |
| 89 | + for x, y in zip(x_arr, y_arr): |
| 90 | + mouse_event_at_relative_plot_pos(win, qapp, (x, y), CLICK) |
| 91 | + |
| 92 | + assert tool.shape is not None |
| 93 | + assert tool.shape.get_points().shape == np.array([x_arr, y_arr]).T.shape |
| 94 | + |
| 95 | + # Delete last point |
| 96 | + keyboard_event(win, qapp, QC.Qt.Key.Key_Backspace) |
| 97 | + |
| 98 | + points_count, _ = tool.shape.get_points().shape |
| 99 | + |
| 100 | + assert points_count == (n - 1) |
| 101 | + |
| 102 | + exec_dialog(win) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + test_rect_zoom_tool() |
| 107 | + test_rect_unzoom_tool() |
| 108 | + test_free_form_tool() |
| 109 | + test_multiline_tool() |
0 commit comments