Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: setModelMenuBar accepts instance of MenusRegistry #139

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/app_model/backends/qt/_qmainwindow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Collection, Mapping, Optional, Sequence, Union
from typing import TYPE_CHECKING, Collection, Mapping, Optional, Sequence, Union

from qtpy.QtCore import Qt
from qtpy.QtWidgets import QMainWindow, QWidget
Expand All @@ -9,6 +9,9 @@

from ._qmenu import QModelMenuBar, QModelToolBar

if TYPE_CHECKING:
from app_model.registries import MenusRegistry


class QModelMainWindow(QMainWindow):
"""QMainWindow with app-model support."""
Expand All @@ -20,14 +23,17 @@ def __init__(
self._app = Application.get_or_create(app) if isinstance(app, str) else app

def setModelMenuBar(
self, menu_ids: Mapping[str, str] | Sequence[str | tuple[str, str]]
self,
menu_ids: Mapping[str, str] | Sequence[str | tuple[str, str]] | MenusRegistry,
) -> QModelMenuBar:
"""Set the menu bar to a list of menu ids.

Parameters
----------
menu_ids : Mapping[str, str] | Sequence[str | tuple[str, str]]
A mapping of menu ids to menu titles or a sequence of menu ids.
menu_ids : Mapping[str, str] | Sequence[str | tuple[str, str]] | MenusRegistry
A mapping of `{menu ids -> menu title}` or a sequence of menu ids. If a
`MenuRegistry` instance is passed, all menus in the registry will be added
to the menu bar.
"""
menu_bar = QModelMenuBar(menu_ids, self._app, self)
self.setMenuBar(menu_bar)
Expand Down
28 changes: 25 additions & 3 deletions src/app_model/backends/qt/_qmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from qtpy.QtWidgets import QMenu, QMenuBar, QToolBar

from app_model import Application
from app_model.registries import MenusRegistry
from app_model.types import SubmenuItem

from ._qaction import QCommandRuleAction, QMenuItemAction
Expand Down Expand Up @@ -261,13 +262,24 @@
self.rebuild()


def _menu_sort(menu_name: str | tuple[str, str]) -> tuple:
if isinstance(menu_name, tuple):
menu_name = menu_name[0]
order = ["file", "edit", "format", "view", None, "window", "help"]
menu_name = menu_name.lower()
idx = order.index(menu_name if menu_name in order else None)
return (idx, menu_name)

Check warning on line 271 in src/app_model/backends/qt/_qmenu.py

View check run for this annotation

Codecov / codecov/patch

src/app_model/backends/qt/_qmenu.py#L266-L271

Added lines #L266 - L271 were not covered by tests


class QModelMenuBar(QMenuBar):
"""QMenuBar that is built from a list of model menu ids.

Parameters
----------
menus : Mapping[str, str] | Sequence[str | tuple[str, str]]
A mapping of menu ids to menu titles or a sequence of menu ids.
A mapping of `{menu ids -> menu title}` or a sequence of menu ids. If a
`MenuRegistry` instance is passed, all menus in the registry will be added
to the menu bar.
app : Union[str, Application]
Application instance or name of application instance.
parent : Optional[QWidget]
Expand All @@ -276,13 +288,23 @@

def __init__(
self,
menus: Mapping[str, str] | Sequence[str | tuple[str, str]],
menus: Mapping[str, str] | Sequence[str | tuple[str, str]] | MenusRegistry,
app: Union[str, Application],
parent: Optional[QWidget] = None,
) -> None:
super().__init__(parent)

menu_items = menus.items() if isinstance(menus, Mapping) else menus
menu_items: Iterable[str | tuple[str, str]]
if isinstance(menus, MenusRegistry):
menu_items = [m for m, *_ in menus if m != MenusRegistry.COMMAND_PALETTE_ID]

Check warning on line 299 in src/app_model/backends/qt/_qmenu.py

View check run for this annotation

Codecov / codecov/patch

src/app_model/backends/qt/_qmenu.py#L299

Added line #L299 was not covered by tests
# sort menus so that they are in this order
# File Edit Format View <...> Window Help
menu_items.sort(key=_menu_sort)

Check warning on line 302 in src/app_model/backends/qt/_qmenu.py

View check run for this annotation

Codecov / codecov/patch

src/app_model/backends/qt/_qmenu.py#L302

Added line #L302 was not covered by tests
elif isinstance(menus, Mapping):
menu_items = menus.items()
else:
menu_items = menus

for item in menu_items:
id_, title = item if isinstance(item, tuple) else (item, item.title())
self.addMenu(QModelMenu(id_, app, title, self))
Expand Down