OneEyeMap.cpp 14 KB

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