anki/qt/aqt/browser/sidebar/toolbar.py
Damien Elmes b9251290ca run pyupgrade over codebase [python upgrade required]
This adds Python 3.9 and 3.10 typing syntax to files that import
attributions from __future___. Python 3.9 should be able to cope with
the 3.10 syntax, but Python 3.8 will no longer work.

On Windows/Mac, install the latest Python 3.9 version from python.org.
There are currently no orjson wheels for Python 3.10 on Windows/Mac,
which will break the build unless you have Rust installed separately.

On Linux, modern distros should have Python 3.9 available already. If
you're on an older distro, you'll need to build Python from source first.
2021-10-04 15:05:48 +10:00

51 lines
1.7 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 enum import Enum, auto
import aqt
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, ":/icons/magnifying_glass.svg", tr.actions_search),
(SidebarTool.SELECT, ":/icons/select.svg", 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(16, 16))
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.setStyle(QStyleFactory.create("fusion"))
def _setup_tools(self) -> None:
for row, tool in enumerate(self._tools):
action = self.addAction(
theme_manager.icon_from_resources(tool[1]), tool[2]()
)
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]