From b7be31bed9b9f8451053ff930ae0aaef6e13406d Mon Sep 17 00:00:00 2001 From: shen Date: Tue, 30 Jun 2026 19:47:54 +0800 Subject: [PATCH] Unify selection for newly created annotations --- docs/v2_annotation_edit_plan.md | 6 +++ docs/v2_requirements_checklist.md | 7 ++-- pdfreaderlite/core/document.py | 67 +++++++++++++++++++++++++++---- pdfreaderlite/ui/main_window.py | 35 ++++++++-------- 4 files changed, 88 insertions(+), 27 deletions(-) diff --git a/docs/v2_annotation_edit_plan.md b/docs/v2_annotation_edit_plan.md index 9b026fd..8f2cfc1 100644 --- a/docs/v2_annotation_edit_plan.md +++ b/docs/v2_annotation_edit_plan.md @@ -87,6 +87,12 @@ V2 重点提升注释和编辑体验,让 PDFReaderLite 从“功能可用” ### 5. 统一对象选择体验 +第二版实施范围: + +- 新建文本、图片、签名、批注、文字图章、遮盖、矩形、圆形、线条、箭头、画笔后自动选中。 +- 选中对象后统一显示选择框,支持拖动、缩放、Delete 删除、Esc 取消。 +- 右侧页面对象列表可选择当前页对象。 + 目标: - 文本、图片、签名、遮盖、图形注释使用统一选择框和控制点。 diff --git a/docs/v2_requirements_checklist.md b/docs/v2_requirements_checklist.md index 8cdd111..ee74d74 100644 --- a/docs/v2_requirements_checklist.md +++ b/docs/v2_requirements_checklist.md @@ -55,7 +55,7 @@ ## 编辑:旧功能完善 -- [ ] 文本、图片、签名、遮盖、图形注释使用统一选择框。 +- [x] 文本、图片、签名、遮盖、图形注释使用统一选择框。 - [x] Delete 删除选中对象。 - [x] Esc 取消当前选择。 - [x] 右键菜单支持删除、复制、属性。 @@ -72,8 +72,8 @@ - [x] Esc 取消放置签名。 - [x] Esc 取消画笔。 - [x] Esc 取消当前选中对象。 -- [ ] 顶部工具栏只保留模式和常用基础动作。 -- [ ] 复杂设置放在右侧面板或右键菜单中。 +- [ ] 顶部工具栏进一步精简。(后续优化,第二版保持现状) +- [ ] 复杂设置进一步集中到右侧面板或右键菜单中。(后续优化,第二版保持现状) ## 验收测试建议 @@ -83,5 +83,6 @@ - [ ] 多页 PDF:注释列表筛选、搜索、跳转、属性编辑正确。 - [ ] 多标签 PDF:每个标签的未保存状态独立。 - [ ] 保存后重新打开:新增文本、图片、图章、遮盖、注释存在。 +- [x] 新建批注、文字图章、遮盖、图形注释后自动进入统一选择框。 - [ ] 阅读模式:不能选中页面对象,右侧工具窗口关闭。 - [ ] 大文件:缩略图和页面滚动不卡死。 diff --git a/pdfreaderlite/core/document.py b/pdfreaderlite/core/document.py index 7bfb373..4e91833 100644 --- a/pdfreaderlite/core/document.py +++ b/pdfreaderlite/core/document.py @@ -193,13 +193,21 @@ class PdfDocument: self.mark_changed() return len(rects) - def add_note(self, page_index: int, text: str) -> None: + def add_note(self, page_index: int, text: str) -> AnnotationInfo: self.snapshot() page = self._page(page_index) point = fitz.Point(72, 72) annot = page.add_text_annot(point, text) + annot.set_info(content=text) annot.update() self.mark_changed() + return AnnotationInfo( + page_index=page_index, + xref=annot.xref, + annotation_type=annot.type[1], + content=text, + rect=self._rect_tuple(annot.rect), + ) def add_text( self, @@ -291,7 +299,7 @@ class PdfDocument: page_index: int, text: str, point: tuple[float, float] | None = None, - ) -> None: + ) -> AnnotationInfo: self.snapshot() page = self._page(page_index) x0, y0 = point or (96, 120) @@ -312,6 +320,13 @@ class PdfDocument: annot.set_info(content=text) annot.update() self.mark_changed() + return AnnotationInfo( + page_index=page_index, + xref=annot.xref, + annotation_type=annot.type[1], + content=text, + rect=self._rect_tuple(annot.rect), + ) def insert_image( self, @@ -389,7 +404,7 @@ class PdfDocument: stroke_color: tuple[float, float, float] = (0.9, 0.1, 0.1), border_width: float = 2, opacity: float = 1.0, - ) -> None: + ) -> AnnotationInfo: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) @@ -397,6 +412,13 @@ class PdfDocument: self._apply_annotation_style(annot, stroke_color, border_width, opacity) 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 add_circle( self, @@ -404,7 +426,7 @@ class PdfDocument: stroke_color: tuple[float, float, float] = (0.1, 0.3, 0.9), border_width: float = 2, opacity: float = 1.0, - ) -> None: + ) -> AnnotationInfo: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) @@ -412,6 +434,13 @@ class PdfDocument: self._apply_annotation_style(annot, stroke_color, border_width, opacity) 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 add_line( self, @@ -420,7 +449,7 @@ class PdfDocument: stroke_color: tuple[float, float, float] = (0.1, 0.1, 0.1), border_width: float = 2, opacity: float = 1.0, - ) -> None: + ) -> AnnotationInfo: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) @@ -432,6 +461,13 @@ class PdfDocument: annot.set_line_ends(fitz.PDF_ANNOT_LE_NONE, fitz.PDF_ANNOT_LE_CLOSED_ARROW) 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 add_ink( self, @@ -440,9 +476,9 @@ class PdfDocument: stroke_color: tuple[float, float, float] = (0.05, 0.05, 0.05), border_width: float = 2, opacity: float = 1.0, - ) -> None: + ) -> AnnotationInfo | None: if len(points) < 2: - return + return None self.snapshot() page = self._page(page_index) stroke_points = [(float(x), float(y)) for x, y in points] @@ -450,6 +486,13 @@ class PdfDocument: self._apply_annotation_style(annot, stroke_color, border_width, opacity) 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 add_cover_area( self, @@ -457,7 +500,7 @@ class PdfDocument: fill_color: tuple[float, float, float] = (1, 1, 1), opacity: float = 1.0, text: str = "", - ) -> None: + ) -> AnnotationInfo: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) @@ -465,6 +508,7 @@ class PdfDocument: annot.set_colors(stroke=fill_color, fill=fill_color) annot.set_border(width=0) annot.set_opacity(max(0.1, min(1.0, float(opacity)))) + annot.set_info(content="遮盖") annot.update() if text.strip(): text_annot = page.add_freetext_annot( @@ -480,6 +524,13 @@ class PdfDocument: text_annot.set_info(content=text.strip()) text_annot.update() self.mark_changed() + return AnnotationInfo( + page_index=page_index, + xref=annot.xref, + annotation_type=annot.type[1], + content="遮盖", + rect=self._rect_tuple(annot.rect), + ) def insert_blank_page(self, after_page_index: int | None = None) -> int: if not self._doc: diff --git a/pdfreaderlite/ui/main_window.py b/pdfreaderlite/ui/main_window.py index 375da1e..991f021 100644 --- a/pdfreaderlite/ui/main_window.py +++ b/pdfreaderlite/ui/main_window.py @@ -1911,9 +1911,9 @@ class MainWindow(QMainWindow): text, ok = QInputDialog.getMultiLineText(self, "批注", "批注内容:") if not ok or not text.strip(): return - self.document.add_note(self.viewer.page_index, text) + annotation = self.document.add_note(self.viewer.page_index, text) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("批注已添加,记得保存", 3000) def add_text_stamp(self) -> None: @@ -1926,9 +1926,9 @@ class MainWindow(QMainWindow): if not ok or not text.strip(): return try: - self.document.add_text_stamp(self.viewer.page_index, text.strip()) + annotation = self.document.add_text_stamp(self.viewer.page_index, text.strip()) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("文字图章已添加,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "图章失败", f"无法添加文字图章:\n{exc}") @@ -2645,9 +2645,12 @@ class MainWindow(QMainWindow): def add_ink_stroke(self, page_index: int, points: list[tuple[float, float]]) -> None: try: color, line_width, opacity = self._annotation_style() - self.document.add_ink(page_index, points, color, line_width, opacity) + annotation = self.document.add_ink(page_index, points, color, line_width, opacity) self.viewer.render() - self._refresh_annotation_panel() + if annotation: + self._select_annotation_object(annotation) + else: + self._refresh_annotation_panel() self.freehand_action.setChecked(False) self.statusBar().showMessage("画笔注释已添加,记得保存", 3000) except Exception as exc: @@ -2944,9 +2947,9 @@ class MainWindow(QMainWindow): if not self._ensure_can_edit("矩形"): return color, line_width, opacity = self._annotation_style() - self.document.add_rectangle(self.viewer.page_index, color, line_width, opacity) + annotation = self.document.add_rectangle(self.viewer.page_index, color, line_width, opacity) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("矩形已添加,记得保存", 3000) def add_circle(self) -> None: @@ -2955,9 +2958,9 @@ class MainWindow(QMainWindow): if not self._ensure_can_edit("圆形"): return color, line_width, opacity = self._annotation_style() - self.document.add_circle(self.viewer.page_index, color, line_width, opacity) + annotation = self.document.add_circle(self.viewer.page_index, color, line_width, opacity) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("圆形已添加,记得保存", 3000) def add_line(self) -> None: @@ -2966,9 +2969,9 @@ class MainWindow(QMainWindow): if not self._ensure_can_edit("线条"): return color, line_width, opacity = self._annotation_style() - self.document.add_line(self.viewer.page_index, False, color, line_width, opacity) + annotation = self.document.add_line(self.viewer.page_index, False, color, line_width, opacity) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("线条已添加,记得保存", 3000) def add_arrow(self) -> None: @@ -2977,9 +2980,9 @@ class MainWindow(QMainWindow): if not self._ensure_can_edit("箭头"): return color, line_width, opacity = self._annotation_style() - self.document.add_line(self.viewer.page_index, True, color, line_width, opacity) + annotation = self.document.add_line(self.viewer.page_index, True, color, line_width, opacity) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("箭头已添加,记得保存", 3000) def add_cover_area(self) -> None: @@ -3038,14 +3041,14 @@ class MainWindow(QMainWindow): self.settings.setValue("coverStyle/color", self.cover_color.name()) self.settings.setValue("coverStyle/opacity", self.cover_opacity) try: - self.document.add_cover_area( + annotation = self.document.add_cover_area( self.viewer.page_index, (self.cover_color.redF(), self.cover_color.greenF(), self.cover_color.blueF()), self.cover_opacity, text_input.text(), ) self.viewer.render() - self._refresh_annotation_panel() + self._select_annotation_object(annotation) self.statusBar().showMessage("遮盖区域已添加,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "遮盖失败", f"无法添加遮盖区域:\n{exc}")