101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable
|
|
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtGui import QAction
|
|
from PySide6.QtWidgets import (
|
|
QFrame,
|
|
QLineEdit,
|
|
QListWidget,
|
|
QPushButton,
|
|
QScrollArea,
|
|
QSizePolicy,
|
|
QToolButton,
|
|
QVBoxLayout,
|
|
QWidget,
|
|
)
|
|
|
|
|
|
class ToolSection(QFrame):
|
|
def __init__(self, title: str, actions: Iterable[QAction]) -> None:
|
|
super().__init__()
|
|
self.setObjectName("toolSection")
|
|
self.actions_container = QWidget()
|
|
self.actions_container.setObjectName("toolSectionActions")
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(8, 8, 8, 8)
|
|
layout.setSpacing(4)
|
|
|
|
self.title_button = QToolButton()
|
|
self.title_button.setObjectName("toolSectionTitle")
|
|
self.title_button.setText(title)
|
|
self.title_button.setCheckable(True)
|
|
self.title_button.setChecked(True)
|
|
self.title_button.setArrowType(Qt.ArrowType.DownArrow)
|
|
self.title_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
|
|
self.title_button.clicked.connect(self._set_expanded)
|
|
layout.addWidget(self.title_button)
|
|
|
|
actions_layout = QVBoxLayout(self.actions_container)
|
|
actions_layout.setContentsMargins(0, 2, 0, 0)
|
|
actions_layout.setSpacing(3)
|
|
|
|
for action in actions:
|
|
button = QPushButton(action.text())
|
|
button.setObjectName("rightToolButton")
|
|
button.setToolTip(action.toolTip() or action.text())
|
|
button.setFlat(True)
|
|
button.setCheckable(action.isCheckable())
|
|
button.setChecked(action.isChecked())
|
|
if action.isCheckable():
|
|
action.toggled.connect(button.setChecked)
|
|
button.clicked.connect(action.trigger)
|
|
button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
|
|
actions_layout.addWidget(button)
|
|
|
|
layout.addWidget(self.actions_container)
|
|
|
|
def _set_expanded(self, expanded: bool) -> None:
|
|
self.actions_container.setVisible(expanded)
|
|
self.title_button.setArrowType(Qt.ArrowType.DownArrow if expanded else Qt.ArrowType.RightArrow)
|
|
|
|
|
|
class RightToolsPanel(QWidget):
|
|
def __init__(self, search_box: QLineEdit, search_results: QListWidget) -> None:
|
|
super().__init__()
|
|
self.search_box = search_box
|
|
self.search_results = search_results
|
|
self.setObjectName("rightToolsPanel")
|
|
self.sections_container = QWidget()
|
|
self.sections_container.setObjectName("rightToolsSections")
|
|
self.sections_layout = QVBoxLayout(self.sections_container)
|
|
self.sections_layout.setContentsMargins(0, 0, 0, 0)
|
|
self.sections_layout.setSpacing(8)
|
|
|
|
scroll = QScrollArea()
|
|
scroll.setObjectName("rightToolsScroll")
|
|
scroll.setWidgetResizable(True)
|
|
scroll.setFrameShape(QFrame.Shape.NoFrame)
|
|
scroll.setWidget(self.sections_container)
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(8, 8, 8, 8)
|
|
layout.setSpacing(8)
|
|
layout.addWidget(search_box)
|
|
layout.addWidget(search_results)
|
|
layout.addWidget(scroll, 1)
|
|
search_results.setVisible(False)
|
|
search_results.setMaximumHeight(170)
|
|
self.setMinimumWidth(300)
|
|
|
|
def add_section(self, title: str, actions: Iterable[QAction]) -> None:
|
|
self.sections_layout.addWidget(ToolSection(title, actions))
|
|
|
|
def finish(self) -> None:
|
|
self.sections_layout.addStretch(1)
|
|
|
|
def set_search_results_visible(self, visible: bool) -> None:
|
|
self.search_results.setVisible(visible)
|