From a3ebad943bed8a414b8a116d1c43df38b5f22f94 Mon Sep 17 00:00:00 2001 From: shen Date: Tue, 30 Jun 2026 18:20:30 +0800 Subject: [PATCH] Add annotation property panel --- docs/v2_annotation_edit_plan.md | 6 ++ docs/v2_requirements_checklist.md | 4 +- pdfreaderlite/core/document.py | 53 +++++++++++++ pdfreaderlite/ui/main_window.py | 121 +++++++++++++++++++++++++++++- pdfreaderlite/ui/right_tools.py | 16 ++++ 5 files changed, 197 insertions(+), 3 deletions(-) diff --git a/docs/v2_annotation_edit_plan.md b/docs/v2_annotation_edit_plan.md index 1061cc2..9b026fd 100644 --- a/docs/v2_annotation_edit_plan.md +++ b/docs/v2_annotation_edit_plan.md @@ -36,6 +36,12 @@ V2 重点提升注释和编辑体验,让 PDFReaderLite 从“功能可用” ### 2. 注释属性面板 +第二版实施范围: + +- 选中注释后,右侧注释面板显示属性区。 +- 支持修改注释内容、颜色、线宽、透明度。 +- 修改后刷新页面、注释列表,并标记文档未保存。 + 目标: - 选中注释后,右侧显示属性区。 diff --git a/docs/v2_requirements_checklist.md b/docs/v2_requirements_checklist.md index 69e0622..8cdd111 100644 --- a/docs/v2_requirements_checklist.md +++ b/docs/v2_requirements_checklist.md @@ -7,7 +7,7 @@ - [x] 框选区域后直接删除线。 - [ ] 鼠标选择真实文本后直接高亮、下划线、删除线。(V3) - [ ] 选中文本后的迷你工具条。(V3) -- [ ] 注释属性面板。 +- [x] 注释属性面板。 - [x] 注释颜色设置。 - [x] 注释线宽设置。 - [x] 注释透明度设置。 @@ -80,7 +80,7 @@ - [x] 普通文本 PDF:框选区域后高亮、下划线、删除线。 - [ ] 普通文本 PDF:选择真实文字后高亮、下划线、删除线。(V3) - [ ] 扫描/图片型 PDF:区域高亮、图章、画笔仍可使用。 -- [ ] 多页 PDF:注释列表筛选、搜索、跳转正确。 +- [ ] 多页 PDF:注释列表筛选、搜索、跳转、属性编辑正确。 - [ ] 多标签 PDF:每个标签的未保存状态独立。 - [ ] 保存后重新打开:新增文本、图片、图章、遮盖、注释存在。 - [ ] 阅读模式:不能选中页面对象,右侧工具窗口关闭。 diff --git a/pdfreaderlite/core/document.py b/pdfreaderlite/core/document.py index 3cbbf3f..7bfb373 100644 --- a/pdfreaderlite/core/document.py +++ b/pdfreaderlite/core/document.py @@ -708,6 +708,59 @@ class PdfDocument: annot = annot.next return False + def update_annotation_style( + self, + page_index: int, + xref: int, + stroke_color: tuple[float, float, float], + border_width: float, + opacity: float, + content: str | None = None, + ) -> AnnotationInfo | None: + page = self._page(page_index) + annot = page.first_annot + while annot and annot.xref != xref: + annot = annot.next + if not annot: + return None + annot_type = annot.type[1].lower() + content_text = annot.info.get("content", "") if annot.info else "" + self.snapshot() + page = self._page(page_index) + annot = page.first_annot + while annot and annot.xref != xref: + annot = annot.next + if not annot: + return None + if annot_type == "square" and content_text == "区域高亮": + annot.set_colors(stroke=stroke_color, fill=stroke_color) + annot.set_border(width=0) + elif annot_type in {"square", "circle"}: + annot.set_colors(stroke=stroke_color) + annot.set_border(width=max(0, float(border_width))) + elif annot_type in {"line", "ink", "underline", "strikeout", "highlight"}: + annot.set_colors(stroke=stroke_color) + annot.set_border(width=max(0, float(border_width))) + elif annot_type == "freetext": + annot.set_colors(stroke=stroke_color) + else: + annot.set_colors(stroke=stroke_color) + annot.set_border(width=max(0, float(border_width))) + if annot_type not in {"text", "freetext", "square", "circle", "line", "ink", "underline", "strikeout", "highlight"}: + annot.set_border(width=max(0, float(border_width))) + annot.set_opacity(max(0.1, min(1.0, float(opacity)))) + if content is not None: + annot.set_info(content=content) + annot.update() + self.mark_changed() + return AnnotationInfo( + page_index=page_index, + xref=annot.xref, + annotation_type=annot.type[1], + content=annot.info.get("content", "") if annot.info else "", + rect=self._rect_tuple(annot.rect), + ) + def duplicate_annotation_info( self, annotation: AnnotationInfo, diff --git a/pdfreaderlite/ui/main_window.py b/pdfreaderlite/ui/main_window.py index df3c75b..375da1e 100644 --- a/pdfreaderlite/ui/main_window.py +++ b/pdfreaderlite/ui/main_window.py @@ -109,6 +109,14 @@ class MainWindow(QMainWindow): self.annotation_search_box = QLineEdit() self.right_annotation_list = QListWidget() self.right_object_list = QListWidget() + self.annotation_properties_panel = QWidget() + self.annotation_property_info_label = QLabel("未选择注释") + self.annotation_property_content = QLineEdit() + self.annotation_property_color = QColor(self.annotation_color) + self.annotation_property_color_button = QPushButton() + self.annotation_property_width = QSpinBox() + self.annotation_property_opacity = QDoubleSpinBox() + self.annotation_property_apply_button = QPushButton("应用属性") self.mode_status_label = QLabel() self.right_tools = RightToolsPanel(self.search_box, self.search_results) self.main_splitter: QSplitter | None = None @@ -1355,6 +1363,12 @@ class MainWindow(QMainWindow): "annotation_list", controls=[self.annotation_filter_combo, self.annotation_search_box], ) + self._build_annotation_properties_panel() + self.right_tools.add_widget_section( + "注释属性", + self.annotation_properties_panel, + "annotation_properties", + ) self.right_tools.add_section( "页面", [ @@ -1404,6 +1418,38 @@ class MainWindow(QMainWindow): self.right_tools.finish() self.right_tools.show_sections(["sign", "export", "settings"]) + def _build_annotation_properties_panel(self) -> None: + panel = self.annotation_properties_panel + panel.setObjectName("annotationPropertiesPanel") + layout = QFormLayout(panel) + layout.setContentsMargins(0, 0, 0, 0) + layout.setSpacing(6) + + self.annotation_property_info_label.setObjectName("annotationPropertyInfo") + self.annotation_property_content.setPlaceholderText("注释内容") + + self.annotation_property_width.setRange(0, 16) + self.annotation_property_width.setValue(self.annotation_line_width) + self.annotation_property_width.setSuffix(" px") + + self.annotation_property_opacity.setRange(10, 100) + self.annotation_property_opacity.setSingleStep(5) + self.annotation_property_opacity.setDecimals(0) + self.annotation_property_opacity.setSuffix("%") + self.annotation_property_opacity.setValue(self.annotation_opacity * 100) + + self._update_annotation_property_color_button() + self.annotation_property_color_button.clicked.connect(self.choose_annotation_property_color) + self.annotation_property_apply_button.clicked.connect(self.apply_selected_annotation_properties) + + layout.addRow(self.annotation_property_info_label) + layout.addRow("内容", self.annotation_property_content) + layout.addRow("颜色", self.annotation_property_color_button) + layout.addRow("线宽", self.annotation_property_width) + layout.addRow("透明度", self.annotation_property_opacity) + layout.addRow(self.annotation_property_apply_button) + self._refresh_annotation_properties_panel() + def _build_toolbar(self) -> None: toolbar = QToolBar("主工具栏") self.main_toolbar = toolbar @@ -1607,7 +1653,9 @@ class MainWindow(QMainWindow): if isinstance(annotation, AnnotationInfo): self.viewer.go_to_page(annotation.page_index) self.selected_annotation = annotation + self.selected_raster_id = None self.viewer.set_selected_object_rect(annotation.rect) + self._refresh_annotation_properties_panel() def show_annotation_list_context_menu(self, position) -> None: item = self.right_annotation_list.itemAt(position) @@ -1654,6 +1702,7 @@ class MainWindow(QMainWindow): ) self.viewer.render() self._refresh_annotation_panel() + self._refresh_annotation_properties_panel() self.statusBar().showMessage("注释内容已更新,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "编辑失败", f"无法更新注释内容:\n{exc}") @@ -1676,6 +1725,7 @@ class MainWindow(QMainWindow): self.viewer.clear_object_selection() self.viewer.render() self._refresh_annotation_panel() + self._refresh_annotation_properties_panel() self.statusBar().showMessage("注释已删除,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "删除失败", f"无法删除注释:\n{exc}") @@ -1801,12 +1851,14 @@ class MainWindow(QMainWindow): self.selected_raster_id = int(raster["id"]) self.selected_annotation = None self.viewer.set_selected_object_rect(raster["rect"]) + self._refresh_annotation_properties_panel() return if kind == "annotation" and isinstance(value, AnnotationInfo): self.viewer.go_to_page(value.page_index) self.selected_annotation = value self.selected_raster_id = None self.viewer.set_selected_object_rect(value.rect) + self._refresh_annotation_properties_panel() def highlight_current_page(self) -> None: self._start_area_markup("highlight", "拖拽框选要高亮的区域") @@ -1962,6 +2014,66 @@ class MainWindow(QMainWindow): self.annotation_opacity, ) + def choose_annotation_property_color(self) -> None: + color = QColorDialog.getColor(self.annotation_property_color, self, "选择注释颜色") + if color.isValid(): + self.annotation_property_color = color + self._update_annotation_property_color_button() + + def _update_annotation_property_color_button(self) -> None: + color = self.annotation_property_color + self.annotation_property_color_button.setText(color.name().upper()) + self.annotation_property_color_button.setStyleSheet( + f"background: {color.name()}; color: {'#ffffff' if color.lightness() < 128 else '#202124'};" + ) + + def _refresh_annotation_properties_panel(self) -> None: + enabled = self.selected_annotation is not None + widgets = [ + self.annotation_property_content, + self.annotation_property_color_button, + self.annotation_property_width, + self.annotation_property_opacity, + self.annotation_property_apply_button, + ] + for widget in widgets: + widget.setEnabled(enabled) + if not self.selected_annotation: + self.annotation_property_info_label.setText("未选择注释") + self.annotation_property_content.clear() + return + annotation = self.selected_annotation + self.annotation_property_info_label.setText( + f"第 {annotation.page_index + 1} 页 {annotation.annotation_type}" + ) + self.annotation_property_content.setText(annotation.content or "") + + def apply_selected_annotation_properties(self) -> None: + if not self.selected_annotation: + self.statusBar().showMessage("请先选择一个注释", 3000) + return + color = self.annotation_property_color + try: + updated = self.document.update_annotation_style( + self.selected_annotation.page_index, + self.selected_annotation.xref, + (color.redF(), color.greenF(), color.blueF()), + self.annotation_property_width.value(), + self.annotation_property_opacity.value() / 100, + self.annotation_property_content.text(), + ) + if not updated: + QMessageBox.warning(self, "属性更新失败", "没有找到对应的注释。") + return + self.selected_annotation = updated + self.viewer.render() + self.viewer.set_selected_object_rect(updated.rect) + self._refresh_annotation_panel() + self._refresh_annotation_properties_panel() + self.statusBar().showMessage("注释属性已更新,记得保存", 3000) + except Exception as exc: + QMessageBox.critical(self, "属性更新失败", f"无法更新注释属性:\n{exc}") + def configure_text_style(self) -> None: dialog = QDialog(self) dialog.setWindowTitle("文本样式") @@ -2033,6 +2145,7 @@ class MainWindow(QMainWindow): else: self.selected_annotation = None self.selected_raster_id = None + self._refresh_annotation_properties_panel() self.viewer.set_object_edit_enabled(enabled) if enabled: self.statusBar().showMessage("选择对象已启用:点击注释对象后可拖动或拖右下角缩放", 5000) @@ -2048,12 +2161,14 @@ class MainWindow(QMainWindow): self.selected_raster_id = int(raster["id"]) self.selected_annotation = None self.viewer.set_selected_object_rect(raster["rect"]) + self._refresh_annotation_properties_panel() self.statusBar().showMessage("已选中图片/签名,可拖动、缩放或按 Delete 删除", 5000) return annotation = self.document.annotation_at_point(page_index, point) self.selected_annotation = annotation self.selected_raster_id = None self.viewer.set_selected_object_rect(annotation.rect if annotation else None) + self._refresh_annotation_properties_panel() if annotation: self.statusBar().showMessage("已选中对象,可拖动、缩放或按 Delete 删除", 5000) else: @@ -2066,6 +2181,7 @@ class MainWindow(QMainWindow): self.viewer.go_to_page(annotation.page_index) self.viewer.set_selected_object_rect(annotation.rect) self._refresh_annotation_panel() + self._refresh_annotation_properties_panel() def update_selected_object_rect(self, page_index: int, rect: tuple[float, float, float, float]) -> None: if self.selected_raster_id is not None: @@ -2091,6 +2207,7 @@ class MainWindow(QMainWindow): ) self.viewer.render() self.viewer.set_selected_object_rect(updated_rect) + self._refresh_annotation_properties_panel() self.statusBar().showMessage("对象已更新,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "对象更新失败", f"无法更新对象:\n{exc}") @@ -2329,6 +2446,7 @@ class MainWindow(QMainWindow): self.viewer.clear_object_selection() self.viewer.render() self._refresh_annotation_panel() + self._refresh_annotation_properties_panel() self.statusBar().showMessage("对象已删除,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "删除失败", f"无法删除对象:\n{exc}") @@ -3092,8 +3210,9 @@ class MainWindow(QMainWindow): if mode == "annotations": self.right_tools.set_title("注释") - self.right_tools.show_sections(["annotations", "annotation_list"]) + self.right_tools.show_sections(["annotations", "annotation_list", "annotation_properties"]) self._refresh_annotation_panel() + self._refresh_annotation_properties_panel() elif mode == "edit": self.right_tools.set_title("编辑PDF") self.right_tools.show_sections(["edit", "object_list"]) diff --git a/pdfreaderlite/ui/right_tools.py b/pdfreaderlite/ui/right_tools.py index 4dfcfee..23cc3bd 100644 --- a/pdfreaderlite/ui/right_tools.py +++ b/pdfreaderlite/ui/right_tools.py @@ -153,6 +153,22 @@ class RightToolsPanel(QWidget): 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)