Move page zoom controls to status bar
This commit is contained in:
@@ -4,7 +4,19 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QSize, QSignalBlocker, QSettings, QStandardPaths, Qt, QUrl
|
||||
from PySide6.QtGui import QAction, QDesktopServices, QDragEnterEvent, QDropEvent, QIcon, QKeySequence, QPainter
|
||||
from PySide6.QtGui import (
|
||||
QAction,
|
||||
QColor,
|
||||
QDesktopServices,
|
||||
QDragEnterEvent,
|
||||
QDropEvent,
|
||||
QFont,
|
||||
QIcon,
|
||||
QKeySequence,
|
||||
QPainter,
|
||||
QPen,
|
||||
QPixmap,
|
||||
)
|
||||
from PySide6.QtPrintSupport import QPrintDialog, QPrinter
|
||||
from PySide6.QtWidgets import (
|
||||
QCheckBox,
|
||||
@@ -28,6 +40,7 @@ from PySide6.QtWidgets import (
|
||||
QStyle,
|
||||
QTabWidget,
|
||||
QToolBar,
|
||||
QToolButton,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
@@ -87,6 +100,7 @@ class MainWindow(QMainWindow):
|
||||
self._build_window()
|
||||
self._build_actions()
|
||||
self._build_toolbar()
|
||||
self._build_status_controls()
|
||||
self._connect_signals()
|
||||
self._apply_theme()
|
||||
self._restore_window_state()
|
||||
@@ -1077,19 +1091,33 @@ class MainWindow(QMainWindow):
|
||||
help_menu.addAction(self.about_action)
|
||||
self._build_right_tools_panel()
|
||||
|
||||
def _text_icon(self, text: str, color: QColor | None = None) -> QIcon:
|
||||
pixmap = QPixmap(24, 24)
|
||||
pixmap.fill(Qt.GlobalColor.transparent)
|
||||
painter = QPainter(pixmap)
|
||||
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
||||
pen_color = color or QColor("#2d73c5")
|
||||
painter.setPen(QPen(pen_color, 2))
|
||||
painter.setBrush(Qt.BrushStyle.NoBrush)
|
||||
painter.drawRoundedRect(3, 3, 18, 18, 4, 4)
|
||||
font = QFont()
|
||||
font.setBold(True)
|
||||
font.setPixelSize(12 if len(text) <= 1 else 9)
|
||||
painter.setFont(font)
|
||||
painter.setPen(pen_color)
|
||||
painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, text)
|
||||
painter.end()
|
||||
return QIcon(pixmap)
|
||||
|
||||
def _apply_action_icons(self) -> None:
|
||||
icon_map = {
|
||||
self.open_action: QStyle.StandardPixmap.SP_DialogOpenButton,
|
||||
self.save_action: QStyle.StandardPixmap.SP_DialogSaveButton,
|
||||
self.save_as_action: QStyle.StandardPixmap.SP_DialogSaveButton,
|
||||
self.print_action: QStyle.StandardPixmap.SP_FileDialogDetailedView,
|
||||
self.undo_action: QStyle.StandardPixmap.SP_ArrowBack,
|
||||
self.redo_action: QStyle.StandardPixmap.SP_ArrowForward,
|
||||
self.previous_action: QStyle.StandardPixmap.SP_ArrowUp,
|
||||
self.next_action: QStyle.StandardPixmap.SP_ArrowDown,
|
||||
self.zoom_out_action: QStyle.StandardPixmap.SP_TitleBarMinButton,
|
||||
self.zoom_in_action: QStyle.StandardPixmap.SP_TitleBarMaxButton,
|
||||
self.fit_width_action: QStyle.StandardPixmap.SP_ArrowLeft,
|
||||
self.fit_page_action: QStyle.StandardPixmap.SP_FileDialogContentsView,
|
||||
self.go_to_page_action: QStyle.StandardPixmap.SP_ArrowRight,
|
||||
self.rotate_action: QStyle.StandardPixmap.SP_BrowserReload,
|
||||
@@ -1130,6 +1158,10 @@ class MainWindow(QMainWindow):
|
||||
}
|
||||
for action, standard_icon in icon_map.items():
|
||||
action.setIcon(self.style().standardIcon(standard_icon))
|
||||
self.save_as_action.setIcon(self._text_icon("另", QColor("#365f91")))
|
||||
self.zoom_out_action.setIcon(self._text_icon("-", QColor("#2f5f9f")))
|
||||
self.zoom_in_action.setIcon(self._text_icon("+", QColor("#2f5f9f")))
|
||||
self.fit_width_action.setIcon(self._text_icon("<>", QColor("#2f5f9f")))
|
||||
|
||||
def _build_right_tools_panel(self) -> None:
|
||||
self.right_tools.add_section(
|
||||
@@ -1206,6 +1238,8 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def _build_toolbar(self) -> None:
|
||||
toolbar = QToolBar("主工具栏")
|
||||
self.main_toolbar = toolbar
|
||||
self._toolbar_page_size_actions = []
|
||||
toolbar.setObjectName("mainToolBar")
|
||||
toolbar.setMovable(False)
|
||||
toolbar.setFloatable(False)
|
||||
@@ -1234,9 +1268,9 @@ class MainWindow(QMainWindow):
|
||||
toolbar.addAction(action)
|
||||
zoom_label = QLabel("缩放")
|
||||
zoom_label.setObjectName("toolbarLabel")
|
||||
toolbar.addWidget(zoom_label)
|
||||
toolbar.addWidget(self.zoom_slider)
|
||||
toolbar.addWidget(self.zoom_percent_input)
|
||||
self._toolbar_page_size_actions.append(toolbar.addWidget(zoom_label))
|
||||
self._toolbar_page_size_actions.append(toolbar.addWidget(self.zoom_slider))
|
||||
self._toolbar_page_size_actions.append(toolbar.addWidget(self.zoom_percent_input))
|
||||
toolbar.addSeparator()
|
||||
for action in [
|
||||
self.fit_width_action,
|
||||
@@ -1259,6 +1293,49 @@ class MainWindow(QMainWindow):
|
||||
toolbar.addSeparator()
|
||||
toolbar.addAction(self.toggle_tools_action)
|
||||
|
||||
def _status_action_button(self, action: QAction) -> QToolButton:
|
||||
button = QToolButton()
|
||||
button.setObjectName("statusToolButton")
|
||||
button.setDefaultAction(action)
|
||||
button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
|
||||
button.setIconSize(QSize(16, 16))
|
||||
button.setAutoRaise(True)
|
||||
return button
|
||||
|
||||
def _build_status_controls(self) -> None:
|
||||
status = self.statusBar()
|
||||
toolbar = getattr(self, "main_toolbar", None)
|
||||
if toolbar:
|
||||
for action in [
|
||||
self.zoom_out_action,
|
||||
self.zoom_in_action,
|
||||
self.fit_width_action,
|
||||
self.fit_page_action,
|
||||
self.go_to_page_action,
|
||||
]:
|
||||
toolbar.removeAction(action)
|
||||
for action in getattr(self, "_toolbar_page_size_actions", []):
|
||||
toolbar.removeAction(action)
|
||||
|
||||
zoom_label = QLabel("页面")
|
||||
zoom_label.setObjectName("statusZoomLabel")
|
||||
self.zoom_slider.setObjectName("statusZoomSlider")
|
||||
self.zoom_slider.setFixedWidth(132)
|
||||
self.zoom_percent_input.setObjectName("statusZoomPercent")
|
||||
self.zoom_percent_input.setFixedWidth(72)
|
||||
|
||||
for widget in [
|
||||
zoom_label,
|
||||
self._status_action_button(self.zoom_out_action),
|
||||
self._status_action_button(self.zoom_in_action),
|
||||
self.zoom_slider,
|
||||
self.zoom_percent_input,
|
||||
self._status_action_button(self.fit_width_action),
|
||||
self._status_action_button(self.fit_page_action),
|
||||
self._status_action_button(self.go_to_page_action),
|
||||
]:
|
||||
status.addPermanentWidget(widget)
|
||||
|
||||
def _connect_signals(self) -> None:
|
||||
self.open_action.triggered.connect(lambda: self.open_file())
|
||||
self.save_action.triggered.connect(self.save_file)
|
||||
@@ -1832,6 +1909,12 @@ class MainWindow(QMainWindow):
|
||||
QToolButton:hover, QPushButton#rightToolButton:hover { background: #3c4045; border-color: #555a60; }
|
||||
QToolButton:checked, QPushButton#rightToolButton:checked { background: #244a78; border-color: #4f89c8; }
|
||||
QLabel#toolbarLabel { color: #cfd3d8; padding-left: 4px; }
|
||||
QLabel#statusZoomLabel { color: #cfd3d8; padding: 0 4px 0 10px; }
|
||||
QToolButton#statusToolButton {
|
||||
color: #f1f1f1; background: transparent; border: 1px solid transparent;
|
||||
border-radius: 4px; padding: 3px;
|
||||
}
|
||||
QToolButton#statusToolButton:hover { background: #3c4045; border-color: #555a60; }
|
||||
QLineEdit, QSpinBox {
|
||||
padding: 6px 8px; background: #202225; color: #f1f1f1;
|
||||
border: 1px solid #555a60; border-radius: 4px;
|
||||
@@ -1899,6 +1982,12 @@ class MainWindow(QMainWindow):
|
||||
QToolButton:pressed, QPushButton#rightToolButton:pressed { background: #dfe6f1; }
|
||||
QToolButton:checked, QPushButton#rightToolButton:checked { background: #dbeafe; border-color: #6aa3dc; color: #0f3f7a; }
|
||||
QLabel#toolbarLabel { color: #5b6067; padding-left: 4px; }
|
||||
QLabel#statusZoomLabel { color: #5b6067; padding: 0 4px 0 10px; }
|
||||
QToolButton#statusToolButton {
|
||||
color: #25272a; background: transparent; border: 1px solid transparent;
|
||||
border-radius: 4px; padding: 3px;
|
||||
}
|
||||
QToolButton#statusToolButton:hover { background: #eceff3; border-color: #c9cdd3; }
|
||||
QLineEdit, QSpinBox {
|
||||
padding: 6px 8px; background: #ffffff; color: #202124;
|
||||
border: 1px solid #b9bec6; border-radius: 4px;
|
||||
|
||||
@@ -23,15 +23,33 @@ class ThumbnailList(QListWidget):
|
||||
self._thumbnail_timer.timeout.connect(self._render_next_thumbnail)
|
||||
self.verticalScrollBar().valueChanged.connect(lambda _: self.schedule_visible_thumbnails())
|
||||
|
||||
self.setIconSize(QSize(118, 158))
|
||||
self._last_thumbnail_width = 0
|
||||
self._update_thumbnail_metrics()
|
||||
self.setViewMode(QListWidget.ViewMode.IconMode)
|
||||
self.setFlow(QListWidget.Flow.TopToBottom)
|
||||
self.setWrapping(False)
|
||||
self.setResizeMode(QListWidget.ResizeMode.Adjust)
|
||||
self.setGridSize(QSize(164, 204))
|
||||
self.setSpacing(6)
|
||||
self.itemClicked.connect(self._emit_page)
|
||||
|
||||
def resizeEvent(self, event) -> None:
|
||||
super().resizeEvent(event)
|
||||
self._update_thumbnail_metrics()
|
||||
self.schedule_visible_thumbnails()
|
||||
|
||||
def _update_thumbnail_metrics(self) -> None:
|
||||
viewport_width = max(120, self.viewport().width())
|
||||
thumbnail_width = max(92, min(170, viewport_width - 34))
|
||||
thumbnail_height = int(thumbnail_width * 1.34)
|
||||
gap = max(2, round(self.fontMetrics().height() * 0.5))
|
||||
label_height = self.fontMetrics().height() + 8
|
||||
grid_size = QSize(thumbnail_width + 26, thumbnail_height + label_height + gap)
|
||||
if thumbnail_width != self._last_thumbnail_width:
|
||||
self._rendered_keys.clear()
|
||||
self._last_thumbnail_width = thumbnail_width
|
||||
self.setIconSize(QSize(thumbnail_width, thumbnail_height))
|
||||
self.setGridSize(grid_size)
|
||||
self.setSpacing(gap)
|
||||
|
||||
def load_document(self, document: PdfDocument) -> None:
|
||||
self._thumbnail_timer.stop()
|
||||
same_document = self.document is document
|
||||
@@ -91,7 +109,7 @@ class ThumbnailList(QListWidget):
|
||||
return
|
||||
|
||||
item = self.item(page_index)
|
||||
image = self.document.render_thumbnail(page_index)
|
||||
image = self.document.render_thumbnail(page_index, self.iconSize().width())
|
||||
item.setIcon(QIcon(QPixmap.fromImage(image)))
|
||||
self._rendered_keys.add(render_key)
|
||||
|
||||
@@ -124,8 +142,8 @@ class Sidebar(QTabWidget):
|
||||
layout.addWidget(self.thumbnails)
|
||||
|
||||
self.addTab(thumb_page, "缩略图")
|
||||
self.setMinimumWidth(176)
|
||||
self.setMaximumWidth(220)
|
||||
self.setMinimumWidth(150)
|
||||
self.setMaximumWidth(340)
|
||||
|
||||
def load_document(self, document: PdfDocument) -> None:
|
||||
self.thumbnails.load_document(document)
|
||||
|
||||
Reference in New Issue
Block a user