From e2bbc2e9fe208042d76ecd52ae5e87637c1b3535 Mon Sep 17 00:00:00 2001 From: Arseniy Terekhin Date: Fri, 27 Sep 2024 21:31:46 +0300 Subject: [PATCH] refactor: more typing --- gmc/main_window.py | 12 ++++++------ gmc/utils/__init__.py | 7 +++++-- gmc/utils/svg.py | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/gmc/main_window.py b/gmc/main_window.py index 8b75b54..0b1d676 100644 --- a/gmc/main_window.py +++ b/gmc/main_window.py @@ -158,18 +158,18 @@ def _menu_button(self): self, "help", tr("&Help"), - shortcuts=(Qt.Key_F1,), + shortcuts=(Qt.Key.Key_F1,), triggered=self._on_help, - shortcutContext=Qt.WindowShortcut, + shortcutContext=Qt.ShortcutContext.WindowShortcut, ) ) settings_act = new_action( self, "gears", tr("Se&ttings"), - shortcuts=(Qt.CTRL + Qt.Key_Comma,), + shortcuts=(Qt.Modifier.CTRL + Qt.Key.Key_Comma,), triggered=self._on_settings, - shortcutContext=Qt.WindowShortcut, + shortcutContext=Qt.ShortcutContext.WindowShortcut, ) main_menu.addAction(settings_act) self.addAction(settings_act) @@ -191,7 +191,7 @@ def _menu_button(self): tr("E&xit"), self, triggered=QtWidgets.qApp.quit, - shortcut=Qt.CTRL + Qt.Key_Q, + shortcut=Qt.Modifier.CTRL + Qt.Ket.Key_Q, menuRole=QtWidgets.QAction.QuitRole, ) ) @@ -207,7 +207,7 @@ def _on_help(self): webbrowser.open("file://" + path) else: msg = tr("Help not bundled. {} does not exist.").format(path) - MB.warning(self, self.windowTitle(), msg, MB.Ok) + MB.warning(self, self.windowTitle(), msg, MB.StandardButton.Ok) def _on_settings(self): from .settings.dialog import create_setting_dialog diff --git a/gmc/utils/__init__.py b/gmc/utils/__init__.py index 6e4f5f0..8514956 100644 --- a/gmc/utils/__init__.py +++ b/gmc/utils/__init__.py @@ -1,3 +1,4 @@ +from __future__ import annotations from typing import Any from PyQt5.QtWidgets import QAction from PyQt5.QtGui import QIcon, QKeySequence @@ -30,13 +31,14 @@ def new_action( icon: str | QIcon, text: str, shortcuts: tuple[str | QKeySequence.StandardKey | Qt.Key | int, ...] = (), + shortcutContext: Qt.ShortcutContext = Qt.ShortcutContext.WidgetShortcut, **kwargs: Any, ) -> QAction: sequences = [QKeySequence(s) for s in shortcuts] shrtctext = "; ".join( - s.toString(format=QKeySequence.NativeText) for s in sequences + s.toString(format=QKeySequence.SequenceFormat.NativeText) + for s in sequences ) - kwargs.setdefault("shortcutContext", Qt.ShortcutContext.WidgetShortcut) if not isinstance(icon, QIcon): icon = get_icon(icon) action = QAction( @@ -44,6 +46,7 @@ def new_action( f"{text}\t{shrtctext}", parent, toolTip=f"{text.replace('&', '')} ({shrtctext})", + shortcutContext=shortcutContext, **kwargs, # type: ignore[call-overload] ) action.setShortcuts(sequences) diff --git a/gmc/utils/svg.py b/gmc/utils/svg.py index 8923d38..c4cfab7 100644 --- a/gmc/utils/svg.py +++ b/gmc/utils/svg.py @@ -1,5 +1,5 @@ from PyQt5 import QtGui -def icon_from_data(data): +def icon_from_data(data: bytes): return QtGui.QIcon(QtGui.QPixmap.fromImage(QtGui.QImage.fromData(data)))