Select new text objects after adding

This commit is contained in:
2026-06-30 16:54:39 +08:00
parent 8ce0c733bc
commit 6b3a90a6e9
3 changed files with 25 additions and 4 deletions
+1 -1
View File
@@ -35,7 +35,7 @@
- [ ] 文本框字号设置。
- [ ] 文本框颜色设置。
- [ ] 文本框加粗设置。
- [ ] 添加文本后自动选中新对象。
- [x] 添加文本后自动选中新对象。
- [x] 图片替换。
- [ ] 图片旋转。
- [ ] 图片透明度。
+8 -1
View File
@@ -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,
+16 -2
View File
@@ -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: