From 5df3d6ceda64bcceac74871bc10aae42acb800d6 Mon Sep 17 00:00:00 2001 From: shen Date: Tue, 30 Jun 2026 16:30:46 +0800 Subject: [PATCH] Add configurable cover annotations --- docs/v2_requirements_checklist.md | 6 +-- pdfreaderlite/core/document.py | 24 ++++++++++- pdfreaderlite/ui/main_window.py | 68 +++++++++++++++++++++++++++++-- 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/docs/v2_requirements_checklist.md b/docs/v2_requirements_checklist.md index 8665019..e161a58 100644 --- a/docs/v2_requirements_checklist.md +++ b/docs/v2_requirements_checklist.md @@ -40,9 +40,9 @@ - [ ] 图片旋转。 - [ ] 图片透明度。 - [ ] 图片裁剪。 -- [ ] 遮盖颜色设置。 -- [ ] 遮盖透明度。 -- [ ] 遮盖后添加文字。 +- [x] 遮盖颜色设置。 +- [x] 遮盖透明度。 +- [x] 遮盖后添加文字。 - [x] 图片/签名对象复制。 - [x] 图片/签名对象粘贴。 - [ ] 对象复制。 diff --git a/pdfreaderlite/core/document.py b/pdfreaderlite/core/document.py index b862722..8d380c8 100644 --- a/pdfreaderlite/core/document.py +++ b/pdfreaderlite/core/document.py @@ -392,14 +392,34 @@ class PdfDocument: annot.update() self.mark_changed() - def add_cover_area(self, page_index: int) -> None: + def add_cover_area( + self, + page_index: int, + fill_color: tuple[float, float, float] = (1, 1, 1), + opacity: float = 1.0, + text: str = "", + ) -> None: self.snapshot() page = self._page(page_index) rect = self._default_rect(page) annot = page.add_rect_annot(rect) - annot.set_colors(stroke=(1, 1, 1), fill=(1, 1, 1)) + 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.update() + if text.strip(): + text_annot = page.add_freetext_annot( + rect, + text.strip(), + fontsize=16, + fontname="china-s", + text_color=(0, 0, 0), + fill_color=None, + border_width=0, + align=1, + ) + text_annot.set_info(content=text.strip()) + text_annot.update() self.mark_changed() def insert_blank_page(self, after_page_index: int | None = None) -> int: diff --git a/pdfreaderlite/ui/main_window.py b/pdfreaderlite/ui/main_window.py index aea5bfb..9494c27 100644 --- a/pdfreaderlite/ui/main_window.py +++ b/pdfreaderlite/ui/main_window.py @@ -89,6 +89,8 @@ class MainWindow(QMainWindow): ) 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.cover_color = QColor(self.settings.value("coverStyle/color", "#ffffff", str)) + self.cover_opacity = float(self.settings.value("coverStyle/opacity", 1.0, float)) self.sessions: list[DocumentSession] = [] self.active_session_index = -1 @@ -2441,10 +2443,68 @@ class MainWindow(QMainWindow): return if not self._ensure_can_edit("遮盖"): return - self.document.add_cover_area(self.viewer.page_index) - self.viewer.render() - self._refresh_annotation_panel() - self.statusBar().showMessage("遮盖区域已添加,记得保存", 3000) + dialog = QDialog(self) + dialog.setWindowTitle("遮盖") + layout = QFormLayout(dialog) + + selected_color = QColor(self.cover_color) + color_button = QPushButton(selected_color.name().upper()) + + 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() + + opacity_input = QDoubleSpinBox() + opacity_input.setRange(10, 100) + opacity_input.setSingleStep(5) + opacity_input.setDecimals(0) + opacity_input.setValue(self.cover_opacity * 100) + opacity_input.setSuffix("%") + + text_input = QLineEdit() + text_input.setPlaceholderText("可选") + + layout.addRow("颜色", color_button) + layout.addRow("透明度", opacity_input) + layout.addRow("文字", text_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.cover_color = selected_color + self.cover_opacity = opacity_input.value() / 100 + self.settings.setValue("coverStyle/color", self.cover_color.name()) + self.settings.setValue("coverStyle/opacity", self.cover_opacity) + try: + 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.statusBar().showMessage("遮盖区域已添加,记得保存", 3000) + except Exception as exc: + QMessageBox.critical(self, "遮盖失败", f"无法添加遮盖区域:\n{exc}") def _remember_file(self, path: str) -> None: files = [item for item in self.recent_files if item != path]