Add configurable annotation styles
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user