#include "channelstatuswidget.h" #include #include #include #include // ===================================================== // ChannelStatusWidget 实现 // ===================================================== ChannelStatusWidget::ChannelStatusWidget(ChannelManager::ChannelId channelId, const QString &channelName, QWidget *parent, ChannelStatusWidget::DisplayMode mode) : QWidget(parent), m_channelId(channelId), m_channelName(channelName), m_currentOperation(ChannelManager::NONE), m_channelManager(ChannelManager::instance()), m_displayMode(mode) { setFixedSize(140, 80); // 紧凑尺寸适配launcher布局 setStyleSheet("background-color: #34495e; border: 2px solid #2c3e50; " "border-radius: 8px;"); } void ChannelStatusWidget::updateStatus(ChannelManager::OperationType operation, const QString &description) { m_currentOperation = operation; m_description = description; update(); // 触发重绘 } void ChannelStatusWidget::setDisplayMode(ChannelStatusWidget::DisplayMode mode) { m_displayMode = mode; update(); // 触发重绘 } void ChannelStatusWidget::paintEvent(QPaintEvent *event) { QWidget::paintEvent(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QRect rect = this->rect().adjusted(6, 6, -6, -6); // 绘制通道连接器 drawChannelConnector(painter, rect); // 绘制通道名称 if (m_displayMode == ChannelStatusWidget::DETAILED_MODE) { // 详细模式:通道名称在顶部 painter.setPen(QColor("#ecf0f1")); QFont nameFont("Microsoft YaHei", 9, QFont::Bold); painter.setFont(nameFont); painter.drawText(rect.adjusted(0, 0, 0, -50), Qt::AlignCenter, m_channelName); // 绘制状态文本 QString statusText = m_channelManager->getOperationDisplayName(m_currentOperation); if (!m_description.isEmpty() && m_description.length() < 15) { statusText += "\n" + m_description; } QFont statusFont("Microsoft YaHei", 8); painter.setFont(statusFont); painter.drawText(rect.adjusted(0, 28, 0, 0), Qt::AlignCenter, statusText); // 绘制接线信息 ChannelManager::ChannelCapability capability = m_channelManager->getChannelCapability(m_channelId); QString wiringText = capability.wireColors.join("-"); painter.setPen(QColor("#bdc3c7")); QFont wiringFont("Microsoft YaHei", 7); painter.setFont(wiringFont); painter.drawText(rect.adjusted(0, 50, 0, 0), Qt::AlignCenter, wiringText); } else { // 概览模式:通道名称在整个区域居中显示 painter.setPen(QColor("#ffffff")); QFont overviewFont("Microsoft YaHei", 10, QFont::Bold); painter.setFont(overviewFont); painter.drawText(rect.adjusted(0, 25, 0, 0), Qt::AlignHCenter | Qt::AlignTop, m_channelName); } } void ChannelStatusWidget::drawChannelConnector(QPainter &painter, const QRect &rect) { // 绘制通道连接器图形,颜色根据状态变化 QRect connectorRect(rect.center().x() - 25, rect.top() + 5, 50, 20); // 根据状态设置连接器颜色 QColor connectorColor = getStatusColor(); painter.setBrush(connectorColor); painter.setPen(QPen(connectorColor.darker(120), 1)); painter.drawRoundedRect(connectorRect, 4, 4); // 绘制接线端子 ChannelManager::ChannelCapability capability = m_channelManager->getChannelCapability(m_channelId); int wireCount = capability.wireColors.size(); int wireSpacing = connectorRect.width() / (wireCount + 1); for (int i = 0; i < wireCount; ++i) { QRect wireRect(connectorRect.left() + wireSpacing * (i + 1) - 2, connectorRect.bottom() - 3, 4, 6); // 根据线色设置颜色 QColor wireColor; QString colorName = capability.wireColors[i]; if (colorName == "白") wireColor = QColor("#f8f9fa"); else if (colorName == "红") wireColor = QColor("#e74c3c"); else if (colorName == "灰") wireColor = QColor("#95a5a6"); else if (colorName == "黑") wireColor = QColor("#1a1a1a"); else wireColor = QColor("#bdc3c7"); painter.setBrush(wireColor); painter.setPen(QPen(QColor("#34495e"), 1)); painter.drawRect(wireRect); } } QColor ChannelStatusWidget::getStatusColor() const { switch (m_currentOperation) { case ChannelManager::NONE: return QColor("#27ae60"); // 绿色 - 空闲 // 输入通道测量操作 - 蓝色系 case ChannelManager::VOLTAGE_MEASUREMENT: case ChannelManager::MV_MEASUREMENT: case ChannelManager::MA_MEASUREMENT: case ChannelManager::AC_MEASUREMENT: case ChannelManager::RESISTANCE_MEASUREMENT: case ChannelManager::FREQUENCY_MEASUREMENT: case ChannelManager::SWITCH_MEASUREMENT: case ChannelManager::INSULATION_MEASUREMENT: return QColor("#3498db"); // 蓝色 - 测量中 // 输出通道操作 - 橙色系 case ChannelManager::VOLTAGE_OUTPUT: case ChannelManager::MV_OUTPUT: case ChannelManager::MA_OUTPUT: case ChannelManager::RESISTANCE_SIMULATION: return QColor("#f39c12"); // 橙色 - 输出中 // 波形输出 - 紫色 case ChannelManager::WAVEFORM_OUTPUT: return QColor("#9b59b6"); // 紫色 - 波形输出中 default: return QColor("#95a5a6"); // 灰色 - 未知 } } // ===================================================== // ChannelStatusPanel 实现 // ===================================================== ChannelStatusPanel::ChannelStatusPanel(QWidget *parent, LayoutOrientation orientation) : QWidget(parent), m_channelManager(ChannelManager::instance()), m_displayMode(ChannelStatusWidget::OVERVIEW_MODE) { setupUI(orientation); // 连接 ChannelManager 的信号 connect(m_channelManager, &ChannelManager::channelStatusChanged, this, &ChannelStatusPanel::onChannelStatusChanged); } void ChannelStatusPanel::setupUI(LayoutOrientation orientation) { QBoxLayout *layout; if (orientation == HORIZONTAL) { layout = new QHBoxLayout(this); } else { layout = new QVBoxLayout(this); } layout->setContentsMargins(4, 4, 4, 4); layout->setSpacing(8); // 创建四个通道的状态显示 struct ChannelInfo { ChannelManager::ChannelId id; QString name; }; QList channels = { {ChannelManager::INPUT_CHANNEL_1, "输入通道1"}, {ChannelManager::INPUT_CHANNEL_2, "输入通道2"}, {ChannelManager::OUTPUT_CHANNEL_1, "输出通道1"}, {ChannelManager::OUTPUT_CHANNEL_2, "输出通道2"}}; for (const auto &channel : channels) { ChannelStatusWidget *widget = new ChannelStatusWidget( channel.id, channel.name, this, m_displayMode); m_channelWidgets[channel.id] = widget; layout->addWidget(widget); } layout->addStretch(); } void ChannelStatusPanel::setDisplayMode(ChannelStatusWidget::DisplayMode mode) { m_displayMode = mode; for (auto widget : m_channelWidgets) { widget->setDisplayMode(mode); } } void ChannelStatusPanel::refresh() { for (auto it = m_channelWidgets.constBegin(); it != m_channelWidgets.constEnd(); ++it) { ChannelManager::ChannelId channelId = it.key(); ChannelStatusWidget *widget = it.value(); ChannelManager::OperationType operation = m_channelManager->getChannelOperation(channelId); QString description = m_channelManager->getChannelDescription(channelId); widget->updateStatus(operation, description); } } void ChannelStatusPanel::onChannelStatusChanged( ChannelManager::ChannelId channel, ChannelManager::OperationType operation, const QString &description) { if (m_channelWidgets.contains(channel)) { m_channelWidgets[channel]->updateStatus(operation, description); } }