Add click placement for PDF objects
This commit is contained in:
@@ -52,6 +52,7 @@ class MainWindow(QMainWindow):
|
||||
self.search_box = QLineEdit()
|
||||
self.search_results = QListWidget()
|
||||
self.right_tools = RightToolsPanel(self.search_box, self.search_results)
|
||||
self.pending_placement: dict[str, object] | None = None
|
||||
|
||||
self._build_window()
|
||||
self._build_actions()
|
||||
@@ -711,8 +712,11 @@ class MainWindow(QMainWindow):
|
||||
self.freehand_action = QAction("自由画笔", self)
|
||||
self.freehand_action.setCheckable(True)
|
||||
self.add_text_action = QAction("添加文本", self)
|
||||
self.place_text_action = QAction("点击放置文本", self)
|
||||
self.insert_image_action = QAction("插入图片", self)
|
||||
self.place_image_action = QAction("点击放置图片", self)
|
||||
self.signature_action = QAction("图片签名", self)
|
||||
self.place_signature_action = QAction("点击放置签名", self)
|
||||
self.draw_signature_action = QAction("手写签名", self)
|
||||
self.rectangle_action = QAction("矩形", self)
|
||||
self.circle_action = QAction("圆形", self)
|
||||
@@ -779,8 +783,11 @@ class MainWindow(QMainWindow):
|
||||
edit_menu.addAction(self.note_action)
|
||||
edit_menu.addAction(self.freehand_action)
|
||||
edit_menu.addAction(self.add_text_action)
|
||||
edit_menu.addAction(self.place_text_action)
|
||||
edit_menu.addAction(self.insert_image_action)
|
||||
edit_menu.addAction(self.place_image_action)
|
||||
edit_menu.addAction(self.signature_action)
|
||||
edit_menu.addAction(self.place_signature_action)
|
||||
edit_menu.addAction(self.draw_signature_action)
|
||||
edit_menu.addAction(self.rectangle_action)
|
||||
edit_menu.addAction(self.circle_action)
|
||||
@@ -802,7 +809,9 @@ class MainWindow(QMainWindow):
|
||||
"编辑 PDF",
|
||||
[
|
||||
self.add_text_action,
|
||||
self.place_text_action,
|
||||
self.insert_image_action,
|
||||
self.place_image_action,
|
||||
self.cover_action,
|
||||
self.undo_action,
|
||||
self.redo_action,
|
||||
@@ -841,6 +850,7 @@ class MainWindow(QMainWindow):
|
||||
"签名与表单",
|
||||
[
|
||||
self.signature_action,
|
||||
self.place_signature_action,
|
||||
self.draw_signature_action,
|
||||
self.form_fill_action,
|
||||
],
|
||||
@@ -893,8 +903,11 @@ class MainWindow(QMainWindow):
|
||||
self.note_action,
|
||||
self.freehand_action,
|
||||
self.add_text_action,
|
||||
self.place_text_action,
|
||||
self.insert_image_action,
|
||||
self.place_image_action,
|
||||
self.signature_action,
|
||||
self.place_signature_action,
|
||||
self.draw_signature_action,
|
||||
self.rectangle_action,
|
||||
self.circle_action,
|
||||
@@ -951,8 +964,11 @@ class MainWindow(QMainWindow):
|
||||
self.note_action.triggered.connect(self.add_note)
|
||||
self.freehand_action.toggled.connect(self.toggle_freehand_drawing)
|
||||
self.add_text_action.triggered.connect(self.add_text)
|
||||
self.place_text_action.triggered.connect(self.prepare_place_text)
|
||||
self.insert_image_action.triggered.connect(self.insert_image)
|
||||
self.place_image_action.triggered.connect(self.prepare_place_image)
|
||||
self.signature_action.triggered.connect(self.add_signature)
|
||||
self.place_signature_action.triggered.connect(self.prepare_place_signature)
|
||||
self.draw_signature_action.triggered.connect(self.draw_signature)
|
||||
self.rectangle_action.triggered.connect(self.add_rectangle)
|
||||
self.circle_action.triggered.connect(self.add_circle)
|
||||
@@ -968,6 +984,7 @@ class MainWindow(QMainWindow):
|
||||
self.search_box.returnPressed.connect(self.search)
|
||||
self.search_results.itemClicked.connect(self._go_to_search_match)
|
||||
self.viewer.ink_stroke_finished.connect(self.add_ink_stroke)
|
||||
self.viewer.placement_requested.connect(self.place_pending_object)
|
||||
|
||||
def _go_to_search_match(self, item: QListWidgetItem) -> None:
|
||||
match: SearchMatch = item.data(Qt.ItemDataRole.UserRole)
|
||||
@@ -1017,6 +1034,9 @@ class MainWindow(QMainWindow):
|
||||
if enabled and not self._ensure_document():
|
||||
self.freehand_action.setChecked(False)
|
||||
return
|
||||
if enabled:
|
||||
self.pending_placement = None
|
||||
self.viewer.set_placement_enabled(False)
|
||||
self.viewer.set_drawing_enabled(enabled)
|
||||
if enabled:
|
||||
self.statusBar().showMessage("自由画笔已启用:在页面上按住鼠标绘制", 5000)
|
||||
@@ -1046,6 +1066,64 @@ class MainWindow(QMainWindow):
|
||||
except Exception as exc:
|
||||
QMessageBox.critical(self, "添加失败", f"无法添加文本:\n{exc}")
|
||||
|
||||
def prepare_place_text(self) -> None:
|
||||
if not self._ensure_document():
|
||||
return
|
||||
text, ok = QInputDialog.getMultiLineText(self, "点击放置文本", "文本内容:")
|
||||
if not ok or not text.strip():
|
||||
return
|
||||
self._start_placement({"type": "text", "text": text}, "点击页面位置放置文本")
|
||||
|
||||
def prepare_place_image(self) -> None:
|
||||
if not self._ensure_document():
|
||||
return
|
||||
selected, _ = QFileDialog.getOpenFileName(
|
||||
self, "点击放置图片", "", "图片文件 (*.png *.jpg *.jpeg *.bmp *.tif *.tiff)"
|
||||
)
|
||||
if not selected:
|
||||
return
|
||||
self._start_placement({"type": "image", "path": selected}, "点击页面位置放置图片")
|
||||
|
||||
def prepare_place_signature(self) -> None:
|
||||
if not self._ensure_document():
|
||||
return
|
||||
selected, _ = QFileDialog.getOpenFileName(
|
||||
self,
|
||||
"点击放置签名",
|
||||
"",
|
||||
"图片文件 (*.png *.jpg *.jpeg *.bmp *.tif *.tiff)",
|
||||
)
|
||||
if not selected:
|
||||
return
|
||||
self._start_placement({"type": "signature", "path": selected}, "点击页面位置放置签名")
|
||||
|
||||
def _start_placement(self, payload: dict[str, object], message: str) -> None:
|
||||
self.pending_placement = payload
|
||||
self.freehand_action.setChecked(False)
|
||||
self.viewer.set_placement_enabled(True)
|
||||
self.statusBar().showMessage(message, 5000)
|
||||
|
||||
def place_pending_object(self, page_index: int, point: tuple[float, float]) -> None:
|
||||
if not self.pending_placement:
|
||||
return
|
||||
payload = self.pending_placement
|
||||
self.pending_placement = None
|
||||
self.viewer.set_placement_enabled(False)
|
||||
try:
|
||||
kind = payload.get("type")
|
||||
if kind == "text":
|
||||
self.document.add_text(page_index, str(payload["text"]), point)
|
||||
elif kind == "image":
|
||||
self.document.insert_image(page_index, str(payload["path"]), point)
|
||||
elif kind == "signature":
|
||||
self.document.add_signature(page_index, str(payload["path"]), point)
|
||||
else:
|
||||
raise ValueError("未知的放置类型")
|
||||
self.viewer.render()
|
||||
self.statusBar().showMessage("已放置,记得保存", 3000)
|
||||
except Exception as exc:
|
||||
QMessageBox.critical(self, "放置失败", f"无法放置内容:\n{exc}")
|
||||
|
||||
def insert_image(self) -> None:
|
||||
if not self._ensure_document():
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user