0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

fix: toolbar.py automatic initialisation works

This commit is contained in:
wyzula-jan
2023-11-19 19:04:32 +01:00
parent ee3b616ec1
commit 8ad3059592
2 changed files with 37 additions and 33 deletions

View File

@ -42,7 +42,7 @@ class ScriptRunnerThread(QThread):
class BECEditor(QWidget): class BECEditor(QWidget):
def __init__(self): def __init__(self, toolbar_enabled=True):
super().__init__() super().__init__()
# Initialize the editor and terminal # Initialize the editor and terminal
@ -52,6 +52,13 @@ class BECEditor(QWidget):
# Layout # Layout
self.layout = QVBoxLayout() self.layout = QVBoxLayout()
# Initialize and add the toolbar if enabled
if toolbar_enabled:
self.toolbar = ModularToolBar(self)
self.layout.addWidget(self.toolbar)
self.layout.addWidget(self.toolbar)
self.layout.addWidget(self.editor) self.layout.addWidget(self.editor)
self.layout.addWidget(self.terminal) self.layout.addWidget(self.terminal)
self.setLayout(self.layout) self.setLayout(self.layout)
@ -142,16 +149,12 @@ class BECEditor(QWidget):
if __name__ == "__main__": if __name__ == "__main__":
from bec_widgets.widgets.toolbar.toolbar import ModularToolBar from bec_widgets.widgets.toolbar.toolbar import ModularToolBar, OpenFileAction, SaveFileAction
app = QApplication([]) app = QApplication([])
qdarktheme.setup_theme("auto") qdarktheme.setup_theme("auto")
mainWin = BECEditor() mainWin = BECEditor()
toolbar_manual = ModularToolBar()
toolbar_manual.set_target_widget(mainWin)
mainWin.layout.insertWidget(0, toolbar_manual)
mainWin.show() mainWin.show()
app.exec() app.exec()

View File

@ -1,12 +1,9 @@
from PyQt6.QtWidgets import QToolBar
from qtpy.QtCore import QTimer
from qtpy.QtGui import QAction
from qtpy.QtWidgets import QPushButton
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from qtpy.QtWidgets import QHBoxLayout, QWidget from qtpy.QtWidgets import QToolBar
from qtpy.QtCore import QTimer
from bec_widgets.widgets.editor.editor import BECEditor from qtpy.QtGui import QAction
from qtpy.QtWidgets import QWidget
class ToolBarAction(ABC): class ToolBarAction(ABC):
@ -37,37 +34,41 @@ class RunScriptAction:
class ModularToolBar(QToolBar): class ModularToolBar(QToolBar):
def __init__(self, parent=None): def __init__(self, parent=None, auto_init=True):
super().__init__(parent) super().__init__(parent)
self.auto_init = auto_init
self.handler = { self.handler = {
BECEditor: [OpenFileAction(), SaveFileAction(), RunScriptAction()], "BECEditor": [OpenFileAction(), SaveFileAction(), RunScriptAction()],
# BECMonitor: [SomeOtherAction(), AnotherAction()], # Example for another widget # BECMonitor: [SomeOtherAction(), AnotherAction()], # Example for another widget
} # TODO also buggy fox later }
QTimer.singleShot(0, self.detect_context_and_update_actions) # TODO buggy disabled for now
self.setStyleSheet("QToolBar { background: transparent; }") self.setStyleSheet("QToolBar { background: transparent; }")
if self.auto_init:
QTimer.singleShot(0, self.auto_detect_and_populate)
def auto_detect_and_populate(self):
if not self.auto_init:
return
def detect_context_and_update_actions(self): # TODO buggy disabled for now
parent_widget = self.parent() parent_widget = self.parent()
if parent_widget is None: if parent_widget is None:
return return
for child in parent_widget.children(): parent_widget_class_name = type(parent_widget).__name__
for widget_type, actions in self.handler.items(): for widget_type_name, actions in self.handler.items():
if isinstance(child, widget_type): if parent_widget_class_name == widget_type_name:
self.populate_toolbar(child, actions) self.populate_toolbar(actions, parent_widget)
return return
def set_target_widget(self, widget): def populate_toolbar(self, actions, target_widget):
self.populate_toolbar(
widget, actions=[OpenFileAction(), SaveFileAction(), RunScriptAction()]
)
# for widget_type, actions in self.handler.items(): #TODO for automatic population
# if isinstance(widget, widget_type):
# self.populate_toolbar(widget, actions)
# break
def populate_toolbar(self, target_widget, actions):
self.clear() self.clear()
for action_creator in actions: for action_creator in actions:
action = action_creator.create(target_widget) action = action_creator.create(target_widget)
self.addAction(action) self.addAction(action)
def set_manual_actions(self, actions, target_widget):
self.clear()
for action in actions:
if isinstance(action, QAction):
self.addAction(action)
elif isinstance(action, ToolBarAction):
self.addAction(action.create(target_widget))