继续优化UI加载速度

This commit is contained in:
xinzhu.yin
2026-06-22 10:52:51 +08:00
parent 92cba114a9
commit 2d8d4119e3
7 changed files with 307 additions and 72 deletions

View File

@@ -25,7 +25,138 @@ def _result_bg_color() -> str:
return "#FFFFFF"
def apply_result_chart_theme(self: "PQAutomationApp"):
def _refresh_ax_theme_in_place(ax, palette) -> None:
"""就地更新已有坐标轴的主题色,无需清空重绘。"""
ax.set_facecolor(palette["card_bg"])
for spine in ax.spines.values():
spine.set_color(palette["border"])
ax.tick_params(axis="both", colors=palette["fg"])
try:
ax.xaxis.label.set_color(palette["fg"])
ax.yaxis.label.set_color(palette["fg"])
except Exception:
pass
try:
if ax.title.get_text():
ax.title.set_color(palette["fg"])
except Exception:
pass
for artist in getattr(ax, "texts", []):
try:
artist.set_color(palette["fg"])
except Exception:
pass
for line in ax.get_lines():
# 保留曲线本身颜色,仅刷新网格等辅助线
pass
for gridline in ax.get_xgridlines() + ax.get_ygridlines():
try:
gridline.set_color(palette["border"])
gridline.set_alpha(0.35)
except Exception:
pass
_CHART_THEME_SPECS = {
"gamut": ("gamut_chart_frame", "gamut_fig", "gamut_canvas"),
"gamma": ("gamma_chart_frame", "gamma_fig", "gamma_canvas"),
"eotf": ("eotf_chart_frame", "eotf_fig", "eotf_canvas"),
"cct": ("cct_chart_frame", "cct_fig", "cct_canvas"),
"contrast": ("contrast_chart_frame", "contrast_fig", "contrast_canvas"),
"accuracy": ("accuracy_chart_frame", "accuracy_fig", "accuracy_canvas"),
}
def _get_active_chart_key(self: "PQAutomationApp") -> str | None:
"""返回当前选中的结果图 Tab 对应的 chart key。"""
notebook = getattr(self, "chart_notebook", None)
if notebook is None:
return None
try:
selected = notebook.select()
if not selected:
return "gamut"
for key, (frame_attr, _, _) in _CHART_THEME_SPECS.items():
frame = getattr(self, frame_attr, None)
if frame is not None and str(frame) == selected:
return key
except Exception:
pass
return "gamut"
def _apply_fig_theme_colors(fig, canvas, palette, bg) -> None:
"""更新 Figure/Axes 主题色,不触发绘制。"""
fig.patch.set_facecolor(bg)
suptitle = getattr(fig, "_suptitle", None)
if suptitle is not None:
try:
suptitle.set_color(palette["fg"])
except Exception:
pass
for ax in fig.axes:
_refresh_ax_theme_in_place(ax, palette)
if canvas is not None:
try:
widget = canvas.get_tk_widget()
widget.configure(bg=bg, highlightthickness=0)
except Exception:
pass
def flush_chart_theme_if_dirty(self: "PQAutomationApp", chart_key: str | None = None) -> None:
"""绘制主题已更新但尚未 draw 的图表(通常在切换到该 Tab 时调用)。"""
dirty = getattr(self, "_charts_theme_dirty", None)
if not dirty:
return
keys = [chart_key] if chart_key and chart_key in dirty else list(dirty)
for key in keys:
spec = _CHART_THEME_SPECS.get(key)
if spec is None:
continue
canvas = getattr(self, spec[2], None)
if canvas is None:
continue
try:
canvas.draw()
except Exception:
pass
dirty.discard(key)
def refresh_result_charts_theme(
self: "PQAutomationApp", *, sync_draw: bool = True, visible_only: bool = True
) -> None:
"""就地刷新结果区 Matplotlib 图表主题色;默认仅绘制当前可见 Tab。"""
palette = get_theme_palette()
bg = _result_bg_color()
active_key = _get_active_chart_key(self) if visible_only else None
dirty = getattr(self, "_charts_theme_dirty", None)
if dirty is None:
dirty = set()
self._charts_theme_dirty = dirty
for key, (_, fig_attr, canvas_attr) in _CHART_THEME_SPECS.items():
fig = getattr(self, fig_attr, None)
canvas = getattr(self, canvas_attr, None)
if fig is None or canvas is None:
continue
_apply_fig_theme_colors(fig, canvas, palette, bg)
should_draw = sync_draw and ((not visible_only) or key == active_key)
if should_draw:
try:
canvas.draw()
except Exception:
pass
dirty.discard(key)
else:
dirty.add(key)
def apply_result_chart_theme(self: "PQAutomationApp", *, sync_draw: bool = False):
"""统一刷新结果图画布背景,使其跟随浅/深色主题。"""
bg = _result_bg_color()
@@ -50,7 +181,10 @@ def apply_result_chart_theme(self: "PQAutomationApp"):
except Exception:
pass
try:
canvas.draw_idle()
if sync_draw:
canvas.draw()
else:
canvas.draw_idle()
except Exception:
pass
@@ -1111,6 +1245,9 @@ def on_chart_tab_changed(self: "PQAutomationApp", event):
if not selected_tab:
return
self._last_tab_index = self.chart_notebook.index(selected_tab)
active_key = _get_active_chart_key(self)
if active_key:
flush_chart_theme_if_dirty(self, active_key)
except Exception as e:
self.log_gui.log(f"Tab切换事件处理失败: {str(e)}", level="error")
@@ -1129,6 +1266,8 @@ class ChartFrameMixin:
init_contrast_chart = init_contrast_chart
init_accuracy_chart = init_accuracy_chart
apply_result_chart_theme = apply_result_chart_theme
refresh_result_charts_theme = refresh_result_charts_theme
flush_chart_theme_if_dirty = flush_chart_theme_if_dirty
_init_accuracy_result_table = _init_accuracy_result_table
clear_accuracy_result_table = clear_accuracy_result_table
update_accuracy_result_table = update_accuracy_result_table