OneEyeMap.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include "OneEyeMap.h"
  2. #include "ui_oneeyemap.h"
  3. #include <QApplication>
  4. #include <QPainter>
  5. #include <QPaintEvent>
  6. #include "GlobalInfo.h"
  7. OneEyeMap::OneEyeMap(QWidget *parent) :
  8. QWidget(parent),
  9. ui(new Ui::OneEyeMap)
  10. {
  11. ui->setupUi(this);
  12. m_logger = spdlog::get("EyeMap");
  13. if(m_logger == nullptr)
  14. {
  15. SPDLOG_ERROR("获取 EyeMap logger 失败");
  16. return;
  17. }
  18. /* 初始化变量,加载qss */
  19. // QFile fileQss(":/qss/OneEyeMap/OneEyeMap.qss");
  20. QFile fileQss(":/qss/OneEyeMap/OneEyeMap.qss");
  21. if(fileQss.open(QFile::ReadOnly))
  22. {
  23. QString qss = fileQss.readAll();
  24. this->setStyleSheet(qss);
  25. fileQss.close();
  26. } else
  27. {
  28. SPDLOG_LOGGER_ERROR(m_logger, "加载qss文件失败");
  29. }
  30. /* 自定义大小 */
  31. // setFixedSize(1600, 900);
  32. /* 设置水平刻度和垂直刻度数目 */
  33. m_hScaleNum = 6;
  34. m_vScaleNum = 4;
  35. /* 设置绘制区域 */
  36. m_leftMargin = 70;
  37. m_topMargin = 24;
  38. m_rightMargin = 24;
  39. m_bottomMargin = 24;
  40. /* 设置刻度区域 */
  41. m_rectScaleValue.setX(0);
  42. m_rectScaleValue.setY(ui->widget_title->height());
  43. m_rectScaleValue.setWidth(this->width());
  44. m_rectScaleValue.setHeight(this->height() - ui->widget_title->height());
  45. /* 设置参考颜色区域 */
  46. m_rectRefColor.setX(m_rectScaleValue.width() - m_rightMargin - 60);
  47. m_rectRefColor.setY(m_rectScaleValue.y() + 7);
  48. m_rectRefColor.setWidth(60);
  49. m_rectRefColor.setHeight(10);
  50. /* 设置眼图区域 */
  51. m_rectEyeMap.setX(m_leftMargin);
  52. m_rectEyeMap.setY(m_rectScaleValue.y() + m_topMargin);
  53. m_rectEyeMap.setWidth(m_rectScaleValue.width() - m_leftMargin - m_rightMargin);
  54. m_rectEyeMap.setHeight(m_rectScaleValue.height() - m_topMargin - m_bottomMargin);
  55. /* 设置“暂无设备”、“设备离线”图片 */
  56. m_rectNoDevice.setRect(0, 0, 280, 160);
  57. m_pixmapNoDevice = new QPixmap(":/image/Resource/image/dev_none.png");
  58. m_pixmapDeviceOffline = new QPixmap(":/image/Resource/image/dev_outline.png");
  59. /* 设置定时器 */
  60. m_timer.setTimerType(Qt::PreciseTimer);
  61. m_timer.setSingleShot(false);
  62. m_timer.start(100); /* 100ms刷新一次,大约10帧 */
  63. connect(this, &OneEyeMap::signal_update, this, &OneEyeMap::do_update);
  64. connect(&m_timer, &QTimer::timeout, this, &OneEyeMap::do_update);
  65. SPDLOG_LOGGER_INFO(m_logger, "OneEyeMap 初始化成功");
  66. }
  67. OneEyeMap::~OneEyeMap()
  68. {
  69. delete ui;
  70. }
  71. /* 根据通道号获取数据指针 */
  72. void OneEyeMap::setEyeDataPtrFromOscData(OscChnNum channel)
  73. {
  74. m_eyeMapMatrix = GOscDataInfo.findEyeMapMatrix(channel);
  75. if(m_eyeMapMatrix == nullptr)
  76. {
  77. SPDLOG_LOGGER_ERROR(m_logger, "获取颜色矩阵错误! 通道号: {}", static_cast<int>(channel));
  78. return;
  79. }
  80. m_eyeMapMatrix->initEyeMapData(this->width(), this->height());
  81. }
  82. /* 设置标题 */
  83. void OneEyeMap::setTitle(const QString &title)
  84. {
  85. m_info.title = title;
  86. ui->label_title->setText(title);
  87. }
  88. /* 设置颜色 */
  89. void OneEyeMap::setTitleBarColor(const QColor &color)
  90. {
  91. m_info.titleBarColor = color;
  92. // QString qss = QString("background-color: %1;border-radius: 4px;").arg(color.name());
  93. QString qss = QString("background-color: %1; border-top-left-radius: 4px; border-top-right-radius: 4px;").arg(color.name());
  94. ui->widget_title->setStyleSheet(qss);
  95. }
  96. /* 设置是否显示 */
  97. void OneEyeMap::setShow(bool isShow)
  98. {
  99. m_info.isShow = isShow;
  100. if(isShow)
  101. {
  102. this->show();
  103. } else
  104. {
  105. this->hide();
  106. }
  107. }
  108. /* 设置电压值 */
  109. void OneEyeMap::setVoltageRange(OscVoltageRange range)
  110. {
  111. m_info.voltageRange = range;
  112. }
  113. /* 更新组件信息 */
  114. void OneEyeMap::updateInfo(const OneEyeMapInfo &info)
  115. {
  116. // m_info = info;
  117. setTitle(info.title);
  118. setTitleBarColor(info.titleBarColor);
  119. setChannelInfo(info.channelInfo);
  120. setShow(info.isShow);
  121. setVoltageRange(info.voltageRange);
  122. setTimeGridValue(info.tGridValue);
  123. update();
  124. }
  125. /* 只更新设置个数页面的信息 */
  126. void OneEyeMap::updateSettingNum(const OneEyeMapInfo &info)
  127. {
  128. setNum(info.num);
  129. setTitle(info.title);
  130. setTitleBarColor(info.titleBarColor);
  131. setChannelInfo(info.channelInfo);
  132. }
  133. /* 刷新页面 */
  134. void OneEyeMap::do_update()
  135. {
  136. update();
  137. }
  138. /**
  139. * @brief 绘制图形
  140. *
  141. * @param event
  142. */
  143. void OneEyeMap::paintEvent(QPaintEvent *event)
  144. {
  145. if(m_eyeMapMatrix == nullptr)
  146. {
  147. return;
  148. }
  149. QPainter painter(this);
  150. painter.setRenderHint(QPainter::Antialiasing, true);
  151. /******************** 绘制背景 ********************/
  152. drawBackground(painter);
  153. /* 判断设备是否打开,没打开就显示“暂无设备”图片 */
  154. // if(!m_isOpen)
  155. // {
  156. // ui->widget_title->hide();
  157. // drawNoDevice(painter);
  158. // return;
  159. // }
  160. ui->widget_title->show();
  161. m_eyeMapMatrix->mutexEyeData.lock();
  162. /******************** 绘制刻度值 ********************/
  163. drawScaleValue(painter);
  164. /******************** 绘制刻度网格 *********************/
  165. drawScale(painter);
  166. // SPDLOG_LOGGER_DEBUG(m_logger, "width = {}, height = {}", this->width(), this->height());
  167. // SPDLOG_LOGGER_DEBUG(m_logger, "m_rectScaleValue: x = {}, y = {}, width = {}, height = {}", m_rectScaleValue.x(), m_rectScaleValue.y(), m_rectScaleValue.width(), m_rectScaleValue.height());
  168. // SPDLOG_LOGGER_DEBUG(m_logger, "m_rectEyeMap: x = {}, y = {}, width = {}, height = {}", m_rectEyeMap.x(), m_rectEyeMap.y(), m_rectEyeMap.width(), m_rectEyeMap.height());
  169. /******************** 绘制眼图区域 ********************/
  170. drawEyeMap(painter);
  171. m_eyeMapMatrix->mutexEyeData.unlock();
  172. event->accept();
  173. }
  174. /* 缩放事件 */
  175. void OneEyeMap::resizeEvent(QResizeEvent *event)
  176. {
  177. /* 设置刻度区域 */
  178. m_rectScaleValue.setX(0);
  179. m_rectScaleValue.setY(ui->widget_title->height());
  180. m_rectScaleValue.setWidth(this->width());
  181. m_rectScaleValue.setHeight(this->height() - ui->widget_title->height());
  182. /* 设置参考颜色区域 */
  183. m_rectRefColor.setX(m_rectScaleValue.width() - m_rightMargin - 60);
  184. m_rectRefColor.setY(m_rectScaleValue.y() + 7);
  185. m_rectRefColor.setWidth(60);
  186. m_rectRefColor.setHeight(10);
  187. /* 设置眼图区域 */
  188. m_rectEyeMap.setX(m_leftMargin);
  189. m_rectEyeMap.setY(m_rectScaleValue.y() + m_topMargin);
  190. m_rectEyeMap.setWidth(m_rectScaleValue.width() - m_leftMargin - m_rightMargin);
  191. m_rectEyeMap.setHeight(m_rectScaleValue.height() - m_topMargin - m_bottomMargin);
  192. /* 刷新一下页面 */
  193. update();
  194. event->accept();
  195. }
  196. /* 绘制背景 */
  197. void OneEyeMap::drawBackground(QPainter &painter)
  198. {
  199. /* 绘制背景颜色 */
  200. painter.setPen(Qt::NoPen);
  201. QBrush brush;
  202. brush.setColor("#1E1E1E");
  203. brush.setStyle(Qt::SolidPattern);
  204. painter.setBrush(brush);
  205. painter.drawRect(m_rectScaleValue);
  206. }
  207. /* 绘制刻度区域 */
  208. void OneEyeMap::drawScaleValue(QPainter &painter)
  209. {
  210. /* 绘制刻度值 */
  211. QPen pen;
  212. pen.setColor("#C8CCD2");
  213. painter.setPen(pen);
  214. QRect rectText(0, 0, 50, 14);
  215. QFont font;
  216. font.setFamily("思源黑体R");
  217. font.setPixelSize(14);
  218. painter.setFont(font);
  219. int oneScale = m_rectEyeMap.height() / m_vScaleNum;
  220. // SPDLOG_LOGGER_DEBUG(m_logger, "oneScale = {}", oneScale);
  221. /* 绘制电压值 */
  222. for(int i = 0; i <= m_vScaleNum; i++)
  223. {
  224. /* 计算字体区域坐标 */
  225. rectText.moveTo(10, m_rectEyeMap.y() + oneScale * i - 7);
  226. painter.drawText(rectText, Qt::AlignRight | Qt::AlignVCenter, getVScaleValue(i));
  227. }
  228. /* 绘制时间刻度值 */
  229. int oneTime = m_rectEyeMap.width() / m_hScaleNum;
  230. for(int i = 0; i <= m_hScaleNum; i++)
  231. {
  232. rectText.moveTo(m_rectEyeMap.x() + oneTime * i - 25, m_rectEyeMap.y() + m_rectEyeMap.height() + 4);
  233. painter.drawText(rectText, Qt::AlignCenter, getTimeValue(i));
  234. }
  235. /* 绘制参渐变考颜色 */
  236. QLinearGradient linearGradient(m_rectRefColor.topLeft(), m_rectRefColor.bottomRight());
  237. linearGradient.setColorAt(0, QColor("#003590"));
  238. linearGradient.setColorAt(0.4, QColor(0, 255, 255));
  239. linearGradient.setColorAt(0.6, QColor(0, 255, 0));
  240. linearGradient.setColorAt(0.8, QColor(255, 255, 0));
  241. linearGradient.setColorAt(1, QColor("#FF4123"));
  242. painter.setBrush(linearGradient);
  243. painter.setPen(Qt::NoPen);
  244. painter.drawRect(m_rectRefColor);
  245. }
  246. /* 绘制刻度 */
  247. void OneEyeMap::drawScale(QPainter &painter)
  248. {
  249. /* 限制操作区域,不过坐标零点依旧是widget的(0,0) */
  250. painter.setClipRect(m_rectEyeMap);
  251. /* 绘制背景颜色 */
  252. painter.setPen(Qt::NoPen);
  253. QBrush brush;
  254. brush.setColor(QColor(0, 0, 0, 0.8 * 255));
  255. brush.setStyle(Qt::SolidPattern);
  256. painter.setBrush(brush);
  257. painter.drawRect(m_rectEyeMap);
  258. /* 绘制刻度线 */
  259. QPen pen;
  260. pen.setWidth(1);
  261. pen.setStyle(Qt::SolidLine);
  262. /* 绘制中线 */
  263. int startX = m_rectEyeMap.x();
  264. int startY = m_rectEyeMap.y();
  265. int width = m_rectEyeMap.width();
  266. int height = m_rectEyeMap.height();
  267. pen.setColor(QColor(255, 255, 255, 0.35 * 255));
  268. pen.setWidth(2);
  269. painter.setPen(pen);
  270. painter.drawLine(startX, startY + (height / 2), startX + width, startY + (height / 2)); /* 绘制水平中线 */
  271. painter.drawLine(startX + (width / 2), startY, startX + (width / 2), startY + height); /* 绘制垂直中线 */
  272. /* 绘制中线上的刻度 */
  273. int hScale = m_hScaleNum * 5;
  274. int vScale = m_vScaleNum * 5;
  275. double scaleW = width * 1.0 / hScale;
  276. double scaleH = height * 1.0 / vScale;
  277. QVector<QLineF> wLines;
  278. QVector<QLineF> hLines;
  279. for(int i = 0; i < hScale; i++)
  280. {
  281. QLineF line1(startX + i * scaleW, startY + height / 2.0 - 4, startX + i * scaleW, startY + height / 2.0 + 4);
  282. wLines.append(line1);
  283. }
  284. for(int i = 0; i < vScale; i++)
  285. {
  286. QLineF line2(startX + width / 2.0 - 4, startY + height - i * scaleH, startX + width / 2.0 + 4, startY + height - i * scaleH);
  287. hLines.append(line2);
  288. }
  289. painter.drawLines(wLines);
  290. painter.drawLines(hLines);
  291. /* 绘制网格,这里是网格线 */
  292. pen.setWidth(1);
  293. pen.setColor(QColor(255, 255, 255, 0.15 * 255));
  294. painter.setPen(pen);
  295. scaleH = height * 1.0 / m_vScaleNum;
  296. scaleW = width * 1.0 / m_hScaleNum;
  297. /* 水平线 */
  298. for(int i = 1; i < m_vScaleNum; i++)
  299. {
  300. int y = startY + i * scaleH;
  301. QLineF line1(startX, y, startX + width, y);
  302. painter.drawLine(line1);
  303. }
  304. /* 垂直线 */
  305. for(int i = 1; i < m_hScaleNum; i++)
  306. {
  307. int x = startX + i * scaleW;
  308. QLineF line1(x, startY, x, startY + height);
  309. painter.drawLine(line1);
  310. }
  311. /* 绘制虚线 */
  312. // painter.setPen(pen);
  313. // scaleW = width / 10.0;
  314. // scaleH = height / 10.0;
  315. // int x = 0;
  316. // int y = 0;
  317. // QVector<QLineF> wGridLines;
  318. // QVector<QLineF> hGridLines;
  319. // for(int i = 1; i < 10; i++)
  320. // {
  321. // y = i * scaleH;
  322. // while(true)
  323. // {
  324. // x = x + 8;
  325. // QLineF line1(x, y, x + 2, y);
  326. // wGridLines.append(line1);
  327. // if(x >= width)
  328. // {
  329. // x = 0;
  330. // y = 0;
  331. // break;
  332. // }
  333. // }
  334. // }
  335. // x = 0;
  336. // y = 0;
  337. // for(int i = 1; i < 10; i++)
  338. // {
  339. // x = i * scaleW;
  340. // while(true)
  341. // {
  342. // y = y + 8;
  343. // QLineF line1(x, y, x, y + 2);
  344. // hGridLines.append(line1);
  345. // if(y >= height)
  346. // {
  347. // x = 0;
  348. // y = 0;
  349. // break;
  350. // }
  351. // }
  352. // }
  353. // painter.drawLines(wGridLines);
  354. // painter.drawLines(hGridLines);
  355. }
  356. /* 绘制眼图区域 */
  357. void OneEyeMap::drawEyeMap(QPainter &painter)
  358. {
  359. /* 绘制眼图,就是绘制 1000 * 256 个矩形 */
  360. painter.setPen(Qt::NoPen);
  361. for(int i = 0; i < g_HorPixel; i++)
  362. {
  363. for(int j = 0; j < g_VerPixel; j++)
  364. {
  365. if(m_eyeMapMatrix->dataMatrix[i][j].isDraw == false)
  366. {
  367. continue;
  368. }
  369. // painter.setBrush(QBrush(g_eyeMapMatrix.dataMatrix[i][j].color));
  370. painter.setBrush(m_eyeMapMatrix->dataMatrix[i][j].brush);
  371. painter.drawRect(m_eyeMapMatrix->dataMatrix[i][j].rect);
  372. }
  373. }
  374. }
  375. /* 绘制“暂无设备” */
  376. void OneEyeMap::drawNoDevice(QPainter &painter)
  377. {
  378. int x = (this->width() - m_rectNoDevice.width()) / 2;
  379. int y = (this->height() - m_rectNoDevice.height()) / 2;
  380. m_rectNoDevice.moveTo(x, y);
  381. painter.drawPixmap(m_rectNoDevice, *m_pixmapNoDevice);
  382. }
  383. /* 获取一个格子的电压值 */
  384. QString OneEyeMap::getVScaleValue(int index)
  385. {
  386. index = 2 - index;
  387. if(m_info.voltageRange == OscVoltageRange::CR_100MV)
  388. {
  389. return QString::number(0.1 * index) + "V";
  390. } else if(m_info.voltageRange == OscVoltageRange::CR_250MV)
  391. {
  392. return QString::number(0.25 * index) + "V";
  393. } else if(m_info.voltageRange == OscVoltageRange::CR_500MV)
  394. {
  395. return QString::number(0.5 * index) + "V";
  396. } else if(m_info.voltageRange == OscVoltageRange::CR_1V)
  397. {
  398. return QString::number(1.0 * index) + "V";
  399. } else if(m_info.voltageRange == OscVoltageRange::CR_2V5)
  400. {
  401. return QString::number(2.5 * index) + "V";
  402. } else if(m_info.voltageRange == OscVoltageRange::CR_5V)
  403. {
  404. return QString::number(5.0 * index) + "V";
  405. } else if(m_info.voltageRange == OscVoltageRange::CR_8V)
  406. {
  407. return QString::number(8.0 * index) + "V";
  408. } else
  409. {
  410. return QString::number(0.1 * index) + "V";
  411. }
  412. }
  413. /* 获取一个时间值 */
  414. QString OneEyeMap::getTimeValue(int index)
  415. {
  416. QString str;
  417. switch (m_info.tGridValue)
  418. {
  419. case OscTimeGridValue::TGV_20NS:
  420. str = QString::number(index * 20 / 1000.0) + "us";
  421. break;
  422. case OscTimeGridValue::TGV_50NS:
  423. str = QString::number(index * 50 / 1000.0) + "us";
  424. break;
  425. case OscTimeGridValue::TGV_100NS:
  426. str = QString::number(index * 100 / 1000.0) + "us";
  427. break;
  428. case OscTimeGridValue::TGV_200NS:
  429. str = QString::number(index * 200 / 1000.0) + "us";
  430. break;
  431. case OscTimeGridValue::TGV_500NS:
  432. str = QString::number(index * 500 / 1000.0) + "us";
  433. break;
  434. case OscTimeGridValue::TGV_1US:
  435. str = QString::number(index * 1) + "us";
  436. break;
  437. case OscTimeGridValue::TGV_2US:
  438. str = QString::number(index * 2) + "us";
  439. break;
  440. case OscTimeGridValue::TGV_5US:
  441. str = QString::number(index * 5) + "us";
  442. break;
  443. case OscTimeGridValue::TGV_10US:
  444. str = QString::number(index * 10) + "us";
  445. break;
  446. case OscTimeGridValue::TGV_20US:
  447. str = QString::number(index * 20) + "us";
  448. break;
  449. case OscTimeGridValue::TGV_100US:
  450. str = QString::number(index * 100) + "us";
  451. break;
  452. default:
  453. str = "us";
  454. break;
  455. }
  456. return str;
  457. }