50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from PySide6.QtCore import QObject
|
|
from PySide6.QtWidgets import QDockWidget, QMenu, QToolBar
|
|
|
|
from bedit_gui.views.main_window import MainWindow
|
|
|
|
|
|
class ViewMenuController(QObject):
|
|
"""Populate View submenus with synchronized visibility actions."""
|
|
|
|
def __init__(self, window: MainWindow) -> None:
|
|
super().__init__(window)
|
|
|
|
self.panels_menu = QMenu("Panels", window.ui.menuView)
|
|
self.toolbars_menu = QMenu("Toolbars", window.ui.menuView)
|
|
|
|
window.ui.actionPanels.setMenu(self.panels_menu)
|
|
window.ui.actionToolbars.setMenu(self.toolbars_menu)
|
|
|
|
self._populate(
|
|
self.panels_menu,
|
|
window.findChildren(QDockWidget),
|
|
)
|
|
self._populate(
|
|
self.toolbars_menu,
|
|
window.findChildren(QToolBar),
|
|
)
|
|
|
|
def _populate(
|
|
self,
|
|
menu: QMenu,
|
|
widgets: list[QDockWidget] | list[QToolBar],
|
|
) -> None:
|
|
for widget in sorted(widgets, key=self._label):
|
|
action = widget.toggleViewAction()
|
|
action.setText(self._label(widget))
|
|
menu.addAction(action)
|
|
|
|
@staticmethod
|
|
def _label(widget: QDockWidget | QToolBar) -> str:
|
|
title = widget.windowTitle().strip()
|
|
if title and title.lower() != "toolbar":
|
|
return title
|
|
|
|
name = re.sub(r"(DockWidget|Widget|ToolBar)$", "", widget.objectName())
|
|
return re.sub(r"(?<!^)(?=[A-Z])", " ", name).title()
|