126 lines
4.5 KiB
Python
126 lines
4.5 KiB
Python
from __future__ import annotations
|
||
|
||
from collections.abc import Iterable
|
||
|
||
from PySide6.QtCore import QSize, Qt, Signal
|
||
from PySide6.QtGui import QAction
|
||
from PySide6.QtWidgets import (
|
||
QFrame,
|
||
QHBoxLayout,
|
||
QLabel,
|
||
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")
|
||
if not action.icon().isNull():
|
||
button.setIcon(action.icon())
|
||
button.setIconSize(QSize(16, 16))
|
||
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):
|
||
collapse_requested = Signal()
|
||
|
||
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)
|
||
|
||
header = QWidget()
|
||
header.setObjectName("rightToolsHeader")
|
||
header_layout = QHBoxLayout(header)
|
||
header_layout.setContentsMargins(0, 0, 0, 0)
|
||
header_layout.setSpacing(6)
|
||
title = QLabel("工具")
|
||
title.setObjectName("rightToolsTitle")
|
||
collapse_button = QToolButton()
|
||
collapse_button.setObjectName("rightToolsCollapseButton")
|
||
collapse_button.setText("‹")
|
||
collapse_button.setToolTip("收起工具面板")
|
||
collapse_button.clicked.connect(self.collapse_requested.emit)
|
||
header_layout.addWidget(title)
|
||
header_layout.addStretch(1)
|
||
header_layout.addWidget(collapse_button)
|
||
|
||
layout = QVBoxLayout(self)
|
||
layout.setContentsMargins(8, 8, 8, 8)
|
||
layout.setSpacing(8)
|
||
layout.addWidget(header)
|
||
layout.addWidget(search_box)
|
||
layout.addWidget(search_results)
|
||
layout.addWidget(scroll, 1)
|
||
search_results.setVisible(False)
|
||
search_results.setMaximumHeight(170)
|
||
self.setMinimumWidth(260)
|
||
self.setMaximumWidth(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)
|