// LightPanel.cpp #include "LightPanel.h" #include "ui_MainWindow.h" #include "LightController.h" #include "AppSettings.h" #include #include #include #include #include #include #include #include namespace { constexpr int kLightCh1Mask = 1; constexpr int kLightCh2Mask = 2; constexpr int kLightAllMask = kLightCh1Mask | kLightCh2Mask; QString previewBrightnessCommand(int targetMask, int value) { value = qBound(0, value, 255); QChar channelCode = 'W'; if (targetMask == kLightCh1Mask) channelCode = 'A'; else if (targetMask == kLightCh2Mask) channelCode = 'B'; return QString("S%1%2#").arg(channelCode).arg(value, 4, 10, QChar('0')); } } LightPanel::LightPanel(Ui::MainWindowClass& u, LightController* ctrl, QObject* parent) : QObject(parent), ui(u), m_ctrl(ctrl) { qRegisterMetaType("LightController::Channel"); refreshComPorts(); ui.comboComPort->installEventFilter(this); connect(ui.comboComPort, QOverload::of(&QComboBox::currentIndexChanged), this, [this](int) { if (m_lightOpen) return; const QString port = ui.comboComPort->currentData().toString(); if (!port.isEmpty()) AppSettings::instance().set(QStringLiteral("light/port"), port); }); ui.comboBaudRate->hide(); auto& settings = AppSettings::instance(); const int ch1 = settings.getRaw(QStringLiteral("light/ch1Brightness"), QStringLiteral("100")).toInt(); const int ch2 = settings.getRaw(QStringLiteral("light/ch2Brightness"), QStringLiteral("100")).toInt(); ui.spinCH1->setValue(qBound(0, ch1, 255)); ui.sliderCH1->setValue(ui.spinCH1->value()); ui.spinCH2->setValue(qBound(0, ch2, 255)); ui.sliderCH2->setValue(ui.spinCH2->value()); ui.chkLightLinked->setChecked(settings.getRaw(QStringLiteral("light/linked"), QStringLiteral("1")) == QStringLiteral("1")); m_brightnessApplyTimer = new QTimer(this); m_brightnessApplyTimer->setSingleShot(true); m_brightnessApplyTimer->setInterval(250); connect(m_brightnessApplyTimer, &QTimer::timeout, this, &LightPanel::applyBrightnessNow); connect(ui.btnLightConnect, &QPushButton::clicked, this, &LightPanel::onConnect); connect(ui.btnLightDisconnect, &QPushButton::clicked, this, &LightPanel::onDisconnect); connect(ui.btnSetCH1, &QPushButton::clicked, this, &LightPanel::onSetCH1); connect(ui.btnSetCH2, &QPushButton::clicked, this, &LightPanel::onSetCH2); connect(ui.btnLightOn, &QPushButton::clicked, this, &LightPanel::onLightOn); connect(ui.btnLightOff,&QPushButton::clicked, this, &LightPanel::onLightOff); // 滑条↔数值框双向绑定 connect(ui.sliderCH1, &QSlider::valueChanged, ui.spinCH1, &QSpinBox::setValue); connect(ui.spinCH1, QOverload::of(&QSpinBox::valueChanged), ui.sliderCH1, &QSlider::setValue); connect(ui.sliderCH2, &QSlider::valueChanged, ui.spinCH2, &QSpinBox::setValue); connect(ui.spinCH2, QOverload::of(&QSpinBox::valueChanged), ui.sliderCH2, &QSlider::setValue); connect(ui.spinCH1, QOverload::of(&QSpinBox::valueChanged), this, [](int v) { AppSettings::instance().set(QStringLiteral("light/ch1Brightness"), QString::number(v)); }); connect(ui.spinCH2, QOverload::of(&QSpinBox::valueChanged), this, [](int v) { AppSettings::instance().set(QStringLiteral("light/ch2Brightness"), QString::number(v)); }); connect(ui.chkLightLinked, &QCheckBox::toggled, this, [](bool checked) { AppSettings::instance().set(QStringLiteral("light/linked"), checked ? QStringLiteral("1") : QStringLiteral("0")); }); // 联动 connect(ui.sliderCH1, &QSlider::valueChanged, this, &LightPanel::onSliderCH1Changed); connect(ui.sliderCH2, &QSlider::valueChanged, this, &LightPanel::onSliderCH2Changed); connect(m_ctrl, &LightController::errorOccurred, this, [this](const QString& msg){ emit logMessage("[Light Error] " + msg); }); connect(m_ctrl, &LightController::commandSent, this, [this](const QString& cmd, const QString& resp){ emit logMessage(QString("[Light] TX:%1 RX:%2").arg(cmd, resp)); }); connect(m_ctrl, &LightController::connected, this, [this]{ m_lightOpen = true; m_lightOutputOn = false; updateStates(); }); connect(m_ctrl, &LightController::disconnected, this, [this]{ m_lightOpen = false; m_lightOutputOn = false; updateStates(); }); updateStates(); } bool LightPanel::eventFilter(QObject* watched, QEvent* event) { if (watched == ui.comboComPort && !m_lightOpen) { switch (event->type()) { case QEvent::MouseButtonPress: refreshComPorts(); break; default: break; } } return QObject::eventFilter(watched, event); } LightPanel::~LightPanel() { if (m_ctrl) m_ctrl->close(); m_ctrl = nullptr; } void LightPanel::refreshComPorts() { const QString currentPort = ui.comboComPort->currentData().toString(); const QString savedPort = AppSettings::instance().getRaw(QStringLiteral("light/port"), QString()); QSignalBlocker blocker(ui.comboComPort); ui.comboComPort->clear(); int currentIndex = -1; int savedIndex = -1; int com6Index = -1; int nonCom1Index = -1; for (const auto& info : QSerialPortInfo::availablePorts()) { const QString port = info.portName(); QString label = port; if (!info.description().isEmpty()) label += " - " + info.description(); ui.comboComPort->addItem(label, port); const int idx = ui.comboComPort->count() - 1; if (!currentPort.isEmpty() && port.compare(currentPort, Qt::CaseInsensitive) == 0) currentIndex = idx; if (!savedPort.isEmpty() && port.compare(savedPort, Qt::CaseInsensitive) == 0) savedIndex = idx; if (port.compare(QStringLiteral("COM6"), Qt::CaseInsensitive) == 0) com6Index = idx; if (nonCom1Index < 0 && port.compare(QStringLiteral("COM1"), Qt::CaseInsensitive) != 0) nonCom1Index = idx; } if (ui.comboComPort->count() == 0) { ui.comboComPort->addItem(QString::fromUtf8("无可用串口"), ""); return; } int preferred = currentIndex; if (preferred < 0) preferred = savedIndex; if (preferred < 0) preferred = com6Index; if (preferred < 0) preferred = nonCom1Index; if (preferred < 0) preferred = 0; ui.comboComPort->setCurrentIndex(preferred); } void LightPanel::updateStates() { bool open = m_lightOpen; ui.btnLightConnect->setEnabled(!open && !m_lightCommandBusy); ui.btnLightDisconnect->setEnabled(open && !m_lightCommandBusy); ui.comboComPort->setEnabled(!open && !m_lightCommandBusy); ui.sliderCH1->setEnabled(open); ui.spinCH1->setEnabled(open); ui.btnSetCH1->setEnabled(open && !m_lightCommandBusy); ui.sliderCH2->setEnabled(open); ui.spinCH2->setEnabled(open); ui.btnSetCH2->setEnabled(open && !m_lightCommandBusy); ui.btnLightOn->setEnabled(open && !m_lightCommandBusy); ui.btnLightOff->setEnabled(open && !m_lightCommandBusy); ui.labelLightStatus->setText(open ? QString::fromUtf8("● 光源已连接") : QString::fromUtf8("● 光源未连接")); ui.labelLightStatus->setStyleSheet(open ? "color:#00cc66;" : "color:gray;"); } void LightPanel::onConnect() { QString port = ui.comboComPort->currentData().toString(); if (port.isEmpty()) { refreshComPorts(); port = ui.comboComPort->currentData().toString(); } if (port.isEmpty()) { emit logMessage(QString::fromUtf8("没有可用光源串口")); updateStates(); return; } emit logMessage(QString::fromUtf8("正在连接光源串口 ") + port + "..."); m_lightCommandBusy = true; updateStates(); const bool ok = m_ctrl->open(port); m_lightOpen = ok; m_lightOutputOn = false; m_lightCommandBusy = false; emit logMessage(ok ? QString::fromUtf8("光源串口已打开") : QString::fromUtf8("光源串口打开失败")); if (ok) { AppSettings::instance().set(QStringLiteral("light/port"), port); emit logMessage(QString::fromUtf8( "亮度协议: CH1=SAxxxx#, CH2=SBxxxx#, 联动=SWxxxx#,xxxx为0000~0255")); } updateStates(); } void LightPanel::onDisconnect() { if (!m_lightOpen || m_lightCommandBusy) return; m_lightCommandBusy = true; updateStates(); m_ctrl->close(); m_lightOpen = false; m_lightOutputOn = false; m_lightCommandBusy = false; emit logMessage(QString::fromUtf8("光源已断开")); refreshComPorts(); updateStates(); } void LightPanel::onSetCH1() { const int value = ui.spinCH1->value(); if (ui.chkLightLinked->isChecked()) { syncLinkedBrightness(value); sendBrightnessCommand(kLightAllMask, value); } else { sendBrightnessCommand(kLightCh1Mask, value); } } void LightPanel::onSetCH2() { const int value = ui.spinCH2->value(); if (ui.chkLightLinked->isChecked()) { syncLinkedBrightness(value); sendBrightnessCommand(kLightAllMask, value); } else { sendBrightnessCommand(kLightCh2Mask, value); } } void LightPanel::applyBrightnessNow() { if (!m_lightOpen) return; const int mask = (m_pendingBrightnessMask == 0) ? kLightAllMask : m_pendingBrightnessMask; m_pendingBrightnessMask = 0; int v1 = ui.spinCH1->value(); int v2 = ui.spinCH2->value(); if (ui.chkLightLinked->isChecked()) { syncLinkedBrightness(v1); sendBrightnessCommand(kLightAllMask, v1); return; } if ((mask & kLightAllMask) == kLightAllMask && v1 == v2) { sendBrightnessCommand(kLightAllMask, v1); return; } if (mask & kLightCh1Mask) { sendBrightnessCommand(kLightCh1Mask, v1); } if (mask & kLightCh2Mask) { sendBrightnessCommand(kLightCh2Mask, v2); } } void LightPanel::queueBrightnessApply(int channelMask) { if (!m_lightOpen || !m_brightnessApplyTimer) return; m_pendingBrightnessMask |= channelMask; m_brightnessApplyTimer->start(); } void LightPanel::onLightOn() { if (!m_lightOpen || m_lightCommandBusy) return; m_lightCommandBusy = true; updateStates(); const bool ok = m_ctrl->setAlwaysOn(); m_lightOutputOn = ok; if (ok) AppSettings::instance().set(QStringLiteral("light/port"), ui.comboComPort->currentData().toString()); m_lightCommandBusy = false; emit logMessage(ok ? QString::fromUtf8("光源常亮") : QString::fromUtf8("光源常亮命令失败")); updateStates(); } void LightPanel::onLightOff() { if (!m_lightOpen || m_lightCommandBusy) return; m_lightCommandBusy = true; updateStates(); const bool ok = m_ctrl->setAlwaysOff(); if (ok) m_lightOutputOn = false; if (ok) AppSettings::instance().set(QStringLiteral("light/port"), ui.comboComPort->currentData().toString()); m_lightCommandBusy = false; emit logMessage(ok ? QString::fromUtf8("光源常关") : QString::fromUtf8("光源常关命令失败")); updateStates(); } void LightPanel::onSliderCH1Changed(int v) { if (ui.chkLightLinked->isChecked()) syncLinkedBrightness(v); queueBrightnessApply(ui.chkLightLinked->isChecked() ? kLightAllMask : kLightCh1Mask); } void LightPanel::onSliderCH2Changed(int v) { if (ui.chkLightLinked->isChecked()) syncLinkedBrightness(v); queueBrightnessApply(ui.chkLightLinked->isChecked() ? kLightAllMask : kLightCh2Mask); } void LightPanel::syncLinkedBrightness(int value) { value = qBound(0, value, 255); QSignalBlocker bSlider1(ui.sliderCH1); QSignalBlocker bSpin1(ui.spinCH1); QSignalBlocker bSlider2(ui.sliderCH2); QSignalBlocker bSpin2(ui.spinCH2); ui.sliderCH1->setValue(value); ui.spinCH1->setValue(value); ui.sliderCH2->setValue(value); ui.spinCH2->setValue(value); } void LightPanel::sendBrightnessCommand(int targetMask, int value) { if (!m_lightOpen || m_lightCommandBusy) return; value = qBound(0, value, 255); int channel = 2; QString label = QString::fromUtf8("联动"); if (targetMask == kLightCh1Mask) { channel = 0; label = "CH1"; } else if (targetMask == kLightCh2Mask) { channel = 1; label = "CH2"; } emit logMessage(QString::fromUtf8("设置光源亮度 %1=%2").arg(label).arg(value)); emit logMessage(QString("发送光源命令: %1").arg(previewBrightnessCommand(targetMask, value))); m_lightCommandBusy = true; updateStates(); const bool ok = m_ctrl->setBrightnessValue(channel, value); if (ok) AppSettings::instance().set(QStringLiteral("light/port"), ui.comboComPort->currentData().toString()); m_lightCommandBusy = false; if (!ok) emit logMessage(QString::fromUtf8("[Light Error] 光源命令执行失败")); updateStates(); } void LightPanel::switchLight(bool leftOn, bool rightOn, int stabilizeMs) { Q_UNUSED(stabilizeMs); if (!m_lightOpen || m_lightCommandBusy) return; int ch1 = leftOn ? ui.spinCH1->value() : 0; int ch2 = rightOn ? ui.spinCH2->value() : 0; m_lightCommandBusy = true; updateStates(); bool ok = m_ctrl->setAlwaysOn(); if (ok) { if (ch1 == ch2) { ok = m_ctrl->setBrightnessValue(2, ch1); } else { ok = m_ctrl->setBrightnessValue(0, ch1); if (ok) ok = m_ctrl->setBrightnessValue(1, ch2); } } if (ok) m_lightOutputOn = true; if (ok) AppSettings::instance().set(QStringLiteral("light/port"), ui.comboComPort->currentData().toString()); m_lightCommandBusy = false; if (!ok) emit logMessage(QString::fromUtf8("[Light Error] 光源切换命令执行失败")); updateStates(); }