OneEyeMap.cpp 13 KB

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