Add configurable annotation styles
This commit is contained in:
@@ -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