OneEyeMap.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "OneEyeMap.h"
  2. #include "ui_oneeyemap.h"
  3. #include <QApplication>
  4. #include <QPainter>
  5. #include <QPaintEvent>
  6. OneEyeMap::OneEyeMap(QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::OneEyeMap)
  9. {
  10. ui->setupUi(this);
  11. m_logger = spdlog::get("EyeMap");
  12. if(m_logger == nullptr)
  13. {
  14. SPDLOG_ERROR("获取 EyeMap logger 失败");
  15. return;
  16. }
  17. /* 初始化变量,加载qss */
  18. // QFile fileQss(":/qss/OneEyeMap/OneEyeMap.qss");
  19. QFile fileQss(":/qss/OneEyeMap/OneEyeMap.qss");
  20. if(fileQss.open(QFile::ReadOnly))
  21. {
  22. QString qss = fileQss.readAll();
  23. this->setStyleSheet(qss);
  24. fileQss.close();
  25. } else
  26. {
  27. SPDLOG_LOGGER_ERROR(m_logger, "加载qss文件失败");
  28. }
  29. /* 自定义大小 */
  30. // setFixedSize(1600, 900);
  31. /* 设置水平刻度和垂直刻度数目 */
  32. m_hScaleNum = 6;
  33. m_vScaleNum = 4;
  34. /* 设置绘制区域 */
  35. m_leftMargin = 70;
  36. m_topMargin = 24;
  37. m_rightMargin = 24;
  38. m_bottomMargin = 24;
  39. /* 设置刻度区域 */
  40. m_rectScaleValue.setX(0);
  41. m_rectScaleValue.setY(ui->widget_title->height());
  42. m_rectScaleValue.setWidth(this->width());
  43. m_rectScaleValue.setHeight(this->height() - ui->widget_title->height());
  44. /* 设置眼图区域 */
  45. m_rectEyeMap.setX(m_leftMargin);
  46. m_rectEyeMap.setY(m_rectScaleValue.y() + m_topMargin);
  47. m_rectEyeMap.setWidth(m_rectScaleValue.width() - m_leftMargin - m_rightMargin);
  48. m_rectEyeMap.setHeight(m_rectScaleValue.height() - m_topMargin - m_bottomMargin);
  49. /* 初始化全局数据 */
  50. g_eyeMapMatrix.initEyeMapData(this->width(), this->height());
  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::do_update()
  65. {
  66. update();
  67. }
  68. /**
  69. * @brief 绘制图形
  70. *
  71. * @param event
  72. */
  73. void OneEyeMap::paintEvent(QPaintEvent *event)
  74. {
  75. g_eyeMapMatrix.mutexEyeData.lock();
  76. QPainter painter(this);
  77. painter.setRenderHint(QPainter::Antialiasing, true);
  78. /******************** 绘制标题背景 ********************/
  79. QBrush brush;
  80. brush.setStyle(Qt::SolidPattern);
  81. brush.setColor("#000000");
  82. painter.setBrush(brush);
  83. painter.drawRect(ui->widget_title->geometry());
  84. /******************** 绘制刻度值 ********************/
  85. drawScaleValue(painter);
  86. /******************** 绘制刻度网格 *********************/
  87. drawScale(painter);
  88. // SPDLOG_LOGGER_DEBUG(m_logger, "width = {}, height = {}", this->width(), this->height());
  89. // SPDLOG_LOGGER_DEBUG(m_logger, "m_rectScaleValue: x = {}, y = {}, width = {}, height = {}", m_rectScaleValue.x(), m_rectScaleValue.y(), m_rectScaleValue.width(), m_rectScaleValue.height());
  90. // SPDLOG_LOGGER_DEBUG(m_logger, "m_rectEyeMap: x = {}, y = {}, width = {}, height = {}", m_rectEyeMap.x(), m_rectEyeMap.y(), m_rectEyeMap.width(), m_rectEyeMap.height());
  91. /* 绘制眼图,就是绘制 1000 * 256 个矩形 */
  92. // painter.setPen(QPen(Qt::NoPen));
  93. // for(int i = 0; i < 1000; i++)
  94. // {
  95. // for(int j = 0; j < 256; j++)
  96. // {
  97. // if(g_eyeMapMatrix.dataMatrix[i][j].isDraw == false)
  98. // {
  99. // continue;
  100. // }
  101. // // painter.setBrush(QBrush(g_eyeMapMatrix.dataMatrix[i][j].color));
  102. // painter.setBrush(g_eyeMapMatrix.dataMatrix[i][j].brush);
  103. // painter.drawRect(g_eyeMapMatrix.dataMatrix[i][j].rect);
  104. // }
  105. // }
  106. g_eyeMapMatrix.mutexEyeData.unlock();
  107. event->accept();
  108. }
  109. /* 缩放事件 */
  110. void OneEyeMap::resizeEvent(QResizeEvent *event)
  111. {
  112. /* 设置刻度区域 */
  113. m_rectScaleValue.setX(0);
  114. m_rectScaleValue.setY(ui->widget_title->height());
  115. m_rectScaleValue.setWidth(this->width());
  116. m_rectScaleValue.setHeight(this->height() - ui->widget_title->height());
  117. /* 设置眼图区域 */
  118. m_rectEyeMap.setX(m_leftMargin);
  119. m_rectEyeMap.setY(m_rectScaleValue.y() + m_topMargin);
  120. m_rectEyeMap.setWidth(m_rectScaleValue.width() - m_leftMargin - m_rightMargin);
  121. m_rectEyeMap.setHeight(m_rectScaleValue.height() - m_topMargin - m_bottomMargin);
  122. /* 刷新一下页面 */
  123. update();
  124. event->accept();
  125. }
  126. /* 绘制刻度区域 */
  127. void OneEyeMap::drawScaleValue(QPainter &painter)
  128. {
  129. /* 绘制背景颜色 */
  130. QBrush brush;
  131. brush.setColor("#1E1E1E");
  132. brush.setStyle(Qt::SolidPattern);
  133. painter.setBrush(brush);
  134. painter.drawRect(m_rectScaleValue);
  135. /* 绘制刻度值 */
  136. QRect rectText(0, 0, 50, 14);
  137. QFont font;
  138. font.setFamily("思源黑体R");
  139. font.setPixelSize(14);
  140. painter.setFont(font);
  141. int oneScale = m_rectEyeMap.height() / m_vScaleNum;
  142. // SPDLOG_LOGGER_DEBUG(m_logger, "oneScale = {}", oneScale);
  143. /* 绘制电压值 */
  144. for(int i = 0; i <= m_vScaleNum; i++)
  145. {
  146. /* 计算字体区域坐标 */
  147. rectText.moveTo(10, m_rectEyeMap.y() + oneScale * i - 7);
  148. painter.drawText(rectText, Qt::AlignRight | Qt::AlignVCenter, getVScaleValue(i));
  149. }
  150. /* 绘制时间刻度值 */
  151. int oneTime = m_rectEyeMap.width() / m_hScaleNum;
  152. for(int i = 0; i <= m_hScaleNum; i++)
  153. {
  154. rectText.moveTo(m_rectEyeMap.x() + oneTime * i - 25, m_rectEyeMap.y() + m_rectEyeMap.height() + 4);
  155. painter.drawText(rectText, Qt::AlignCenter, getTimeValue(i));
  156. }
  157. }
  158. /* 绘制刻度 */
  159. void OneEyeMap::drawScale(QPainter &painter)
  160. {
  161. /* 限制操作区域,不过坐标零点依旧是widget的(0,0) */
  162. painter.setClipRect(m_rectEyeMap);
  163. /* 绘制背景颜色 */
  164. QBrush brush;
  165. brush.setColor(QColor(0, 0, 0, 0.8 * 255));
  166. brush.setStyle(Qt::SolidPattern);
  167. painter.setBrush(brush);
  168. painter.drawRect(m_rectEyeMap);
  169. /* 绘制刻度线 */
  170. QPen pen;
  171. pen.setWidth(1);
  172. pen.setStyle(Qt::SolidLine);
  173. /* 绘制中线 */
  174. int startX = m_rectEyeMap.x();
  175. int startY = m_rectEyeMap.y();
  176. int width = m_rectEyeMap.width();
  177. int height = m_rectEyeMap.height();
  178. pen.setColor(QColor(255, 255, 255, 0.35 * 255));
  179. pen.setWidth(2);
  180. painter.setPen(pen);
  181. painter.drawLine(startX, startY + (height / 2), startX + width, startY + (height / 2)); /* 绘制水平中线 */
  182. painter.drawLine(startX + (width / 2), startY, startX + (width / 2), startY + height); /* 绘制垂直中线 */
  183. /* 绘制中线上的刻度 */
  184. int hScale = m_hScaleNum * 5;
  185. int vScale = m_vScaleNum * 5;
  186. double scaleW = width * 1.0 / hScale;
  187. double scaleH = height * 1.0 / vScale;
  188. QVector<QLineF> wLines;
  189. QVector<QLineF> hLines;
  190. for(int i = 0; i < hScale; i++)
  191. {
  192. QLineF line1(startX + i * scaleW, startY + height / 2.0 - 4, startX + i * scaleW, startY + height / 2.0 + 4);
  193. wLines.append(line1);
  194. }
  195. for(int i = 0; i < vScale; i++)
  196. {
  197. QLineF line2(startX + width / 2.0 - 4, startY + height - i * scaleH, startX + width / 2.0 + 4, startY + height - i * scaleH);
  198. hLines.append(line2);
  199. }
  200. painter.drawLines(wLines);
  201. painter.drawLines(hLines);
  202. /* 绘制网格,这里是网格线 */
  203. pen.setWidth(1);
  204. pen.setColor(QColor(255, 255, 255, 0.15 * 255));
  205. painter.setPen(pen);
  206. scaleH = height * 1.0 / m_vScaleNum;
  207. scaleW = width * 1.0 / m_hScaleNum;
  208. /* 水平线 */
  209. for(int i = 1; i < m_vScaleNum; i++)
  210. {
  211. int y = startY + i * scaleH;
  212. QLineF line1(startX, y, startX + width, y);
  213. painter.drawLine(line1);
  214. }
  215. /* 垂直线 */
  216. for(int i = 1; i < m_hScaleNum; i++)
  217. {
  218. int x = startX + i * scaleW;
  219. QLineF line1(x, startY, x, startY + height);
  220. painter.drawLine(line1);
  221. }
  222. /* 绘制虚线 */
  223. // painter.setPen(pen);
  224. // scaleW = width / 10.0;
  225. // scaleH = height / 10.0;
  226. // int x = 0;
  227. // int y = 0;
  228. // QVector<QLineF> wGridLines;
  229. // QVector<QLineF> hGridLines;
  230. // for(int i = 1; i < 10; i++)
  231. // {
  232. // y = i * scaleH;
  233. // while(true)
  234. // {
  235. // x = x + 8;
  236. // QLineF line1(x, y, x + 2, y);
  237. // wGridLines.append(line1);
  238. // if(x >= width)
  239. // {
  240. // x = 0;
  241. // y = 0;
  242. // break;
  243. // }
  244. // }
  245. // }
  246. // x = 0;
  247. // y = 0;
  248. // for(int i = 1; i < 10; i++)
  249. // {
  250. // x = i * scaleW;
  251. // while(true)
  252. // {
  253. // y = y + 8;
  254. // QLineF line1(x, y, x, y + 2);
  255. // hGridLines.append(line1);
  256. // if(y >= height)
  257. // {
  258. // x = 0;
  259. // y = 0;
  260. // break;
  261. // }
  262. // }
  263. // }
  264. // painter.drawLines(wGridLines);
  265. // painter.drawLines(hGridLines);
  266. }
  267. /* 绘制眼图区域 */
  268. void OneEyeMap::drawEyeMap(QPainter &painter)
  269. {
  270. }
  271. /* 获取一个格子的电压值 */
  272. QString OneEyeMap::getVScaleValue(int index)
  273. {
  274. index = 2 - index;
  275. if(m_cRange == OscChannelRange::CR_100MV)
  276. {
  277. return QString::number(0.1 * index) + "V";
  278. } else if(m_cRange == OscChannelRange::CR_250MV)
  279. {
  280. return QString::number(0.25 * index) + "V";
  281. } else if(m_cRange == OscChannelRange::CR_500MV)
  282. {
  283. return QString::number(0.5 * index) + "V";
  284. } else if(m_cRange == OscChannelRange::CR_1V)
  285. {
  286. return QString::number(1.0 * index) + "V";
  287. } else if(m_cRange == OscChannelRange::CR_2V5)
  288. {
  289. return QString::number(2.5 * index) + "V";
  290. } else if(m_cRange == OscChannelRange::CR_5V)
  291. {
  292. return QString::number(5.0 * index) + "V";
  293. } else if(m_cRange == OscChannelRange::CR_8V)
  294. {
  295. return QString::number(8.0 * index) + "V";
  296. } else
  297. {
  298. return QString::number(0.1 * index) + "V";
  299. }
  300. }
  301. /* 获取一个时间值 */
  302. QString OneEyeMap::getTimeValue(int index)
  303. {
  304. QString str;
  305. switch (m_tGridValue)
  306. {
  307. case OscTimeGridValue::TGV_20NS:
  308. str = QString::number(index * 20 / 1000.0) + "us";
  309. break;
  310. case OscTimeGridValue::TGV_50NS:
  311. str = QString::number(index * 50 / 1000.0) + "us";
  312. break;
  313. case OscTimeGridValue::TGV_100NS:
  314. str = QString::number(index * 100 / 1000.0) + "us";
  315. break;
  316. case OscTimeGridValue::TGV_200NS:
  317. str = QString::number(index * 200 / 1000.0) + "us";
  318. break;
  319. case OscTimeGridValue::TGV_500NS:
  320. str = QString::number(index * 500 / 1000.0) + "us";
  321. break;
  322. case OscTimeGridValue::TGV_1US:
  323. str = QString::number(index * 1) + "us";
  324. break;
  325. case OscTimeGridValue::TGV_2US:
  326. str = QString::number(index * 2) + "us";
  327. break;
  328. case OscTimeGridValue::TGV_5US:
  329. str = QString::number(index * 5) + "us";
  330. break;
  331. case OscTimeGridValue::TGV_10US:
  332. str = QString::number(index * 10) + "us";
  333. break;
  334. case OscTimeGridValue::TGV_20US:
  335. str = QString::number(index * 20) + "us";
  336. break;
  337. case OscTimeGridValue::TGV_100US:
  338. str = QString::number(index * 100) + "us";
  339. break;
  340. default:
  341. str = "us";
  342. break;
  343. }
  344. return str;
  345. }