Files
CalibratorLauncher/widgets/devicestatuswidget.cpp
2026-01-02 19:20:35 +09:00

173 lines
4.5 KiB
C++

#include "devicestatuswidget.h"
#include <QPainter>
#include <QPainterPath>
#include <QHBoxLayout>
DeviceStatusWidget::DeviceStatusWidget(QWidget *parent)
: QWidget(parent)
{
// 初始化通道状态
m_channels = {
{"输入通道1", true, "input"},
{"输入通道2", true, "input"},
{"输出通道1", true, "output"},
{"输出通道2", true, "output"}};
setupUI();
}
DeviceStatusWidget::~DeviceStatusWidget()
{
}
void DeviceStatusWidget::setupUI()
{
setStyleSheet(R"(
DeviceStatusWidget {
background-color: white;
border: 1px solid #E0E0E0;
border-radius: 8px;
margin: 16px;
}
)");
}
void DeviceStatusWidget::setChannelStatus(int channel, bool connected)
{
if (channel >= 0 && channel < m_channels.size())
{
m_channels[channel].connected = connected;
update();
}
}
void DeviceStatusWidget::updateChannels(const QVector<ChannelStatus> &channels)
{
m_channels = channels;
update();
}
void DeviceStatusWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// 绘制背景
painter.setPen(Qt::NoPen);
painter.setBrush(QColor("#FFFFFF"));
painter.drawRoundedRect(rect().adjusted(16, 8, -16, -8), 12, 12);
// 绘制边框
painter.setPen(QPen(QColor("#E0E0E0"), 1));
painter.setBrush(Qt::NoBrush);
painter.drawRoundedRect(rect().adjusted(16, 8, -16, -8), 12, 12);
// 绘制线缆图
drawCableDiagram(painter);
}
void DeviceStatusWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
update();
}
void DeviceStatusWidget::drawCableDiagram(QPainter &painter)
{
int contentWidth = width() - 64;
int contentHeight = height() - 32;
int startX = 48;
int startY = 24;
// 绘制主线缆(曲线)
QPainterPath cablePath;
painter.setPen(QPen(QColor("#F44336"), 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
int cableY = startY + contentHeight / 3;
cablePath.moveTo(startX, cableY);
// 绘制波浪形线缆
int segments = 4;
int segmentWidth = contentWidth / segments;
for (int i = 0; i < segments; ++i)
{
int x1 = startX + i * segmentWidth + segmentWidth / 3;
int y1 = cableY - 20 * (i % 2 == 0 ? 1 : -1);
int x2 = startX + i * segmentWidth + 2 * segmentWidth / 3;
int y2 = cableY + 20 * (i % 2 == 0 ? 1 : -1);
int x3 = startX + (i + 1) * segmentWidth;
int y3 = cableY;
cablePath.cubicTo(x1, y1, x2, y2, x3, y3);
}
painter.drawPath(cablePath);
// 绘制通道指示器
int channelSpacing = contentWidth / (m_channels.size() + 1);
int indicatorY = startY + contentHeight * 2 / 3;
for (int i = 0; i < m_channels.size(); ++i)
{
int x = startX + (i + 1) * channelSpacing - 30;
int connectorY = cableY + 10;
// 绘制连接器
painter.setPen(QPen(QColor("#4CAF50"), 2));
painter.setBrush(QColor("#4CAF50"));
// 连接器主体
QRect connectorRect(x, connectorY, 60, 35);
painter.drawRoundedRect(connectorRect, 4, 4);
// 连接器引脚
painter.setPen(Qt::NoPen);
painter.setBrush(QColor("#388E3C"));
painter.drawRect(x + 5, connectorY + 35, 8, 10);
painter.drawRect(x + 47, connectorY + 35, 8, 10);
// 绘制状态标签
QString statusText = m_channels[i].name;
QFont font = painter.font();
font.setPixelSize(11);
painter.setFont(font);
QColor statusColor = m_channels[i].connected ? QColor("#4CAF50") : QColor("#9E9E9E");
painter.setPen(statusColor);
QRect textRect(x - 10, connectorY + 50, 80, 20);
painter.drawText(textRect, Qt::AlignCenter, statusText);
// 绘制状态指示点
int dotX = x + 25;
int dotY = connectorY + 70;
painter.setPen(Qt::NoPen);
painter.setBrush(statusColor);
painter.drawEllipse(QPoint(dotX + 5, dotY + 5), 4, 4);
}
}
void DeviceStatusWidget::drawChannelIndicator(QPainter &painter, int x, int y,
const QString &label, bool connected)
{
// 绘制指示器背景
QColor bgColor = connected ? QColor("#E8F5E9") : QColor("#FAFAFA");
painter.setPen(Qt::NoPen);
painter.setBrush(bgColor);
painter.drawRoundedRect(x, y, 100, 40, 6, 6);
// 绘制状态点
QColor dotColor = connected ? QColor("#4CAF50") : QColor("#9E9E9E");
painter.setBrush(dotColor);
painter.drawEllipse(x + 10, y + 14, 12, 12);
// 绘制标签
painter.setPen(QColor("#424242"));
QFont font = painter.font();
font.setPixelSize(12);
painter.setFont(font);
painter.drawText(x + 28, y + 8, 68, 24, Qt::AlignVCenter, label);
}