From 04f1d901b2f50f9e1e8a85aa4f097ed2f2891ba1 Mon Sep 17 00:00:00 2001 From: shen Date: Tue, 30 Jun 2026 14:50:05 +0800 Subject: [PATCH] Add configurable annotation styles --- README.md | 6 ++ docs/v2_requirements_checklist.md | 12 ++-- pdfreaderlite/core/document.py | 61 +++++++++++++++----- pdfreaderlite/ui/main_window.py | 94 +++++++++++++++++++++++++++++-- 4 files changed, 148 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b018927..0389c62 100644 --- a/README.md +++ b/README.md @@ -106,3 +106,9 @@ dist\PDFReaderLite.exe - 敏感信息真正永久擦除。 - 自动更新。 - 单 exe 内置完整 OCR。 + +## 说明 + +这是我用CODEX完成的软件,感兴趣的朋友可以下载学习或者自用,如果对软件有建议,可以联系我。 + +wenhao.shen@pkuncii.cn diff --git a/docs/v2_requirements_checklist.md b/docs/v2_requirements_checklist.md index 782c7a8..cf416b3 100644 --- a/docs/v2_requirements_checklist.md +++ b/docs/v2_requirements_checklist.md @@ -7,9 +7,9 @@ - [ ] 鼠标选择文本后直接删除线。 - [ ] 选中文本后的迷你工具条。 - [ ] 注释属性面板。 -- [ ] 注释颜色设置。 -- [ ] 注释线宽设置。 -- [ ] 注释透明度设置。 +- [x] 注释颜色设置。 +- [x] 注释线宽设置。 +- [x] 注释透明度设置。 - [x] 便签/批注内容二次编辑。 - [x] 注释列表按类型筛选。 - [x] 注释列表搜索。 @@ -18,13 +18,13 @@ - [ ] 图章工具。 - [ ] 常用中文图章预设。 - [ ] 自定义图片图章。 -- [ ] 注释样式预设。 +- [x] 注释样式预设。 ## 注释:旧功能完善 - [ ] 高亮、下划线、删除线保留“输入文字查找后标注”的备用方式。 -- [ ] 图形注释支持修改颜色、线宽、透明度。 -- [ ] 自由画笔支持颜色、线宽、透明度。 +- [x] 图形注释支持修改颜色、线宽、透明度。 +- [x] 自由画笔支持颜色、线宽、透明度。 - [ ] 注释创建后自动刷新右侧注释列表。 - [x] 注释删除后自动刷新右侧注释列表。 - [x] 阅读模式下禁止选中注释对象。 diff --git a/pdfreaderlite/core/document.py b/pdfreaderlite/core/document.py index 735d782..d6eb7d7 100644 --- a/pdfreaderlite/core/document.py +++ b/pdfreaderlite/core/document.py @@ -297,49 +297,71 @@ class PdfDocument: page.insert_image(fitz.Rect(*rect), filename=str(image_path), keep_proportion=True) self.mark_changed() - def add_rectangle(self, page_index: int) -> None: + def add_rectangle( + self, + page_index: int, + stroke_color: tuple[float, float, float] = (0.9, 0.1, 0.1), + border_width: float = 2, + opacity: float = 1.0, + ) -> None: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) annot = page.add_rect_annot(rect) - annot.set_colors(stroke=(0.9, 0.1, 0.1)) - annot.set_border(width=2) + self._apply_annotation_style(annot, stroke_color, border_width, opacity) annot.update() self.mark_changed() - def add_circle(self, page_index: int) -> None: + def add_circle( + self, + page_index: int, + stroke_color: tuple[float, float, float] = (0.1, 0.3, 0.9), + border_width: float = 2, + opacity: float = 1.0, + ) -> None: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) annot = page.add_circle_annot(rect) - annot.set_colors(stroke=(0.1, 0.3, 0.9)) - annot.set_border(width=2) + self._apply_annotation_style(annot, stroke_color, border_width, opacity) annot.update() self.mark_changed() - def add_line(self, page_index: int, arrow: bool = False) -> None: + def add_line( + self, + page_index: int, + arrow: bool = False, + stroke_color: tuple[float, float, float] = (0.1, 0.1, 0.1), + border_width: float = 2, + opacity: float = 1.0, + ) -> None: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) start = fitz.Point(rect.x0, rect.y0) end = fitz.Point(rect.x1, rect.y1) annot = page.add_line_annot(start, end) - annot.set_colors(stroke=(0.1, 0.1, 0.1)) - annot.set_border(width=2) + self._apply_annotation_style(annot, stroke_color, border_width, opacity) if arrow: annot.set_line_ends(fitz.PDF_ANNOT_LE_NONE, fitz.PDF_ANNOT_LE_CLOSED_ARROW) annot.update() self.mark_changed() - def add_ink(self, page_index: int, points: list[tuple[float, float]]) -> None: + def add_ink( + self, + page_index: int, + points: list[tuple[float, float]], + stroke_color: tuple[float, float, float] = (0.05, 0.05, 0.05), + border_width: float = 2, + opacity: float = 1.0, + ) -> None: if len(points) < 2: return self.snapshot() page = self._page(page_index) - stroke = [(float(x), float(y)) for x, y in points] - annot = page.add_ink_annot([stroke]) - annot.set_colors(stroke=(0.05, 0.05, 0.05)) - annot.set_border(width=2) + stroke_points = [(float(x), float(y)) for x, y in points] + annot = page.add_ink_annot([stroke_points]) + self._apply_annotation_style(annot, stroke_color, border_width, opacity) annot.update() self.mark_changed() @@ -754,6 +776,17 @@ class PdfDocument: annot = annot.next return None + def _apply_annotation_style( + self, + annot, + stroke_color: tuple[float, float, float], + border_width: float, + opacity: float, + ) -> None: + annot.set_colors(stroke=stroke_color) + annot.set_border(width=max(1, float(border_width))) + annot.set_opacity(max(0.1, min(1.0, float(opacity)))) + def mark_changed(self) -> None: self._revision += 1 self._dirty = True diff --git a/pdfreaderlite/ui/main_window.py b/pdfreaderlite/ui/main_window.py index b66e7ad..95cb51e 100644 --- a/pdfreaderlite/ui/main_window.py +++ b/pdfreaderlite/ui/main_window.py @@ -22,9 +22,11 @@ from PySide6.QtGui import ( from PySide6.QtPrintSupport import QPrintDialog, QPrinter from PySide6.QtWidgets import ( QCheckBox, + QColorDialog, QComboBox, QDialog, QDialogButtonBox, + QDoubleSpinBox, QFormLayout, QFileDialog, QFrame, @@ -37,8 +39,10 @@ from PySide6.QtWidgets import ( QMainWindow, QMenu, QMessageBox, + QPushButton, QSplitter, QSlider, + QSpinBox, QStatusBar, QStyle, QTabWidget, @@ -80,6 +84,11 @@ class MainWindow(QMainWindow): self.settings = QSettings() self.recent_files: list[str] = self.settings.value("recentFiles", [], list) self.dark_mode = self.settings.value("darkMode", False, bool) + self.annotation_color = QColor( + self.settings.value("annotationStyle/color", "#1f6fd1", str) + ) + self.annotation_line_width = int(self.settings.value("annotationStyle/lineWidth", 2, int)) + self.annotation_opacity = float(self.settings.value("annotationStyle/opacity", 1.0, float)) self.sessions: list[DocumentSession] = [] self.active_session_index = -1 @@ -1068,6 +1077,7 @@ class MainWindow(QMainWindow): self.underline_action = QAction("下划线", self) self.strikeout_action = QAction("删除线", self) self.note_action = QAction("批注", self) + self.annotation_style_action = QAction("注释样式", self) self.select_object_action = QAction("选择对象", self) self.select_object_action.setCheckable(True) self.select_object_action.setShortcut(QKeySequence("V")) @@ -1224,6 +1234,7 @@ class MainWindow(QMainWindow): self.underline_action: QStyle.StandardPixmap.SP_DialogApplyButton, self.strikeout_action: QStyle.StandardPixmap.SP_DialogCancelButton, self.note_action: QStyle.StandardPixmap.SP_MessageBoxInformation, + self.annotation_style_action: QStyle.StandardPixmap.SP_DialogApplyButton, self.select_object_action: QStyle.StandardPixmap.SP_ArrowRight, self.freehand_action: QStyle.StandardPixmap.SP_DialogApplyButton, self.add_text_action: QStyle.StandardPixmap.SP_FileDialogDetailedView, @@ -1273,6 +1284,7 @@ class MainWindow(QMainWindow): self.underline_action, self.strikeout_action, self.note_action, + self.annotation_style_action, self.select_object_action, self.freehand_action, self.rectangle_action, @@ -1483,6 +1495,7 @@ class MainWindow(QMainWindow): self.underline_action.triggered.connect(self.underline_current_page) self.strikeout_action.triggered.connect(self.strikeout_current_page) self.note_action.triggered.connect(self.add_note) + self.annotation_style_action.triggered.connect(self.configure_annotation_style) self.select_object_action.toggled.connect(self.toggle_object_selection) self.freehand_action.toggled.connect(self.toggle_freehand_drawing) self.add_text_action.triggered.connect(self.add_text) @@ -1717,6 +1730,72 @@ class MainWindow(QMainWindow): self._refresh_annotation_panel() self.statusBar().showMessage("批注已添加,记得保存", 3000) + def configure_annotation_style(self) -> None: + dialog = QDialog(self) + dialog.setWindowTitle("注释样式") + layout = QFormLayout(dialog) + + selected_color = QColor(self.annotation_color) + color_button = QPushButton(selected_color.name().upper()) + color_button.setAutoFillBackground(True) + + def update_color_button() -> None: + color_button.setText(selected_color.name().upper()) + color_button.setStyleSheet( + f"background: {selected_color.name()}; color: {'#ffffff' if selected_color.lightness() < 128 else '#202124'};" + ) + + def choose_color() -> None: + nonlocal selected_color + color = QColorDialog.getColor(selected_color, self, "选择注释颜色") + if color.isValid(): + selected_color = color + update_color_button() + + color_button.clicked.connect(choose_color) + update_color_button() + + width_input = QSpinBox() + width_input.setRange(1, 12) + width_input.setValue(self.annotation_line_width) + width_input.setSuffix(" px") + + opacity_input = QDoubleSpinBox() + opacity_input.setRange(10, 100) + opacity_input.setSingleStep(5) + opacity_input.setDecimals(0) + opacity_input.setValue(self.annotation_opacity * 100) + opacity_input.setSuffix("%") + + layout.addRow("颜色", color_button) + layout.addRow("线宽", width_input) + layout.addRow("透明度", opacity_input) + + buttons = QDialogButtonBox( + QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel + ) + buttons.accepted.connect(dialog.accept) + buttons.rejected.connect(dialog.reject) + layout.addRow(buttons) + if dialog.exec() != QDialog.DialogCode.Accepted: + return + + self.annotation_color = selected_color + self.annotation_line_width = width_input.value() + self.annotation_opacity = opacity_input.value() / 100 + self.settings.setValue("annotationStyle/color", self.annotation_color.name()) + self.settings.setValue("annotationStyle/lineWidth", self.annotation_line_width) + self.settings.setValue("annotationStyle/opacity", self.annotation_opacity) + self.statusBar().showMessage("注释样式已更新", 3000) + + def _annotation_style(self) -> tuple[tuple[float, float, float], int, float]: + color = self.annotation_color + return ( + (color.redF(), color.greenF(), color.blueF()), + self.annotation_line_width, + self.annotation_opacity, + ) + def toggle_object_selection(self, enabled: bool) -> None: if enabled and not self._ensure_document(): self.select_object_action.setChecked(False) @@ -1898,7 +1977,8 @@ class MainWindow(QMainWindow): def add_ink_stroke(self, page_index: int, points: list[tuple[float, float]]) -> None: try: - self.document.add_ink(page_index, points) + color, line_width, opacity = self._annotation_style() + self.document.add_ink(page_index, points, color, line_width, opacity) self.viewer.render() self._refresh_annotation_panel() self.freehand_action.setChecked(False) @@ -2118,7 +2198,8 @@ class MainWindow(QMainWindow): return if not self._ensure_can_edit("矩形"): return - self.document.add_rectangle(self.viewer.page_index) + color, line_width, opacity = self._annotation_style() + self.document.add_rectangle(self.viewer.page_index, color, line_width, opacity) self.viewer.render() self._refresh_annotation_panel() self.statusBar().showMessage("矩形已添加,记得保存", 3000) @@ -2128,7 +2209,8 @@ class MainWindow(QMainWindow): return if not self._ensure_can_edit("圆形"): return - self.document.add_circle(self.viewer.page_index) + color, line_width, opacity = self._annotation_style() + self.document.add_circle(self.viewer.page_index, color, line_width, opacity) self.viewer.render() self._refresh_annotation_panel() self.statusBar().showMessage("圆形已添加,记得保存", 3000) @@ -2138,7 +2220,8 @@ class MainWindow(QMainWindow): return if not self._ensure_can_edit("线条"): return - self.document.add_line(self.viewer.page_index) + color, line_width, opacity = self._annotation_style() + self.document.add_line(self.viewer.page_index, False, color, line_width, opacity) self.viewer.render() self._refresh_annotation_panel() self.statusBar().showMessage("线条已添加,记得保存", 3000) @@ -2148,7 +2231,8 @@ class MainWindow(QMainWindow): return if not self._ensure_can_edit("箭头"): return - self.document.add_line(self.viewer.page_index, arrow=True) + color, line_width, opacity = self._annotation_style() + self.document.add_line(self.viewer.page_index, True, color, line_width, opacity) self.viewer.render() self._refresh_annotation_panel() self.statusBar().showMessage("箭头已添加,记得保存", 3000)