Add configurable cover annotations

This commit is contained in:
2026-06-30 16:30:46 +08:00
parent 8e81995982
commit 5df3d6ceda
3 changed files with 89 additions and 9 deletions
+22 -2
View File
@@ -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:
+64 -4
View File
@@ -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]