2026-05-13 17:17:13 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import copy
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
from app.data_range_converter import convert_pattern_params
|
2026-05-19 11:50:53 +08:00
|
|
|
from app.pq.pq_config import get_pattern
|
2026-05-13 17:17:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class PatternSession:
|
|
|
|
|
mode: str
|
|
|
|
|
test_type: str
|
|
|
|
|
active_config: object
|
|
|
|
|
pattern_params: list[list[int]]
|
|
|
|
|
total_patterns: int
|
|
|
|
|
display_names: list[str]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PatternService:
|
|
|
|
|
def __init__(self, app):
|
|
|
|
|
self.app = app
|
|
|
|
|
|
2026-06-02 17:34:46 +08:00
|
|
|
def _build_apply_config_error(self, test_type):
|
|
|
|
|
timing = self.app.config.current_test_types.get(test_type, {}).get("timing", "-")
|
|
|
|
|
detail = ""
|
|
|
|
|
try:
|
|
|
|
|
ctrl = getattr(self.app.signal_service.device, "raw_controller", None)
|
|
|
|
|
if ctrl is not None:
|
|
|
|
|
d = getattr(ctrl, "last_error", None)
|
|
|
|
|
if d:
|
|
|
|
|
detail = f", detail={d}"
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
return f"UCD profile apply_config failed for {test_type}, timing={timing}{detail}"
|
|
|
|
|
|
2026-05-13 17:17:13 +08:00
|
|
|
def prepare_session(self, mode, *, test_type=None, log_details=False):
|
|
|
|
|
test_type = test_type or self.app.config.current_test_type
|
2026-06-02 17:34:46 +08:00
|
|
|
if hasattr(self.app.config, "set_current_test_type"):
|
|
|
|
|
self.app.config.set_current_test_type(test_type)
|
2026-05-13 17:17:13 +08:00
|
|
|
if not self.app.config.set_current_pattern(mode):
|
|
|
|
|
raise ValueError(f"未知的图案模式: {mode}")
|
|
|
|
|
|
|
|
|
|
active_config = self.app.config
|
|
|
|
|
source_params = self._get_source_pattern_params(mode)
|
|
|
|
|
|
|
|
|
|
if test_type == "screen_module":
|
2026-05-28 10:20:17 +08:00
|
|
|
screen_cfg = self.app.config.current_test_types.get("screen_module", {})
|
|
|
|
|
color_space = (
|
|
|
|
|
self.app.screen_module_color_space_var.get()
|
|
|
|
|
if hasattr(self.app, "screen_module_color_space_var")
|
|
|
|
|
else screen_cfg.get("colorimetry", "sRGB")
|
|
|
|
|
)
|
|
|
|
|
data_range = (
|
|
|
|
|
self.app.screen_module_data_range_var.get()
|
|
|
|
|
if hasattr(self.app, "screen_module_data_range_var")
|
|
|
|
|
else screen_cfg.get("data_range", "Full")
|
|
|
|
|
)
|
|
|
|
|
bit_depth = (
|
|
|
|
|
self.app.screen_module_bit_depth_var.get()
|
|
|
|
|
if hasattr(self.app, "screen_module_bit_depth_var")
|
|
|
|
|
else f"{int(screen_cfg.get('bpc', 8))}bit"
|
|
|
|
|
)
|
|
|
|
|
output_format = (
|
|
|
|
|
self.app.screen_module_output_format_var.get()
|
|
|
|
|
if hasattr(self.app, "screen_module_output_format_var")
|
|
|
|
|
else screen_cfg.get("color_format", "RGB")
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-13 17:17:13 +08:00
|
|
|
if log_details:
|
|
|
|
|
self._log("=" * 50, "separator")
|
|
|
|
|
self._log("设置屏模组信号格式:", "info")
|
|
|
|
|
self._log("=" * 50, "separator")
|
2026-05-28 10:20:17 +08:00
|
|
|
for label, value in [
|
|
|
|
|
("色彩空间", color_space),
|
|
|
|
|
("色彩格式", output_format),
|
|
|
|
|
("数据范围", data_range),
|
|
|
|
|
("编码位深", bit_depth),
|
|
|
|
|
("Timing", self.app.config.current_test_types[test_type]["timing"]),
|
|
|
|
|
]:
|
|
|
|
|
self._log(f" {label}: {value}", "info")
|
2026-06-02 17:34:46 +08:00
|
|
|
if not self.app.signal_service.apply_config(active_config):
|
|
|
|
|
raise RuntimeError(self._build_apply_config_error(test_type))
|
2026-05-28 10:20:17 +08:00
|
|
|
success = self.app.signal_service.update_signal_format(
|
|
|
|
|
color_space=color_space,
|
|
|
|
|
data_range=data_range,
|
|
|
|
|
bit_depth=bit_depth,
|
|
|
|
|
output_format=output_format,
|
|
|
|
|
)
|
|
|
|
|
if log_details:
|
2026-05-13 17:17:13 +08:00
|
|
|
self._log(
|
2026-05-28 10:20:17 +08:00
|
|
|
f"屏模组信号格式设置{'成功' if success else '失败'}",
|
|
|
|
|
"success" if success else "error",
|
2026-05-13 17:17:13 +08:00
|
|
|
)
|
2026-05-22 11:31:36 +08:00
|
|
|
|
2026-05-13 17:17:13 +08:00
|
|
|
elif test_type == "sdr_movie":
|
2026-05-22 11:31:36 +08:00
|
|
|
data_range = self.app.sdr_data_range_var.get()
|
|
|
|
|
if log_details:
|
|
|
|
|
self._log("=" * 50, "separator")
|
|
|
|
|
self._log("设置 SDR 信号格式:", "info")
|
|
|
|
|
self._log("=" * 50, "separator")
|
|
|
|
|
for label, value in [
|
2026-05-13 17:17:13 +08:00
|
|
|
("色彩空间", self.app.sdr_color_space_var.get()),
|
2026-05-22 11:31:36 +08:00
|
|
|
("色彩格式", self.app.sdr_output_format_var.get()),
|
|
|
|
|
("Gamma", self.app.sdr_gamma_type_var.get()),
|
|
|
|
|
("数据范围", data_range),
|
2026-05-13 17:17:13 +08:00
|
|
|
("编码位深", self.app.sdr_bit_depth_var.get()),
|
2026-05-22 11:31:36 +08:00
|
|
|
]:
|
|
|
|
|
self._log(f" {label}: {value}", "info")
|
|
|
|
|
converted_params = convert_pattern_params(
|
|
|
|
|
source_params, data_range=data_range, verbose=False
|
|
|
|
|
)
|
|
|
|
|
active_config = self.app.config.get_temp_config_with_converted_params(
|
|
|
|
|
mode=mode, converted_params=converted_params
|
|
|
|
|
)
|
2026-06-02 17:34:46 +08:00
|
|
|
if hasattr(active_config, "set_current_test_type"):
|
|
|
|
|
active_config.set_current_test_type(test_type)
|
|
|
|
|
if not self.app.signal_service.apply_config(active_config):
|
|
|
|
|
raise RuntimeError(self._build_apply_config_error(test_type))
|
2026-05-24 11:02:37 +08:00
|
|
|
success = self.app.signal_service.update_signal_format(
|
2026-05-22 11:31:36 +08:00
|
|
|
color_space=self.app.sdr_color_space_var.get(),
|
|
|
|
|
data_range=data_range,
|
|
|
|
|
bit_depth=self.app.sdr_bit_depth_var.get(),
|
2026-05-24 11:02:37 +08:00
|
|
|
output_format=self.app.sdr_output_format_var.get(),
|
2026-05-13 17:17:13 +08:00
|
|
|
)
|
2026-05-22 11:31:36 +08:00
|
|
|
if log_details:
|
|
|
|
|
self._log(f"SDR 信号格式设置{'成功' if success else '失败'}", "success" if success else "error")
|
|
|
|
|
self._log(f"图案参数已设置,共 {len(converted_params)} 个图案", "success")
|
|
|
|
|
|
2026-05-13 17:17:13 +08:00
|
|
|
elif test_type == "hdr_movie":
|
2026-05-22 11:31:36 +08:00
|
|
|
data_range = self.app.hdr_data_range_var.get()
|
|
|
|
|
if log_details:
|
|
|
|
|
self._log("=" * 50, "separator")
|
|
|
|
|
self._log("设置 HDR 信号格式:", "info")
|
|
|
|
|
self._log("=" * 50, "separator")
|
|
|
|
|
for label, value in [
|
2026-05-13 17:17:13 +08:00
|
|
|
("色彩空间", self.app.hdr_color_space_var.get()),
|
2026-05-22 11:31:36 +08:00
|
|
|
("色彩格式", self.app.hdr_output_format_var.get()),
|
|
|
|
|
("数据范围", data_range),
|
2026-05-13 17:17:13 +08:00
|
|
|
("编码位深", self.app.hdr_bit_depth_var.get()),
|
2026-05-22 11:31:36 +08:00
|
|
|
("MaxCLL", self.app.hdr_maxcll_var.get()),
|
2026-05-13 17:17:13 +08:00
|
|
|
("MaxFALL", self.app.hdr_maxfall_var.get()),
|
2026-05-22 11:31:36 +08:00
|
|
|
]:
|
|
|
|
|
self._log(f" {label}: {value}", "info")
|
|
|
|
|
converted_params = convert_pattern_params(
|
|
|
|
|
source_params, data_range=data_range, verbose=False
|
2026-05-13 17:17:13 +08:00
|
|
|
)
|
2026-05-22 11:31:36 +08:00
|
|
|
active_config = self.app.config.get_temp_config_with_converted_params(
|
|
|
|
|
mode=mode, converted_params=converted_params
|
|
|
|
|
)
|
2026-06-02 17:34:46 +08:00
|
|
|
if hasattr(active_config, "set_current_test_type"):
|
|
|
|
|
active_config.set_current_test_type(test_type)
|
|
|
|
|
if not self.app.signal_service.apply_config(active_config):
|
|
|
|
|
raise RuntimeError(self._build_apply_config_error(test_type))
|
2026-05-24 11:02:37 +08:00
|
|
|
success = self.app.signal_service.update_signal_format(
|
2026-05-22 11:31:36 +08:00
|
|
|
color_space=self.app.hdr_color_space_var.get(),
|
|
|
|
|
data_range=data_range,
|
|
|
|
|
bit_depth=self.app.hdr_bit_depth_var.get(),
|
2026-05-24 11:02:37 +08:00
|
|
|
output_format=self.app.hdr_output_format_var.get(),
|
2026-05-22 11:31:36 +08:00
|
|
|
max_cll=self.app.hdr_maxcll_var.get(),
|
|
|
|
|
max_fall=self.app.hdr_maxfall_var.get(),
|
|
|
|
|
)
|
|
|
|
|
if log_details:
|
|
|
|
|
self._log(f"HDR 信号格式设置{'成功' if success else '失败'}", "success" if success else "error")
|
|
|
|
|
self._log(f"图案参数已设置,共 {len(converted_params)} 个图案", "success")
|
|
|
|
|
|
2026-05-13 17:17:13 +08:00
|
|
|
else:
|
|
|
|
|
raise ValueError(f"不支持的测试类型: {test_type}")
|
|
|
|
|
|
|
|
|
|
pattern_params = copy.deepcopy(active_config.current_pattern["pattern_params"])
|
|
|
|
|
return PatternSession(
|
|
|
|
|
mode=mode,
|
|
|
|
|
test_type=test_type,
|
|
|
|
|
active_config=active_config,
|
|
|
|
|
pattern_params=pattern_params,
|
|
|
|
|
total_patterns=len(pattern_params),
|
|
|
|
|
display_names=self._get_display_names(mode, len(pattern_params)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def send_session_pattern(self, session, index):
|
|
|
|
|
if index < 0 or index >= session.total_patterns:
|
|
|
|
|
raise IndexError(f"pattern 索引越界: {index}")
|
|
|
|
|
|
|
|
|
|
pattern_param = session.pattern_params[index]
|
2026-05-24 11:21:30 +08:00
|
|
|
if not self.app.signal_service.send_pattern_params(pattern_param):
|
2026-05-13 17:17:13 +08:00
|
|
|
raise RuntimeError(f"发送 pattern 失败: {index}")
|
|
|
|
|
return pattern_param
|
|
|
|
|
|
|
|
|
|
def send_rgb(self, rgb, *, session=None, test_type=None):
|
|
|
|
|
active_session = session or self.prepare_session(
|
|
|
|
|
"rgb",
|
|
|
|
|
test_type=test_type,
|
|
|
|
|
log_details=False,
|
|
|
|
|
)
|
|
|
|
|
converted_rgb = self._convert_rgb_for_test_type(rgb, active_session.test_type)
|
2026-05-24 11:02:37 +08:00
|
|
|
self.app.signal_service.send_solid_rgb(converted_rgb)
|
2026-05-13 17:17:13 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _get_source_pattern_params(self, mode):
|
2026-05-19 11:50:53 +08:00
|
|
|
return copy.deepcopy(get_pattern(mode)["pattern_params"])
|
2026-05-13 17:17:13 +08:00
|
|
|
|
|
|
|
|
def _get_display_names(self, mode, total_patterns):
|
|
|
|
|
if mode == "accuracy":
|
|
|
|
|
return self.app.config.get_accuracy_color_names()
|
|
|
|
|
if mode == "custom" and hasattr(self.app.config, "get_temp_pattern_names"):
|
|
|
|
|
return self.app.config.get_temp_pattern_names()
|
|
|
|
|
return [f"P {index + 1}" for index in range(total_patterns)]
|
|
|
|
|
|
|
|
|
|
def _convert_rgb_for_test_type(self, rgb, test_type):
|
|
|
|
|
if test_type == "sdr_movie":
|
|
|
|
|
data_range = self.app.sdr_data_range_var.get()
|
|
|
|
|
elif test_type == "hdr_movie":
|
|
|
|
|
data_range = self.app.hdr_data_range_var.get()
|
|
|
|
|
else:
|
|
|
|
|
data_range = "Full"
|
|
|
|
|
|
|
|
|
|
return convert_pattern_params([list(rgb)], data_range=data_range, verbose=False)[0]
|
|
|
|
|
|
|
|
|
|
def _log(self, message, level):
|
|
|
|
|
if hasattr(self.app, "log_gui"):
|
|
|
|
|
self.app.log_gui.log(message, level=level)
|