mainwindow.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4. #include <QMessageBox>
  5. #define WINDOW_SIZE 200
  6. MainWindow::MainWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. , ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. initThread();
  12. initPlot();
  13. initCombox();
  14. QList<ClickFrame*> frames = ui->widgetLegend->findChildren<ClickFrame*>();
  15. for(ClickFrame *frame : frames)
  16. {
  17. connect(frame,&ClickFrame::clicked,this,&MainWindow::onLegendClick);
  18. }
  19. }
  20. void MainWindow::initThread()
  21. {
  22. portTh = new QThread(this);
  23. serialthread = new SerialWorker();
  24. serialthread->moveToThread(portTh);
  25. connect(serialthread,&SerialWorker::signalNewTemps,this,&MainWindow::updatePlot,Qt::QueuedConnection);
  26. connect(portTh,&QThread::started,serialthread,&SerialWorker::start);
  27. connect(this,&MainWindow::sigOpenSerialBMC,serialthread,&SerialWorker::openBMCSerial,Qt::QueuedConnection);
  28. connect(this,&MainWindow::sigOpenSerialCPLD,serialthread,&SerialWorker::openCPLDSerial,Qt::QueuedConnection);
  29. connect(this,&MainWindow::siganlStartTest,serialthread,&SerialWorker::onStartTest,Qt::QueuedConnection);
  30. // connect(this,&MainWindow::destroyed,portTh,&QThread::quit);
  31. // connect(portTh,&QThread::finished,serialthread,&QObject::deleteLater);
  32. // connect(portTh,&QThread::finished,portTh,&QObject::deleteLater);
  33. portTh->start();
  34. }
  35. void MainWindow::onLegendClick()
  36. {
  37. ClickFrame *frame = qobject_cast<ClickFrame*>(sender());
  38. if(!frame) return;
  39. int index = m_names.indexOf(frame->objectName());
  40. if(m_currentIndex == index)
  41. {
  42. QCPGraph *graph = ui->customPlot->graph(index);
  43. QPen pen = graph->pen();
  44. pen.setWidth(2);
  45. graph->setPen(pen);
  46. m_currentIndex = -1;
  47. }
  48. else
  49. {
  50. if(m_currentIndex >= 0)
  51. {
  52. QCPGraph *beforeGraph = ui->customPlot->graph(m_currentIndex);
  53. QPen beforePen = beforeGraph->pen();
  54. beforePen.setWidth(2);
  55. beforeGraph->setPen(beforePen);
  56. }
  57. QCPGraph *nextGraph = ui->customPlot->graph(index);
  58. QPen nextPen = nextGraph->pen();
  59. nextPen.setWidth(3);
  60. nextGraph->setPen(nextPen);
  61. m_currentIndex = index;
  62. }
  63. ui->customPlot->replot();
  64. }
  65. void MainWindow::initCombox()
  66. {
  67. QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
  68. if(ports.isEmpty())
  69. return;
  70. for(const auto &info : ports)
  71. {
  72. ui->comboBox->addItem(info.portName());
  73. }
  74. }
  75. MainWindow::~MainWindow()
  76. {
  77. portTh->quit();
  78. portTh->wait();
  79. delete ui;
  80. }
  81. void MainWindow::initPlot()
  82. {
  83. m_names <<
  84. "CPU0_CHA"<<"CPU0_CHB"<<"CPU0_CHC"<<"CPU0_CHD"<<
  85. "CPU0_CHE"<<"CPU0_CHF"<<"CPU0_CHG"<<"CPU0_CHH"<<
  86. "CPU0_CHI"<<"CPU0_CHJ"<<"CPU0_CHK"<<"CPU0_CHL"<<
  87. "CPU1_CHA"<<"CPU1_CHB"<<"CPU1_CHC"<<"CPU1_CHD"<<
  88. "CPU1_CHE"<<"CPU1_CHF"<<"CPU1_CHG"<<"CPU1_CHH"<<
  89. "CPU1_CHI"<<"CPU1_CHJ"<<"CPU1_CHK"<<"CPU1_CHL";
  90. QList<QColor> colors =
  91. {
  92. QColor("#1f77b4"), QColor("#ff7f0e"), QColor("#2ca02c"),
  93. QColor("#d62728"), QColor("#9467bd"), QColor("#8c564b"),
  94. QColor("#e377c2"), QColor("#7f7f7f"), QColor("#bcbd22"),
  95. QColor("#17becf"), QColor("#393b79"), QColor("#637939"),
  96. QColor("#8c6d31"), QColor("#843c39"), QColor("#7b4173"),
  97. QColor("#3182bd"), QColor("#31a354"), QColor("#756bb1"),
  98. QColor("#636363"), QColor("#e6550d"), QColor("#969696"),
  99. QColor("#6baed6"), QColor("#74c476"), QColor("#fd8d3c")
  100. };
  101. for(int i = 0; i < m_names.size(); i++)
  102. {
  103. QCPGraph *graph = ui->customPlot->addGraph();
  104. graph->setName(m_names[i]); QPen pen(colors[i]);
  105. pen.setWidth(1);
  106. graph->setPen(pen);
  107. }
  108. ui->customPlot->xAxis->setLabel("Time");
  109. ui->customPlot->yAxis->setLabel("Temperature (°C)");
  110. ui->customPlot->xAxis->setRange(0, WINDOW_SIZE);
  111. ui->customPlot->yAxis->setRange(30.0, 100.0);
  112. ui->customPlot->yAxis->setNumberFormat("f");
  113. ui->customPlot->yAxis->setNumberPrecision(1);
  114. // 主刻度: 每5℃显示数字
  115. QSharedPointer<QCPAxisTickerFixed> yTicker(new QCPAxisTickerFixed);
  116. yTicker->setTickStep(5);
  117. ui->customPlot->yAxis->setTicker(yTicker);
  118. // 开启子刻度
  119. ui->customPlot->yAxis->setSubTicks(true);
  120. // X轴不要网格(不要竖线)
  121. ui->customPlot->xAxis->grid()->setVisible(false);
  122. // X轴不要子刻度
  123. ui->customPlot->xAxis->setSubTicks(false);
  124. // Y轴主网格
  125. ui->customPlot->yAxis->grid()->setVisible(true);
  126. // Y轴子网格
  127. ui->customPlot->yAxis->grid()->setSubGridVisible(true);
  128. // ================= 允许x轴拖动 缩放 =================
  129. ui->customPlot->setInteractions(QCP::iNone);
  130. ui->customPlot->replot();
  131. }
  132. void MainWindow::updatePlot(QVector<double> values)
  133. {
  134. for(int i = 0; i < 24; i++)
  135. {
  136. if(!std::isnan(values[i]))
  137. {
  138. ui->customPlot->graph(i)->addData(m_timeIndex,values[i]);
  139. }
  140. }
  141. for(int i=0;i<24;i++)
  142. {
  143. ui->customPlot->graph(i)->data()->removeBefore(m_timeIndex - WINDOW_SIZE);
  144. }
  145. if(m_timeIndex > WINDOW_SIZE)
  146. {
  147. ui->customPlot->xAxis->setRange(m_timeIndex - WINDOW_SIZE,m_timeIndex);
  148. }
  149. ui->customPlot->replot();
  150. m_timeIndex++;
  151. }
  152. void MainWindow::on_start_btn_clicked()
  153. {
  154. int index = ui->comboBox_temp->currentIndex();
  155. emit siganlStartTest(index);
  156. }
  157. void MainWindow::on_pushButton_BMC_clicked()
  158. {
  159. QString portname = ui->comboBox->currentText();
  160. emit sigOpenSerialBMC(portname);
  161. }
  162. void MainWindow::on_pushButton_CPLD_clicked()
  163. {
  164. QString portname = ui->comboBox->currentText();
  165. emit sigOpenSerialCPLD(portname);
  166. }