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:
@ -42,7 +42,7 @@ class ScriptRunnerThread(QThread):
|
||||
|
||||
|
||||
class BECEditor(QWidget):
|
||||
def __init__(self):
|
||||
def __init__(self, toolbar_enabled=True):
|
||||
super().__init__()
|
||||
|
||||
# Initialize the editor and terminal
|
||||
@ -52,6 +52,13 @@ class BECEditor(QWidget):
|
||||
|
||||
# Layout
|
||||
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.terminal)
|
||||
self.setLayout(self.layout)
|
||||
@ -142,16 +149,12 @@ class BECEditor(QWidget):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from bec_widgets.widgets.toolbar.toolbar import ModularToolBar
|
||||
from bec_widgets.widgets.toolbar.toolbar import ModularToolBar, OpenFileAction, SaveFileAction
|
||||
|
||||
app = QApplication([])
|
||||
qdarktheme.setup_theme("auto")
|
||||
|
||||
mainWin = BECEditor()
|
||||
|
||||
toolbar_manual = ModularToolBar()
|
||||
toolbar_manual.set_target_widget(mainWin)
|
||||
|
||||
mainWin.layout.insertWidget(0, toolbar_manual)
|
||||
mainWin.show()
|
||||
app.exec()
|
||||
|
@ -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 qtpy.QtWidgets import QHBoxLayout, QWidget
|
||||
|
||||
from bec_widgets.widgets.editor.editor import BECEditor
|
||||
from qtpy.QtWidgets import QToolBar
|
||||
from qtpy.QtCore import QTimer
|
||||
from qtpy.QtGui import QAction
|
||||
from qtpy.QtWidgets import QWidget
|
||||
|
||||
|
||||
class ToolBarAction(ABC):
|
||||
@ -37,37 +34,41 @@ class RunScriptAction:
|
||||
|
||||
|
||||
class ModularToolBar(QToolBar):
|
||||
def __init__(self, parent=None):
|
||||
def __init__(self, parent=None, auto_init=True):
|
||||
super().__init__(parent)
|
||||
self.auto_init = auto_init
|
||||
self.handler = {
|
||||
BECEditor: [OpenFileAction(), SaveFileAction(), RunScriptAction()],
|
||||
"BECEditor": [OpenFileAction(), SaveFileAction(), RunScriptAction()],
|
||||
# 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; }")
|
||||
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()
|
||||
if parent_widget is None:
|
||||
return
|
||||
|
||||
for child in parent_widget.children():
|
||||
for widget_type, actions in self.handler.items():
|
||||
if isinstance(child, widget_type):
|
||||
self.populate_toolbar(child, actions)
|
||||
parent_widget_class_name = type(parent_widget).__name__
|
||||
for widget_type_name, actions in self.handler.items():
|
||||
if parent_widget_class_name == widget_type_name:
|
||||
self.populate_toolbar(actions, parent_widget)
|
||||
return
|
||||
|
||||
def set_target_widget(self, 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):
|
||||
def populate_toolbar(self, actions, target_widget):
|
||||
self.clear()
|
||||
for action_creator in actions:
|
||||
action = action_creator.create(target_widget)
|
||||
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))
|
||||
|
Reference in New Issue
Block a user