mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
refactor(plot_base): toolbar buttons adapted for the Switch actions from toolbar; plot export and mouse modes consolidated into one switch button
This commit is contained in:
@ -1,7 +1,8 @@
|
|||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
|
from qtpy.QtCore import QTimer
|
||||||
|
|
||||||
from bec_widgets.qt_utils.error_popups import SafeSlot
|
from bec_widgets.qt_utils.error_popups import SafeSlot
|
||||||
from bec_widgets.qt_utils.toolbar import MaterialIconAction, ToolbarBundle
|
from bec_widgets.qt_utils.toolbar import MaterialIconAction, SwitchableToolBarAction, ToolbarBundle
|
||||||
|
|
||||||
|
|
||||||
class MouseInteractionToolbarBundle(ToolbarBundle):
|
class MouseInteractionToolbarBundle(ToolbarBundle):
|
||||||
@ -15,6 +16,7 @@ class MouseInteractionToolbarBundle(ToolbarBundle):
|
|||||||
def __init__(self, bundle_id="mouse_interaction", target_widget=None, **kwargs):
|
def __init__(self, bundle_id="mouse_interaction", target_widget=None, **kwargs):
|
||||||
super().__init__(bundle_id=bundle_id, actions=[], **kwargs)
|
super().__init__(bundle_id=bundle_id, actions=[], **kwargs)
|
||||||
self.target_widget = target_widget
|
self.target_widget = target_widget
|
||||||
|
self.mouse_mode = None
|
||||||
|
|
||||||
# Create each MaterialIconAction with a parent
|
# Create each MaterialIconAction with a parent
|
||||||
# so the signals can fire even if the toolbar isn't added yet.
|
# so the signals can fire even if the toolbar isn't added yet.
|
||||||
@ -43,9 +45,16 @@ class MouseInteractionToolbarBundle(ToolbarBundle):
|
|||||||
parent=self.target_widget,
|
parent=self.target_widget,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.switch_mouse_action = SwitchableToolBarAction(
|
||||||
|
actions={"drag_mode": drag, "rectangle_mode": rect},
|
||||||
|
initial_action="drag_mode",
|
||||||
|
tooltip="Mouse Modes",
|
||||||
|
checkable=True,
|
||||||
|
parent=self,
|
||||||
|
)
|
||||||
|
|
||||||
# Add them to the bundle
|
# Add them to the bundle
|
||||||
self.add_action("drag_mode", drag)
|
self.add_action("switch_mouse", self.switch_mouse_action)
|
||||||
self.add_action("rectangle_mode", rect)
|
|
||||||
self.add_action("auto_range", auto)
|
self.add_action("auto_range", auto)
|
||||||
self.add_action("aspect_ratio", aspect_ratio)
|
self.add_action("aspect_ratio", aspect_ratio)
|
||||||
|
|
||||||
@ -55,33 +64,34 @@ class MouseInteractionToolbarBundle(ToolbarBundle):
|
|||||||
auto.action.triggered.connect(self.autorange_plot)
|
auto.action.triggered.connect(self.autorange_plot)
|
||||||
aspect_ratio.action.toggled.connect(self.lock_aspect_ratio)
|
aspect_ratio.action.toggled.connect(self.lock_aspect_ratio)
|
||||||
|
|
||||||
mode = self.get_viewbox_mode()
|
# Give some time to check the state
|
||||||
if mode == "PanMode":
|
QTimer.singleShot(10, self.get_viewbox_mode)
|
||||||
drag.action.setChecked(True)
|
|
||||||
elif mode == "RectMode":
|
|
||||||
rect.action.setChecked(True)
|
|
||||||
|
|
||||||
def get_viewbox_mode(self) -> str:
|
def get_viewbox_mode(self):
|
||||||
|
"""
|
||||||
|
Returns the current interaction mode of a PyQtGraph ViewBox and sets the corresponding action.
|
||||||
"""
|
"""
|
||||||
Returns the current interaction mode of a PyQtGraph ViewBox.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: "PanMode" if pan is enabled, "RectMode" if zoom is enabled, "Unknown" otherwise.
|
|
||||||
"""
|
|
||||||
if self.target_widget:
|
if self.target_widget:
|
||||||
viewbox = self.target_widget.plot_item.getViewBox()
|
viewbox = self.target_widget.plot_item.getViewBox()
|
||||||
if viewbox.getState()["mouseMode"] == 3:
|
if viewbox.getState()["mouseMode"] == 3:
|
||||||
return "PanMode"
|
self.switch_mouse_action.set_default_action("drag_mode")
|
||||||
|
self.switch_mouse_action.main_button.setChecked(True)
|
||||||
|
self.mouse_mode = "PanMode"
|
||||||
elif viewbox.getState()["mouseMode"] == 1:
|
elif viewbox.getState()["mouseMode"] == 1:
|
||||||
return "RectMode"
|
self.switch_mouse_action.set_default_action("rectangle_mode")
|
||||||
return "Unknown"
|
self.switch_mouse_action.main_button.setChecked(True)
|
||||||
|
self.mouse_mode = "RectMode"
|
||||||
|
|
||||||
@SafeSlot(bool)
|
@SafeSlot(bool)
|
||||||
def enable_mouse_rectangle_mode(self, checked: bool):
|
def enable_mouse_rectangle_mode(self, checked: bool):
|
||||||
"""
|
"""
|
||||||
Enable the rectangle zoom mode on the plot widget.
|
Enable the rectangle zoom mode on the plot widget.
|
||||||
"""
|
"""
|
||||||
self.actions["drag_mode"].action.setChecked(not checked)
|
if self.mouse_mode == "RectMode":
|
||||||
|
self.switch_mouse_action.main_button.setChecked(True)
|
||||||
|
return
|
||||||
|
self.actions["switch_mouse"].actions["drag_mode"].action.setChecked(not checked)
|
||||||
if self.target_widget and checked:
|
if self.target_widget and checked:
|
||||||
self.target_widget.plot_item.getViewBox().setMouseMode(pg.ViewBox.RectMode)
|
self.target_widget.plot_item.getViewBox().setMouseMode(pg.ViewBox.RectMode)
|
||||||
|
|
||||||
@ -90,7 +100,10 @@ class MouseInteractionToolbarBundle(ToolbarBundle):
|
|||||||
"""
|
"""
|
||||||
Enable the pan mode on the plot widget.
|
Enable the pan mode on the plot widget.
|
||||||
"""
|
"""
|
||||||
self.actions["rectangle_mode"].action.setChecked(not checked)
|
if self.mouse_mode == "PanMode":
|
||||||
|
self.switch_mouse_action.main_button.setChecked(True)
|
||||||
|
return
|
||||||
|
self.actions["switch_mouse"].actions["rectangle_mode"].action.setChecked(not checked)
|
||||||
if self.target_widget and checked:
|
if self.target_widget and checked:
|
||||||
self.target_widget.plot_item.getViewBox().setMouseMode(pg.ViewBox.PanMode)
|
self.target_widget.plot_item.getViewBox().setMouseMode(pg.ViewBox.PanMode)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from pyqtgraph.exporters import MatplotlibExporter
|
from pyqtgraph.exporters import MatplotlibExporter
|
||||||
|
|
||||||
from bec_widgets.qt_utils.error_popups import SafeSlot, WarningPopupUtility
|
from bec_widgets.qt_utils.error_popups import SafeSlot, WarningPopupUtility
|
||||||
from bec_widgets.qt_utils.toolbar import MaterialIconAction, ToolbarBundle
|
from bec_widgets.qt_utils.toolbar import MaterialIconAction, SwitchableToolBarAction, ToolbarBundle
|
||||||
|
|
||||||
|
|
||||||
class PlotExportBundle(ToolbarBundle):
|
class PlotExportBundle(ToolbarBundle):
|
||||||
@ -25,9 +25,16 @@ class PlotExportBundle(ToolbarBundle):
|
|||||||
icon_name="photo_library", tooltip="Open Matplotlib Dialog", parent=self.target_widget
|
icon_name="photo_library", tooltip="Open Matplotlib Dialog", parent=self.target_widget
|
||||||
)
|
)
|
||||||
|
|
||||||
|
switch_export_action = SwitchableToolBarAction(
|
||||||
|
actions={"save": save, "matplotlib": matplotlib},
|
||||||
|
initial_action="save",
|
||||||
|
tooltip="Switchable Action",
|
||||||
|
checkable=False,
|
||||||
|
parent=self,
|
||||||
|
)
|
||||||
|
|
||||||
# Add them to the bundle
|
# Add them to the bundle
|
||||||
self.add_action("save", save)
|
self.add_action("export_switch", switch_export_action)
|
||||||
self.add_action("matplotlib", matplotlib)
|
|
||||||
|
|
||||||
# Immediately connect signals
|
# Immediately connect signals
|
||||||
save.action.triggered.connect(self.export_dialog)
|
save.action.triggered.connect(self.export_dialog)
|
||||||
|
Reference in New Issue
Block a user