Add keyboard nudging for selected objects

This commit is contained in:
2026-06-30 15:04:38 +08:00
parent 04f1d901b2
commit 25274a9d95
2 changed files with 44 additions and 6 deletions
+38
View File
@@ -1867,6 +1867,28 @@ class MainWindow(QMainWindow):
QMessageBox.critical(self, "对象更新失败", f"无法更新对象:\n{exc}")
self.viewer.render()
def nudge_selected_object(self, dx: float, dy: float) -> bool:
page_index, rect = self._selected_object_rect()
if page_index is None or rect is None:
return False
x0, y0, x1, y1 = rect
self.update_selected_object_rect(page_index, (x0 + dx, y0 + dy, x1 + dx, y1 + dy))
return True
def _selected_object_rect(self) -> tuple[int | None, tuple[float, float, float, float] | None]:
if self.selected_raster_id is not None:
raster = self._session_raster_by_id(self.selected_raster_id)
if raster:
return int(raster["page_index"]), raster["rect"]
return None, None
if self.selected_annotation:
rect = self.selected_annotation.rect or self.document.annotation_rect(
self.selected_annotation.page_index,
self.selected_annotation.xref,
)
return self.selected_annotation.page_index, rect
return None, None
def delete_selected_object(self) -> None:
if self.selected_raster_id is not None:
raster_id = self.selected_raster_id
@@ -1939,6 +1961,22 @@ class MainWindow(QMainWindow):
self.delete_selected_object()
return
if self.document.is_open and not isinstance(self.focusWidget(), QLineEdit):
has_movable_selection = self.selected_raster_id is not None or (
self.selected_annotation is not None and self.select_object_action.isChecked()
)
if has_movable_selection:
step = 10 if event.modifiers() & Qt.KeyboardModifier.ShiftModifier else 1
offsets = {
Qt.Key.Key_Left: (-step, 0),
Qt.Key.Key_Right: (step, 0),
Qt.Key.Key_Up: (0, -step),
Qt.Key.Key_Down: (0, step),
}
if event.key() in offsets:
dx, dy = offsets[event.key()]
if self.nudge_selected_object(dx, dy):
event.accept()
return
if event.key() == Qt.Key.Key_Left:
self.previous_page()
event.accept()