103 lines
4.2 KiB
Python
103 lines
4.2 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("彩色", "colorful")
|
|
self.theme_color_combo.addItem("白色", "white")
|
|
self.theme_color_combo.addItem("黑色", "black")
|
|
self.theme_color_combo.addItem("深灰色", "dark_gray")
|
|
self.theme_color_combo.setCurrentIndex(max(0, self.theme_color_combo.findData(preferences.theme_color)))
|
|
self.theme_color_combo.currentIndexChanged.connect(self._update_background_availability)
|
|
|
|
self.theme_background_combo = QComboBox()
|
|
self.theme_background_combo.addItem("无", "none")
|
|
self.theme_background_combo.addItem("中国结", "chinese_knot")
|
|
self.theme_background_combo.addItem("祥云", "cloud")
|
|
self.theme_background_combo.addItem("如意", "ruyi")
|
|
self.theme_background_combo.addItem("毛笔", "brush")
|
|
self.theme_background_combo.addItem("牡丹", "peony")
|
|
self.theme_background_combo.addItem("竹", "bamboo")
|
|
self.theme_background_combo.addItem("回纹", "meander")
|
|
self.theme_background_combo.setCurrentIndex(
|
|
max(0, self.theme_background_combo.findData(preferences.theme_background))
|
|
)
|
|
self._update_background_availability()
|
|
|
|
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())
|
|
theme_background = str(self.theme_background_combo.currentData())
|
|
if theme_color != "colorful":
|
|
theme_background = "none"
|
|
return AppPreferences(
|
|
theme="dark" if theme_color in {"black", "dark_gray"} else "light",
|
|
theme_color=theme_color,
|
|
theme_background=theme_background,
|
|
font_scale=float(self.font_scale_spin.value()),
|
|
auto_refresh=self.auto_refresh_checkbox.isChecked(),
|
|
non_preview_action=str(self.non_preview_combo.currentData()),
|
|
)
|
|
|
|
def _update_background_availability(self) -> None:
|
|
enabled = str(self.theme_color_combo.currentData()) == "colorful"
|
|
self.theme_background_combo.setEnabled(enabled)
|
|
if not enabled:
|
|
self.theme_background_combo.setCurrentIndex(max(0, self.theme_background_combo.findData("none")))
|