Anki/qt/aqt/browser/sidebar/toolbar.py
Ben Nguyen 931e1d80f2
Enable strict_optional in aqt/. and aqt/browser (#3486)
* Boolean naming convention

* Rename no_strict_optional -> strict_optional

* Update CONTRIBUTORS

* Enable strict optional for aqt module

* Fix errors

* Enable strict optional for aqt browser

* Fix layout.py errors

* Fix find_duplicates.py errors

* Fix browser.py errors

* Revert a0 a1 names

* Fix tree.py errors

* Fix previewer.py errors

* Fix model.py errors

* Fix find_and_replace.py errors

* Fix item.py errors

* Fix toolbar.py errors

* Fix table/__init__.py errors

* Fix model.py errors

* Fix state.py errors

* Fix table.py errors

* Fix errors in card_info.py

* Fix searchbar.py errors

* Fix name

* Fix assert in browser.py

* Formatting

* Fix assert vh

* assert is not None instead of truthy

* Split _move_current() up to correct type signature (dae)

We want either index or direction, but not both.
2024-10-11 23:12:48 +10:00

70 lines
2.3 KiB
Python

# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
from collections.abc import Callable
from enum import Enum, auto
import aqt
import aqt.browser
import aqt.gui_hooks
from aqt.qt import *
from aqt.theme import theme_manager
from aqt.utils import tr
class SidebarTool(Enum):
SELECT = auto()
SEARCH = auto()
class SidebarToolbar(QToolBar):
_tools: tuple[tuple[SidebarTool, str, Callable[[], str]], ...] = (
(
SidebarTool.SEARCH,
"mdi:magnify",
tr.actions_search,
),
(
SidebarTool.SELECT,
"mdi:selection-drag",
tr.actions_select,
),
)
def __init__(self, sidebar: aqt.browser.sidebar.SidebarTreeView) -> None:
super().__init__()
self.sidebar = sidebar
self._action_group = QActionGroup(self)
qconnect(self._action_group.triggered, self._on_action_group_triggered)
self._setup_tools()
self.setIconSize(QSize(18, 18))
self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
self.setStyle(QStyleFactory.create("fusion"))
aqt.gui_hooks.theme_did_change.append(self._update_icons)
def _setup_tools(self) -> None:
for row, tool in enumerate(self._tools):
action = self.addAction(
theme_manager.icon_from_resources(tool[1]), tool[2]()
)
assert action is not None
action.setCheckable(True)
action.setShortcut(f"Alt+{row + 1}")
self._action_group.addAction(action)
# always start with first tool
active = 0
self._action_group.actions()[active].setChecked(True)
self.sidebar.tool = self._tools[active][0]
def _on_action_group_triggered(self, action: QAction) -> None:
index = self._action_group.actions().index(action)
self.sidebar.tool = self._tools[index][0]
def cleanup(self) -> None:
aqt.gui_hooks.theme_did_change.remove(self._update_icons)
def _update_icons(self) -> None:
for idx, action in enumerate(self._action_group.actions()):
action.setIcon(theme_manager.icon_from_resources(self._tools[idx][1]))