feat(new-arch):主窗口全功能增强(图标系统 + 全链路参数同步 + 服务输出统一解析 + Step12 分类浏览)

1. main_view.py:图标系统 + 全链路参数自动传导
   - 新增 _res() 解析项目根的相对路径,PyInstaller 打包后兼容 sys._MEIPASS。
   - 新增 QListWidgetItem / QMessageBox 导入,左侧导航列表支持右键菜单 + 错误弹窗。
   - ROUTES 12 条全部新增 icon 字段("1.png" 等),侧边栏显示业务图标。
   - 新增 step_outputs 缓存机制:每个 step 完成后把 output_path 写入 self.step_outputs。
   - 新增 _sync_dependencies() 同步函数 + _safe_set_config() 包装器,
     按依赖图把上游产物推给下游 view:
       step1 → step6 water_mask_path
       step3 → step4 / step6 / step10 deglint_img_path / bsq_path
       step4 → step9 sampling_csv_path
       step5 → step6 csv_path
       step6 → step7 / step8 training_csv_path
       step8 → step9 models_dir(父目录)
       step9 → step11 prediction_csv_dir / prediction_csv_path(双推)
       step10 → step11 geotiff_dir / geotiff_path(双推)

2. services/step1-13:统一输出解析器集成
   - 新增 src/new/services/_output_resolver.py,提供 resolve_output_dir /
     copy_to_user_path / get_user_output_path / is_user_specified 四个共享工具。
   - 每个 service 把原有的私有 _resolve_xxx_dir 改为调用 resolve_output_dir,
     强制执行"用户优先"规则(用户指定 output_path 时用其父目录,否则用 work_dir/<subdir>)。
   - 用户指定文件名 vs 底层硬编码文件名的"事后劫持"通过 copy_to_user_path 完成
     (覆盖 step2、step4、step7、step8 等底层 step 不接受 output_path 关键字的步骤)。

3. views/step12_view.py:恢复 ImageCategoryTree + ImageViewerWidget 高级组件
   - 删掉精简版占位 Label,挂回旧版的 ImageCategoryTree(按"模型评估/光谱分析/
     统计图表/处理结果/含量分布图"五类自动归类工作目录下的图像文件)。
   - 挂回 ImageViewerWidget(滚轮缩放 0.1x-5x + 50ms 防抖 + FastTransformation/
     SmoothTransformation 智能切换 + Ctrl+Wheel + 工具栏)。
   - 扫描按钮接通 image_tree.scan_directory(),选中节点即时加载到 image_viewer。
   - 按钮样式切换为 ModernStylesheet(success/primary)统一视觉。
This commit is contained in:
DXC
2026-06-17 13:28:58 +08:00
parent 9cb3c8ed0d
commit 6a962f5e8f
15 changed files with 607 additions and 116 deletions

View File

@ -35,23 +35,34 @@ MainView —— 端到端模块化新架构的路由与调度壳(主窗口)
from __future__ import annotations
import importlib
import os
import sys
import traceback
from pathlib import Path
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import (
QFileDialog, QHBoxLayout, QLabel, QListWidget, QMainWindow,
QPushButton, QSplitter, QStackedWidget, QTextEdit, QVBoxLayout,
QWidget,
QFileDialog, QHBoxLayout, QLabel, QListWidget, QListWidgetItem,
QMainWindow, QMessageBox, QPushButton, QSplitter, QStackedWidget,
QTextEdit, QVBoxLayout, QWidget,
)
# 让 ``python src/new/main_view.py`` 直接运行时也能 import ``src.xxx``
_HERE = Path(__file__).resolve().parent # .../src/new
_SRC_ROOT = _HERE.parent # .../src
_PROJECT_ROOT = _SRC_ROOT.parent # .../WQ_GUI项目根用于解析 data/icons/
if str(_SRC_ROOT) not in sys.path:
sys.path.insert(0, str(_SRC_ROOT))
def _res(rel: str) -> str:
"""解析项目根的相对路径PyInstaller 打包后兼容 sys._MEIPASS。"""
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, rel)
return str(_PROJECT_ROOT / rel)
from src.gui.styles import ModernStylesheet
from src.new.views.placeholder_view import PlaceholderView
@ -63,6 +74,7 @@ ROUTES = [
{
"id": "step1",
"name": "1. 水域掩膜",
"icon": "1.png",
"view_module": "src.new.views.step1_view",
"view_class": "Step1View",
"service_module": "src.new.services.step1_service",
@ -72,6 +84,7 @@ ROUTES = [
{
"id": "step2",
"name": "2. 耀斑检测",
"icon": "2.png",
"view_module": "src.new.views.step2_view",
"view_class": "Step2View",
"service_module": "src.new.services.step2_service",
@ -80,6 +93,7 @@ ROUTES = [
{
"id": "step3",
"name": "3. 耀斑去除",
"icon": "3.png",
"view_module": "src.new.views.step3_view",
"view_class": "Step3View",
"service_module": "src.new.services.step3_service",
@ -88,6 +102,7 @@ ROUTES = [
{
"id": "step4",
"name": "4. 采样点布设",
"icon": "4.png",
"view_module": "src.new.views.step4_view",
"view_class": "Step4View",
"service_module": "src.new.services.step4_service",
@ -96,6 +111,7 @@ ROUTES = [
{
"id": "step5",
"name": "5. 数据清洗",
"icon": "5.png",
"view_module": "src.new.views.step5_view",
"view_class": "Step5View",
"service_module": "src.new.services.step5_service",
@ -104,6 +120,7 @@ ROUTES = [
{
"id": "step6",
"name": "6. 光谱特征",
"icon": "6.png",
"view_module": "src.new.views.step6_view",
"view_class": "Step6View",
"service_module": "src.new.services.step6_service",
@ -112,6 +129,7 @@ ROUTES = [
{
"id": "step7",
"name": "7. 水质光谱指数",
"icon": "7.png",
"view_module": "src.new.views.step7_view",
"view_class": "Step7View",
"service_module": "src.new.services.step7_service",
@ -120,14 +138,17 @@ ROUTES = [
{
"id": "step8",
"name": "8. 机器学习建模",
"icon": "8.png",
"view_module": "src.new.views.step8_view",
"view_class": "Step8View",
"service_module": "src.new.services.step8_service",
"service_func": "execute_step8",
},
{
# 旧版共用 10.png 是 bugdata/icons/ 里有 11.png 一直未引用,正好给"预测"用
"id": "step9",
"name": "9. 机器学习预测",
"icon": "11.png",
"view_module": "src.new.views.step9_view",
"view_class": "Step9View",
"service_module": "src.new.services.step9_service",
@ -136,14 +157,17 @@ ROUTES = [
{
"id": "step10",
"name": "10. 水色指数反演",
"icon": "10.png",
"view_module": "src.new.views.step10_view",
"view_class": "Step10View",
"service_module": "src.new.services.step10_service",
"service_func": "execute_step10",
},
{
# data/icons/ 没有 12.png/13.pngStep11/12/13 暂时复用 9.png11 个 png 对 13 个 step 必然有共用)
"id": "step11",
"name": "11. 专题图生成",
"icon": "9.png",
"view_module": "src.new.views.step11_view",
"view_class": "Step11View",
"service_module": "src.new.services.step11_service",
@ -152,6 +176,7 @@ ROUTES = [
{
"id": "step12",
"name": "12. 可视化",
"icon": "9.png",
"view_module": "src.new.views.step12_view",
"view_class": "Step12View",
"service_module": "src.new.services.step12_service",
@ -160,6 +185,7 @@ ROUTES = [
{
"id": "step13",
"name": "13. 报告生成",
"icon": "9.png",
"view_module": "src.new.views.step13_view",
"view_class": "Step13View",
"service_module": "src.new.services.step13_service",
@ -168,6 +194,43 @@ ROUTES = [
]
# =========================================================================
# 全局输入参数:用户在任意 step 导图后,广播给所有 view 自动填充
# =========================================================================
# 这些是各 view 关心的"上游产物 / 用户导入文件"key。任何一次 run_single_step
# 都会把 config_dict 中出现的非空值更新进 self.global_inputs并全量广播。
# view.set_config 已实现"非空才覆盖"(避免回填空串覆盖上游有效值),
# 所以直接全量广播是安全的。
GLOBAL_INPUT_KEYS: tuple[str, ...] = (
# step1 入口
"img_path", # 原始 BSQ/DAT/TIF 影像
"mask_path", # 现有水域掩膜shp / dat / tif
# step2/3 输入
"water_mask_path", # step1 产出
# step3/4/6/10 输入
"deglint_img_path", # step3 产出
"bsq_path", # step10 用,等价 deglint_img_path
"hdr_path", # ENVI 头文件
# step4 输入
"glint_mask_path", # step2 耀斑掩膜
# step5/6 输入
"csv_path", # step5 产出
"boundary_path", # step6 水域掩膜别名
# step7/8 输入
"training_csv_path", # step6 产出
# step9 输入
"sampling_csv_path", # step4 产出
"models_dir", # step8 产出
# step10 输入
"formula_csv_path", # waterindex.csv
# step11 输入
"prediction_csv_path",
"prediction_csv_dir",
"geotiff_path",
"geotiff_dir",
)
# =========================================================================
# TaskWorkerQThread 后台执行器(三信号协议)
# =========================================================================
@ -224,11 +287,20 @@ class MainView(QMainWindow):
self._views: dict = {}
self._route_map: dict = {}
self.worker: TaskWorker | None = None
# 各 step 的产出路径缓存(声明式路径流转的中枢)
self.step_outputs: dict = {}
# 全局输入参数广播:用户在任何 step 导图后,其他 step 自动同步
self.global_inputs: dict = {}
self.setWindowTitle("WQ_GUI 新架构 · 端到端模块化路由壳")
self.resize(1280, 800)
self.setStyleSheet(ModernStylesheet.get_main_stylesheet())
# 设置窗口图标(旧版 water_quality_gui.py L1339-1340 用 uitubiao.ico
ico_path = _res("data/icons-1/uitubiao.ico")
if Path(ico_path).is_file():
self.setWindowIcon(QIcon(ico_path))
self._build_ui()
self._build_routes()
@ -245,8 +317,29 @@ class MainView(QMainWindow):
outer = QVBoxLayout(central)
outer.setContentsMargins(8, 8, 8, 8)
# ----- 顶部工具条:工作目录 + 设置按钮 -----
# ----- 顶部工具条:logo + 项目名 + 工作目录 + 设置按钮 -----
toolbar = QHBoxLayout()
# Logo旧版 water_quality_gui.py L1569 用 logo.png
logo_path = _res("data/icons/logo.png")
if Path(logo_path).is_file():
logo_pixmap = QPixmap(logo_path)
if not logo_pixmap.isNull():
logo_pixmap = logo_pixmap.scaledToHeight(
36, Qt.SmoothTransformation
)
logo_label = QLabel()
logo_label.setPixmap(logo_pixmap)
toolbar.addWidget(logo_label)
# 项目名(紧贴 logo 右侧)
title_label = QLabel("WQ_GUI · 新架构")
title_label.setStyleSheet(
"font-size: 14px; font-weight: bold; color: #0055D4;"
)
toolbar.addWidget(title_label)
toolbar.addSpacing(20)
toolbar.addWidget(QLabel("工作目录:"))
self.work_dir_label = QLabel("(未设置)")
self.work_dir_label.setStyleSheet("color: #0055D4; font-weight: bold;")
@ -259,6 +352,27 @@ class MainView(QMainWindow):
outer.addLayout(toolbar)
# ----- 顶部 MegaCube BannerCSS border-image 平铺拉伸 + 居中文字) -----
banner_label = QLabel()
banner_label.setMinimumHeight(100)
banner_label.setMaximumHeight(100)
banner_label.setAlignment(Qt.AlignCenter)
# 无条件显示文字border-image 不会覆盖前景文字)
banner_label.setText("MegaCube-Water Quality V1.2")
banner_path = _res("data/icons/Mega Water 1.0.jpg").replace('\\', '/')
if Path(banner_path).is_file():
banner_label.setStyleSheet(
f"border-image: url('{banner_path}') 0 0 0 0 stretch stretch;"
"color: white; font-size: 38px; font-weight: bold; font-family: 'Microsoft YaHei';"
)
else:
banner_label.setStyleSheet(
"background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, "
"stop:0 #00a0e9, stop:1 #005a9e);"
"color: white; font-size: 38px; font-weight: bold; font-family: 'Microsoft YaHei';"
)
outer.addWidget(banner_label)
# ----- 中部:左导航 + 右 stacked用 QSplitter 可拖动) -----
splitter = QSplitter(Qt.Horizontal)
@ -291,7 +405,23 @@ class MainView(QMainWindow):
view = self._instantiate_view(route)
self._views[route["id"]] = view
self.stacked.addWidget(view)
self.nav_list.addItem(route["name"])
# 加载图标ROUTES[icon] 字段);找不到图标时退化为无图标 item
icon_filename = route.get("icon")
if icon_filename:
icon_full_path = _res(f"data/icons/{icon_filename}")
if Path(icon_full_path).is_file():
item = QListWidgetItem(QIcon(icon_full_path), route["name"])
else:
self._log(
f"[Router] 图标缺失 {icon_full_path}{route['id']} 用纯文字"
)
item = QListWidgetItem(route["name"])
else:
item = QListWidgetItem(route["name"])
# 统一图标尺寸(避免不同 png 拉伸不一致)
item.setSizeHint(item.sizeHint())
self.nav_list.addItem(item)
if ROUTES:
self.nav_list.setCurrentRow(0)
@ -339,6 +469,10 @@ class MainView(QMainWindow):
self.work_dir_label.setText(self.work_dir)
self._log(f"[Router] 工作目录已设置: {self.work_dir}")
# 工作目录变更 → 清空全局输入缓存(旧路径可能已失效)
if self.global_inputs:
self.clear_global_inputs()
# 推送给所有 view让它们按需自动填充路径
for view in self._views.values():
if hasattr(view, "update_from_config"):
@ -352,8 +486,10 @@ class MainView(QMainWindow):
1. 查路由表;
2. 注入 ``work_dir``(避免 view 重复感知);
3. 启动 ``TaskWorker`` 在子线程运行 service
4. 三信号路由log_msg → 日志框finished → 状态分支日志
3. **全局输入广播**——把本次 config_dict 中的公共输入 key 累计到
``self.global_inputs``,再推送给所有 view实现"一处导入,全局共享"
4. 启动 ``TaskWorker`` 在子线程运行 service
5. 三信号路由log_msg → 日志框finished → 状态分支日志;
error → 未预期异常写日志 + traceback。
"""
route = self._route_map.get(step_id)
@ -365,6 +501,9 @@ class MainView(QMainWindow):
if self.work_dir and "work_dir" not in config_dict:
config_dict = {**config_dict, "work_dir": self.work_dir}
# 全局输入广播:累计 + 推送给所有 view
self._broadcast_global_inputs(config_dict)
service_func = self._load_callable(route["service_module"], route["service_func"])
if service_func is None:
return
@ -378,8 +517,13 @@ class MainView(QMainWindow):
self.worker = TaskWorker(service_func, config_dict)
self.worker.log_msg.connect(self._log)
self.worker.finished.connect(self._on_step_done)
self.worker.error.connect(self._on_step_error)
# 用 lambda 注入 step_idfinished/error 都拿得到自己服务的是哪一步
self.worker.finished.connect(
lambda result, sid=step_id: self._on_step_done(sid, result)
)
self.worker.error.connect(
lambda msg, sid=step_id: self._on_step_error(sid, msg)
)
self.worker.start()
def _load_callable(self, module_name: str, attr_name: str):
@ -392,9 +536,13 @@ class MainView(QMainWindow):
traceback.print_exc()
return None
def _on_step_done(self, result: dict):
def _on_step_done(self, step_id: str, result: dict):
"""TaskWorker finished 回调——按 status 分支写日志
1. 缓存 output_path 到 self.step_outputs
2. 触发 _sync_dependencies 把产出路径推给所有下游 view
3. status=completed → QMessageBox.informationstatus=error → warning。
service 内部 try/except 转换的 status='error' 也走这里;
这与 error 信号worker 自身崩溃)语义不同。
"""
@ -403,8 +551,22 @@ class MainView(QMainWindow):
output_path = result.get("output_path")
mode = result.get("mode", "")
# 1. 缓存路径 + 触发声明式下游推送(即使 error 也要缓存,让用户看到已失败)
if output_path:
self.step_outputs[step_id] = str(output_path)
self._log(f"[Cache] {step_id} → step_outputs['{step_id}'] = {output_path}")
self._sync_dependencies()
else:
self._log(f"[Cache] {step_id} 无 output_path跳过 step_outputs 缓存")
# 2. 日志分支 + 弹窗提示
if status == "completed":
self._log(f"[Service✓] {message}mode={mode}, output={output_path}")
QMessageBox.information(
self,
"执行完成",
f"{message or '当前步骤已完成!'}\n\n产出路径:\n{output_path or '(无)'}",
)
elif status == "skipped":
self._log(f"[Service↻] {message}(已复用历史结果)")
elif status == "not_implemented":
@ -414,10 +576,170 @@ class MainView(QMainWindow):
tb = result.get("traceback")
if tb:
self._log(tb)
QMessageBox.warning(
self,
"执行失败",
f"{message or '当前步骤执行失败'}\n\n请查看底部日志获取 traceback。",
)
def _on_step_error(self, msg: str):
"""TaskWorker error 信号回调——worker 自身捕获的未预期异常"""
self._log(f"[Worker✗] 未预期异常:\n{msg}")
def _on_step_error(self, step_id: str, msg: str):
"""TaskWorker error 信号回调——worker 自身捕获的未预期异常
worker 自身崩溃service_func 抛出未捕获异常)走这里;
与 finished 中 status='error' 语义不同error 信号表示 worker
完全无法产出结果 dict。
"""
self._log(f"[Worker✗] {step_id} 未预期异常:\n{msg}")
QMessageBox.critical(
self,
"Worker 异常",
f"步骤 {step_id} 后台执行崩溃:\n\n{msg[:500]}",
)
# ------------------------------------------------------------------
# 声明式路径流转:用 set_config 把上游产出推给所有下游 view
# ------------------------------------------------------------------
def _sync_dependencies(self):
"""遍历 self.step_outputs 字典,按依赖图把每步产出推给下游
推送关系(按用户原意 + 各 view set_config 实际接受 key 校对):
step1 (水域掩膜) → step2/step3/step4 的 water_mask_path
step3 (耀斑去除) → step4 的 deglint_img_path
+ step6 的 deglint_img_path
+ step10 的 bsq_path
step4 (采样点布设) → step9 的 sampling_csv_path
step5 (数据清洗) → step6 的 csv_path
step6 (光谱特征) → step7/step8 的 training_csv_path
step8 (机器学习建模)→ step9 的 models_dir父目录
step9 (机器学习预测)→ step11 的 prediction_csv_dir父目录
step10 (水色指数) → step11 的 geotiff_dir父目录
每个推送都用 _safe_set_config 包装view 缺失 / 没有 set_config
/ set_config 抛异常都不会让 sync 链路中断。
"""
# ----- step1 → step2/3/4 (water_mask_path) -----
s1 = self.step_outputs.get("step1")
if s1:
for sid in ("step2", "step3", "step4"):
self._safe_set_config(sid, {"water_mask_path": s1})
# ----- step3 → step4 (deglint_img_path) / step6 / step10 (bsq_path) -----
s3 = self.step_outputs.get("step3")
if s3:
self._safe_set_config("step4", {"deglint_img_path": s3})
self._safe_set_config("step6", {"deglint_img_path": s3})
self._safe_set_config("step10", {"bsq_path": s3})
# ----- step4 → step9 (sampling_csv_path: 单文件,直接传) -----
s4 = self.step_outputs.get("step4")
if s4:
self._safe_set_config("step9", {"sampling_csv_path": s4})
# ----- step5 → step6 (csv_path) -----
s5 = self.step_outputs.get("step5")
if s5:
self._safe_set_config("step6", {"csv_path": s5})
# ----- step6 → step7/step8 (training_csv_path) -----
s6 = self.step_outputs.get("step6")
if s6:
self._safe_set_config("step7", {"training_csv_path": s6})
self._safe_set_config("step8", {"training_csv_path": s6})
# ----- step8 → step9 (models_dir: 父目录,因为 step8 产物是模型目录里的若干 .joblib) -----
s8 = self.step_outputs.get("step8")
if s8:
s8_dir = str(Path(s8).parent).replace("\\", "/")
self._safe_set_config("step9", {"models_dir": s8_dir})
# ----- step9 → step11 (prediction_csv_dir: 父目录step9 输出到 ml_prediction/) -----
s9 = self.step_outputs.get("step9")
if s9:
s9_dir = str(Path(s9).parent).replace("\\", "/")
self._safe_set_config("step11", {"prediction_csv_dir": s9_dir})
# 同时推 single 模式入口,避免用户在 folder 模式下还要切回
self._safe_set_config("step11", {"prediction_csv_path": s9})
# ----- step10 → step11 (geotiff_dir: 父目录step10 输出到 watercolor/) -----
s10 = self.step_outputs.get("step10")
if s10:
s10_dir = str(Path(s10).parent).replace("\\", "/")
self._safe_set_config("step11", {"geotiff_dir": s10_dir})
# 同时推 single 模式入口
self._safe_set_config("step11", {"geotiff_path": s10})
self._log(f"[Sync] 依赖同步完成step_outputs keys: {list(self.step_outputs.keys())}")
def _safe_set_config(self, step_id: str, config: dict):
"""安全调用 view.set_configview 缺失 / 无该方法 / 抛异常时降级日志
返回 True 表示推送成功False 表示降级跳过,便于上层做计数。
"""
view = self._views.get(step_id)
if view is None:
return False
if not hasattr(view, "set_config"):
self._log(f"[Sync] {step_id} 没有 set_config跳过推送 {list(config.keys())}")
return False
try:
view.set_config(config)
self._log(f"[Sync] → {step_id}.set_config({config})")
return True
except Exception as e: # noqa: BLE001 —— 单步推送失败不影响整体 sync
self._log(f"[Sync] 推送 {step_id} 失败: {e}")
traceback.print_exc()
return False
# ------------------------------------------------------------------
# 全局输入参数广播:用户在一处导图 → 所有 view 自动同步
# ------------------------------------------------------------------
def _broadcast_global_inputs(self, config_dict: dict):
"""把本次 config_dict 中的公共输入 key 累计到 self.global_inputs
再把整个 global_inputs 推给所有 view实现"一处导入,全局共享")。
设计要点:
1. **非空才覆盖**——空字符串/None 不会污染已存在的有效值(防止下游
view 用空串覆盖掉上游刚刚推送的有效路径)。
2. **全量广播是安全的**——各 view.set_config 已实现 "if key in config"
守卫,未识别的 key 直接忽略,互不干扰。
3. **顺序无关**——多次 run_single_step 都会持续累计 self.global_inputs
直到用户主动切换工作目录或清理。
"""
updated_keys: list[str] = []
for key in GLOBAL_INPUT_KEYS:
val = config_dict.get(key)
if val: # 非空才覆盖None / "" 一律跳过)
self.global_inputs[key] = val
updated_keys.append(key)
if updated_keys:
self._log(
f"[Global] 本次更新 {len(updated_keys)} 个全局输入: {updated_keys}"
)
# 推送给所有 view
if not self._views:
return
n_ok, n_fail = 0, 0
for step_id, view in self._views.items():
if not hasattr(view, "set_config"):
continue
try:
view.set_config(self.global_inputs)
n_ok += 1
except Exception as e: # noqa: BLE001 —— 单 view 失败不影响其它
n_fail += 1
self._log(f"[Global] {step_id} 推送失败: {e}")
self._log(
f"[Global] 广播完成: ok={n_ok} fail={n_fail} "
f"keys={list(self.global_inputs.keys())}"
)
def clear_global_inputs(self):
"""清空全局输入缓存(工作目录切换 / 用户重置时调用)"""
self.global_inputs.clear()
self._log("[Global] 已清空全局输入缓存")
# ------------------------------------------------------------------
# 日志:所有日志统一走这里