Files
2026-06-30 18:20:30 +08:00

185 lines
6.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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], section_id: str | None = None) -> None:
super().__init__()
self.section_id = section_id
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.sections: dict[str, QWidget] = {}
self.annotation_list: QListWidget | None = None
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")
self.title_label = title
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], section_id: str | None = None) -> ToolSection:
section = ToolSection(title, actions, section_id)
self.sections_layout.addWidget(section)
if section_id:
self.sections[section_id] = section
return section
def add_annotation_list(
self,
title: str,
annotation_list: QListWidget,
section_id: str,
controls: Iterable[QWidget] | None = None,
) -> None:
section = QFrame()
section.setObjectName("toolSection")
layout = QVBoxLayout(section)
layout.setContentsMargins(8, 8, 8, 8)
layout.setSpacing(6)
label = QLabel(title)
label.setObjectName("toolSectionLabel")
layout.addWidget(label)
for control in controls or []:
layout.addWidget(control)
layout.addWidget(annotation_list)
annotation_list.setObjectName("rightAnnotationList")
annotation_list.setMinimumHeight(180)
self.annotation_list = annotation_list
self.sections[section_id] = section
self.sections_layout.addWidget(section)
def add_widget_section(self, title: str, widget: QWidget, section_id: str) -> None:
section = QFrame()
section.setObjectName("toolSection")
layout = QVBoxLayout(section)
layout.setContentsMargins(8, 8, 8, 8)
layout.setSpacing(6)
label = QLabel(title)
label.setObjectName("toolSectionLabel")
layout.addWidget(label)
layout.addWidget(widget)
self.sections[section_id] = section
self.sections_layout.addWidget(section)
def finish(self) -> None:
self.sections_layout.addStretch(1)
def set_search_results_visible(self, visible: bool) -> None:
self.search_results.setVisible(visible)
def set_title(self, title: str) -> None:
self.title_label.setText(title)
def show_sections(self, section_ids: Iterable[str]) -> None:
visible_ids = set(section_ids)
for section_id, section in self.sections.items():
section.setVisible(section_id in visible_ids)