92 lines
3.7 KiB
Python
92 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from PySide6.QtWidgets import (
|
|
QCheckBox,
|
|
QComboBox,
|
|
QDialog,
|
|
QDialogButtonBox,
|
|
QDoubleSpinBox,
|
|
QFormLayout,
|
|
QVBoxLayout,
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AppPreferences:
|
|
theme: str
|
|
theme_color: str
|
|
theme_background: str
|
|
font_scale: float
|
|
auto_refresh: bool
|
|
non_preview_action: str
|
|
|
|
|
|
class SettingsDialog(QDialog):
|
|
def __init__(self, preferences: AppPreferences, parent: object | None = None) -> None:
|
|
super().__init__(parent)
|
|
self.setWindowTitle("设置")
|
|
self.setMinimumWidth(420)
|
|
|
|
self.theme_color_combo = QComboBox()
|
|
self.theme_color_combo.addItem("白色", "white")
|
|
self.theme_color_combo.addItem("黑色", "black")
|
|
self.theme_color_combo.addItem("深灰色", "dark_gray")
|
|
self.theme_color_combo.addItem("彩色", "colorful")
|
|
self.theme_color_combo.setCurrentIndex(max(0, self.theme_color_combo.findData(preferences.theme_color)))
|
|
|
|
self.theme_background_combo = QComboBox()
|
|
self.theme_background_combo.addItem("无", "none")
|
|
self.theme_background_combo.addItem("春天", "spring")
|
|
self.theme_background_combo.addItem("稻草", "straw")
|
|
self.theme_background_combo.addItem("电路", "circuit")
|
|
self.theme_background_combo.addItem("年轮", "tree_rings")
|
|
self.theme_background_combo.addItem("几何图形", "geometry")
|
|
self.theme_background_combo.addItem("书法", "calligraphy")
|
|
self.theme_background_combo.addItem("水下", "underwater")
|
|
self.theme_background_combo.setCurrentIndex(
|
|
max(0, self.theme_background_combo.findData(preferences.theme_background))
|
|
)
|
|
|
|
self.font_scale_spin = QDoubleSpinBox()
|
|
self.font_scale_spin.setRange(0.8, 1.6)
|
|
self.font_scale_spin.setSingleStep(0.1)
|
|
self.font_scale_spin.setDecimals(1)
|
|
self.font_scale_spin.setValue(preferences.font_scale)
|
|
|
|
self.auto_refresh_checkbox = QCheckBox("文件变化后自动刷新当前阅读页")
|
|
self.auto_refresh_checkbox.setChecked(preferences.auto_refresh)
|
|
|
|
self.non_preview_combo = QComboBox()
|
|
self.non_preview_combo.addItem("每次询问", "ask")
|
|
self.non_preview_combo.addItem("直接用本地默认软件打开", "open")
|
|
self.non_preview_combo.addItem("阻止打开,仅提示路径", "block")
|
|
self.non_preview_combo.setCurrentIndex(max(0, self.non_preview_combo.findData(preferences.non_preview_action)))
|
|
|
|
form = QFormLayout()
|
|
form.addRow("主题颜色", self.theme_color_combo)
|
|
form.addRow("主题背景", self.theme_background_combo)
|
|
form.addRow("正文字号倍率", self.font_scale_spin)
|
|
form.addRow("自动刷新", self.auto_refresh_checkbox)
|
|
form.addRow("其他本地文件", self.non_preview_combo)
|
|
|
|
buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
|
|
buttons.accepted.connect(self.accept)
|
|
buttons.rejected.connect(self.reject)
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.addLayout(form)
|
|
layout.addWidget(buttons)
|
|
|
|
def preferences(self) -> AppPreferences:
|
|
theme_color = str(self.theme_color_combo.currentData())
|
|
return AppPreferences(
|
|
theme="dark" if theme_color in {"black", "dark_gray"} else "light",
|
|
theme_color=theme_color,
|
|
theme_background=str(self.theme_background_combo.currentData()),
|
|
font_scale=float(self.font_scale_spin.value()),
|
|
auto_refresh=self.auto_refresh_checkbox.isChecked(),
|
|
non_preview_action=str(self.non_preview_combo.currentData()),
|
|
)
|