|
| 1 | +# -*- coding: utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +selectdialog |
| 5 | +------------ |
| 6 | +
|
| 7 | +The `selectdialog` module provides a dialog box to select an area of the plot |
| 8 | +using a tool: |
| 9 | +
|
| 10 | + * :py:class:`.widgets.selectdialog.SelectDialog`: dialog box |
| 11 | + * :py:func:`.widgets.selectdialog.select_with_shape_tool`: function to |
| 12 | + select an area with a shape tool and return the rectangle |
| 13 | + * :py:func:`.widgets.selectdialog.set_items_unselectable`: function to set |
| 14 | + items unselectable except for the given item |
| 15 | +
|
| 16 | +Example: get segment |
| 17 | +~~~~~~~~~~~~~~~~~~~~ |
| 18 | +
|
| 19 | +.. literalinclude:: ../../plotpy/tests/gui/test_get_segment.py |
| 20 | +
|
| 21 | +Example: get rectangle |
| 22 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | +
|
| 24 | +.. literalinclude:: ../../plotpy/tests/gui/test_get_rectangle.py |
| 25 | +
|
| 26 | +Reference |
| 27 | +~~~~~~~~~ |
| 28 | +
|
| 29 | +.. autoclass:: SelectDialog |
| 30 | + :members: |
| 31 | +.. autofunction:: select_with_shape_tool |
| 32 | +.. autofunction:: set_items_unselectable |
| 33 | +""" |
| 34 | + |
| 35 | +from __future__ import annotations |
| 36 | + |
| 37 | +from typing import TYPE_CHECKING |
| 38 | + |
| 39 | +from guidata.configtools import get_icon |
| 40 | +from guidata.qthelpers import exec_dialog |
| 41 | +from qtpy import QtWidgets as QW |
| 42 | +from qtpy.QtWidgets import QWidget # only to help intersphinx find QWidget |
| 43 | +from qwt.plot import QwtPlotItem |
| 44 | + |
| 45 | +from plotpy.config import _ |
| 46 | +from plotpy.items import AbstractShape, ImageItem |
| 47 | +from plotpy.panels.base import PanelWidget |
| 48 | +from plotpy.plot import BasePlot, PlotDialog, PlotOptions |
| 49 | +from plotpy.tools import RectangularShapeTool, SelectTool |
| 50 | + |
| 51 | +if TYPE_CHECKING: # pragma: no cover |
| 52 | + from plotpy.panels.base import PanelWidget |
| 53 | + |
| 54 | + |
| 55 | +class SelectDialog(PlotDialog): |
| 56 | + """Plot dialog box to select an area of the plot using a tool |
| 57 | +
|
| 58 | + Args: |
| 59 | + parent: parent widget |
| 60 | + toolbar: show/hide toolbar |
| 61 | + options: plot options |
| 62 | + panels: additionnal panels |
| 63 | + auto_tools: If True, the plot tools are automatically registered. |
| 64 | + If False, the user must register the tools manually. |
| 65 | + title: The window title |
| 66 | + icon: The window icon |
| 67 | + edit: If True, the plot is editable |
| 68 | + """ |
| 69 | + |
| 70 | + def __init__( |
| 71 | + self, |
| 72 | + parent: QWidget | None = None, |
| 73 | + toolbar: bool = False, |
| 74 | + options: PlotOptions | None = None, |
| 75 | + panels: list[PanelWidget] | None = None, |
| 76 | + auto_tools: bool = True, |
| 77 | + title: str = "PlotPy", |
| 78 | + icon: str = "plotpy.svg", |
| 79 | + edit: bool = False, |
| 80 | + ) -> None: |
| 81 | + super().__init__( |
| 82 | + parent, toolbar, options, panels, auto_tools, title, icon, edit |
| 83 | + ) |
| 84 | + self.sel_tool: RectangularShapeTool | None = None |
| 85 | + |
| 86 | + def set_image_and_tool( |
| 87 | + self, item: ImageItem, toolclass: RectangularShapeTool, **kwargs |
| 88 | + ) -> None: |
| 89 | + """Set the image item to be displayed and the tool to be used |
| 90 | +
|
| 91 | + Args: |
| 92 | + item: Image item |
| 93 | + toolclass: Tool class |
| 94 | + kwargs: Keyword arguments for the tool class |
| 95 | + """ |
| 96 | + default = self.manager.add_tool(SelectTool) |
| 97 | + self.manager.set_default_tool(default) |
| 98 | + self.sel_tool: RectangularShapeTool = self.manager.add_tool( |
| 99 | + toolclass, |
| 100 | + switch_to_default_tool=True, |
| 101 | + **kwargs, |
| 102 | + ) # pylint: disable=attribute-defined-outside-init |
| 103 | + self.sel_tool.activate() |
| 104 | + set_ok_btn_enabled = self.button_box.button(QW.QDialogButtonBox.Ok).setEnabled |
| 105 | + set_ok_btn_enabled(False) |
| 106 | + self.sel_tool.SIG_TOOL_JOB_FINISHED.connect(lambda: set_ok_btn_enabled(True)) |
| 107 | + plot = self.get_plot() |
| 108 | + plot.add_item(item) |
| 109 | + plot.set_active_item(item) |
| 110 | + item.set_selectable(False) |
| 111 | + item.set_readonly(True) |
| 112 | + plot.unselect_item(item) |
| 113 | + |
| 114 | + def get_new_shape(self) -> AbstractShape: |
| 115 | + """Get newly created shape |
| 116 | +
|
| 117 | + Returns: |
| 118 | + Newly created shape |
| 119 | + """ |
| 120 | + return self.sel_tool.get_last_final_shape() |
| 121 | + |
| 122 | + |
| 123 | +def set_items_unselectable(plot: BasePlot, except_item: QwtPlotItem = None) -> None: |
| 124 | + """Set items unselectable except for the given item""" |
| 125 | + for item_i in plot.get_items(): |
| 126 | + if except_item is None: |
| 127 | + item_i.set_selectable(False) |
| 128 | + else: |
| 129 | + item_i.set_selectable(item_i is except_item) |
| 130 | + |
| 131 | + |
| 132 | +def select_with_shape_tool( |
| 133 | + parent: QW.QWidget, |
| 134 | + toolclass: RectangularShapeTool, |
| 135 | + item: ImageItem, |
| 136 | + title: str = None, |
| 137 | + size: tuple[int, int] = None, |
| 138 | + other_items: list[QwtPlotItem] = [], |
| 139 | + tooldialogclass: SelectDialog = SelectDialog, |
| 140 | + icon=None, |
| 141 | + **kwargs, |
| 142 | +) -> AbstractShape: |
| 143 | + """Select an area with a shape tool and return the rectangle |
| 144 | +
|
| 145 | + Args: |
| 146 | + parent: Parent widget |
| 147 | + toolclass: Tool class |
| 148 | + item: Image item |
| 149 | + title: Dialog title |
| 150 | + size: Dialog size |
| 151 | + other_items: Other items to be displayed |
| 152 | + tooldialogclass: Tool dialog class |
| 153 | + icon: Icon |
| 154 | + kwargs: Keyword arguments for the tool class |
| 155 | +
|
| 156 | + Returns: |
| 157 | + Selected shape |
| 158 | + """ |
| 159 | + if title is None: |
| 160 | + title = "Select an area then press OK to accept" |
| 161 | + if icon is not None: |
| 162 | + icon = get_icon(icon) if isinstance(icon, str) else icon |
| 163 | + win: SelectDialog = tooldialogclass(parent, title=title, edit=True, icon=icon) |
| 164 | + win.set_image_and_tool(item, toolclass, **kwargs) |
| 165 | + plot = win.get_plot() |
| 166 | + for other_item in other_items: |
| 167 | + plot.add_item(other_item) |
| 168 | + set_items_unselectable(plot) |
| 169 | + if size is not None: |
| 170 | + win.resize(*size) |
| 171 | + win.show() |
| 172 | + if exec_dialog(win): |
| 173 | + return win.get_new_shape() |
| 174 | + return None |
0 commit comments