123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- #include "OneEyeMap.h"
- #include "EyeMapInfo.h"
- #include "spdlog.h"
- #include "ui_oneeyemap.h"
- #include <QApplication>
- #include <QPainter>
- #include <QPaintEvent>
- #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<int>(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<int>(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<int>(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<OscChnNum>(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<OscChnNum>(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<QLineF> wLines;
- QVector<QLineF> 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<QLineF> wGridLines;
- // QVector<QLineF> 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;
- }
|