2026-05-07 14:23:58 +08:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
|
|
|
|
|
ReportGenerationPanel - Word 分析报告生成面板
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QSettings
|
2026-05-07 14:23:58 +08:00
|
|
|
|
from PyQt5.QtWidgets import (
|
|
|
|
|
|
QWidget, QVBoxLayout, QHBoxLayout, QGroupBox, QFormLayout,
|
2026-06-08 14:58:16 +08:00
|
|
|
|
QLabel, QCheckBox, QPushButton, QLineEdit,
|
2026-05-07 14:23:58 +08:00
|
|
|
|
QMessageBox, QFileDialog,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from src.gui.styles import ModernStylesheet
|
2026-06-08 14:58:16 +08:00
|
|
|
|
from src.gui.dialogs import AISettingsDialog, AI_SETTINGS_ORG, AI_SETTINGS_APP
|
2026-05-07 14:23:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReportGenerateThread(QThread):
|
|
|
|
|
|
"""后台生成 Word 报告(避免阻塞 UI)。"""
|
|
|
|
|
|
finished_ok = pyqtSignal(str)
|
|
|
|
|
|
failed = pyqtSignal(str)
|
|
|
|
|
|
log_message = pyqtSignal(str, str)
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
def __init__(self, work_dir: str, output_dir: Optional[str], report_title: str, enable_ai: bool):
|
2026-05-07 14:23:58 +08:00
|
|
|
|
super().__init__()
|
|
|
|
|
|
self.work_dir = work_dir
|
|
|
|
|
|
self.output_dir = output_dir
|
|
|
|
|
|
self.report_title = report_title
|
2026-06-08 14:58:16 +08:00
|
|
|
|
self.enable_ai = enable_ai
|
2026-05-07 14:23:58 +08:00
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
|
try:
|
|
|
|
|
|
from src.postprocessing.report_word import WaterQualityReportGenerator, ReportGenerationConfig
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
# 唯一数据源:直接从 QSettings 读取 AI 配置
|
|
|
|
|
|
s = QSettings(AI_SETTINGS_ORG, AI_SETTINGS_APP)
|
|
|
|
|
|
provider = s.value("ai_provider", "minimax", type=str)
|
|
|
|
|
|
timeout = int(s.value("timeout_s", 120, type=int))
|
|
|
|
|
|
|
|
|
|
|
|
if provider == "ollama":
|
|
|
|
|
|
ai_cfg = ReportGenerationConfig(
|
|
|
|
|
|
ai_provider="ollama",
|
|
|
|
|
|
ollama_base_url=s.value("api_base_url", "", type=str) or None,
|
|
|
|
|
|
ollama_vision_model=s.value("vision_model", "", type=str) or None,
|
|
|
|
|
|
ollama_text_model=s.value("text_model", "", type=str) or None,
|
|
|
|
|
|
ollama_timeout_s=timeout,
|
|
|
|
|
|
enable_ai_analysis=self.enable_ai,
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
ai_cfg = ReportGenerationConfig(
|
|
|
|
|
|
ai_provider="minimax",
|
|
|
|
|
|
minimax_api_key=s.value("api_key", "", type=str) or "",
|
|
|
|
|
|
minimax_vision_model=s.value("vision_model", "", type=str) or None,
|
|
|
|
|
|
minimax_text_model=s.value("text_model", "", type=str) or None,
|
|
|
|
|
|
minimax_timeout_s=timeout,
|
|
|
|
|
|
enable_ai_analysis=self.enable_ai,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-05-07 14:23:58 +08:00
|
|
|
|
self.log_message.emit(
|
2026-06-08 14:58:16 +08:00
|
|
|
|
f"报告生成:工作目录={self.work_dir},AI={'开' if self.enable_ai else '关'},Provider={provider}",
|
2026-05-07 14:23:58 +08:00
|
|
|
|
"info",
|
|
|
|
|
|
)
|
|
|
|
|
|
gen = WaterQualityReportGenerator(
|
|
|
|
|
|
work_dir=self.work_dir,
|
|
|
|
|
|
output_dir=self.output_dir,
|
|
|
|
|
|
ai_config=ai_cfg,
|
|
|
|
|
|
)
|
|
|
|
|
|
out_path = gen.generate_report(
|
|
|
|
|
|
work_dir=self.work_dir,
|
|
|
|
|
|
report_title=self.report_title or "水质参数反演分析报告",
|
|
|
|
|
|
)
|
|
|
|
|
|
self.finished_ok.emit(str(out_path))
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
self.failed.emit(f"{e}\n{traceback.format_exc()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-11 14:56:33 +08:00
|
|
|
|
class Step12ReportPanel(QWidget):
|
|
|
|
|
|
"""步骤12:分析报告生成。AI 配置统一由 AISettingsDialog 管理,本面板不持有配置状态。"""
|
2026-05-07 14:23:58 +08:00
|
|
|
|
|
|
|
|
|
|
def __init__(self, main_window=None, parent=None):
|
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
|
self.main_window = main_window
|
|
|
|
|
|
self._report_thread = None
|
2026-06-08 14:58:16 +08:00
|
|
|
|
self._ai_label = None
|
2026-05-07 14:23:58 +08:00
|
|
|
|
self.init_ui()
|
|
|
|
|
|
|
|
|
|
|
|
def init_ui(self):
|
|
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
|
|
layout.setContentsMargins(10, 10, 10, 10)
|
|
|
|
|
|
layout.setSpacing(10)
|
|
|
|
|
|
|
|
|
|
|
|
intro = QLabel(
|
|
|
|
|
|
"根据工作目录下的可视化结果(14_visualization 等)生成 Word 分析报告。"
|
2026-06-08 14:58:16 +08:00
|
|
|
|
"需已存在可视化图表;AI 分析通过 Ollama 或 Minimax 调用云端/本地服务。"
|
2026-05-07 14:23:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
intro.setWordWrap(True)
|
|
|
|
|
|
intro.setStyleSheet(
|
|
|
|
|
|
f"color: {ModernStylesheet.COLORS.get('text_secondary', '#666')};"
|
|
|
|
|
|
)
|
|
|
|
|
|
layout.addWidget(intro)
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
# ── 路径 ──────────────────────────────────────────────────────────────
|
2026-05-07 14:23:58 +08:00
|
|
|
|
path_group = QGroupBox("路径")
|
|
|
|
|
|
path_form = QFormLayout()
|
|
|
|
|
|
|
|
|
|
|
|
wd_row = QHBoxLayout()
|
|
|
|
|
|
self.work_dir_edit = QLineEdit()
|
|
|
|
|
|
self.work_dir_edit.setPlaceholderText("选择流程工作目录(含 14_visualization)…")
|
|
|
|
|
|
wd_browse = QPushButton("浏览…")
|
|
|
|
|
|
wd_browse.clicked.connect(self.browse_work_dir)
|
|
|
|
|
|
sync_btn = QPushButton("同步主窗口工作目录")
|
|
|
|
|
|
sync_btn.clicked.connect(self.sync_work_dir_from_main)
|
|
|
|
|
|
wd_row.addWidget(self.work_dir_edit, 1)
|
|
|
|
|
|
wd_row.addWidget(wd_browse)
|
|
|
|
|
|
wd_row.addWidget(sync_btn)
|
|
|
|
|
|
path_form.addRow("工作目录:", wd_row)
|
|
|
|
|
|
|
|
|
|
|
|
out_row = QHBoxLayout()
|
|
|
|
|
|
self.output_dir_edit = QLineEdit()
|
|
|
|
|
|
self.output_dir_edit.setPlaceholderText("留空则保存到 工作目录/14_visualization")
|
|
|
|
|
|
out_browse = QPushButton("浏览…")
|
|
|
|
|
|
out_browse.clicked.connect(self.browse_output_dir)
|
|
|
|
|
|
out_row.addWidget(self.output_dir_edit, 1)
|
|
|
|
|
|
out_row.addWidget(out_browse)
|
|
|
|
|
|
path_form.addRow("报告输出目录:", out_row)
|
|
|
|
|
|
|
|
|
|
|
|
self.report_title_edit = QLineEdit()
|
|
|
|
|
|
self.report_title_edit.setText("水质参数反演分析报告")
|
|
|
|
|
|
path_form.addRow("报告标题:", self.report_title_edit)
|
|
|
|
|
|
|
|
|
|
|
|
path_group.setLayout(path_form)
|
|
|
|
|
|
layout.addWidget(path_group)
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
# ── AI 分析 ───────────────────────────────────────────────────────────
|
|
|
|
|
|
ai_group = QGroupBox("AI 分析")
|
|
|
|
|
|
ai_layout = QVBoxLayout()
|
2026-05-07 14:23:58 +08:00
|
|
|
|
|
|
|
|
|
|
self.enable_ai_cb = QCheckBox("启用 AI 图表解读与综合总结")
|
|
|
|
|
|
self.enable_ai_cb.setChecked(
|
|
|
|
|
|
os.environ.get("ENABLE_AI_ANALYSIS", "1") not in {"0", "false", "False"}
|
|
|
|
|
|
)
|
2026-06-08 14:58:16 +08:00
|
|
|
|
ai_layout.addWidget(self.enable_ai_cb)
|
|
|
|
|
|
|
|
|
|
|
|
# 只读提示行:当前引擎 + 配置按钮
|
|
|
|
|
|
ai_status_row = QHBoxLayout()
|
|
|
|
|
|
ai_status_row.addWidget(QLabel("当前 AI 引擎:"))
|
|
|
|
|
|
self._ai_label = QLabel()
|
|
|
|
|
|
self._ai_label.setStyleSheet("color: #0078d4; font-weight: bold;")
|
|
|
|
|
|
ai_status_row.addWidget(self._ai_label)
|
|
|
|
|
|
ai_status_row.addStretch(1)
|
|
|
|
|
|
open_settings_btn = QPushButton("高级配置...")
|
|
|
|
|
|
open_settings_btn.clicked.connect(self._open_ai_settings)
|
|
|
|
|
|
ai_status_row.addWidget(open_settings_btn)
|
|
|
|
|
|
ai_layout.addLayout(ai_status_row)
|
|
|
|
|
|
|
|
|
|
|
|
ai_group.setLayout(ai_layout)
|
2026-05-07 14:23:58 +08:00
|
|
|
|
layout.addWidget(ai_group)
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
# ── 按钮 ──────────────────────────────────────────────────────────────
|
2026-05-07 14:23:58 +08:00
|
|
|
|
btn_row = QHBoxLayout()
|
|
|
|
|
|
self.generate_btn = QPushButton("生成 Word 报告")
|
|
|
|
|
|
self.generate_btn.setStyleSheet(
|
|
|
|
|
|
ModernStylesheet.get_button_stylesheet("success")
|
|
|
|
|
|
)
|
|
|
|
|
|
self.generate_btn.clicked.connect(self.on_generate_clicked)
|
|
|
|
|
|
btn_row.addWidget(self.generate_btn)
|
|
|
|
|
|
btn_row.addStretch()
|
|
|
|
|
|
layout.addLayout(btn_row)
|
|
|
|
|
|
|
|
|
|
|
|
layout.addStretch()
|
|
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
# 刷新引擎提示文字
|
|
|
|
|
|
self._refresh_ai_label()
|
|
|
|
|
|
|
|
|
|
|
|
def _refresh_ai_label(self):
|
|
|
|
|
|
"""从 QSettings 读取当前 Provider 并更新只读标签。"""
|
|
|
|
|
|
s = QSettings(AI_SETTINGS_ORG, AI_SETTINGS_APP)
|
|
|
|
|
|
provider = s.value("ai_provider", "minimax", type=str)
|
|
|
|
|
|
label_map = {"ollama": "Ollama (本地)", "minimax": "Minimax (云端)"}
|
|
|
|
|
|
self._ai_label.setText(label_map.get(provider, provider))
|
2026-05-07 14:23:58 +08:00
|
|
|
|
|
2026-06-08 14:58:16 +08:00
|
|
|
|
def _open_ai_settings(self):
|
|
|
|
|
|
"""弹出全局 AI 设置对话框,保存后刷新提示标签。"""
|
|
|
|
|
|
dlg = AISettingsDialog(self)
|
|
|
|
|
|
if dlg.exec_() == dlg.Accepted:
|
|
|
|
|
|
self._refresh_ai_label()
|
2026-05-07 14:23:58 +08:00
|
|
|
|
|
2026-05-07 16:49:24 +08:00
|
|
|
|
def _get_default_work_dir(self):
|
|
|
|
|
|
"""获取 work_dir,优先用主窗口缓存的 work_dir"""
|
|
|
|
|
|
if self.main_window and hasattr(self.main_window, 'work_dir') and self.main_window.work_dir:
|
|
|
|
|
|
return str(self.main_window.work_dir)
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
2026-05-07 14:23:58 +08:00
|
|
|
|
def browse_work_dir(self):
|
2026-05-07 16:49:24 +08:00
|
|
|
|
default = self._get_default_work_dir()
|
|
|
|
|
|
d = QFileDialog.getExistingDirectory(self, "选择工作目录", default)
|
2026-05-07 14:23:58 +08:00
|
|
|
|
if d:
|
|
|
|
|
|
self.work_dir_edit.setText(d)
|
|
|
|
|
|
|
|
|
|
|
|
def browse_output_dir(self):
|
2026-05-07 16:49:24 +08:00
|
|
|
|
default = self._get_default_work_dir()
|
|
|
|
|
|
if default:
|
|
|
|
|
|
default = os.path.join(default, "14_visualization")
|
|
|
|
|
|
d = QFileDialog.getExistingDirectory(self, "选择报告输出目录", default)
|
2026-05-07 14:23:58 +08:00
|
|
|
|
if d:
|
|
|
|
|
|
self.output_dir_edit.setText(d)
|
|
|
|
|
|
|
|
|
|
|
|
def sync_work_dir_from_main(self):
|
|
|
|
|
|
mw = self.main_window
|
|
|
|
|
|
if mw is not None and getattr(mw, "work_dir", None):
|
|
|
|
|
|
self.work_dir_edit.setText(str(mw.work_dir))
|
|
|
|
|
|
else:
|
|
|
|
|
|
QMessageBox.information(self, "提示", "主窗口尚未设置工作目录。")
|
|
|
|
|
|
|
|
|
|
|
|
def set_work_dir(self, work_dir):
|
|
|
|
|
|
if work_dir:
|
|
|
|
|
|
self.work_dir_edit.setText(str(work_dir))
|
|
|
|
|
|
|
|
|
|
|
|
def get_config(self):
|
2026-06-08 14:58:16 +08:00
|
|
|
|
"""返回路径和标题配置(AI 配置不由本面板持有)。"""
|
2026-05-07 14:23:58 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"work_dir": self.work_dir_edit.text().strip() or None,
|
|
|
|
|
|
"output_dir": self.output_dir_edit.text().strip() or None,
|
|
|
|
|
|
"report_title": self.report_title_edit.text().strip() or "水质参数反演分析报告",
|
|
|
|
|
|
"enable_ai_analysis": self.enable_ai_cb.isChecked(),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def set_config(self, config):
|
|
|
|
|
|
if not config:
|
|
|
|
|
|
return
|
|
|
|
|
|
if config.get("work_dir"):
|
|
|
|
|
|
self.work_dir_edit.setText(str(config["work_dir"]))
|
|
|
|
|
|
if "output_dir" in config:
|
|
|
|
|
|
self.output_dir_edit.setText(str(config["output_dir"] or ""))
|
|
|
|
|
|
if config.get("report_title"):
|
|
|
|
|
|
self.report_title_edit.setText(str(config["report_title"]))
|
|
|
|
|
|
if "enable_ai_analysis" in config:
|
|
|
|
|
|
self.enable_ai_cb.setChecked(bool(config["enable_ai_analysis"]))
|
|
|
|
|
|
|
|
|
|
|
|
def on_generate_clicked(self):
|
|
|
|
|
|
wd = self.work_dir_edit.text().strip()
|
|
|
|
|
|
if not wd or not os.path.isdir(wd):
|
|
|
|
|
|
QMessageBox.warning(self, "提示", "请选择有效的工作目录。")
|
|
|
|
|
|
return
|
|
|
|
|
|
viz = Path(wd) / "14_visualization"
|
|
|
|
|
|
if not viz.is_dir():
|
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
|
self,
|
|
|
|
|
|
"提示",
|
|
|
|
|
|
f"未找到可视化目录:\n{viz}\n请先完成流程或生成可视化。",
|
|
|
|
|
|
)
|
|
|
|
|
|
return
|
|
|
|
|
|
if self._report_thread and self._report_thread.isRunning():
|
|
|
|
|
|
QMessageBox.information(self, "提示", "报告正在生成中,请稍候。")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
out = self.output_dir_edit.text().strip() or None
|
|
|
|
|
|
title = self.report_title_edit.text().strip() or "水质参数反演分析报告"
|
2026-06-08 14:58:16 +08:00
|
|
|
|
enable_ai = self.enable_ai_cb.isChecked()
|
|
|
|
|
|
|
2026-05-07 14:23:58 +08:00
|
|
|
|
self.generate_btn.setEnabled(False)
|
2026-06-08 14:58:16 +08:00
|
|
|
|
self._report_thread = ReportGenerateThread(wd, out, title, enable_ai)
|
2026-05-07 14:23:58 +08:00
|
|
|
|
self._report_thread.log_message.connect(self._forward_log, Qt.QueuedConnection)
|
|
|
|
|
|
self._report_thread.finished_ok.connect(self._on_report_ok, Qt.QueuedConnection)
|
|
|
|
|
|
self._report_thread.failed.connect(self._on_report_fail, Qt.QueuedConnection)
|
|
|
|
|
|
self._report_thread.finished.connect(
|
|
|
|
|
|
lambda: self.generate_btn.setEnabled(True), Qt.QueuedConnection
|
|
|
|
|
|
)
|
|
|
|
|
|
self._report_thread.start()
|
|
|
|
|
|
self._forward_log("已开始生成 Word 报告…", "info")
|
|
|
|
|
|
|
|
|
|
|
|
def _forward_log(self, msg: str, level: str):
|
|
|
|
|
|
mw = self.main_window
|
|
|
|
|
|
if mw is not None and hasattr(mw, "log_message"):
|
|
|
|
|
|
mw.log_message(msg, level)
|
|
|
|
|
|
else:
|
|
|
|
|
|
print(f"[{level}] {msg}")
|
|
|
|
|
|
|
|
|
|
|
|
def _on_report_ok(self, path: str):
|
|
|
|
|
|
QMessageBox.information(self, "完成", f"报告已生成:\n{path}")
|
|
|
|
|
|
self._forward_log(f"Word 报告已保存: {path}", "info")
|
|
|
|
|
|
|
|
|
|
|
|
def _on_report_fail(self, err: str):
|
|
|
|
|
|
QMessageBox.critical(self, "失败", f"报告生成失败:\n{err[:800]}")
|
2026-06-08 14:58:16 +08:00
|
|
|
|
self._forward_log(err, "error")
|