Skip to content

Commit ee2081f

Browse files
committed
New ActionTool (+ associated test)
1 parent 11c6565 commit ee2081f

4 files changed

Lines changed: 90 additions & 3 deletions

File tree

doc/features/tools/reference.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Reference
22
---------
33

4-
.. autoclass:: plotpy.tools.base.GuiTool
4+
.. autoclass:: plotpy.tools.GuiTool
55
:members:
6-
.. autoclass:: plotpy.tools.base.InteractiveTool
6+
.. autoclass:: plotpy.tools.InteractiveTool
77
:members:
8-
.. autoclass:: plotpy.tools.base.CommandTool
8+
.. autoclass:: plotpy.tools.CommandTool
9+
:members:
10+
.. autoclass:: plotpy.tools.ActionTool
911
:members:
1012
.. autoclass:: plotpy.tools.RectZoomTool
1113
:members:
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Licensed under the terms of the BSD 3-Clause
4+
# (see plotpy/LICENSE for details)
5+
6+
"""ActionTool test"""
7+
8+
# guitest: show
9+
10+
from guidata.qthelpers import create_action, qt_app_context
11+
from qtpy import QtWidgets as QW
12+
13+
from plotpy.builder import make
14+
from plotpy.items import ImageItem
15+
from plotpy.plot import PlotDialog, PlotOptions
16+
from plotpy.tests.data import gen_image4
17+
from plotpy.tools import ActionTool
18+
19+
20+
class MyPlotDialog(PlotDialog):
21+
def __init__(self):
22+
"""Reimplement PlotDialog method"""
23+
super().__init__(
24+
title="ActionTool test", toolbar=True, options=PlotOptions(type="image")
25+
)
26+
self.info_action = create_action(self, "Show infos", triggered=self.show_info)
27+
self.manager.add_tool(ActionTool, self.info_action, item_types=(ImageItem,))
28+
29+
def show_info(self):
30+
"""Show infos on selected item(s)"""
31+
# This is just a demo of what can be done with ActionTool
32+
plot = self.get_plot()
33+
for item in plot.get_selected_items():
34+
QW.QMessageBox.information(self, "Item infos", str(item))
35+
36+
37+
def test_image_plot_tools():
38+
"""Test"""
39+
with qt_app_context(exec_loop=True):
40+
win = MyPlotDialog()
41+
win.show()
42+
image = make.image(gen_image4(500, 500), colormap="Spectral")
43+
plot = win.manager.get_plot()
44+
plot.add_item(image)
45+
46+
47+
if __name__ == "__main__":
48+
test_image_plot_tools()

plotpy/tools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
)
1313
from .axes import AxisScaleTool, PlaceAxesTool
1414
from .base import (
15+
ActionTool,
1516
CommandTool,
1617
DefaultToolbarID,
18+
GuiTool,
1719
InteractiveTool,
1820
PanelTool,
1921
RectangularActionTool,

plotpy/tools/base.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,41 @@ def set_status_active_item(self, plot):
255255
self.action.setEnabled(False)
256256

257257

258+
class ActionTool(CommandTool):
259+
"""Tool that simply associate an action to a tool"""
260+
261+
def __init__(
262+
self,
263+
manager,
264+
action,
265+
item_types=None,
266+
toolbar_id=DefaultToolbarID,
267+
):
268+
self.associated_action = action
269+
self.item_types = item_types
270+
super().__init__(
271+
manager,
272+
action.text(),
273+
action.icon(),
274+
action.toolTip(),
275+
toolbar_id=toolbar_id,
276+
)
277+
278+
def update_status(self, plot):
279+
"""Update tool status"""
280+
if self.item_types is None:
281+
self.action.setEnabled(True)
282+
else:
283+
items = plot.get_selected_items()
284+
self.action.setEnabled(
285+
any(isinstance(item, self.item_types) for item in items)
286+
)
287+
288+
def create_action(self, manager):
289+
"""Create and return tool's action"""
290+
return self.associated_action
291+
292+
258293
class ToggleTool(CommandTool):
259294
""" """
260295

0 commit comments

Comments
 (0)