Improve status controls and keyboard navigation
This commit is contained in:
@@ -12,6 +12,7 @@ from PySide6.QtGui import (
|
||||
QDropEvent,
|
||||
QFont,
|
||||
QIcon,
|
||||
QIntValidator,
|
||||
QKeySequence,
|
||||
QPainter,
|
||||
QPen,
|
||||
@@ -25,6 +26,7 @@ from PySide6.QtWidgets import (
|
||||
QDialogButtonBox,
|
||||
QFormLayout,
|
||||
QFileDialog,
|
||||
QHBoxLayout,
|
||||
QInputDialog,
|
||||
QLabel,
|
||||
QLineEdit,
|
||||
@@ -35,7 +37,6 @@ from PySide6.QtWidgets import (
|
||||
QMessageBox,
|
||||
QSplitter,
|
||||
QSlider,
|
||||
QSpinBox,
|
||||
QStatusBar,
|
||||
QStyle,
|
||||
QTabWidget,
|
||||
@@ -287,6 +288,20 @@ class MainWindow(QMainWindow):
|
||||
return
|
||||
self.viewer.set_zoom(value / 100)
|
||||
|
||||
def apply_zoom_percent_input(self) -> None:
|
||||
if not self.document.is_open:
|
||||
self._sync_zoom_controls(self.viewer.zoom)
|
||||
return
|
||||
text = self.zoom_percent_input.text().strip().removesuffix("%")
|
||||
try:
|
||||
percent = int(text)
|
||||
except ValueError:
|
||||
self._sync_zoom_controls(self.viewer.zoom)
|
||||
return
|
||||
if 10 <= percent <= 1000:
|
||||
self.viewer.set_zoom(percent / 100)
|
||||
self._sync_zoom_controls(self.viewer.zoom)
|
||||
|
||||
def _sync_zoom_controls(
|
||||
self,
|
||||
zoom: float,
|
||||
@@ -299,29 +314,33 @@ class MainWindow(QMainWindow):
|
||||
percent = max(10, min(1000, round(zoom * 100)))
|
||||
with QSignalBlocker(self.zoom_slider), QSignalBlocker(self.zoom_percent_input):
|
||||
self.zoom_slider.setValue(percent)
|
||||
self.zoom_percent_input.setValue(percent)
|
||||
self.zoom_percent_input.setText(str(percent))
|
||||
|
||||
def _sync_page_controls(self) -> None:
|
||||
if not hasattr(self, "page_number_input"):
|
||||
return
|
||||
if not self.document.is_open:
|
||||
with QSignalBlocker(self.page_number_input):
|
||||
self.page_number_input.setRange(1, 1)
|
||||
self.page_number_input.setValue(1)
|
||||
self.page_number_input.setValidator(QIntValidator(1, 1, self.page_number_input))
|
||||
self.page_number_input.setText("1")
|
||||
self.page_number_input.setEnabled(False)
|
||||
self.page_total_label.setText("/ 0 页")
|
||||
return
|
||||
with QSignalBlocker(self.page_number_input):
|
||||
self.page_number_input.setEnabled(True)
|
||||
self.page_number_input.setRange(1, self.document.page_count)
|
||||
self.page_number_input.setValue(self.viewer.page_index + 1)
|
||||
self.page_number_input.setValidator(QIntValidator(1, self.document.page_count, self.page_number_input))
|
||||
self.page_number_input.setText(str(self.viewer.page_index + 1))
|
||||
self.page_total_label.setText(f"/ {self.document.page_count} 页")
|
||||
|
||||
def apply_page_number_input(self) -> None:
|
||||
if not self.document.is_open:
|
||||
self._sync_page_controls()
|
||||
return
|
||||
page = self.page_number_input.value()
|
||||
try:
|
||||
page = int(self.page_number_input.text().strip())
|
||||
except ValueError:
|
||||
self._sync_page_controls()
|
||||
return
|
||||
if 1 <= page <= self.document.page_count:
|
||||
self.viewer.go_to_page(page - 1)
|
||||
self._sync_page_controls()
|
||||
@@ -982,16 +1001,13 @@ class MainWindow(QMainWindow):
|
||||
self.zoom_slider.setPageStep(25)
|
||||
self.zoom_slider.setFixedWidth(150)
|
||||
self.zoom_slider.setToolTip("缩放")
|
||||
self.zoom_percent_input = QSpinBox()
|
||||
self.zoom_percent_input.setRange(10, 1000)
|
||||
self.zoom_percent_input.setSingleStep(5)
|
||||
self.zoom_percent_input.setSuffix("%")
|
||||
self.zoom_percent_input.setFixedWidth(78)
|
||||
self.zoom_percent_input = QLineEdit()
|
||||
self.zoom_percent_input.setValidator(QIntValidator(10, 1000, self))
|
||||
self.zoom_percent_input.setFixedWidth(64)
|
||||
self.zoom_percent_input.setToolTip("缩放百分比")
|
||||
self.page_number_input = QSpinBox()
|
||||
self.page_number_input.setRange(1, 1)
|
||||
self.page_number_input.setKeyboardTracking(False)
|
||||
self.page_number_input.setFixedWidth(64)
|
||||
self.page_number_input = QLineEdit()
|
||||
self.page_number_input.setValidator(QIntValidator(1, 1, self))
|
||||
self.page_number_input.setFixedWidth(48)
|
||||
self.page_number_input.setToolTip("当前页码")
|
||||
self.page_number_input.setEnabled(False)
|
||||
self.page_total_label = QLabel("/ 0 页")
|
||||
@@ -1358,6 +1374,11 @@ class MainWindow(QMainWindow):
|
||||
self.zoom_percent_input.setObjectName("statusZoomPercent")
|
||||
self.zoom_percent_input.setFixedWidth(72)
|
||||
|
||||
controls = QWidget()
|
||||
controls.setObjectName("statusPageControls")
|
||||
controls_layout = QHBoxLayout(controls)
|
||||
controls_layout.setContentsMargins(0, 0, 0, 0)
|
||||
controls_layout.setSpacing(6)
|
||||
for widget in [
|
||||
self._status_action_button(self.zoom_out_action, 66),
|
||||
self._status_action_button(self.zoom_in_action, 66),
|
||||
@@ -1370,7 +1391,9 @@ class MainWindow(QMainWindow):
|
||||
self.page_number_input,
|
||||
self.page_total_label,
|
||||
]:
|
||||
status.addPermanentWidget(widget)
|
||||
controls_layout.addWidget(widget)
|
||||
widget.show()
|
||||
status.addPermanentWidget(controls, 1)
|
||||
|
||||
def _connect_signals(self) -> None:
|
||||
self.open_action.triggered.connect(lambda: self.open_file())
|
||||
@@ -1390,7 +1413,7 @@ class MainWindow(QMainWindow):
|
||||
self.zoom_out_action.triggered.connect(self.zoom_out)
|
||||
self.zoom_in_action.triggered.connect(self.zoom_in)
|
||||
self.zoom_slider.valueChanged.connect(self.set_zoom_percent)
|
||||
self.zoom_percent_input.valueChanged.connect(self.set_zoom_percent)
|
||||
self.zoom_percent_input.editingFinished.connect(self.apply_zoom_percent_input)
|
||||
self.page_number_input.editingFinished.connect(self.apply_page_number_input)
|
||||
self.fit_width_action.triggered.connect(self.fit_width)
|
||||
self.fit_page_action.triggered.connect(self.fit_page)
|
||||
@@ -1595,6 +1618,22 @@ class MainWindow(QMainWindow):
|
||||
if event.key() == Qt.Key.Key_Delete and (self.selected_annotation or self.selected_raster_id is not None):
|
||||
self.delete_selected_object()
|
||||
return
|
||||
if self.document.is_open and not isinstance(self.focusWidget(), QLineEdit):
|
||||
if event.key() == Qt.Key.Key_Left:
|
||||
self.previous_page()
|
||||
event.accept()
|
||||
return
|
||||
if event.key() == Qt.Key.Key_Right:
|
||||
self.next_page()
|
||||
event.accept()
|
||||
return
|
||||
if event.key() in {Qt.Key.Key_Up, Qt.Key.Key_Down}:
|
||||
bar = self.viewer.verticalScrollBar()
|
||||
direction = -1 if event.key() == Qt.Key.Key_Up else 1
|
||||
step = max(48, bar.pageStep() // 5)
|
||||
bar.setValue(bar.value() + direction * step)
|
||||
event.accept()
|
||||
return
|
||||
super().keyPressEvent(event)
|
||||
|
||||
def toggle_freehand_drawing(self, enabled: bool) -> None:
|
||||
@@ -2152,6 +2191,7 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def _update_status(self) -> None:
|
||||
self._update_current_tab_title()
|
||||
self._sync_zoom_controls(self.viewer.zoom)
|
||||
self._sync_page_controls()
|
||||
if not self.document.is_open:
|
||||
self.statusBar().showMessage("就绪")
|
||||
|
||||
@@ -40,7 +40,7 @@ class ThumbnailList(QListWidget):
|
||||
def _update_thumbnail_metrics(self) -> None:
|
||||
viewport_width = max(120, self.viewport().width())
|
||||
thumbnail_width = max(92, min(self.MAX_THUMBNAIL_WIDTH, viewport_width - 34))
|
||||
thumbnail_height = int(thumbnail_width * 1.34)
|
||||
thumbnail_height = int(thumbnail_width * 0.76)
|
||||
gap = max(1, round(self.fontMetrics().height() * 0.25))
|
||||
label_height = self.fontMetrics().height() + 8
|
||||
grid_size = QSize(thumbnail_width + 26, thumbnail_height + label_height + gap)
|
||||
|
||||
Reference in New Issue
Block a user