Add annotation content editing from side panel
This commit is contained in:
@@ -565,6 +565,21 @@ class PdfDocument:
|
||||
rect = annot.rect + (dx, dy, dx, dy)
|
||||
return self.update_annotation_rect(page_index, xref, self._rect_tuple(rect))
|
||||
|
||||
def update_annotation_content(self, page_index: int, xref: int, content: str) -> bool:
|
||||
if not self._annotation(page_index, xref):
|
||||
return False
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
annot = page.first_annot
|
||||
while annot:
|
||||
if annot.xref == xref:
|
||||
annot.set_info(content=content)
|
||||
annot.update()
|
||||
self.mark_changed()
|
||||
return True
|
||||
annot = annot.next
|
||||
return False
|
||||
|
||||
def delete_annotation(self, page_index: int, xref: int) -> bool:
|
||||
page = self._page(page_index)
|
||||
annot = page.first_annot
|
||||
|
||||
@@ -1513,6 +1513,7 @@ class MainWindow(QMainWindow):
|
||||
self.annotation_search_box.textChanged.connect(self._refresh_annotation_panel)
|
||||
self.right_annotation_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.right_annotation_list.itemClicked.connect(self._go_to_annotation_item)
|
||||
self.right_annotation_list.itemDoubleClicked.connect(self.edit_annotation_from_panel_item)
|
||||
self.right_annotation_list.customContextMenuRequested.connect(
|
||||
self.show_annotation_list_context_menu
|
||||
)
|
||||
@@ -1537,11 +1538,46 @@ class MainWindow(QMainWindow):
|
||||
return
|
||||
|
||||
menu = QMenu(self)
|
||||
edit_action = menu.addAction("编辑内容")
|
||||
delete_action = menu.addAction("删除注释")
|
||||
chosen = menu.exec(self.right_annotation_list.mapToGlobal(position))
|
||||
if chosen == delete_action:
|
||||
if chosen == edit_action:
|
||||
self.edit_annotation_from_panel(annotation)
|
||||
elif chosen == delete_action:
|
||||
self.delete_annotation_from_panel(annotation)
|
||||
|
||||
def edit_annotation_from_panel_item(self, item: QListWidgetItem) -> None:
|
||||
annotation = item.data(Qt.ItemDataRole.UserRole)
|
||||
if isinstance(annotation, AnnotationInfo):
|
||||
self.edit_annotation_from_panel(annotation)
|
||||
|
||||
def edit_annotation_from_panel(self, annotation: AnnotationInfo) -> None:
|
||||
text, ok = QInputDialog.getMultiLineText(
|
||||
self,
|
||||
"编辑注释内容",
|
||||
"注释内容:",
|
||||
annotation.content or "",
|
||||
)
|
||||
if not ok:
|
||||
return
|
||||
try:
|
||||
if not self.document.update_annotation_content(annotation.page_index, annotation.xref, text):
|
||||
QMessageBox.warning(self, "编辑失败", "没有找到对应的注释。")
|
||||
return
|
||||
if self.selected_annotation and self.selected_annotation.xref == annotation.xref:
|
||||
self.selected_annotation = AnnotationInfo(
|
||||
page_index=annotation.page_index,
|
||||
xref=annotation.xref,
|
||||
annotation_type=annotation.annotation_type,
|
||||
content=text,
|
||||
rect=annotation.rect,
|
||||
)
|
||||
self.viewer.render()
|
||||
self._refresh_annotation_panel()
|
||||
self.statusBar().showMessage("注释内容已更新,记得保存", 3000)
|
||||
except Exception as exc:
|
||||
QMessageBox.critical(self, "编辑失败", f"无法更新注释内容:\n{exc}")
|
||||
|
||||
def delete_annotation_from_panel(self, annotation: AnnotationInfo) -> None:
|
||||
reply = QMessageBox.question(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user