Add annotation property panel
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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"])
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user