| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from qtpy.QtCore import Qt |
| 6 |
from qtpy.QtWidgets import QMainWindow, QWidget |
| 7 |
|
| 8 |
from app_model import Application |
| 9 |
|
| 10 |
from ._qmenu import QModelMenuBar, QModelToolBar |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from collections.abc import Collection, Mapping, Sequence |
| 14 |
|
| 15 |
|
| 16 |
class QModelMainWindow(QMainWindow): |
| 17 |
"""QMainWindow with app-model support.""" |
| 18 |
|
| 19 |
def __init__(self, app: Application | str, parent: QWidget | None = None) -> None: |
| 20 |
super().__init__(parent) |
| 21 |
self._app = Application.get_or_create(app) if isinstance(app, str) else app |
| 22 |
|
| 23 |
def setModelMenuBar( |
| 24 |
self, menu_ids: Mapping[str, str] | Sequence[str | tuple[str, str]] |
| 25 |
) -> QModelMenuBar: |
| 26 |
"""Set the menu bar to a list of menu ids. |
| 27 |
|
| 28 |
Parameters |
| 29 |
---------- |
| 30 |
menu_ids : Mapping[str, str] | Sequence[str | tuple[str, str]] |
| 31 |
A mapping of menu ids to menu titles or a sequence of menu ids. |
| 32 |
""" |
| 33 |
menu_bar = QModelMenuBar(menu_ids, self._app, self) |
| 34 |
self.setMenuBar(menu_bar) |
| 35 |
return menu_bar |
| 36 |
|
| 37 |
def addModelToolBar( |
| 38 |
self, |
| 39 |
menu_id: str, |
| 40 |
*, |
| 41 |
exclude: Collection[str] | None = None, |
| 42 |
area: Qt.ToolBarArea | None = None, |
| 43 |
toolbutton_style: Qt.ToolButtonStyle = Qt.ToolButtonStyle.ToolButtonIconOnly, |
| 44 |
) -> QModelToolBar: |
| 45 |
"""Add a tool bar to the main window.""" |
| 46 |
toolbar = QModelToolBar(menu_id, self._app, exclude=exclude, parent=self) |
| 47 |
toolbar.setToolButtonStyle(toolbutton_style) |
| 48 |
if area is not None: |
| 49 |
self.addToolBar(area, toolbar) |
| 50 |
else: |
| 51 |
self.addToolBar(toolbar) |
| 52 |
return toolbar |