Files
PDFReaderLite/pdfreaderlite/ui/right_tools.py
T

169 lines
6.0 KiB
Python
Raw Normal View History

2026-06-29 16:23:33 +08:00
from __future__ import annotations
from collections.abc import Iterable
2026-06-30 00:27:58 +08:00
from PySide6.QtCore import QSize, Qt, Signal
2026-06-29 16:23:33 +08:00
from PySide6.QtGui import QAction
from PySide6.QtWidgets import (
QFrame,
2026-06-30 00:27:58 +08:00
QHBoxLayout,
QLabel,
2026-06-29 16:23:33 +08:00
QLineEdit,
QListWidget,
QPushButton,
QScrollArea,
QSizePolicy,
2026-06-30 00:01:34 +08:00
QToolButton,
2026-06-29 16:23:33 +08:00
QVBoxLayout,
QWidget,
)
class ToolSection(QFrame):
2026-06-30 10:33:48 +08:00
def __init__(self, title: str, actions: Iterable[QAction], section_id: str | None = None) -> None:
2026-06-29 16:23:33 +08:00
super().__init__()
2026-06-30 10:33:48 +08:00
self.section_id = section_id
2026-06-29 16:23:33 +08:00
self.setObjectName("toolSection")
2026-06-30 00:01:34 +08:00
self.actions_container = QWidget()
self.actions_container.setObjectName("toolSectionActions")
2026-06-29 16:23:33 +08:00
layout = QVBoxLayout(self)
2026-06-30 00:01:34 +08:00
layout.setContentsMargins(8, 8, 8, 8)
layout.setSpacing(4)
2026-06-29 16:23:33 +08:00
2026-06-30 00:01:34 +08:00
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)
2026-06-29 16:23:33 +08:00
for action in actions:
button = QPushButton(action.text())
2026-06-29 22:40:41 +08:00
button.setObjectName("rightToolButton")
2026-06-30 00:27:58 +08:00
if not action.icon().isNull():
button.setIcon(action.icon())
button.setIconSize(QSize(16, 16))
2026-06-29 16:23:33 +08:00
button.setToolTip(action.toolTip() or action.text())
2026-06-29 22:40:41 +08:00
button.setFlat(True)
button.setCheckable(action.isCheckable())
button.setChecked(action.isChecked())
if action.isCheckable():
action.toggled.connect(button.setChecked)
2026-06-29 16:23:33 +08:00
button.clicked.connect(action.trigger)
button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
2026-06-30 00:01:34 +08:00
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)
2026-06-29 16:23:33 +08:00
class RightToolsPanel(QWidget):
2026-06-30 00:27:58 +08:00
collapse_requested = Signal()
2026-06-29 16:23:33 +08:00
def __init__(self, search_box: QLineEdit, search_results: QListWidget) -> None:
super().__init__()
self.search_box = search_box
self.search_results = search_results
2026-06-30 10:33:48 +08:00
self.sections: dict[str, QWidget] = {}
self.annotation_list: QListWidget | None = None
2026-06-29 22:40:41 +08:00
self.setObjectName("rightToolsPanel")
2026-06-29 16:23:33 +08:00
self.sections_container = QWidget()
2026-06-29 22:40:41 +08:00
self.sections_container.setObjectName("rightToolsSections")
2026-06-29 16:23:33 +08:00
self.sections_layout = QVBoxLayout(self.sections_container)
self.sections_layout.setContentsMargins(0, 0, 0, 0)
self.sections_layout.setSpacing(8)
scroll = QScrollArea()
2026-06-29 22:40:41 +08:00
scroll.setObjectName("rightToolsScroll")
2026-06-29 16:23:33 +08:00
scroll.setWidgetResizable(True)
scroll.setFrameShape(QFrame.Shape.NoFrame)
scroll.setWidget(self.sections_container)
2026-06-30 00:27:58 +08:00
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")
2026-06-30 10:33:48 +08:00
self.title_label = title
2026-06-30 00:27:58 +08:00
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)
2026-06-29 16:23:33 +08:00
layout = QVBoxLayout(self)
layout.setContentsMargins(8, 8, 8, 8)
layout.setSpacing(8)
2026-06-30 00:27:58 +08:00
layout.addWidget(header)
2026-06-29 16:23:33 +08:00
layout.addWidget(search_box)
2026-06-30 00:01:34 +08:00
layout.addWidget(search_results)
layout.addWidget(scroll, 1)
search_results.setVisible(False)
search_results.setMaximumHeight(170)
2026-06-30 00:27:58 +08:00
self.setMinimumWidth(260)
self.setMaximumWidth(300)
2026-06-29 16:23:33 +08:00
2026-06-30 10:33:48 +08:00
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
2026-06-30 13:46:28 +08:00
def add_annotation_list(
self,
title: str,
annotation_list: QListWidget,
section_id: str,
controls: Iterable[QWidget] | None = None,
) -> None:
2026-06-30 10:33:48 +08:00
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)
2026-06-30 13:46:28 +08:00
for control in controls or []:
layout.addWidget(control)
2026-06-30 10:33:48 +08:00
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)
2026-06-29 16:23:33 +08:00
def finish(self) -> None:
self.sections_layout.addStretch(1)
2026-06-30 00:01:34 +08:00
def set_search_results_visible(self, visible: bool) -> None:
self.search_results.setVisible(visible)
2026-06-30 10:33:48 +08:00
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)