diff --git a/docs/v2_requirements_checklist.md b/docs/v2_requirements_checklist.md index dd1cd34..af4b0d8 100644 --- a/docs/v2_requirements_checklist.md +++ b/docs/v2_requirements_checklist.md @@ -35,7 +35,7 @@ - [ ] 文本框字号设置。 - [ ] 文本框颜色设置。 - [ ] 文本框加粗设置。 -- [ ] 添加文本后自动选中新对象。 +- [x] 添加文本后自动选中新对象。 - [x] 图片替换。 - [ ] 图片旋转。 - [ ] 图片透明度。 diff --git a/pdfreaderlite/core/document.py b/pdfreaderlite/core/document.py index 8d380c8..da5e5d4 100644 --- a/pdfreaderlite/core/document.py +++ b/pdfreaderlite/core/document.py @@ -206,7 +206,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 (72, 96) @@ -226,6 +226,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 add_text_stamp( self, diff --git a/pdfreaderlite/ui/main_window.py b/pdfreaderlite/ui/main_window.py index 991b22b..0b0a698 100644 --- a/pdfreaderlite/ui/main_window.py +++ b/pdfreaderlite/ui/main_window.py @@ -1984,6 +1984,14 @@ class MainWindow(QMainWindow): else: self.statusBar().showMessage("未选中对象", 3000) + def _select_annotation_object(self, annotation: AnnotationInfo) -> None: + self.selected_annotation = annotation + self.selected_raster_id = None + self.select_object_action.setChecked(True) + self.viewer.go_to_page(annotation.page_index) + self.viewer.set_selected_object_rect(annotation.rect) + self._refresh_annotation_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: raster = self._session_raster_by_id(self.selected_raster_id) @@ -2353,8 +2361,9 @@ class MainWindow(QMainWindow): if not ok or not text.strip(): return try: - self.document.add_text(self.viewer.page_index, text) + annotation = self.document.add_text(self.viewer.page_index, text) self.viewer.render() + self._select_annotation_object(annotation) self.statusBar().showMessage("文本已添加,记得保存", 3000) except Exception as exc: QMessageBox.critical(self, "添加失败", f"无法添加文本:\n{exc}") @@ -2484,16 +2493,21 @@ class MainWindow(QMainWindow): try: kind = payload.get("type") if kind == "text": - self.document.add_text(page_index, str(payload["text"]), point) + annotation = self.document.add_text(page_index, str(payload["text"]), point) elif kind == "image": self._add_session_raster_object("image", page_index, str(payload["path"]), point) + annotation = None elif kind == "image_stamp": self._add_session_raster_object("stamp", page_index, str(payload["path"]), point) + annotation = None elif kind == "signature": self._add_session_raster_object("signature", page_index, str(payload["path"]), point) + annotation = None else: raise ValueError("未知的放置类型") self.viewer.render() + if annotation: + self._select_annotation_object(annotation) self._refresh_session_raster_overlays() self.statusBar().showMessage("已放置,记得保存", 3000) except Exception as exc: