2021-04-13 11:05:49 +02:00
|
|
|
# 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):
|
2021-10-03 10:59:42 +02:00
|
|
|
_tools: tuple[tuple[SidebarTool, str, Callable[[], str]], ...] = (
|
2021-10-05 06:44:07 +02:00
|
|
|
(SidebarTool.SEARCH, "icons:magnifying_glass.svg", tr.actions_search),
|
|
|
|
(SidebarTool.SELECT, "icons:select.svg", tr.actions_select),
|
2021-04-13 11:05:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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))
|
2021-10-05 05:53:01 +02:00
|
|
|
self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
|
2021-04-13 11:05:49 +02:00
|
|
|
self.setStyle(QStyleFactory.create("fusion"))
|
2021-11-24 22:17:41 +01:00
|
|
|
aqt.gui_hooks.theme_did_change.append(self._update_icons)
|
2021-04-13 11:05:49 +02:00
|
|
|
|
|
|
|
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)
|
2021-05-24 06:51:05 +02:00
|
|
|
# always start with first tool
|
|
|
|
active = 0
|
2021-04-13 11:05:49 +02:00
|
|
|
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]
|
2021-11-24 22:17:41 +01:00
|
|
|
|
|
|
|
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]))
|