#include "OneEyeMap.h" #include "EyeMapInfo.h" #include "spdlog.h" #include "ui_oneeyemap.h" #include #include #include #include "GlobalInfo.h" #include "OneOsc.h" OneEyeMap::OneEyeMap(QWidget *parent) : QWidget(parent), ui(new Ui::OneEyeMap) { ui->setupUi(this); m_logger = spdlog::get("EyeMap"); if(m_logger == nullptr) { SPDLOG_ERROR("获取 EyeMap logger 失败"); return; } /* 初始化变量,加载qss */ // QFile fileQss(":/qss/OneEyeMap/OneEyeMap.qss"); QFile fileQss(":/qss/OneEyeMap/OneEyeMap.qss"); if(fileQss.open(QFile::ReadOnly)) { QString qss = fileQss.readAll(); this->setStyleSheet(qss); fileQss.close(); } else { SPDLOG_LOGGER_ERROR(m_logger, "加载qss文件失败"); } /* 初始化变量 */ // m_info.uid = EyeMapUID::EMUID_0; /* 自定义大小 */ // setFixedSize(1600, 900); /* 设置水平刻度和垂直刻度数目 */ m_hScaleNum = 6; m_vScaleNum = 4; /* 设置绘制区域 */ m_leftMargin = 70; m_topMargin = 24; m_rightMargin = 24; m_bottomMargin = 24; /* 设置刻度区域 */ m_rectScaleValue.setX(0); m_rectScaleValue.setY(ui->widget_title->height()); m_rectScaleValue.setWidth(this->width()); m_rectScaleValue.setHeight(this->height() - ui->widget_title->height()); /* 设置参考颜色区域 */ m_rectRefColor.setX(m_rectScaleValue.width() - m_rightMargin - 60); m_rectRefColor.setY(m_rectScaleValue.y() + 7); m_rectRefColor.setWidth(60); m_rectRefColor.setHeight(10); /* 设置眼图区域 */ m_rectEyeMap.setX(m_leftMargin); m_rectEyeMap.setY(m_rectScaleValue.y() + m_topMargin); m_rectEyeMap.setWidth(m_rectScaleValue.width() - m_leftMargin - m_rightMargin); m_rectEyeMap.setHeight(m_rectScaleValue.height() - m_topMargin - m_bottomMargin); /* 设置“暂无设备”、“设备离线”图片 */ m_rectNoDevice.setRect(0, 0, 280, 160); m_pixmapNoDevice = new QPixmap(":/image/Resource/image/dev_none.png"); m_pixmapDeviceOffline = new QPixmap(":/image/Resource/image/dev_outline.png"); /* 设置定时器 */ m_timer.setTimerType(Qt::PreciseTimer); m_timer.setSingleShot(false); connect(&m_timer, &QTimer::timeout, this, &OneEyeMap::do_update); m_timer.start(1000); /* 1s刷新一次 */ // connect(&GOscDataInfo, &OscilloscopeData::signal_updateOneEyeMap, this, &OneEyeMap::do_updateColorMatrix); connect(&GOscDataInfo, &OscilloscopeData::signal_oscOffline, this, &OneEyeMap::do_deviceOffline); connect(&GOscDataInfo, &OscilloscopeData::signal_oscOnline, this, &OneEyeMap::do_deviceOnline); SPDLOG_LOGGER_INFO(m_logger, "{} OneEyeMap 初始化成功", m_info.channelInfo.channelName.toStdString()); } OneEyeMap::~OneEyeMap() { delete ui; } /* 根据通道号获取示波器指针 */ void OneEyeMap::getOneOscFromGlobalOscData() { /* 初始化本地的颜色矩阵 */ if(m_colorMatrix == nullptr) { m_colorMatrix = new EyeColorMatrix(); m_colorMatrix->initEyeMapData(this->width(), this->height()); } m_osc = GOscDataInfo.findOsc(m_info.channelInfo.channel); if(m_osc == nullptr) { SPDLOG_LOGGER_ERROR(m_logger, "获取示波器指针错误! 通道号: {}", static_cast(m_info.channelInfo.channel)); return; } m_osc->setColorMatrixPtr(m_info.channelInfo.channel, m_colorMatrix); /* 先设置为不使用通道采集 */ m_osc->setChannelUsed(m_info.channelInfo.channel, false); } /* 设置UID,只能设置一次 */ // void OneEyeMap::setUID(EyeMapUID uid) // { // if(m_info.uid == EyeMapUID::EMUID_0) // { // m_info.uid = uid; // } // } /* 设置标题 */ void OneEyeMap::setTitle(const QString &title) { m_info.title = title; ui->label_title->setText(title); } /* 设置颜色 */ void OneEyeMap::setTitleBarColor(const QColor &color) { m_info.titleBarColor = color; // QString qss = QString("background-color: %1;border-radius: 4px;").arg(color.name()); QString qss = QString("background-color: %1; border-top-left-radius: 4px; border-top-right-radius: 4px;").arg(color.name()); ui->widget_title->setStyleSheet(qss); } /* 设置是否显示 */ void OneEyeMap::setShow(bool isShow) { m_info.isShow = isShow; if(isShow) { m_osc->setChannelUsed(m_info.channelInfo.channel, true); this->show(); } else { m_osc->setChannelUsed(m_info.channelInfo.channel, false); this->hide(); } } /* 获取通道信息 */ OneChannelInfo& OneEyeMap::getChannelInfo() { if(m_osc == nullptr) { SPDLOG_LOGGER_ERROR(m_logger, "通道号:{} 示波器指针为空", static_cast(m_info.channelInfo.channel)); return m_info.channelInfo; } if(m_osc->isOpen()) { m_info.channelInfo.isConnected = true; m_info.channelInfo.channelName = GEyeMapInfo.getChannelName(m_info.channelInfo.channel, true); } else { m_info.channelInfo.isConnected = false; m_info.channelInfo.channelName = GEyeMapInfo.getChannelName(m_info.channelInfo.channel, false); } return m_info.channelInfo; } /* 设置电压值 */ void OneEyeMap::setVoltageRange(OscVoltageRange range) { m_info.voltageRange = range; /* 设置示波器的电压范围 */ if(m_osc != nullptr) { if(static_cast(m_info.channelInfo.channel) % 2 == 1) { m_osc->setChannelARange(range); } else { m_osc->setChannelBRange(range); } } } /* 设置时间刻度 */ void OneEyeMap::setTimeGridValue(OscTimeGridValue value) { m_info.tGridValue = value; /* 设置示波器的时间刻度 */ if(m_osc != nullptr) { m_osc->setTimeGridValue(value); } } /* 更新组件信息 */ void OneEyeMap::updateInfo(const OneEyeMapInfo &info) { // m_info = info; setTitle(info.title); setTitleBarColor(info.titleBarColor); setChannelInfo(info.channelInfo); setShow(info.isShow); setVoltageRange(info.voltageRange); setTimeGridValue(info.tGridValue); update(); } /* 只更新设置个数页面的信息 */ void OneEyeMap::updateSettingNum(const OneEyeMapInfo &info) { setNum(info.num); setTitle(info.title); setTitleBarColor(info.titleBarColor); setChannelInfo(info.channelInfo); } /* 重置眼图显示区域为默认 */ void OneEyeMap::resetEyeMap() { m_colorMatrix->initEyeMapData(this->width(), this->height()); update(); } /* 刷新页面 */ void OneEyeMap::do_update() { // SPDLOG_LOGGER_DEBUG(m_logger, "{} 刷新页面", m_info.title.toStdString()); if(!m_osc->isOpen()) { return; } update(); } /* 设备离线了 */ void OneEyeMap::do_deviceOffline(int oscNum) { /* 判断是不是这个编号 */ if(static_cast(oscNum) != m_info.channelInfo.channel) { return; } SPDLOG_LOGGER_WARN(m_logger, "{} 离线了", oscNum, m_info.channelInfo.channelName.toStdString()); m_isOpen = false; update(); } /* 设备上线了 */ void OneEyeMap::do_deviceOnline(int oscNum) { /* 判断是不是这个编号 */ if(static_cast(oscNum) != m_info.channelInfo.channel) { return; } m_isOpen = true; update(); } /** * @brief 绘制图形 * * @param event */ void OneEyeMap::paintEvent(QPaintEvent *event) { if(m_colorMatrix == nullptr) { return; } QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); /******************** 绘制背景 ********************/ drawBackground(painter); ui->widget_title->show(); /* 判断设备是否打开,没打开就显示“暂无设备”图片 */ if(!m_isOpen) { // ui->widget_title->hide(); drawNoDevice(painter); event->accept(); return; } m_colorMatrix->mutexEyeData.lock(); // SPDLOG_LOGGER_DEBUG(m_logger, "{} 获取到了颜色矩阵锁", m_info.title.toStdString()); /******************** 绘制刻度值 ********************/ drawScaleValue(painter); /******************** 绘制刻度网格 *********************/ drawScale(painter); // SPDLOG_LOGGER_DEBUG(m_logger, "width = {}, height = {}", this->width(), this->height()); // SPDLOG_LOGGER_DEBUG(m_logger, "m_rectScaleValue: x = {}, y = {}, width = {}, height = {}", m_rectScaleValue.x(), m_rectScaleValue.y(), m_rectScaleValue.width(), m_rectScaleValue.height()); // SPDLOG_LOGGER_DEBUG(m_logger, "m_rectEyeMap: x = {}, y = {}, width = {}, height = {}", m_rectEyeMap.x(), m_rectEyeMap.y(), m_rectEyeMap.width(), m_rectEyeMap.height()); /******************** 绘制眼图区域 ********************/ drawEyeMap(painter); m_colorMatrix->mutexEyeData.unlock(); event->accept(); } /* 缩放事件 */ void OneEyeMap::resizeEvent(QResizeEvent *event) { /* 设置刻度区域 */ m_rectScaleValue.setX(0); m_rectScaleValue.setY(ui->widget_title->height()); m_rectScaleValue.setWidth(this->width()); m_rectScaleValue.setHeight(this->height() - ui->widget_title->height()); /* 设置参考颜色区域 */ m_rectRefColor.setX(m_rectScaleValue.width() - m_rightMargin - 60); m_rectRefColor.setY(m_rectScaleValue.y() + 7); m_rectRefColor.setWidth(60); m_rectRefColor.setHeight(10); /* 设置眼图区域 */ m_rectEyeMap.setX(m_leftMargin); m_rectEyeMap.setY(m_rectScaleValue.y() + m_topMargin); m_rectEyeMap.setWidth(m_rectScaleValue.width() - m_leftMargin - m_rightMargin); m_rectEyeMap.setHeight(m_rectScaleValue.height() - m_topMargin - m_bottomMargin); /* 重新设置颜色矩阵 */ if(m_colorMatrix != nullptr) { m_colorMatrix->initEyeMapData(this->width(), this->height()); } /* 刷新一下页面 */ update(); event->accept(); } /* 绘制背景 */ void OneEyeMap::drawBackground(QPainter &painter) { /* 绘制背景颜色 */ painter.setPen(Qt::NoPen); QBrush brush; brush.setColor("#1E1E1E"); brush.setStyle(Qt::SolidPattern); painter.setBrush(brush); painter.drawRect(m_rectScaleValue); } /* 绘制刻度区域 */ void OneEyeMap::drawScaleValue(QPainter &painter) { /* 绘制刻度值 */ QPen pen; pen.setColor("#C8CCD2"); painter.setPen(pen); QRect rectText(0, 0, 50, 14); QFont font; font.setFamily("思源黑体R"); font.setPixelSize(14); painter.setFont(font); int oneScale = m_rectEyeMap.height() / m_vScaleNum; // SPDLOG_LOGGER_DEBUG(m_logger, "oneScale = {}", oneScale); /* 绘制电压值 */ for(int i = 0; i <= m_vScaleNum; i++) { /* 计算字体区域坐标 */ rectText.moveTo(10, m_rectEyeMap.y() + oneScale * i - 7); painter.drawText(rectText, Qt::AlignRight | Qt::AlignVCenter, getVScaleValue(i)); } /* 绘制时间刻度值 */ int oneTime = m_rectEyeMap.width() / m_hScaleNum; for(int i = 0; i <= m_hScaleNum; i++) { rectText.moveTo(m_rectEyeMap.x() + oneTime * i - 25, m_rectEyeMap.y() + m_rectEyeMap.height() + 4); painter.drawText(rectText, Qt::AlignCenter, getTimeValue(i)); } /* 绘制参渐变考颜色 */ drawRefColor(painter); } /* 绘制刻度 */ void OneEyeMap::drawScale(QPainter &painter) { /* 限制操作区域,不过坐标零点依旧是widget的(0,0) */ painter.setClipRect(m_rectEyeMap); /* 绘制背景颜色 */ painter.setPen(Qt::NoPen); QBrush brush; brush.setColor(QColor(0, 0, 0, 0.8 * 255)); brush.setStyle(Qt::SolidPattern); painter.setBrush(brush); painter.drawRect(m_rectEyeMap); /* 绘制刻度线 */ QPen pen; pen.setWidth(1); pen.setStyle(Qt::SolidLine); /* 绘制中线 */ int startX = m_rectEyeMap.x(); int startY = m_rectEyeMap.y(); int width = m_rectEyeMap.width(); int height = m_rectEyeMap.height(); pen.setColor(QColor(255, 255, 255, 0.35 * 255)); pen.setWidth(2); painter.setPen(pen); painter.drawLine(startX, startY + (height / 2), startX + width, startY + (height / 2)); /* 绘制水平中线 */ painter.drawLine(startX + (width / 2), startY, startX + (width / 2), startY + height); /* 绘制垂直中线 */ /* 绘制中线上的刻度 */ int hScale = m_hScaleNum * 5; int vScale = m_vScaleNum * 5; double scaleW = width * 1.0 / hScale; double scaleH = height * 1.0 / vScale; QVector wLines; QVector hLines; for(int i = 0; i < hScale; i++) { QLineF line1(startX + i * scaleW, startY + height / 2.0 - 4, startX + i * scaleW, startY + height / 2.0 + 4); wLines.append(line1); } for(int i = 0; i < vScale; i++) { QLineF line2(startX + width / 2.0 - 4, startY + height - i * scaleH, startX + width / 2.0 + 4, startY + height - i * scaleH); hLines.append(line2); } painter.drawLines(wLines); painter.drawLines(hLines); /* 绘制网格,这里是网格线 */ pen.setWidth(1); pen.setColor(QColor(255, 255, 255, 0.15 * 255)); painter.setPen(pen); scaleH = height * 1.0 / m_vScaleNum; scaleW = width * 1.0 / m_hScaleNum; /* 水平线 */ for(int i = 1; i < m_vScaleNum; i++) { int y = startY + i * scaleH; QLineF line1(startX, y, startX + width, y); painter.drawLine(line1); } /* 垂直线 */ for(int i = 1; i < m_hScaleNum; i++) { int x = startX + i * scaleW; QLineF line1(x, startY, x, startY + height); painter.drawLine(line1); } /* 绘制虚线 */ // painter.setPen(pen); // scaleW = width / 10.0; // scaleH = height / 10.0; // int x = 0; // int y = 0; // QVector wGridLines; // QVector hGridLines; // for(int i = 1; i < 10; i++) // { // y = i * scaleH; // while(true) // { // x = x + 8; // QLineF line1(x, y, x + 2, y); // wGridLines.append(line1); // if(x >= width) // { // x = 0; // y = 0; // break; // } // } // } // x = 0; // y = 0; // for(int i = 1; i < 10; i++) // { // x = i * scaleW; // while(true) // { // y = y + 8; // QLineF line1(x, y, x, y + 2); // hGridLines.append(line1); // if(y >= height) // { // x = 0; // y = 0; // break; // } // } // } // painter.drawLines(wGridLines); // painter.drawLines(hGridLines); } /* 绘制眼图区域 */ void OneEyeMap::drawEyeMap(QPainter &painter) { // SPDLOG_ERROR("绘制眼图区域"); /* 绘制眼图,就是绘制 1000 * 256 个矩形 */ painter.setPen(Qt::NoPen); for(int i = 0; i < g_HorPixel; i++) { for(int j = 0; j < g_VerPixel; j++) { /* 这里需要根据是否需要显示进行判断,不然全绘制太卡了 */ if(m_colorMatrix->dataMatrix[i][j].isDraw == false) { continue; } /* 打印出颜色 */ // SPDLOG_LOGGER_WARN(m_logger, "颜色: {}", m_eyeColorMatrix->dataMatrix[i][j].brush.color().name().toStdString()); painter.setBrush(m_colorMatrix->dataMatrix[i][j].brush); painter.drawRect(m_colorMatrix->dataMatrix[i][j].rect); } } } /* 绘制“暂无设备” */ void OneEyeMap::drawNoDevice(QPainter &painter) { int x = (this->width() - m_rectNoDevice.width()) / 2; int y = (this->height() - m_rectNoDevice.height()) / 2; m_rectNoDevice.moveTo(x, y); painter.drawPixmap(m_rectNoDevice, *m_pixmapDeviceOffline); } /* 绘制参考渐变色 */ void OneEyeMap::drawRefColor(QPainter &painter) { QLinearGradient linearGradient(m_rectRefColor.topLeft(), m_rectRefColor.bottomRight()); linearGradient.setColorAt(0, QColor("#003590")); linearGradient.setColorAt(0.4, QColor(0, 255, 255)); linearGradient.setColorAt(0.6, QColor(0, 255, 0)); linearGradient.setColorAt(0.8, QColor(255, 255, 0)); linearGradient.setColorAt(1, QColor("#FF4123")); painter.setBrush(linearGradient); painter.setPen(Qt::NoPen); painter.drawRect(m_rectRefColor); } /* 获取一个格子的电压值 */ QString OneEyeMap::getVScaleValue(int index) { index = 2 - index; if(m_info.voltageRange == OscVoltageRange::CR_100MV) { return QString::number(0.1 * index) + "V"; } else if(m_info.voltageRange == OscVoltageRange::CR_250MV) { return QString::number(0.25 * index) + "V"; } else if(m_info.voltageRange == OscVoltageRange::CR_500MV) { return QString::number(0.5 * index) + "V"; } else if(m_info.voltageRange == OscVoltageRange::CR_1V) { return QString::number(1.0 * index) + "V"; } else if(m_info.voltageRange == OscVoltageRange::CR_2V5) { return QString::number(2.5 * index) + "V"; } else if(m_info.voltageRange == OscVoltageRange::CR_5V) { return QString::number(5.0 * index) + "V"; } else if(m_info.voltageRange == OscVoltageRange::CR_8V) { return QString::number(8.0 * index) + "V"; } else { return QString::number(0.1 * index) + "V"; } } /* 获取一个时间值 */ QString OneEyeMap::getTimeValue(int index) { QString str; switch (m_info.tGridValue) { case OscTimeGridValue::TGV_20NS: str = QString::number(index * 20 / 1000.0) + "us"; break; case OscTimeGridValue::TGV_50NS: str = QString::number(index * 50 / 1000.0) + "us"; break; case OscTimeGridValue::TGV_100NS: str = QString::number(index * 100 / 1000.0) + "us"; break; case OscTimeGridValue::TGV_200NS: str = QString::number(index * 200 / 1000.0) + "us"; break; case OscTimeGridValue::TGV_500NS: str = QString::number(index * 500 / 1000.0) + "us"; break; case OscTimeGridValue::TGV_1US: str = QString::number(index * 1) + "us"; break; case OscTimeGridValue::TGV_2US: str = QString::number(index * 2) + "us"; break; case OscTimeGridValue::TGV_5US: str = QString::number(index * 5) + "us"; break; case OscTimeGridValue::TGV_10US: str = QString::number(index * 10) + "us"; break; case OscTimeGridValue::TGV_20US: str = QString::number(index * 20) + "us"; break; case OscTimeGridValue::TGV_100US: str = QString::number(index * 100) + "us"; break; default: str = "us"; break; } return str; }