From afa917c163765b7972696f5f516e961032a5fe64 Mon Sep 17 00:00:00 2001 From: CPD Date: Tue, 4 Mar 2025 18:06:12 +0100 Subject: [PATCH] Add toolbar --- app/ui/main_window.py | 1 - app/ui/widgets/toolbar.py | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 app/ui/widgets/toolbar.py diff --git a/app/ui/main_window.py b/app/ui/main_window.py index 8292a01..c33c9d1 100644 --- a/app/ui/main_window.py +++ b/app/ui/main_window.py @@ -5,7 +5,6 @@ from PyQt6.QtWidgets import QToolBox from PyQt6.QtGui import QIcon from .widgets.menubar import MenuBar from .widgets.toolbar import ToolBar -from .widgets.statusbar import StatusBar from .widgets.metadata_input import MetadataInput from .widgets.measurement_settings import ScriptSelection, MeasurementSettings from .widgets.plot import Plot diff --git a/app/ui/widgets/toolbar.py b/app/ui/widgets/toolbar.py new file mode 100644 index 0000000..be09f9b --- /dev/null +++ b/app/ui/widgets/toolbar.py @@ -0,0 +1,80 @@ +from PyQt6.QtCore import Qt, QSize +from PyQt6.QtGui import QAction, QIcon +from PyQt6.QtWidgets import QToolBar, QWidget, QSizePolicy + + +class ToolBar(QToolBar): + def __init__(self, parent, + orientation: Qt.Orientation = Qt.Orientation.Horizontal, + style: Qt.ToolButtonStyle = Qt.ToolButtonStyle.ToolButtonTextUnderIcon, + icon_size: tuple[int, int] = (32, 32)) -> None: + """ + Initialize the toolbar. + + Parameters + ---------- + parent : QWidget + The parent widget. + orientation : Qt.Orientation, optional + The toolbar's orientation, by default Qt.Orientation.Horizontal. + style : Qt.ToolButtonStyle, optional + The toolbar's tool button style, by default Qt.ToolButtonStyle.ToolButtonTextUnderIcon. + icon_size : tuple[int, int], optional + The toolbar's icon size, by default (32, 32). + """ + super().__init__(parent) + self.actions_call = {} + self.setOrientation(orientation) + + self.setToolButtonStyle(style) + self.setIconSize(QSize(icon_size[0], icon_size[1])) + + def add_button(self, key: str, text: str, icon: str, trigger_action) -> None: + """ + Add a button to the toolbar. + + Parameters + ---------- + key : str + Buttons key. + text : str + The button's text. + icon : str + The button's icon. + trigger_action : callable + The action to be executed when the button is clicked. + """ + self.actions_call[key] = QAction(QIcon(icon), text, self) + self.actions_call[key].triggered.connect(trigger_action) + self.addAction(self.actions_call[key]) + + def add_separator(self) -> None: + """ + Add a separator to the toolbar. + """ + separator = QWidget(self) + separator.setSizePolicy( + QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) + self.addWidget(separator) + + def enable_button(self, key): + """ + Enable a button in the toolbar. + + Parameters + ---------- + key : str + The button's key. + """ + self.actions_call[key].setEnabled(True) + + def disable_button(self, key): + """ + Disable a button in the toolbar. + + Parameters + ---------- + key : str + The button's key. + """ + self.actions_call[key].setEnabled(False) \ No newline at end of file