audit-e2w 0.0.0-stage → 1.11.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +152 -3
- package/cordis.patch.yml +3 -0
- package/dsh_plugins/audit-e2w/lib/engine.mjs +174 -0
- package/dsh_plugins/audit-e2w/lib/index.js +122 -0
- package/dsh_plugins/audit-e2w/lib/skill.js +42 -0
- package/excel_word/README.md +388 -0
- package/excel_word/qoder_engine.py +437 -0
- package/excel_word/requirements.txt +4 -0
- package/excel_word/wesync/__init__.py +19 -0
- package/excel_word/wesync/__main__.py +6 -0
- package/excel_word/wesync/ai/__init__.py +52 -0
- package/excel_word/wesync/ai/actions.py +369 -0
- package/excel_word/wesync/ai/client.py +340 -0
- package/excel_word/wesync/ai/config.py +291 -0
- package/excel_word/wesync/ai/docs.py +177 -0
- package/excel_word/wesync/ai/prompts.py +288 -0
- package/excel_word/wesync/ai/tasks/__init__.py +26 -0
- package/excel_word/wesync/ai/tasks/actions_task.py +97 -0
- package/excel_word/wesync/ai/tasks/combined.py +123 -0
- package/excel_word/wesync/ai/tasks/diff_note.py +61 -0
- package/excel_word/wesync/ai/tasks/headings.py +119 -0
- package/excel_word/wesync/ai/tasks/help_task.py +69 -0
- package/excel_word/wesync/ai/tasks/roles.py +128 -0
- package/excel_word/wesync/align.py +155 -0
- package/excel_word/wesync/api.py +1292 -0
- package/excel_word/wesync/backup_history.py +100 -0
- package/excel_word/wesync/changelog.py +58 -0
- package/excel_word/wesync/cli.py +191 -0
- package/excel_word/wesync/commands/__init__.py +8 -0
- package/excel_word/wesync/commands/blank_cmd.py +279 -0
- package/excel_word/wesync/commands/bookmark_cmd.py +146 -0
- package/excel_word/wesync/commands/clear_cmd.py +50 -0
- package/excel_word/wesync/commands/export_cmd.py +270 -0
- package/excel_word/wesync/commands/incremental_cmd.py +157 -0
- package/excel_word/wesync/commands/marks_cmd.py +160 -0
- package/excel_word/wesync/commands/placeholders_cmd.py +92 -0
- package/excel_word/wesync/commands/writeback_cmd.py +469 -0
- package/excel_word/wesync/config.py +97 -0
- package/excel_word/wesync/console.py +85 -0
- package/excel_word/wesync/docx_blank.py +156 -0
- package/excel_word/wesync/docx_bookmarks.py +294 -0
- package/excel_word/wesync/docx_columns.py +257 -0
- package/excel_word/wesync/docx_edit.py +77 -0
- package/excel_word/wesync/docx_grid.py +212 -0
- package/excel_word/wesync/docx_marks.py +399 -0
- package/excel_word/wesync/docx_meta.py +157 -0
- package/excel_word/wesync/docx_newtable.py +220 -0
- package/excel_word/wesync/docx_placeholders.py +200 -0
- package/excel_word/wesync/docx_rows.py +67 -0
- package/excel_word/wesync/errors.py +5 -0
- package/excel_word/wesync/excel_blocks.py +502 -0
- package/excel_word/wesync/excel_export.py +143 -0
- package/excel_word/wesync/excel_marks.py +265 -0
- package/excel_word/wesync/excel_totals.py +53 -0
- package/excel_word/wesync/fsops.py +219 -0
- package/excel_word/wesync/gui/__init__.py +53 -0
- package/excel_word/wesync/gui/__main__.py +6 -0
- package/excel_word/wesync/gui/app.py +613 -0
- package/excel_word/wesync/gui/manual.py +70 -0
- package/excel_word/wesync/gui/pages/__init__.py +80 -0
- package/excel_word/wesync/gui/pages/_page.py +211 -0
- package/excel_word/wesync/gui/pages/ai_page.py +854 -0
- package/excel_word/wesync/gui/pages/batch_page.py +456 -0
- package/excel_word/wesync/gui/pages/blank_page.py +530 -0
- package/excel_word/wesync/gui/pages/bookmark_page.py +97 -0
- package/excel_word/wesync/gui/pages/export_page.py +141 -0
- package/excel_word/wesync/gui/pages/feedback_page.py +196 -0
- package/excel_word/wesync/gui/pages/format_page.py +551 -0
- package/excel_word/wesync/gui/pages/help_page.py +158 -0
- package/excel_word/wesync/gui/pages/history_page.py +168 -0
- package/excel_word/wesync/gui/pages/marks_page.py +211 -0
- package/excel_word/wesync/gui/pages/settings_page.py +450 -0
- package/excel_word/wesync/gui/pages/writeback_page.py +473 -0
- package/excel_word/wesync/gui/resources.py +22 -0
- package/excel_word/wesync/gui/runner.py +90 -0
- package/excel_word/wesync/gui/store.py +50 -0
- package/excel_word/wesync/gui/theme.py +271 -0
- package/excel_word/wesync/gui/widgets/__init__.py +8 -0
- package/excel_word/wesync/gui/widgets/ai_config.py +362 -0
- package/excel_word/wesync/gui/widgets/cards.py +139 -0
- package/excel_word/wesync/gui/widgets/chips.py +565 -0
- package/excel_word/wesync/gui/widgets/filebar.py +58 -0
- package/excel_word/wesync/gui/widgets/logpane.py +126 -0
- package/excel_word/wesync/gui/widgets/nav.py +294 -0
- package/excel_word/wesync/gui/widgets/theme_choice.py +93 -0
- package/excel_word/wesync/report_format.py +1585 -0
- package/excel_word/wesync/settings.py +35 -0
- package/excel_word/wesync/sort_rules.py +293 -0
- package/excel_word/wesync/valuefmt.py +485 -0
- package/package.json +53 -6
- package/packaging//344/275/277/347/224/250/350/257/264/346/230/216.txt +247 -0
- package/qoder_plugins/audit-toolbox/README.md +159 -0
- package/qoder_plugins/audit-toolbox/catalog.mjs +265 -0
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
"""主窗口:左侧导航 + 顶部文件条 + 中间页面 + 右侧可隐藏日志 + 状态栏。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from PySide6.QtCore import QEasingCurve, Qt, QTimer, QVariantAnimation
|
|
11
|
+
from PySide6.QtGui import QIcon, QKeySequence, QShortcut
|
|
12
|
+
from PySide6.QtWidgets import (
|
|
13
|
+
QApplication,
|
|
14
|
+
QFileDialog,
|
|
15
|
+
QLabel,
|
|
16
|
+
QMainWindow,
|
|
17
|
+
QMessageBox,
|
|
18
|
+
QProgressBar,
|
|
19
|
+
QPushButton,
|
|
20
|
+
QScrollArea,
|
|
21
|
+
QSizePolicy,
|
|
22
|
+
QSplitter,
|
|
23
|
+
QStackedWidget,
|
|
24
|
+
QVBoxLayout,
|
|
25
|
+
QWidget,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from .. import __version__, api
|
|
29
|
+
from ..errors import SyncError
|
|
30
|
+
from ..fsops import find_default_docx
|
|
31
|
+
from ..settings import normalize
|
|
32
|
+
from . import store, theme
|
|
33
|
+
from .pages import iter_pages
|
|
34
|
+
from .resources import icon_path
|
|
35
|
+
from .runner import TaskRunner
|
|
36
|
+
from .widgets.filebar import FileBar
|
|
37
|
+
from .widgets.theme_choice import ThemeChoiceDialog, needs_choice
|
|
38
|
+
from .widgets.logpane import LogPane
|
|
39
|
+
from .widgets.nav import COLLAPSED_WIDTH, EXPANDED_WIDTH, NavBar
|
|
40
|
+
|
|
41
|
+
DEFAULT_SIZE = (1100, 640)
|
|
42
|
+
MIN_SIZE = (1080, 560)
|
|
43
|
+
DEFAULT_LOG_WIDTH = 320
|
|
44
|
+
NAV_MIN_WIDTH = 176
|
|
45
|
+
NAV_MAX_WIDTH = 320
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _int_pair(value, default: tuple[int, int]) -> tuple[int, int]:
|
|
49
|
+
if not isinstance(value, (list, tuple)) or len(value) != 2:
|
|
50
|
+
return default
|
|
51
|
+
try:
|
|
52
|
+
return int(value[0]), int(value[1])
|
|
53
|
+
except (TypeError, ValueError, OverflowError):
|
|
54
|
+
return default
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class UiContext:
|
|
58
|
+
"""页面与主窗口之间的桥梁:文件状态、日志、弹窗、任务执行。"""
|
|
59
|
+
|
|
60
|
+
def __init__(self, app: "WesyncApp") -> None:
|
|
61
|
+
self._app = app
|
|
62
|
+
|
|
63
|
+
# ---------- 全局设置 ----------
|
|
64
|
+
def global_settings(self, *, strict: bool = False) -> dict:
|
|
65
|
+
if strict:
|
|
66
|
+
self._app.settings = store.load(strict=True)
|
|
67
|
+
return normalize(self._app.settings.get("global", {}))
|
|
68
|
+
|
|
69
|
+
def save_global_settings(self, values: dict, *, repair: bool = False) -> None:
|
|
70
|
+
normalized = normalize(values)
|
|
71
|
+
store.save({"global": normalized, "appearance": self.appearance()}, repair=repair)
|
|
72
|
+
self._app.settings["global"] = normalized
|
|
73
|
+
for page in self._app._pages.values():
|
|
74
|
+
page.reload_global_defaults()
|
|
75
|
+
|
|
76
|
+
# ---------- 外观 ----------
|
|
77
|
+
def appearance(self) -> str:
|
|
78
|
+
return theme.normalize_mode(self._app.settings.get("appearance"))
|
|
79
|
+
|
|
80
|
+
def save_appearance(self, mode: str) -> None:
|
|
81
|
+
normalized = theme.normalize_mode(mode)
|
|
82
|
+
store.save({"appearance": normalized})
|
|
83
|
+
self._app.settings["appearance"] = normalized
|
|
84
|
+
self._app.apply_appearance(normalized)
|
|
85
|
+
|
|
86
|
+
# ---------- 文件 ----------
|
|
87
|
+
@property
|
|
88
|
+
def docx(self) -> Path | None:
|
|
89
|
+
return self._app.docx_path
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def excel(self) -> Path | None:
|
|
93
|
+
return self._app.excel_path
|
|
94
|
+
|
|
95
|
+
def workbook_path(self) -> Path:
|
|
96
|
+
if self._app.docx_path is None:
|
|
97
|
+
return Path("")
|
|
98
|
+
return api.workbook_path(self._app.docx_path, self._app.excel_path)
|
|
99
|
+
|
|
100
|
+
def require_docx(self) -> bool:
|
|
101
|
+
if self._app.docx_path is None:
|
|
102
|
+
self.error("未选择文件", "请先在顶部选择要处理的 Word 文件。")
|
|
103
|
+
return False
|
|
104
|
+
if not Path(self._app.docx_path).exists():
|
|
105
|
+
self.error("文件不存在", f"找不到 Word 文件:\n{self._app.docx_path}")
|
|
106
|
+
return False
|
|
107
|
+
return True
|
|
108
|
+
|
|
109
|
+
def set_docx(self, path: Path) -> None:
|
|
110
|
+
self._app.set_docx(Path(path))
|
|
111
|
+
|
|
112
|
+
def set_excel(self, path: Path | None) -> None:
|
|
113
|
+
self._app.set_excel(None if path is None else Path(path))
|
|
114
|
+
|
|
115
|
+
# ---------- 日志与状态 ----------
|
|
116
|
+
def log(self, text: str, level: str = "info") -> None:
|
|
117
|
+
self._app.append_log(text, level)
|
|
118
|
+
|
|
119
|
+
def set_status(self, text: str) -> None:
|
|
120
|
+
self._app.set_status(text)
|
|
121
|
+
|
|
122
|
+
# ---------- 弹窗 ----------
|
|
123
|
+
def confirm(self, title: str, message: str) -> bool:
|
|
124
|
+
box = QMessageBox(self._app)
|
|
125
|
+
box.setIcon(QMessageBox.Question)
|
|
126
|
+
box.setWindowTitle(title)
|
|
127
|
+
box.setText(message)
|
|
128
|
+
box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
|
|
129
|
+
box.button(QMessageBox.Ok).setText("确定")
|
|
130
|
+
box.button(QMessageBox.Cancel).setText("取消")
|
|
131
|
+
return box.exec() == QMessageBox.Ok
|
|
132
|
+
|
|
133
|
+
def info(self, title: str, message: str) -> None:
|
|
134
|
+
QMessageBox.information(self._app, title, message)
|
|
135
|
+
|
|
136
|
+
def error(self, title: str, message: str, log: bool = True) -> None:
|
|
137
|
+
if log:
|
|
138
|
+
self._app.append_log(f"错误:{message}", "error")
|
|
139
|
+
QMessageBox.critical(self._app, title, message)
|
|
140
|
+
|
|
141
|
+
def open_folder(self) -> None:
|
|
142
|
+
self._app.open_folder()
|
|
143
|
+
|
|
144
|
+
# ---------- 任务 ----------
|
|
145
|
+
@property
|
|
146
|
+
def busy(self) -> bool:
|
|
147
|
+
return self._app.runner.busy
|
|
148
|
+
|
|
149
|
+
def page(self, key: str):
|
|
150
|
+
"""按页面类名取页面实例;不存在时返回 None(供跨页联动使用)。"""
|
|
151
|
+
return self._app._pages.get(key)
|
|
152
|
+
|
|
153
|
+
def go_to_page(self, key: str) -> bool:
|
|
154
|
+
"""切换到指定页面;页面不存在时返回 False。"""
|
|
155
|
+
if key not in self._app._pages:
|
|
156
|
+
return False
|
|
157
|
+
self._app._select_page(key)
|
|
158
|
+
return True
|
|
159
|
+
|
|
160
|
+
def run_steps(self, steps, on_success=None) -> bool:
|
|
161
|
+
if self._app.runner.busy:
|
|
162
|
+
self.log("已有任务在执行,请等待完成后再操作。", "warn")
|
|
163
|
+
return False
|
|
164
|
+
started = self._app.runner.submit(steps)
|
|
165
|
+
if started:
|
|
166
|
+
self._app._task_callback = on_success
|
|
167
|
+
self._app.set_busy(True)
|
|
168
|
+
return started
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class WesyncApp(QMainWindow):
|
|
172
|
+
"""界面主窗口。"""
|
|
173
|
+
|
|
174
|
+
def __init__(self, base_dir: Path | None = None) -> None:
|
|
175
|
+
super().__init__()
|
|
176
|
+
self.base_dir = Path(base_dir) if base_dir else Path(__file__).resolve().parents[2]
|
|
177
|
+
self.settings = store.load()
|
|
178
|
+
self.appearance_mode = theme.normalize_mode(self.settings.get("appearance"))
|
|
179
|
+
theme.apply(QApplication.instance(), self.appearance_mode)
|
|
180
|
+
self.docx_path: Path | None = None
|
|
181
|
+
self.excel_path: Path | None = None
|
|
182
|
+
self.excel_is_custom = False
|
|
183
|
+
self.runner = TaskRunner()
|
|
184
|
+
self._task_callback = None
|
|
185
|
+
self.ctx = UiContext(self)
|
|
186
|
+
self._pages: dict[str, QWidget] = {}
|
|
187
|
+
|
|
188
|
+
self.setWindowTitle("审计工具箱")
|
|
189
|
+
self.setWindowIcon(QIcon(str(icon_path())))
|
|
190
|
+
self.setMinimumSize(*MIN_SIZE)
|
|
191
|
+
width, height = _int_pair(self.settings.get("qt_size"), DEFAULT_SIZE)
|
|
192
|
+
if width < MIN_SIZE[0] or height < MIN_SIZE[1] or width > 10000 or height > 10000:
|
|
193
|
+
width, height = DEFAULT_SIZE
|
|
194
|
+
self.resize(width, height)
|
|
195
|
+
pos = self.settings.get("qt_pos")
|
|
196
|
+
if pos:
|
|
197
|
+
x, y = _int_pair(pos, (0, 0))
|
|
198
|
+
self.move(x, y)
|
|
199
|
+
|
|
200
|
+
self._build_layout()
|
|
201
|
+
self._load_initial_paths()
|
|
202
|
+
self._select_page(next(iter(self._pages)))
|
|
203
|
+
self._help_shortcut = QShortcut(QKeySequence("F1"), self)
|
|
204
|
+
self._help_shortcut.activated.connect(lambda: self._select_page("HelpPage"))
|
|
205
|
+
QTimer(self, timeout=self._poll_queue, interval=120).start()
|
|
206
|
+
QApplication.instance().styleHints().colorSchemeChanged.connect(self._system_appearance_changed)
|
|
207
|
+
self._appearance_listener_connected = True
|
|
208
|
+
# 首次使用(配置里还没有 appearance)时让用户自己选,不替他决定
|
|
209
|
+
QTimer.singleShot(0, self._ask_appearance_once)
|
|
210
|
+
|
|
211
|
+
def _ask_appearance_once(self) -> None:
|
|
212
|
+
"""首次启动弹一次外观选择;用户选过就不再打扰。
|
|
213
|
+
|
|
214
|
+
注意:`exec()` 是模态阻塞调用。测试环境(AUDIT_TOOLBOX_NO_THEME_PROMPT=1)
|
|
215
|
+
必须跳过,否则构造 WesyncApp 会永久等待用户点击。
|
|
216
|
+
"""
|
|
217
|
+
if os.environ.get("AUDIT_TOOLBOX_NO_THEME_PROMPT"):
|
|
218
|
+
return
|
|
219
|
+
if not needs_choice(self.settings):
|
|
220
|
+
return
|
|
221
|
+
dialog = ThemeChoiceDialog(self, current="dark")
|
|
222
|
+
dialog.exec()
|
|
223
|
+
# 无论确定还是直接关掉,都记下选择,避免每次启动都弹
|
|
224
|
+
self.ctx.save_appearance(dialog.chosen())
|
|
225
|
+
|
|
226
|
+
def apply_appearance(self, mode: str) -> None:
|
|
227
|
+
self.appearance_mode = theme.normalize_mode(mode)
|
|
228
|
+
theme.apply(QApplication.instance(), self.appearance_mode)
|
|
229
|
+
self.nav.refresh_theme()
|
|
230
|
+
self.logpane.refresh_theme()
|
|
231
|
+
help_page = self._pages.get("HelpPage")
|
|
232
|
+
if help_page is not None:
|
|
233
|
+
help_page.refresh_theme()
|
|
234
|
+
|
|
235
|
+
def _system_appearance_changed(self, _scheme) -> None:
|
|
236
|
+
if self.appearance_mode == "system":
|
|
237
|
+
self.apply_appearance("system")
|
|
238
|
+
|
|
239
|
+
# ------------------------------------------------------------------ 布局
|
|
240
|
+
|
|
241
|
+
def _build_layout(self) -> None:
|
|
242
|
+
central = QWidget()
|
|
243
|
+
self.setCentralWidget(central)
|
|
244
|
+
layout = QVBoxLayout(central)
|
|
245
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
246
|
+
layout.setSpacing(0)
|
|
247
|
+
|
|
248
|
+
self.filebar = FileBar(
|
|
249
|
+
central,
|
|
250
|
+
on_pick_docx=self.pick_docx,
|
|
251
|
+
on_pick_excel=self.pick_excel,
|
|
252
|
+
on_reset_excel=self.reset_excel,
|
|
253
|
+
on_open_folder=self.open_folder,
|
|
254
|
+
)
|
|
255
|
+
layout.addWidget(self.filebar)
|
|
256
|
+
|
|
257
|
+
self.splitter = QSplitter(Qt.Horizontal)
|
|
258
|
+
self.nav = NavBar(self.splitter, self._select_page)
|
|
259
|
+
self.nav.collapse_toggled.connect(self._set_nav_collapsed)
|
|
260
|
+
self.nav_scroll = QScrollArea()
|
|
261
|
+
self.nav_scroll.setWidget(self.nav)
|
|
262
|
+
self.nav_scroll.setWidgetResizable(True)
|
|
263
|
+
self.nav_scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
264
|
+
# 窗口很矮时导航放不下:让 Nav 保持内容所需高度,由滚动区出竖向滚动条,
|
|
265
|
+
# 否则 setWidgetResizable 会把 Nav 压扁、最后几项被裁掉且无法访问。
|
|
266
|
+
self.nav_scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
267
|
+
self.nav.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
|
|
268
|
+
QTimer.singleShot(0, self._sync_nav_min_height)
|
|
269
|
+
self.nav_scroll.setMinimumWidth(COLLAPSED_WIDTH)
|
|
270
|
+
self.nav_scroll.setMaximumWidth(NAV_MAX_WIDTH)
|
|
271
|
+
self._nav_animation = QVariantAnimation(self)
|
|
272
|
+
self._nav_animation.setDuration(180)
|
|
273
|
+
self._nav_animation.setEasingCurve(QEasingCurve.Type.OutCubic)
|
|
274
|
+
self._nav_animation.valueChanged.connect(self._set_splitter_nav_width)
|
|
275
|
+
self._nav_animation.finished.connect(self._finish_nav_animation)
|
|
276
|
+
self.stack = QStackedWidget()
|
|
277
|
+
self.logpane = LogPane(self.splitter, on_hide=lambda: self.set_log_visible(False))
|
|
278
|
+
self.splitter.addWidget(self.nav_scroll)
|
|
279
|
+
self.splitter.addWidget(self.stack)
|
|
280
|
+
self.splitter.addWidget(self.logpane)
|
|
281
|
+
self.splitter.setStretchFactor(0, 0)
|
|
282
|
+
self.splitter.setStretchFactor(1, 1)
|
|
283
|
+
self.splitter.setStretchFactor(2, 0)
|
|
284
|
+
self.splitter.setCollapsible(0, False)
|
|
285
|
+
self.splitter.setCollapsible(2, False)
|
|
286
|
+
self.nav.set_collapsed(bool(self.settings.get("nav_collapsed", False)))
|
|
287
|
+
if self.nav.collapsed:
|
|
288
|
+
self.nav_scroll.setFixedWidth(COLLAPSED_WIDTH)
|
|
289
|
+
else:
|
|
290
|
+
self.nav_scroll.setMinimumWidth(NAV_MIN_WIDTH)
|
|
291
|
+
log_width = int(self.settings.get("log_width") or DEFAULT_LOG_WIDTH)
|
|
292
|
+
# 首次布局会按 sizeHint 重分宽度,等布局生效后再按设定值分配
|
|
293
|
+
QTimer.singleShot(0, lambda: self._apply_splitter_sizes(log_width))
|
|
294
|
+
layout.addWidget(self.splitter, 1)
|
|
295
|
+
|
|
296
|
+
group_keys = []
|
|
297
|
+
for page_class in iter_pages():
|
|
298
|
+
page = page_class(self.ctx)
|
|
299
|
+
key = page_class.__name__
|
|
300
|
+
self._pages[key] = page
|
|
301
|
+
self.stack.addWidget(page)
|
|
302
|
+
group = getattr(page_class, "nav_group", "维护工具")
|
|
303
|
+
if group not in group_keys:
|
|
304
|
+
group_keys.append(group)
|
|
305
|
+
self.nav.add_group(group, group)
|
|
306
|
+
self.nav.add_item(key, getattr(page, "title", key), group)
|
|
307
|
+
self.nav.set_group_states(self.settings.get("nav_groups", {}))
|
|
308
|
+
|
|
309
|
+
self._status_label = QLabel("就绪")
|
|
310
|
+
self._status_label.setObjectName("Status")
|
|
311
|
+
self.statusBar().addWidget(self._status_label, 1)
|
|
312
|
+
self._version_label = QLabel(f"审计工具箱 v{__version__}")
|
|
313
|
+
self._version_label.setObjectName("Version")
|
|
314
|
+
self._version_label.setToolTip("当前软件版本")
|
|
315
|
+
self.statusBar().addPermanentWidget(self._version_label)
|
|
316
|
+
self._toggle_log_btn = QPushButton("隐藏日志")
|
|
317
|
+
self._toggle_log_btn.clicked.connect(lambda _checked=False: self.set_log_visible(not self.logpane.isVisible()))
|
|
318
|
+
self.statusBar().addPermanentWidget(self._toggle_log_btn)
|
|
319
|
+
self._progress = QProgressBar()
|
|
320
|
+
self._progress.setFixedWidth(150)
|
|
321
|
+
self._progress.setRange(0, 100)
|
|
322
|
+
self._progress.setValue(0)
|
|
323
|
+
self._progress.setTextVisible(False)
|
|
324
|
+
self.statusBar().addPermanentWidget(self._progress)
|
|
325
|
+
self.set_log_visible(bool(self.settings.get("log_visible", True)))
|
|
326
|
+
|
|
327
|
+
def _sync_nav_min_height(self) -> None:
|
|
328
|
+
"""让侧栏保持内容所需高度,窗口过矮时由滚动区出滚动条而不是裁掉尾部项目。"""
|
|
329
|
+
hint = self.nav.sizeHint().height()
|
|
330
|
+
if hint > 0 and self.nav.minimumHeight() != hint:
|
|
331
|
+
self.nav.setMinimumHeight(hint)
|
|
332
|
+
|
|
333
|
+
def _apply_splitter_sizes(self, log_width: int) -> None:
|
|
334
|
+
if self.nav.collapsed:
|
|
335
|
+
nav_width = COLLAPSED_WIDTH
|
|
336
|
+
else:
|
|
337
|
+
try:
|
|
338
|
+
stored = int(self.settings.get("nav_width", EXPANDED_WIDTH))
|
|
339
|
+
except (TypeError, ValueError, OverflowError):
|
|
340
|
+
stored = EXPANDED_WIDTH
|
|
341
|
+
nav_width = max(NAV_MIN_WIDTH, min(stored, NAV_MAX_WIDTH))
|
|
342
|
+
total = sum(self.splitter.sizes()) or self.width()
|
|
343
|
+
self.splitter.setSizes([nav_width, max(total - nav_width - log_width, 300), log_width])
|
|
344
|
+
|
|
345
|
+
def _set_splitter_nav_width(self, width) -> None:
|
|
346
|
+
sizes = self.splitter.sizes()
|
|
347
|
+
if len(sizes) != 3:
|
|
348
|
+
return
|
|
349
|
+
width = max(COLLAPSED_WIDTH, min(int(width), NAV_MAX_WIDTH))
|
|
350
|
+
delta = width - sizes[0]
|
|
351
|
+
content = sizes[1] - delta
|
|
352
|
+
log = sizes[2]
|
|
353
|
+
if content < 300:
|
|
354
|
+
log = max(80, log - (300 - content))
|
|
355
|
+
content = 300
|
|
356
|
+
self.splitter.setSizes([width, content, log])
|
|
357
|
+
|
|
358
|
+
def _finish_nav_animation(self) -> None:
|
|
359
|
+
self.nav_scroll.setMaximumWidth(NAV_MAX_WIDTH)
|
|
360
|
+
if self.nav.collapsed:
|
|
361
|
+
self.nav_scroll.setFixedWidth(COLLAPSED_WIDTH)
|
|
362
|
+
else:
|
|
363
|
+
self.nav_scroll.setMinimumWidth(NAV_MIN_WIDTH)
|
|
364
|
+
|
|
365
|
+
def _set_nav_collapsed(self, collapsed: bool, *, animate: bool = True) -> None:
|
|
366
|
+
collapsed = bool(collapsed)
|
|
367
|
+
self.nav.set_collapsed(collapsed)
|
|
368
|
+
self.settings["nav_collapsed"] = collapsed
|
|
369
|
+
self.nav_scroll.setMaximumWidth(NAV_MAX_WIDTH)
|
|
370
|
+
self.nav_scroll.setMinimumWidth(COLLAPSED_WIDTH)
|
|
371
|
+
target = COLLAPSED_WIDTH if collapsed else EXPANDED_WIDTH
|
|
372
|
+
if animate and self.isVisible():
|
|
373
|
+
self._nav_animation.stop()
|
|
374
|
+
self._nav_animation.setStartValue(self.splitter.sizes()[0])
|
|
375
|
+
self._nav_animation.setEndValue(target)
|
|
376
|
+
self._nav_animation.start()
|
|
377
|
+
else:
|
|
378
|
+
self._nav_animation.stop()
|
|
379
|
+
self._set_splitter_nav_width(target)
|
|
380
|
+
self._finish_nav_animation()
|
|
381
|
+
|
|
382
|
+
def set_log_visible(self, visible: bool) -> None:
|
|
383
|
+
self.logpane.setVisible(visible)
|
|
384
|
+
self._toggle_log_btn.setText("隐藏日志" if visible else "显示日志")
|
|
385
|
+
if visible:
|
|
386
|
+
self._apply_splitter_sizes(int(self.settings.get("log_width") or DEFAULT_LOG_WIDTH))
|
|
387
|
+
|
|
388
|
+
# ------------------------------------------------------------ 文件与路径
|
|
389
|
+
|
|
390
|
+
def _load_initial_paths(self) -> None:
|
|
391
|
+
docx: Path | None = None
|
|
392
|
+
remembered = self.settings.get("last_docx")
|
|
393
|
+
if remembered and Path(remembered).exists():
|
|
394
|
+
docx = Path(remembered)
|
|
395
|
+
else:
|
|
396
|
+
try:
|
|
397
|
+
docx = find_default_docx(self.base_dir)
|
|
398
|
+
except SyncError:
|
|
399
|
+
docx = None
|
|
400
|
+
self.set_docx(docx)
|
|
401
|
+
remembered_excel = self.settings.get("last_excel")
|
|
402
|
+
if (
|
|
403
|
+
docx is not None
|
|
404
|
+
and remembered
|
|
405
|
+
and Path(remembered).resolve() == docx.resolve()
|
|
406
|
+
and remembered_excel
|
|
407
|
+
and Path(remembered_excel).exists()
|
|
408
|
+
):
|
|
409
|
+
self.set_excel(Path(remembered_excel))
|
|
410
|
+
|
|
411
|
+
def set_docx(self, path: Path | None) -> None:
|
|
412
|
+
changed = path != self.docx_path
|
|
413
|
+
self.docx_path = path
|
|
414
|
+
if changed:
|
|
415
|
+
self.excel_is_custom = False
|
|
416
|
+
if path is not None and not self.excel_is_custom:
|
|
417
|
+
self.excel_path = api.workbook_path(path, None)
|
|
418
|
+
self.filebar.set_paths(self.docx_path, self.excel_path)
|
|
419
|
+
if path is None:
|
|
420
|
+
self.set_status("尚未选择 Word 文件")
|
|
421
|
+
else:
|
|
422
|
+
exists = self.excel_path.exists() if self.excel_path else False
|
|
423
|
+
suffix = "" if exists else "(工作簿尚未生成)"
|
|
424
|
+
self.set_status(f"当前文件:{path.name}{suffix}")
|
|
425
|
+
page = self.stack.currentWidget()
|
|
426
|
+
if hasattr(page, "on_show"):
|
|
427
|
+
page.on_show()
|
|
428
|
+
|
|
429
|
+
def set_excel(self, path: Path | None) -> None:
|
|
430
|
+
self.excel_path = path
|
|
431
|
+
self.excel_is_custom = path is not None
|
|
432
|
+
self.filebar.set_paths(self.docx_path, self.excel_path)
|
|
433
|
+
self.set_status("已指定工作簿:" + (path.name if path else "(未指定)"))
|
|
434
|
+
|
|
435
|
+
def pick_docx(self) -> None:
|
|
436
|
+
initial = str(self.docx_path.parent if self.docx_path else self.base_dir)
|
|
437
|
+
path, _ = QFileDialog.getOpenFileName(
|
|
438
|
+
self, "选择 Word 文件", initial, "Word 文档 (*.docx);;所有文件 (*.*)"
|
|
439
|
+
)
|
|
440
|
+
if path:
|
|
441
|
+
self.set_docx(Path(path))
|
|
442
|
+
self.append_log(f"已选择 Word 文件:{path}", "muted")
|
|
443
|
+
|
|
444
|
+
def pick_excel(self) -> None:
|
|
445
|
+
initial = self.excel_path or self.base_dir
|
|
446
|
+
path, _ = QFileDialog.getSaveFileName(
|
|
447
|
+
self,
|
|
448
|
+
"选择或指定工作簿",
|
|
449
|
+
str(initial.parent / (initial.name if self.excel_path else "表格数据.xlsx")),
|
|
450
|
+
"Excel 工作簿 (*.xlsx);;所有文件 (*.*)",
|
|
451
|
+
)
|
|
452
|
+
if path:
|
|
453
|
+
if not path.lower().endswith(".xlsx"):
|
|
454
|
+
path += ".xlsx"
|
|
455
|
+
self.set_excel(Path(path))
|
|
456
|
+
self.append_log(f"已指定工作簿:{path}", "muted")
|
|
457
|
+
|
|
458
|
+
def reset_excel(self) -> None:
|
|
459
|
+
self.excel_is_custom = False
|
|
460
|
+
self.set_excel_default()
|
|
461
|
+
|
|
462
|
+
def set_excel_default(self) -> None:
|
|
463
|
+
if self.docx_path is None:
|
|
464
|
+
self.excel_path = None
|
|
465
|
+
else:
|
|
466
|
+
self.excel_path = api.workbook_path(self.docx_path, None)
|
|
467
|
+
self.filebar.set_paths(self.docx_path, self.excel_path)
|
|
468
|
+
self.set_status("工作簿已恢复为默认命名:" + (self.excel_path.name if self.excel_path else "(无)"))
|
|
469
|
+
|
|
470
|
+
def open_folder(self) -> None:
|
|
471
|
+
target = self.docx_path.parent if self.docx_path else self.base_dir
|
|
472
|
+
try:
|
|
473
|
+
if sys.platform.startswith("win"):
|
|
474
|
+
os.startfile(str(target)) # type: ignore[attr-defined]
|
|
475
|
+
elif sys.platform == "darwin":
|
|
476
|
+
subprocess.Popen(["open", str(target)])
|
|
477
|
+
else:
|
|
478
|
+
subprocess.Popen(["xdg-open", str(target)])
|
|
479
|
+
except Exception as exc:
|
|
480
|
+
self.ctx.error("无法打开目录", f"{target}\n{exc}")
|
|
481
|
+
|
|
482
|
+
# -------------------------------------------------------------- 页面切换
|
|
483
|
+
|
|
484
|
+
def _select_page(self, key: str) -> None:
|
|
485
|
+
if key not in self._pages:
|
|
486
|
+
return
|
|
487
|
+
page = self._pages[key]
|
|
488
|
+
self.stack.setCurrentWidget(page)
|
|
489
|
+
self.nav.select(key)
|
|
490
|
+
if hasattr(page, "on_show"):
|
|
491
|
+
page.on_show()
|
|
492
|
+
QTimer.singleShot(0, page._fit_content_height)
|
|
493
|
+
self.set_status(f"当前页面:{getattr(page, 'title', key)}")
|
|
494
|
+
|
|
495
|
+
# -------------------------------------------------------- 日志与状态栏
|
|
496
|
+
|
|
497
|
+
@staticmethod
|
|
498
|
+
def _level_for(text: str) -> str:
|
|
499
|
+
stripped = text.strip()
|
|
500
|
+
if stripped.startswith("警告:"):
|
|
501
|
+
return "warn"
|
|
502
|
+
if stripped.startswith("错误:") or stripped.startswith("Traceback"):
|
|
503
|
+
return "error"
|
|
504
|
+
if any(mark in stripped for mark in ("已保存", "已写入", "已更新", "已创建", "完成")):
|
|
505
|
+
return "ok"
|
|
506
|
+
return "info"
|
|
507
|
+
|
|
508
|
+
def append_log(self, text: str, level: str | None = None) -> None:
|
|
509
|
+
self.logpane.append(text, level if level else self._level_for(text))
|
|
510
|
+
|
|
511
|
+
def set_status(self, text: str) -> None:
|
|
512
|
+
self._status_label.setText(text)
|
|
513
|
+
|
|
514
|
+
# ---------------------------------------------------------- 后台任务处理
|
|
515
|
+
|
|
516
|
+
def set_busy(self, busy: bool) -> None:
|
|
517
|
+
buttons = self.stack.findChildren(QPushButton) + self.filebar.findChildren(QPushButton)
|
|
518
|
+
if busy:
|
|
519
|
+
# 记住执行前就禁用的按钮,结束后不能一律启用
|
|
520
|
+
self._disabled_before_busy = {btn for btn in buttons if not btn.isEnabled()}
|
|
521
|
+
for widget in buttons:
|
|
522
|
+
widget.setEnabled(False)
|
|
523
|
+
self.stack.setEnabled(False)
|
|
524
|
+
self.filebar.setEnabled(False)
|
|
525
|
+
self.nav.setEnabled(False)
|
|
526
|
+
self.set_status("执行中…请稍候")
|
|
527
|
+
self._progress.setRange(0, 0)
|
|
528
|
+
else:
|
|
529
|
+
disabled = getattr(self, "_disabled_before_busy", set())
|
|
530
|
+
for widget in buttons:
|
|
531
|
+
widget.setEnabled(widget not in disabled)
|
|
532
|
+
self.stack.setEnabled(True)
|
|
533
|
+
self.filebar.setEnabled(True)
|
|
534
|
+
self.nav.setEnabled(True)
|
|
535
|
+
self._disabled_before_busy = set()
|
|
536
|
+
# 任务期间页面可能清空了清单,按当前数据重算按钮可用性
|
|
537
|
+
for page in self._pages.values():
|
|
538
|
+
refresh = getattr(page, "refresh_button_state", None)
|
|
539
|
+
if refresh is not None:
|
|
540
|
+
refresh()
|
|
541
|
+
self._progress.setRange(0, 100)
|
|
542
|
+
self._progress.setValue(0)
|
|
543
|
+
|
|
544
|
+
def _poll_queue(self) -> None:
|
|
545
|
+
for event, payload in self.runner.drain():
|
|
546
|
+
if event == "log":
|
|
547
|
+
self.append_log(str(payload))
|
|
548
|
+
elif event == "step":
|
|
549
|
+
self.append_log(f"▶ {payload} …", "step")
|
|
550
|
+
self.set_status(f"执行中:{payload}")
|
|
551
|
+
elif event == "done":
|
|
552
|
+
data = payload if isinstance(payload, dict) else {}
|
|
553
|
+
titles = "、".join(title for title, _ in data.get("steps", []))
|
|
554
|
+
elapsed = float(data.get("elapsed", 0.0))
|
|
555
|
+
self.append_log(f"✔ 完成:{titles}(耗时 {elapsed:.1f} 秒)", "ok")
|
|
556
|
+
self.set_status(f"最近一次:{titles} · 耗时 {elapsed:.1f} 秒")
|
|
557
|
+
self.set_busy(False)
|
|
558
|
+
callback = self._task_callback
|
|
559
|
+
self._task_callback = None
|
|
560
|
+
if callback is not None:
|
|
561
|
+
try:
|
|
562
|
+
callback(data)
|
|
563
|
+
except Exception as exc:
|
|
564
|
+
self.append_log(f"✘ 任务结果加载失败:{type(exc).__name__}: {exc}", "error")
|
|
565
|
+
self.ctx.error("任务结果加载失败", f"{type(exc).__name__}: {exc}")
|
|
566
|
+
else:
|
|
567
|
+
QTimer.singleShot(
|
|
568
|
+
200, lambda: self.ctx.info("执行完成", f"{titles}\n耗时:{elapsed:.1f} 秒\n\n详细过程见日志区。")
|
|
569
|
+
)
|
|
570
|
+
elif event == "error":
|
|
571
|
+
self._task_callback = None
|
|
572
|
+
data = payload if isinstance(payload, dict) else {}
|
|
573
|
+
title = str(data.get("title", "任务"))
|
|
574
|
+
message = str(data.get("message", "未知错误"))
|
|
575
|
+
elapsed = float(data.get("elapsed", 0.0))
|
|
576
|
+
self.append_log(f"✘ 「{title}」执行失败(耗时 {elapsed:.1f} 秒):{message}", "error")
|
|
577
|
+
self.set_status(f"失败:{title}")
|
|
578
|
+
self.set_busy(False)
|
|
579
|
+
QTimer.singleShot(200, lambda: self.ctx.error("执行失败", f"{title}\n\n{message}", log=False))
|
|
580
|
+
|
|
581
|
+
# ------------------------------------------------------------------ 退出
|
|
582
|
+
|
|
583
|
+
def closeEvent(self, event) -> None:
|
|
584
|
+
if self.runner.busy or not self.runner.queue.empty():
|
|
585
|
+
self.set_status("任务正在执行,暂时不能关闭")
|
|
586
|
+
event.ignore()
|
|
587
|
+
return
|
|
588
|
+
if self._appearance_listener_connected:
|
|
589
|
+
hints = QApplication.instance().styleHints()
|
|
590
|
+
try:
|
|
591
|
+
hints.colorSchemeChanged.disconnect(self._system_appearance_changed)
|
|
592
|
+
except (TypeError, RuntimeError):
|
|
593
|
+
pass
|
|
594
|
+
self._appearance_listener_connected = False
|
|
595
|
+
self._nav_animation.stop()
|
|
596
|
+
self._finish_nav_animation()
|
|
597
|
+
sizes = self.splitter.sizes()
|
|
598
|
+
values = {
|
|
599
|
+
"qt_size": [self.width(), self.height()],
|
|
600
|
+
"qt_pos": [self.x(), self.y()],
|
|
601
|
+
"log_visible": self.logpane.isVisible(),
|
|
602
|
+
"log_width": sizes[2] if sizes[2] >= 80 else int(self.settings.get("log_width") or DEFAULT_LOG_WIDTH),
|
|
603
|
+
"nav_collapsed": self.nav.collapsed,
|
|
604
|
+
"nav_width": sizes[0] if not self.nav.collapsed else int(self.settings.get("nav_width") or EXPANDED_WIDTH),
|
|
605
|
+
"last_docx": str(self.docx_path) if self.docx_path else "",
|
|
606
|
+
"last_excel": str(self.excel_path) if (self.excel_path and self.excel_is_custom) else "",
|
|
607
|
+
"nav_groups": self.nav.group_states(),
|
|
608
|
+
}
|
|
609
|
+
try:
|
|
610
|
+
store.save(values)
|
|
611
|
+
except Exception as exc:
|
|
612
|
+
self.append_log(f"✘ 退出时保存界面设置失败:{exc}", "error")
|
|
613
|
+
super().closeEvent(event)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""把随包说明文档转成可导航、可搜索的 HTML。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import html
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from . import theme
|
|
9
|
+
from .resources import manual_path
|
|
10
|
+
|
|
11
|
+
HEADING_RE = re.compile(r"^(?:[一二三四五六七八九十]+、)\s*(.+)$")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def load_manual_text() -> str:
|
|
15
|
+
return manual_path().read_text(encoding="utf-8-sig")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def parse_manual(text: str) -> list[tuple[str, list[str]]]:
|
|
19
|
+
sections: list[tuple[str, list[str]]] = []
|
|
20
|
+
title = "软件说明"
|
|
21
|
+
lines: list[str] = []
|
|
22
|
+
for raw in text.splitlines():
|
|
23
|
+
line = raw.rstrip()
|
|
24
|
+
stripped = line.strip()
|
|
25
|
+
if HEADING_RE.match(stripped):
|
|
26
|
+
if any(item.strip() for item in lines):
|
|
27
|
+
sections.append((title, lines))
|
|
28
|
+
title = stripped
|
|
29
|
+
lines = []
|
|
30
|
+
elif stripped and set(stripped) <= {"=", "-"}:
|
|
31
|
+
continue
|
|
32
|
+
else:
|
|
33
|
+
lines.append(line)
|
|
34
|
+
if any(item.strip() for item in lines):
|
|
35
|
+
sections.append((title, lines))
|
|
36
|
+
return sections or [("软件说明", text.splitlines())]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _lines_html(lines: list[str]) -> str:
|
|
40
|
+
result: list[str] = []
|
|
41
|
+
for raw in lines:
|
|
42
|
+
line = raw.rstrip()
|
|
43
|
+
stripped = line.strip()
|
|
44
|
+
if not stripped:
|
|
45
|
+
result.append("<div class='gap'> </div>")
|
|
46
|
+
elif stripped.startswith("·"):
|
|
47
|
+
result.append(f"<div class='bullet'>{html.escape(stripped)}</div>")
|
|
48
|
+
elif re.match(r"^\d+\)", stripped):
|
|
49
|
+
result.append(f"<div class='step'>{html.escape(stripped)}</div>")
|
|
50
|
+
else:
|
|
51
|
+
result.append(f"<div class='line'>{html.escape(stripped)}</div>")
|
|
52
|
+
return "".join(result)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def manual_html(sections: list[tuple[str, list[str]]]) -> str:
|
|
56
|
+
c = theme.colors()
|
|
57
|
+
parts = [
|
|
58
|
+
"<!doctype html><html><head><meta charset='utf-8'><style>",
|
|
59
|
+
f"body{{font-family:'{theme.font_family()}',sans-serif;background:{c['card']};color:{c['text']};font-size:14px;line-height:1.75;margin:18px 24px;}}",
|
|
60
|
+
f"h1{{font-size:24px;color:{c['primary']};margin:0 0 6px;}}h2{{font-size:18px;color:{c['primary']};margin:26px 0 10px;border-bottom:1px solid {c['border']};padding-bottom:6px;}}",
|
|
61
|
+
f".intro,.line,.step,.bullet{{margin:4px 0;}}.intro{{color:{c['muted']};}}.gap{{height:6px;}}",
|
|
62
|
+
".bullet{padding-left:16px;text-indent:-12px;}.step{padding-left:20px;text-indent:-16px;}",
|
|
63
|
+
"section{scroll-margin-top:12px;}</style></head><body>",
|
|
64
|
+
"<h1>审计工具箱使用说明</h1><p class='intro'>本说明书与软件一同分发,无需联网。左侧选择章节,右上角可搜索关键词。</p>",
|
|
65
|
+
]
|
|
66
|
+
for index, (title, lines) in enumerate(sections):
|
|
67
|
+
anchor = f"chapter-{index}"
|
|
68
|
+
parts.append(f"<section id='{anchor}'><h2>{html.escape(title)}</h2>{_lines_html(lines)}</section>")
|
|
69
|
+
parts.append("</body></html>")
|
|
70
|
+
return "".join(parts)
|