706 lines
27 KiB
Python
706 lines
27 KiB
Python
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import os
|
|
from copy import deepcopy
|
|
from typing import Any
|
|
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtGui import QFont
|
|
from PySide6.QtWidgets import (
|
|
QCheckBox,
|
|
QComboBox,
|
|
QDialog,
|
|
QDialogButtonBox,
|
|
QDoubleSpinBox,
|
|
QFrame,
|
|
QFontComboBox,
|
|
QFormLayout,
|
|
QGroupBox,
|
|
QHBoxLayout,
|
|
QLabel,
|
|
QLineEdit,
|
|
QListWidget,
|
|
QListWidgetItem,
|
|
QPushButton,
|
|
QSpinBox,
|
|
QTabWidget,
|
|
QTextEdit,
|
|
QVBoxLayout,
|
|
QWidget,
|
|
)
|
|
|
|
|
|
STYLE_TARGETS = (
|
|
("paragraph", "正文段落", ".reader p"),
|
|
("h1", "一级标题", ".reader h1"),
|
|
("h2", "二级标题", ".reader h2"),
|
|
("h3", "三级标题", ".reader h3"),
|
|
("h4", "四级标题", ".reader h4"),
|
|
("h5", "五级标题", ".reader h5"),
|
|
("h6", "六级标题", ".reader h6"),
|
|
("list", "列表项", ".reader li"),
|
|
("blockquote", "引用段落", ".reader blockquote, .reader blockquote p, .reader .admonition, .reader .admonition p"),
|
|
("code", "代码块", ".reader pre, .reader code"),
|
|
("table", "表格", ".reader table"),
|
|
)
|
|
DEFAULT_CHINESE_FONT = "宋体"
|
|
DEFAULT_ENGLISH_FONT = "Times New Roman"
|
|
|
|
|
|
def build_document_style_css(rules: dict[str, dict[str, Any]] | None, extra_css: str = "") -> str:
|
|
parts: list[str] = []
|
|
selector_by_target = {target_id: selector for target_id, _label, selector in STYLE_TARGETS}
|
|
for target_id, selector in selector_by_target.items():
|
|
rule = rules.get(target_id, {}) if isinstance(rules, dict) else {}
|
|
declarations = _css_declarations(rule)
|
|
if declarations:
|
|
parts.append(f"{selector} {{\n" + "\n".join(f" {declaration}" for declaration in declarations) + "\n}")
|
|
extra = extra_css.strip()
|
|
if extra:
|
|
parts.append(extra)
|
|
return "\n\n".join(parts).strip()
|
|
|
|
|
|
def _css_declarations(rule: dict[str, Any]) -> list[str]:
|
|
declarations: list[str] = []
|
|
font_declaration = _font_family_declaration(rule)
|
|
if font_declaration:
|
|
declarations.append(font_declaration)
|
|
|
|
font_size = _positive_float(rule.get("font_size"))
|
|
if font_size > 0:
|
|
declarations.append(f"font-size: {_format_number(font_size)}px;")
|
|
|
|
if bool(rule.get("bold")):
|
|
declarations.append("font-weight: 700;")
|
|
if bool(rule.get("italic")):
|
|
declarations.append("font-style: italic;")
|
|
|
|
line_height = _positive_float(rule.get("line_height_value", rule.get("line_height")))
|
|
if line_height > 0:
|
|
declarations.append(f"line-height: {_css_length_or_multiple(line_height, str(rule.get('line_height_unit') or 'multiple'))};")
|
|
|
|
first_line_indent = _positive_float(rule.get("first_line_indent_chars", rule.get("first_line_indent")))
|
|
hanging_indent = _positive_float(rule.get("hanging_indent_chars", rule.get("hanging_indent")))
|
|
if hanging_indent > 0:
|
|
declarations.append(f"padding-left: {_format_number(hanging_indent)}em;")
|
|
declarations.append(f"text-indent: -{_format_number(hanging_indent)}em;")
|
|
elif first_line_indent > 0:
|
|
declarations.append(f"text-indent: {_format_number(first_line_indent)}em;")
|
|
|
|
space_before = _positive_float(rule.get("space_before_value", rule.get("space_before")))
|
|
if space_before > 0:
|
|
unit = str(rule.get("space_before_unit") or "multiple")
|
|
declarations.append(f"margin-top: {_css_length_or_multiple(space_before, unit, multiple_unit='em')};")
|
|
space_after = _positive_float(rule.get("space_after_value", rule.get("space_after")))
|
|
if space_after > 0:
|
|
unit = str(rule.get("space_after_unit") or "multiple")
|
|
declarations.append(f"margin-bottom: {_css_length_or_multiple(space_after, unit, multiple_unit='em')};")
|
|
return declarations
|
|
|
|
|
|
def _font_family_declaration(rule: dict[str, Any]) -> str:
|
|
english_font = str(rule.get("english_font_family") or "").strip()
|
|
chinese_font = str(rule.get("chinese_font_family") or rule.get("font_family") or "").strip()
|
|
fonts = [font for font in (english_font, chinese_font, "Microsoft YaHei UI", "Arial", "sans-serif") if font]
|
|
unique_fonts: list[str] = []
|
|
for font in fonts:
|
|
if font not in unique_fonts:
|
|
unique_fonts.append(font)
|
|
if not english_font and not chinese_font:
|
|
return ""
|
|
css_fonts = [
|
|
font if font in {"serif", "sans-serif", "monospace"} else f'"{font.replace("\\", "\\\\").replace("\"", "\\\"")}"'
|
|
for font in unique_fonts
|
|
]
|
|
return f"font-family: {', '.join(css_fonts)};"
|
|
|
|
|
|
def _css_length_or_multiple(value: float, unit: str, multiple_unit: str = "") -> str:
|
|
formatted = _format_number(value)
|
|
if unit == "pt":
|
|
return f"{formatted}pt"
|
|
if multiple_unit:
|
|
return f"{formatted}{multiple_unit}"
|
|
return formatted
|
|
|
|
|
|
def _positive_float(value: object) -> float:
|
|
try:
|
|
return max(0.0, float(value))
|
|
except (TypeError, ValueError):
|
|
return 0.0
|
|
|
|
|
|
def _format_number(value: float) -> str:
|
|
return f"{value:.2f}".rstrip("0").rstrip(".")
|
|
|
|
|
|
class DocumentStyleEditorDialog(QDialog):
|
|
def __init__(
|
|
self,
|
|
style: dict[str, Any] | None = None,
|
|
default_name: str = "自定义样式",
|
|
parent: QWidget | None = None,
|
|
) -> None:
|
|
super().__init__(parent)
|
|
self.setWindowTitle("文档样式")
|
|
self.setMinimumSize(565, 620)
|
|
self.resize(565, 620)
|
|
source = style or {}
|
|
self.rules: dict[str, dict[str, Any]] = deepcopy(source.get("rules", {})) if isinstance(source.get("rules"), dict) else {}
|
|
self._current_target = STYLE_TARGETS[0][0]
|
|
self._loading = False
|
|
|
|
self.name_edit = QLineEdit(str(source.get("name") or default_name))
|
|
self.name_edit.setObjectName("styleNameEdit")
|
|
self.name_edit.setPlaceholderText("样式名称")
|
|
self.name_edit.setMaximumWidth(420)
|
|
|
|
name_row = QHBoxLayout()
|
|
name_row.setContentsMargins(0, 0, 0, 0)
|
|
name_row.setSpacing(8)
|
|
name_row.addWidget(QLabel("样式名称"))
|
|
name_row.addWidget(self.name_edit)
|
|
name_row.addStretch(1)
|
|
|
|
self.tabs = QTabWidget()
|
|
self.tabs.setMaximumWidth(535)
|
|
self.tabs.addTab(self._build_visual_tab(), "图形设置")
|
|
self.tabs.addTab(self._build_advanced_tab(str(source.get("extra_css") or source.get("css") or "")), "高级 CSS")
|
|
|
|
buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
|
|
buttons.accepted.connect(self.accept)
|
|
buttons.rejected.connect(self.reject)
|
|
button_row = QWidget()
|
|
button_row.setMaximumWidth(535)
|
|
button_layout = QHBoxLayout(button_row)
|
|
button_layout.setContentsMargins(0, 0, 0, 0)
|
|
button_layout.addStretch(1)
|
|
button_layout.addWidget(buttons)
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(16, 14, 16, 16)
|
|
layout.setSpacing(12)
|
|
layout.addLayout(name_row)
|
|
layout.addWidget(self.tabs)
|
|
layout.addWidget(button_row)
|
|
self._load_rule_to_widgets(self.rules.get(self._current_target, {}))
|
|
|
|
def apply_theme(self, theme_color: str) -> None:
|
|
palettes = {
|
|
"white": {
|
|
"chrome": "#f7f8fb",
|
|
"surface": "#ffffff",
|
|
"text": "#111827",
|
|
"muted": "#6b7280",
|
|
"border": "#dde2eb",
|
|
"hover": "#eef4ff",
|
|
"selected": "#dbeafe",
|
|
"focus": "#3b82f6",
|
|
},
|
|
"black": {
|
|
"chrome": "#0c0d10",
|
|
"surface": "#17191f",
|
|
"text": "#f3f4f6",
|
|
"muted": "#a7adb7",
|
|
"border": "#2a2e38",
|
|
"hover": "#252b36",
|
|
"selected": "#243b5a",
|
|
"focus": "#60a5fa",
|
|
},
|
|
"dark_gray": {
|
|
"chrome": "#282c33",
|
|
"surface": "#343943",
|
|
"text": "#f1f5f9",
|
|
"muted": "#c1c7d0",
|
|
"border": "#48505d",
|
|
"hover": "#3f4a59",
|
|
"selected": "#365b82",
|
|
"focus": "#7dd3fc",
|
|
},
|
|
"colorful": {
|
|
"chrome": "#eef5ff",
|
|
"surface": "#ffffff",
|
|
"text": "#132238",
|
|
"muted": "#667085",
|
|
"border": "#cbd9ef",
|
|
"hover": "#e7f0ff",
|
|
"selected": "#cfe2ff",
|
|
"focus": "#2563eb",
|
|
},
|
|
}
|
|
palette = palettes.get(theme_color, palettes["white"])
|
|
chrome = palette["chrome"]
|
|
surface = palette["surface"]
|
|
text = palette["text"]
|
|
muted = palette["muted"]
|
|
border = palette["border"]
|
|
hover = palette["hover"]
|
|
selected = palette["selected"]
|
|
focus = palette["focus"]
|
|
self.setStyleSheet(
|
|
f"""
|
|
QDialog {{
|
|
background: {chrome};
|
|
color: {text};
|
|
}}
|
|
|
|
QLabel {{
|
|
color: {text};
|
|
}}
|
|
|
|
QLineEdit,
|
|
QTextEdit,
|
|
QComboBox,
|
|
QFontComboBox,
|
|
QSpinBox,
|
|
QDoubleSpinBox {{
|
|
background: {surface};
|
|
color: {text};
|
|
border: 1px solid {border};
|
|
border-radius: 4px;
|
|
padding: 4px 6px;
|
|
selection-background-color: {selected};
|
|
}}
|
|
|
|
QLineEdit#styleNameEdit {{
|
|
min-height: 24px;
|
|
font-weight: 600;
|
|
}}
|
|
|
|
QLineEdit:focus,
|
|
QTextEdit:focus,
|
|
QComboBox:focus,
|
|
QFontComboBox:focus,
|
|
QSpinBox:focus,
|
|
QDoubleSpinBox:focus {{
|
|
border-color: {focus};
|
|
}}
|
|
|
|
QSpinBox,
|
|
QDoubleSpinBox {{
|
|
padding-right: 18px;
|
|
}}
|
|
|
|
QSpinBox::up-button,
|
|
QDoubleSpinBox::up-button,
|
|
QSpinBox::down-button,
|
|
QDoubleSpinBox::down-button {{
|
|
width: 15px;
|
|
background: transparent;
|
|
border-left: 1px solid {border};
|
|
}}
|
|
|
|
QSpinBox::up-button,
|
|
QDoubleSpinBox::up-button {{
|
|
subcontrol-origin: border;
|
|
subcontrol-position: top right;
|
|
border-bottom: 1px solid {border};
|
|
border-top-right-radius: 3px;
|
|
}}
|
|
|
|
QSpinBox::down-button,
|
|
QDoubleSpinBox::down-button {{
|
|
subcontrol-origin: border;
|
|
subcontrol-position: bottom right;
|
|
border-bottom-right-radius: 3px;
|
|
}}
|
|
|
|
QSpinBox::up-button:hover,
|
|
QDoubleSpinBox::up-button:hover,
|
|
QSpinBox::down-button:hover,
|
|
QDoubleSpinBox::down-button:hover {{
|
|
background: {hover};
|
|
}}
|
|
|
|
QSpinBox::up-arrow,
|
|
QDoubleSpinBox::up-arrow,
|
|
QSpinBox::down-arrow,
|
|
QDoubleSpinBox::down-arrow {{
|
|
width: 7px;
|
|
height: 7px;
|
|
}}
|
|
|
|
QCheckBox {{
|
|
color: {text};
|
|
}}
|
|
|
|
QListWidget#styleTargetList {{
|
|
background: {surface};
|
|
color: {text};
|
|
border: 1px solid {border};
|
|
border-radius: 6px;
|
|
padding: 4px;
|
|
outline: 0;
|
|
}}
|
|
|
|
QListWidget#styleTargetList::item {{
|
|
min-height: 28px;
|
|
padding: 4px 8px;
|
|
border-radius: 4px;
|
|
}}
|
|
|
|
QListWidget#styleTargetList::item:hover {{
|
|
background: {hover};
|
|
}}
|
|
|
|
QListWidget#styleTargetList::item:selected {{
|
|
background: {selected};
|
|
color: {text};
|
|
}}
|
|
|
|
QGroupBox#styleSection {{
|
|
background: {surface};
|
|
color: {text};
|
|
border: 1px solid {border};
|
|
border-radius: 6px;
|
|
margin-top: 10px;
|
|
font-weight: 600;
|
|
}}
|
|
|
|
QGroupBox#styleSection::title {{
|
|
subcontrol-origin: margin;
|
|
left: 10px;
|
|
padding: 0 5px;
|
|
background: {chrome};
|
|
}}
|
|
|
|
QFrame#stylePreview {{
|
|
background: {surface};
|
|
border: 1px solid {border};
|
|
border-radius: 6px;
|
|
}}
|
|
|
|
QLabel#stylePreviewTitle {{
|
|
color: {muted};
|
|
font-weight: 600;
|
|
}}
|
|
|
|
QLabel#stylePreviewText {{
|
|
color: {text};
|
|
line-height: 1.5;
|
|
}}
|
|
|
|
QPushButton {{
|
|
background: {surface};
|
|
color: {text};
|
|
border: 1px solid {border};
|
|
border-radius: 4px;
|
|
padding: 6px 12px;
|
|
}}
|
|
|
|
QPushButton:hover {{
|
|
background: {hover};
|
|
}}
|
|
|
|
QPushButton#styleResetButton {{
|
|
color: {muted};
|
|
}}
|
|
|
|
QTabWidget::pane {{
|
|
border: 1px solid {border};
|
|
background: {surface};
|
|
border-radius: 6px;
|
|
top: -1px;
|
|
}}
|
|
|
|
QTabBar::tab {{
|
|
background: {chrome};
|
|
color: {muted};
|
|
border: 1px solid {border};
|
|
border-bottom: 0;
|
|
padding: 7px 14px;
|
|
}}
|
|
|
|
QTabBar::tab:selected {{
|
|
background: {surface};
|
|
color: {text};
|
|
border-color: {border};
|
|
}}
|
|
"""
|
|
)
|
|
self._apply_window_title_bar_style(chrome, text, theme_color in {"black", "dark_gray"})
|
|
|
|
def _apply_window_title_bar_style(self, background: str, foreground: str, dark: bool) -> None:
|
|
if os.name != "nt":
|
|
return
|
|
|
|
def colorref(value: str) -> int:
|
|
value = value.lstrip("#")
|
|
red = int(value[0:2], 16)
|
|
green = int(value[2:4], 16)
|
|
blue = int(value[4:6], 16)
|
|
return red | (green << 8) | (blue << 16)
|
|
|
|
try:
|
|
hwnd = int(self.winId())
|
|
dwmapi = ctypes.windll.dwmapi
|
|
dark_mode = ctypes.c_int(1 if dark else 0)
|
|
for attribute in (20, 19):
|
|
dwmapi.DwmSetWindowAttribute(hwnd, attribute, ctypes.byref(dark_mode), ctypes.sizeof(dark_mode))
|
|
for attribute, value in ((35, background), (36, foreground), (34, background)):
|
|
color = ctypes.c_int(colorref(value))
|
|
dwmapi.DwmSetWindowAttribute(hwnd, attribute, ctypes.byref(color), ctypes.sizeof(color))
|
|
except (AttributeError, OSError, ValueError):
|
|
return
|
|
|
|
def style_payload(self) -> dict[str, object]:
|
|
self._store_current_rule()
|
|
extra_css = self.extra_css_edit.toPlainText().strip()
|
|
return {
|
|
"name": self.name_edit.text().strip() or "自定义样式",
|
|
"rules": deepcopy(self.rules),
|
|
"extra_css": extra_css,
|
|
"css": build_document_style_css(self.rules, extra_css),
|
|
}
|
|
|
|
def _build_visual_tab(self) -> QWidget:
|
|
tab = QWidget()
|
|
layout = QHBoxLayout(tab)
|
|
layout.setContentsMargins(10, 10, 10, 10)
|
|
layout.setSpacing(14)
|
|
|
|
self.target_list = QListWidget()
|
|
self.target_list.setObjectName("styleTargetList")
|
|
self.target_list.setFixedWidth(156)
|
|
for target_id, label, _selector in STYLE_TARGETS:
|
|
item = QListWidgetItem(label)
|
|
item.setData(Qt.ItemDataRole.UserRole, target_id)
|
|
self.target_list.addItem(item)
|
|
self.target_list.currentRowChanged.connect(self._change_target)
|
|
layout.addWidget(self.target_list)
|
|
|
|
content = QWidget()
|
|
content.setObjectName("styleEditorContent")
|
|
content.setMaximumWidth(500)
|
|
content_layout = QVBoxLayout(content)
|
|
content_layout.setContentsMargins(0, 0, 0, 0)
|
|
content_layout.setSpacing(12)
|
|
|
|
self.chinese_font_combo = QFontComboBox()
|
|
self.chinese_font_combo.setCurrentFont(QFont(DEFAULT_CHINESE_FONT))
|
|
self.chinese_font_combo.setMinimumWidth(220)
|
|
self.chinese_font_combo.setMaximumWidth(315)
|
|
self.chinese_font_combo.currentFontChanged.connect(self._store_and_refresh)
|
|
|
|
self.english_font_combo = QFontComboBox()
|
|
self.english_font_combo.setCurrentFont(QFont(DEFAULT_ENGLISH_FONT))
|
|
self.english_font_combo.setMinimumWidth(220)
|
|
self.english_font_combo.setMaximumWidth(315)
|
|
self.english_font_combo.currentFontChanged.connect(self._store_and_refresh)
|
|
|
|
self.font_size_spin = QSpinBox()
|
|
self.font_size_spin.setRange(0, 96)
|
|
self.font_size_spin.setSpecialValueText("继承")
|
|
self.font_size_spin.setSuffix(" px")
|
|
self.font_size_spin.setMaximumWidth(150)
|
|
self.font_size_spin.valueChanged.connect(self._store_and_refresh)
|
|
|
|
self.bold_checkbox = QCheckBox("加粗")
|
|
self.bold_checkbox.toggled.connect(self._store_and_refresh)
|
|
self.italic_checkbox = QCheckBox("斜体")
|
|
self.italic_checkbox.toggled.connect(self._store_and_refresh)
|
|
|
|
font_style_row = QHBoxLayout()
|
|
font_style_row.addWidget(self.bold_checkbox)
|
|
font_style_row.addWidget(self.italic_checkbox)
|
|
font_style_row.addStretch(1)
|
|
|
|
font_form = QFormLayout()
|
|
font_form.setContentsMargins(10, 12, 10, 10)
|
|
font_form.setHorizontalSpacing(12)
|
|
font_form.setVerticalSpacing(9)
|
|
font_form.addRow("中文字体", self.chinese_font_combo)
|
|
font_form.addRow("英文字体", self.english_font_combo)
|
|
font_form.addRow("字号", self.font_size_spin)
|
|
font_form.addRow("字形", font_style_row)
|
|
font_group = QGroupBox("字体")
|
|
font_group.setObjectName("styleSection")
|
|
font_group.setMaximumWidth(500)
|
|
font_group.setLayout(font_form)
|
|
|
|
self.line_height_spin = self._number_spin(0.0, 200.0, "继承")
|
|
self.line_height_spin.setSingleStep(0.05)
|
|
self.line_height_spin.setMaximumWidth(150)
|
|
self.line_height_spin.valueChanged.connect(self._store_and_refresh)
|
|
self.line_height_unit_combo = self._unit_combo((("倍", "multiple"), ("磅", "pt")))
|
|
self.line_height_unit_combo.currentIndexChanged.connect(self._store_and_refresh)
|
|
self.first_indent_spin = self._number_spin(0.0, 20.0, "无")
|
|
self.first_indent_spin.setSuffix(" 字符")
|
|
self.first_indent_spin.setMaximumWidth(170)
|
|
self.first_indent_spin.valueChanged.connect(self._store_and_refresh)
|
|
self.hanging_indent_spin = self._number_spin(0.0, 20.0, "无")
|
|
self.hanging_indent_spin.setSuffix(" 字符")
|
|
self.hanging_indent_spin.setMaximumWidth(170)
|
|
self.hanging_indent_spin.valueChanged.connect(self._store_and_refresh)
|
|
self.space_before_spin = self._number_spin(0.0, 200.0, "无")
|
|
self.space_before_spin.setMaximumWidth(150)
|
|
self.space_before_spin.valueChanged.connect(self._store_and_refresh)
|
|
self.space_before_unit_combo = self._unit_combo((("倍", "multiple"), ("磅", "pt")))
|
|
self.space_before_unit_combo.currentIndexChanged.connect(self._store_and_refresh)
|
|
self.space_after_spin = self._number_spin(0.0, 200.0, "无")
|
|
self.space_after_spin.setMaximumWidth(150)
|
|
self.space_after_spin.valueChanged.connect(self._store_and_refresh)
|
|
self.space_after_unit_combo = self._unit_combo((("倍", "multiple"), ("磅", "pt")))
|
|
self.space_after_unit_combo.currentIndexChanged.connect(self._store_and_refresh)
|
|
|
|
reset_button = QPushButton("重置当前对象")
|
|
reset_button.setObjectName("styleResetButton")
|
|
reset_button.setMaximumWidth(128)
|
|
reset_button.clicked.connect(self._reset_current_target)
|
|
reset_row = QHBoxLayout()
|
|
reset_row.setContentsMargins(0, 0, 0, 0)
|
|
reset_row.addStretch(1)
|
|
reset_row.addWidget(reset_button)
|
|
|
|
paragraph_form = QFormLayout()
|
|
paragraph_form.setContentsMargins(10, 12, 10, 10)
|
|
paragraph_form.setHorizontalSpacing(12)
|
|
paragraph_form.setVerticalSpacing(9)
|
|
paragraph_form.addRow("行间距", self._measurement_row(self.line_height_spin, self.line_height_unit_combo))
|
|
paragraph_form.addRow("首行缩进", self.first_indent_spin)
|
|
paragraph_form.addRow("悬挂缩进", self.hanging_indent_spin)
|
|
paragraph_form.addRow("段前间距", self._measurement_row(self.space_before_spin, self.space_before_unit_combo))
|
|
paragraph_form.addRow("段后间距", self._measurement_row(self.space_after_spin, self.space_after_unit_combo))
|
|
paragraph_form.addRow("", reset_row)
|
|
paragraph_group = QGroupBox("段落")
|
|
paragraph_group.setObjectName("styleSection")
|
|
paragraph_group.setMaximumWidth(500)
|
|
paragraph_group.setLayout(paragraph_form)
|
|
|
|
preview = QFrame()
|
|
preview.setObjectName("stylePreview")
|
|
preview.setMaximumWidth(500)
|
|
preview_layout = QVBoxLayout(preview)
|
|
preview_layout.setContentsMargins(14, 12, 14, 12)
|
|
preview_layout.setSpacing(6)
|
|
preview_title = QLabel("预览")
|
|
preview_title.setObjectName("stylePreviewTitle")
|
|
preview_text = QLabel("标题示例\n正文段落示例 Aa 123,查看字体、缩进和段落间距的整体感觉。")
|
|
preview_text.setObjectName("stylePreviewText")
|
|
preview_text.setWordWrap(True)
|
|
preview_layout.addWidget(preview_title)
|
|
preview_layout.addWidget(preview_text)
|
|
|
|
content_layout.addWidget(font_group)
|
|
content_layout.addWidget(paragraph_group)
|
|
content_layout.addWidget(preview)
|
|
content_layout.addStretch(1)
|
|
layout.addWidget(content)
|
|
layout.addStretch(1)
|
|
previous_loading = self._loading
|
|
self._loading = True
|
|
self.target_list.setCurrentRow(0)
|
|
self._loading = previous_loading
|
|
return tab
|
|
|
|
def _build_advanced_tab(self, css: str) -> QWidget:
|
|
tab = QWidget()
|
|
self.extra_css_edit = QTextEdit()
|
|
self.extra_css_edit.setAcceptRichText(False)
|
|
self.extra_css_edit.setPlainText(css)
|
|
self.extra_css_edit.setPlaceholderText("可选:补充 CSS,会叠加到图形设置生成的样式之后。")
|
|
layout = QVBoxLayout(tab)
|
|
layout.addWidget(QLabel("附加 CSS"))
|
|
layout.addWidget(self.extra_css_edit, 1)
|
|
return tab
|
|
|
|
def _number_spin(self, minimum: float, maximum: float, special_text: str) -> QDoubleSpinBox:
|
|
spin = QDoubleSpinBox()
|
|
spin.setRange(minimum, maximum)
|
|
spin.setDecimals(2)
|
|
spin.setSingleStep(0.1)
|
|
spin.setSpecialValueText(special_text)
|
|
return spin
|
|
|
|
def _unit_combo(self, items: tuple[tuple[str, str], ...]) -> QComboBox:
|
|
combo = QComboBox()
|
|
combo.setFixedWidth(58)
|
|
for label, value in items:
|
|
combo.addItem(label, value)
|
|
return combo
|
|
|
|
def _measurement_row(self, spin: QDoubleSpinBox, unit_combo: QComboBox) -> QHBoxLayout:
|
|
row = QHBoxLayout()
|
|
row.setContentsMargins(0, 0, 0, 0)
|
|
row.setSpacing(6)
|
|
row.addWidget(spin)
|
|
row.addWidget(unit_combo)
|
|
row.addStretch(1)
|
|
return row
|
|
|
|
def _change_target(self, row: int = -1) -> None:
|
|
if self._loading:
|
|
return
|
|
del row
|
|
self._store_current_rule()
|
|
item = self.target_list.currentItem()
|
|
if item is None:
|
|
return
|
|
self._current_target = str(item.data(Qt.ItemDataRole.UserRole) or STYLE_TARGETS[0][0])
|
|
self._load_rule_to_widgets(self.rules.get(self._current_target, {}))
|
|
|
|
def _store_current_rule(self) -> None:
|
|
rule: dict[str, object] = {}
|
|
rule["chinese_font_family"] = self.chinese_font_combo.currentFont().family() or DEFAULT_CHINESE_FONT
|
|
rule["english_font_family"] = self.english_font_combo.currentFont().family() or DEFAULT_ENGLISH_FONT
|
|
if self.font_size_spin.value() > 0:
|
|
rule["font_size"] = self.font_size_spin.value()
|
|
if self.bold_checkbox.isChecked():
|
|
rule["bold"] = True
|
|
if self.italic_checkbox.isChecked():
|
|
rule["italic"] = True
|
|
if self.line_height_spin.value() > 0:
|
|
rule["line_height_value"] = self.line_height_spin.value()
|
|
rule["line_height_unit"] = str(self.line_height_unit_combo.currentData() or "multiple")
|
|
if self.first_indent_spin.value() > 0:
|
|
rule["first_line_indent_chars"] = self.first_indent_spin.value()
|
|
if self.hanging_indent_spin.value() > 0:
|
|
rule["hanging_indent_chars"] = self.hanging_indent_spin.value()
|
|
if self.space_before_spin.value() > 0:
|
|
rule["space_before_value"] = self.space_before_spin.value()
|
|
rule["space_before_unit"] = str(self.space_before_unit_combo.currentData() or "multiple")
|
|
if self.space_after_spin.value() > 0:
|
|
rule["space_after_value"] = self.space_after_spin.value()
|
|
rule["space_after_unit"] = str(self.space_after_unit_combo.currentData() or "multiple")
|
|
if rule:
|
|
self.rules[self._current_target] = rule
|
|
else:
|
|
self.rules.pop(self._current_target, None)
|
|
|
|
def _load_rule_to_widgets(self, rule: dict[str, Any]) -> None:
|
|
self._loading = True
|
|
chinese_family = str(rule.get("chinese_font_family") or rule.get("font_family") or DEFAULT_CHINESE_FONT)
|
|
english_family = str(rule.get("english_font_family") or DEFAULT_ENGLISH_FONT)
|
|
self.chinese_font_combo.setCurrentFont(QFont(chinese_family))
|
|
self.english_font_combo.setCurrentFont(QFont(english_family))
|
|
self.font_size_spin.setValue(int(_positive_float(rule.get("font_size"))))
|
|
self.bold_checkbox.setChecked(bool(rule.get("bold")))
|
|
self.italic_checkbox.setChecked(bool(rule.get("italic")))
|
|
self.line_height_spin.setValue(_positive_float(rule.get("line_height_value", rule.get("line_height"))))
|
|
self._set_combo_data(self.line_height_unit_combo, str(rule.get("line_height_unit") or "multiple"))
|
|
self.first_indent_spin.setValue(_positive_float(rule.get("first_line_indent_chars", rule.get("first_line_indent"))))
|
|
self.hanging_indent_spin.setValue(_positive_float(rule.get("hanging_indent_chars", rule.get("hanging_indent"))))
|
|
self.space_before_spin.setValue(_positive_float(rule.get("space_before_value", rule.get("space_before"))))
|
|
self._set_combo_data(self.space_before_unit_combo, str(rule.get("space_before_unit") or "multiple"))
|
|
self.space_after_spin.setValue(_positive_float(rule.get("space_after_value", rule.get("space_after"))))
|
|
self._set_combo_data(self.space_after_unit_combo, str(rule.get("space_after_unit") or "multiple"))
|
|
self._loading = False
|
|
|
|
def _sync_font_enabled(self) -> None:
|
|
return
|
|
|
|
def _set_combo_data(self, combo: QComboBox, value: str) -> None:
|
|
index = combo.findData(value)
|
|
combo.setCurrentIndex(max(0, index))
|
|
|
|
def _store_and_refresh(self) -> None:
|
|
if not self._loading:
|
|
self._store_current_rule()
|
|
|
|
def _reset_current_target(self) -> None:
|
|
self.rules.pop(self._current_target, None)
|
|
self._load_rule_to_widgets({})
|