Separate annotation tools panel

This commit is contained in:
2026-06-30 10:33:48 +08:00
parent a12cc9ba75
commit 380653d58d
2 changed files with 134 additions and 6 deletions
+96 -3
View File
@@ -91,6 +91,7 @@ class MainWindow(QMainWindow):
self.sidebar = Sidebar()
self.search_box = QLineEdit()
self.search_results = QListWidget()
self.right_annotation_list = QListWidget()
self.right_tools = RightToolsPanel(self.search_box, self.search_results)
self.main_splitter: QSplitter | None = None
self.pending_placement: dict[str, object] | None = None
@@ -911,6 +912,7 @@ class MainWindow(QMainWindow):
self.document.delete_annotation(annotation.page_index, annotation.xref)
self.viewer.render()
self.statusBar().showMessage("注释已删除,记得保存", 3000)
self._refresh_annotation_panel()
deleted["value"] = True
dialog.accept()
@@ -1076,6 +1078,8 @@ class MainWindow(QMainWindow):
self.toggle_tools_action = QAction("工具", self)
self.toggle_tools_action.setCheckable(True)
self.toggle_tools_action.setChecked(True)
self.toggle_annotations_action = QAction("注释", self)
self.toggle_annotations_action.setCheckable(True)
self._apply_action_icons()
file_menu = self.menuBar().addMenu("文件")
@@ -1217,6 +1221,7 @@ class MainWindow(QMainWindow):
self.cover_action: QStyle.StandardPixmap.SP_DialogCloseButton,
self.dark_mode_action: QStyle.StandardPixmap.SP_ComputerIcon,
self.toggle_tools_action: QStyle.StandardPixmap.SP_FileDialogDetailedView,
self.toggle_annotations_action: QStyle.StandardPixmap.SP_FileDialogListView,
}
for action, standard_icon in icon_map.items():
action.setIcon(self.style().standardIcon(standard_icon))
@@ -1237,6 +1242,7 @@ class MainWindow(QMainWindow):
self.undo_action,
self.redo_action,
],
section_id="edit",
)
self.right_tools.add_section(
"注释",
@@ -1251,9 +1257,10 @@ class MainWindow(QMainWindow):
self.circle_action,
self.line_action,
self.arrow_action,
self.annotation_list_action,
],
section_id="annotations",
)
self.right_tools.add_annotation_list("注释列表", self.right_annotation_list, "annotation_list")
self.right_tools.add_section(
"页面",
[
@@ -1267,6 +1274,7 @@ class MainWindow(QMainWindow):
self.split_pdf_action,
self.merge_pdfs_action,
],
section_id="pages",
)
self.right_tools.add_section(
"签名与表单",
@@ -1276,6 +1284,7 @@ class MainWindow(QMainWindow):
self.draw_signature_action,
self.form_fill_action,
],
section_id="sign",
)
self.right_tools.add_section(
"导出与安全",
@@ -1287,6 +1296,7 @@ class MainWindow(QMainWindow):
self.save_encrypted_action,
self.remove_password_action,
],
section_id="export",
)
self.right_tools.add_section(
"设置",
@@ -1295,8 +1305,10 @@ class MainWindow(QMainWindow):
self.default_apps_action,
self.open_log_dir_action,
],
section_id="settings",
)
self.right_tools.finish()
self.right_tools.show_sections(["edit", "pages", "sign", "export", "settings"])
def _build_toolbar(self) -> None:
toolbar = QToolBar("主工具栏")
@@ -1354,6 +1366,7 @@ class MainWindow(QMainWindow):
toolbar.addAction(action)
toolbar.addSeparator()
toolbar.addAction(self.toggle_tools_action)
toolbar.addAction(self.toggle_annotations_action)
def _status_action_button(self, action: QAction, width: int = 64) -> QToolButton:
button = QToolButton()
@@ -1482,18 +1495,52 @@ class MainWindow(QMainWindow):
self.arrow_action.triggered.connect(self.add_arrow)
self.cover_action.triggered.connect(self.add_cover_area)
self.dark_mode_action.triggered.connect(self.toggle_dark_mode)
self.toggle_tools_action.toggled.connect(self.set_right_tools_visible)
self.right_tools.collapse_requested.connect(lambda: self.toggle_tools_action.setChecked(False))
self.toggle_tools_action.toggled.connect(self.toggle_tools_panel)
self.toggle_annotations_action.toggled.connect(self.toggle_annotations_panel)
self.right_tools.collapse_requested.connect(self.close_right_tools_panel)
self.tab_widget.currentChanged.connect(self._on_tab_changed)
self.tab_widget.tabCloseRequested.connect(self._close_tab)
self.search_box.returnPressed.connect(self.search)
self.search_results.itemClicked.connect(self._go_to_search_match)
self.right_annotation_list.itemClicked.connect(self._go_to_annotation_item)
def _go_to_search_match(self, item: QListWidgetItem) -> None:
match: SearchMatch = item.data(Qt.ItemDataRole.UserRole)
self.viewer.go_to_page(match.page_index)
def _go_to_annotation_item(self, item: QListWidgetItem) -> None:
annotation = item.data(Qt.ItemDataRole.UserRole)
if isinstance(annotation, AnnotationInfo):
self.viewer.go_to_page(annotation.page_index)
self.selected_annotation = annotation
self.viewer.set_selected_object_rect(annotation.rect)
def _refresh_annotation_panel(self) -> None:
if not hasattr(self, "right_annotation_list"):
return
self.right_annotation_list.clear()
if not self.document.is_open:
item = QListWidgetItem("打开 PDF 后显示注释列表")
item.setFlags(item.flags() & ~Qt.ItemFlag.ItemIsEnabled)
self.right_annotation_list.addItem(item)
return
annotations = self.document.annotations()
if not annotations:
item = QListWidgetItem("当前 PDF 没有注释")
item.setFlags(item.flags() & ~Qt.ItemFlag.ItemIsEnabled)
self.right_annotation_list.addItem(item)
return
for annotation in annotations:
content = annotation.content or annotation.annotation_type
item = QListWidgetItem(
f"第 {annotation.page_index + 1} 页 {annotation.annotation_type} {content}"
)
item.setData(Qt.ItemDataRole.UserRole, annotation)
self.right_annotation_list.addItem(item)
def highlight_current_page(self) -> None:
if not self._ensure_document():
return
@@ -1502,6 +1549,7 @@ class MainWindow(QMainWindow):
return
count = self.document.highlight_text(self.viewer.page_index, text)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage(f"已高亮 {count} 处,记得保存", 3000)
def underline_current_page(self) -> None:
@@ -1512,6 +1560,7 @@ class MainWindow(QMainWindow):
return
count = self.document.underline_text(self.viewer.page_index, text)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage(f"已添加下划线 {count} 处,记得保存", 3000)
def strikeout_current_page(self) -> None:
@@ -1522,6 +1571,7 @@ class MainWindow(QMainWindow):
return
count = self.document.strikeout_text(self.viewer.page_index, text)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage(f"已添加删除线 {count} 处,记得保存", 3000)
def add_note(self) -> None:
@@ -1532,6 +1582,7 @@ class MainWindow(QMainWindow):
return
self.document.add_note(self.viewer.page_index, text)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("批注已添加,记得保存", 3000)
def toggle_object_selection(self, enabled: bool) -> None:
@@ -1616,6 +1667,7 @@ class MainWindow(QMainWindow):
self.selected_annotation = None
self.viewer.clear_object_selection()
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("对象已删除,记得保存", 3000)
except Exception as exc:
QMessageBox.critical(self, "删除失败", f"无法删除对象:\n{exc}")
@@ -1681,6 +1733,7 @@ class MainWindow(QMainWindow):
try:
self.document.add_ink(page_index, points)
self.viewer.render()
self._refresh_annotation_panel()
self.freehand_action.setChecked(False)
self.statusBar().showMessage("画笔注释已添加,记得保存", 3000)
except Exception as exc:
@@ -1882,6 +1935,7 @@ class MainWindow(QMainWindow):
return
self.document.add_rectangle(self.viewer.page_index)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("矩形已添加,记得保存", 3000)
def add_circle(self) -> None:
@@ -1889,6 +1943,7 @@ class MainWindow(QMainWindow):
return
self.document.add_circle(self.viewer.page_index)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("圆形已添加,记得保存", 3000)
def add_line(self) -> None:
@@ -1896,6 +1951,7 @@ class MainWindow(QMainWindow):
return
self.document.add_line(self.viewer.page_index)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("线条已添加,记得保存", 3000)
def add_arrow(self) -> None:
@@ -1903,6 +1959,7 @@ class MainWindow(QMainWindow):
return
self.document.add_line(self.viewer.page_index, arrow=True)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("箭头已添加,记得保存", 3000)
def add_cover_area(self) -> None:
@@ -1910,6 +1967,7 @@ class MainWindow(QMainWindow):
return
self.document.add_cover_area(self.viewer.page_index)
self.viewer.render()
self._refresh_annotation_panel()
self.statusBar().showMessage("遮盖区域已添加,记得保存", 3000)
def _remember_file(self, path: str) -> None:
@@ -1984,6 +2042,38 @@ class MainWindow(QMainWindow):
self.settings.setValue("darkMode", self.dark_mode)
self._apply_theme()
def toggle_tools_panel(self, checked: bool) -> None:
if checked:
self._show_right_panel_mode("tools")
elif not self.toggle_annotations_action.isChecked():
self.set_right_tools_visible(False)
def toggle_annotations_panel(self, checked: bool) -> None:
if checked:
self._show_right_panel_mode("annotations")
elif not self.toggle_tools_action.isChecked():
self.set_right_tools_visible(False)
def _show_right_panel_mode(self, mode: str) -> None:
with QSignalBlocker(self.toggle_tools_action), QSignalBlocker(self.toggle_annotations_action):
self.toggle_tools_action.setChecked(mode == "tools")
self.toggle_annotations_action.setChecked(mode == "annotations")
if mode == "annotations":
self.right_tools.set_title("注释")
self.right_tools.show_sections(["annotations", "annotation_list"])
self._refresh_annotation_panel()
else:
self.right_tools.set_title("工具")
self.right_tools.show_sections(["edit", "pages", "sign", "export", "settings"])
self.set_right_tools_visible(True)
def close_right_tools_panel(self) -> None:
with QSignalBlocker(self.toggle_tools_action), QSignalBlocker(self.toggle_annotations_action):
self.toggle_tools_action.setChecked(False)
self.toggle_annotations_action.setChecked(False)
self.set_right_tools_visible(False)
def set_right_tools_visible(self, visible: bool) -> None:
self.right_tools.setVisible(visible)
if self.main_splitter:
@@ -2046,6 +2136,7 @@ class MainWindow(QMainWindow):
color: #f1f1f1; font-weight: 600; text-align: left;
padding: 6px 4px; border: none; background: transparent;
}
QLabel#toolSectionLabel { color: #f1f1f1; font-weight: 600; padding: 6px 4px; }
QToolButton#toolSectionTitle:hover { background: #3c4045; border-radius: 4px; }
QPushButton#rightToolButton {
color: #f1f1f1; text-align: left; padding: 6px 8px 6px 14px;
@@ -2122,6 +2213,7 @@ class MainWindow(QMainWindow):
color: #25272a; font-weight: 600; text-align: left;
padding: 6px 4px; border: none; background: transparent;
}
QLabel#toolSectionLabel { color: #25272a; font-weight: 600; padding: 6px 4px; }
QToolButton#toolSectionTitle:hover { background: #f0f3f8; border-radius: 4px; }
QPushButton#rightToolButton {
color: #25272a; text-align: left; padding: 6px 8px 6px 14px;
@@ -2222,6 +2314,7 @@ class MainWindow(QMainWindow):
self._update_current_tab_title()
self._sync_zoom_controls(self.viewer.zoom)
self._sync_page_controls()
self._refresh_annotation_panel()
if not self.document.is_open:
self.statusBar().showMessage("就绪")
return
+38 -3
View File
@@ -20,8 +20,9 @@ from PySide6.QtWidgets import (
class ToolSection(QFrame):
def __init__(self, title: str, actions: Iterable[QAction]) -> None:
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")
@@ -74,6 +75,8 @@ class RightToolsPanel(QWidget):
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")
@@ -94,6 +97,7 @@ class RightToolsPanel(QWidget):
header_layout.setSpacing(6)
title = QLabel("工具")
title.setObjectName("rightToolsTitle")
self.title_label = title
collapse_button = QToolButton()
collapse_button.setObjectName("rightToolsCollapseButton")
collapse_button.setText("‹")
@@ -115,11 +119,42 @@ class RightToolsPanel(QWidget):
self.setMinimumWidth(260)
self.setMaximumWidth(300)
def add_section(self, title: str, actions: Iterable[QAction]) -> None:
self.sections_layout.addWidget(ToolSection(title, actions))
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) -> 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(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 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)