| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QDebug>
- #include <QMessageBox>
- #define WINDOW_SIZE 200
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- initThread();
- initPlot();
- initCombox();
- QList<ClickFrame*> frames = ui->widgetLegend->findChildren<ClickFrame*>();
- for(ClickFrame *frame : frames)
- {
- connect(frame,&ClickFrame::clicked,this,&MainWindow::onLegendClick);
- }
- }
- void MainWindow::initThread()
- {
- portTh = new QThread(this);
- serialthread = new SerialWorker();
- serialthread->moveToThread(portTh);
- connect(serialthread,&SerialWorker::signalNewTemps,this,&MainWindow::updatePlot,Qt::QueuedConnection);
- connect(portTh,&QThread::started,serialthread,&SerialWorker::start);
- connect(this,&MainWindow::sigOpenSerialBMC,serialthread,&SerialWorker::openBMCSerial,Qt::QueuedConnection);
- connect(this,&MainWindow::sigOpenSerialCPLD,serialthread,&SerialWorker::openCPLDSerial,Qt::QueuedConnection);
- connect(this,&MainWindow::siganlStartTest,serialthread,&SerialWorker::onStartTest,Qt::QueuedConnection);
- // connect(this,&MainWindow::destroyed,portTh,&QThread::quit);
- // connect(portTh,&QThread::finished,serialthread,&QObject::deleteLater);
- // connect(portTh,&QThread::finished,portTh,&QObject::deleteLater);
- portTh->start();
- }
- void MainWindow::onLegendClick()
- {
- ClickFrame *frame = qobject_cast<ClickFrame*>(sender());
- if(!frame) return;
- int index = m_names.indexOf(frame->objectName());
- if(m_currentIndex == index)
- {
- QCPGraph *graph = ui->customPlot->graph(index);
- QPen pen = graph->pen();
- pen.setWidth(2);
- graph->setPen(pen);
- m_currentIndex = -1;
- }
- else
- {
- if(m_currentIndex >= 0)
- {
- QCPGraph *beforeGraph = ui->customPlot->graph(m_currentIndex);
- QPen beforePen = beforeGraph->pen();
- beforePen.setWidth(2);
- beforeGraph->setPen(beforePen);
- }
- QCPGraph *nextGraph = ui->customPlot->graph(index);
- QPen nextPen = nextGraph->pen();
- nextPen.setWidth(3);
- nextGraph->setPen(nextPen);
- m_currentIndex = index;
- }
- ui->customPlot->replot();
- }
- void MainWindow::initCombox()
- {
- QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
- if(ports.isEmpty())
- return;
- for(const auto &info : ports)
- {
- ui->comboBox->addItem(info.portName());
- }
- }
- MainWindow::~MainWindow()
- {
- portTh->quit();
- portTh->wait();
- delete ui;
- }
- void MainWindow::initPlot()
- {
- m_names <<
- "CPU0_CHA"<<"CPU0_CHB"<<"CPU0_CHC"<<"CPU0_CHD"<<
- "CPU0_CHE"<<"CPU0_CHF"<<"CPU0_CHG"<<"CPU0_CHH"<<
- "CPU0_CHI"<<"CPU0_CHJ"<<"CPU0_CHK"<<"CPU0_CHL"<<
- "CPU1_CHA"<<"CPU1_CHB"<<"CPU1_CHC"<<"CPU1_CHD"<<
- "CPU1_CHE"<<"CPU1_CHF"<<"CPU1_CHG"<<"CPU1_CHH"<<
- "CPU1_CHI"<<"CPU1_CHJ"<<"CPU1_CHK"<<"CPU1_CHL";
- QList<QColor> colors =
- {
- QColor("#1f77b4"), QColor("#ff7f0e"), QColor("#2ca02c"),
- QColor("#d62728"), QColor("#9467bd"), QColor("#8c564b"),
- QColor("#e377c2"), QColor("#7f7f7f"), QColor("#bcbd22"),
- QColor("#17becf"), QColor("#393b79"), QColor("#637939"),
- QColor("#8c6d31"), QColor("#843c39"), QColor("#7b4173"),
- QColor("#3182bd"), QColor("#31a354"), QColor("#756bb1"),
- QColor("#636363"), QColor("#e6550d"), QColor("#969696"),
- QColor("#6baed6"), QColor("#74c476"), QColor("#fd8d3c")
- };
- for(int i = 0; i < m_names.size(); i++)
- {
- QCPGraph *graph = ui->customPlot->addGraph();
- graph->setName(m_names[i]); QPen pen(colors[i]);
- pen.setWidth(1);
- graph->setPen(pen);
- }
- ui->customPlot->xAxis->setLabel("Time");
- ui->customPlot->yAxis->setLabel("Temperature (°C)");
- ui->customPlot->xAxis->setRange(0, WINDOW_SIZE);
- ui->customPlot->yAxis->setRange(30.0, 100.0);
- ui->customPlot->yAxis->setNumberFormat("f");
- ui->customPlot->yAxis->setNumberPrecision(1);
- // 主刻度: 每5℃显示数字
- QSharedPointer<QCPAxisTickerFixed> yTicker(new QCPAxisTickerFixed);
- yTicker->setTickStep(5);
- ui->customPlot->yAxis->setTicker(yTicker);
- // 开启子刻度
- ui->customPlot->yAxis->setSubTicks(true);
- // X轴不要网格(不要竖线)
- ui->customPlot->xAxis->grid()->setVisible(false);
- // X轴不要子刻度
- ui->customPlot->xAxis->setSubTicks(false);
- // Y轴主网格
- ui->customPlot->yAxis->grid()->setVisible(true);
- // Y轴子网格
- ui->customPlot->yAxis->grid()->setSubGridVisible(true);
- // ================= 允许x轴拖动 缩放 =================
- ui->customPlot->setInteractions(QCP::iNone);
- ui->customPlot->replot();
- }
- void MainWindow::updatePlot(QVector<double> values)
- {
- for(int i = 0; i < 24; i++)
- {
- if(!std::isnan(values[i]))
- {
- ui->customPlot->graph(i)->addData(m_timeIndex,values[i]);
- }
- }
- for(int i=0;i<24;i++)
- {
- ui->customPlot->graph(i)->data()->removeBefore(m_timeIndex - WINDOW_SIZE);
- }
- if(m_timeIndex > WINDOW_SIZE)
- {
- ui->customPlot->xAxis->setRange(m_timeIndex - WINDOW_SIZE,m_timeIndex);
- }
- ui->customPlot->replot();
- m_timeIndex++;
- }
- void MainWindow::on_start_btn_clicked()
- {
- int index = ui->comboBox_temp->currentIndex();
- emit siganlStartTest(index);
- }
- void MainWindow::on_pushButton_BMC_clicked()
- {
- QString portname = ui->comboBox->currentText();
- emit sigOpenSerialBMC(portname);
- }
- void MainWindow::on_pushButton_CPLD_clicked()
- {
- QString portname = ui->comboBox->currentText();
- emit sigOpenSerialCPLD(portname);
- }
|