Unify selection for newly created annotations
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user