#include "WaveDisplay.h" #include "ui_WaveDisplay.h" #include "NetMgr.h" #include #include #include "log.h" CWaveDisPlay::CWaveDisPlay(QWidget *parent) : QWidget(parent), ui(new Ui::CWaveDisPlay) { ui->setupUi(this); m_ChannelID = ""; m_PackgNum = 1; bFlag = false; m_Times = 0; m_Count = 0; OneSecond = true; ui->widget->setProperty("flag", "Title"); ui->widget_4->setProperty("flag", "normal"); ui->widget_wave->plotLayout()->insertRow(0); QString strTitle = QString("主频频率 %1 Hz,主频幅值 %2,相位角 %3").arg(0).arg(0).arg(0); customLogMessageHandler(QtDebugMsg,strTitle); m_title = new QCPTextElement(ui->widget_wave, strTitle); ui->widget_wave->plotLayout()->addElement(0, 0, m_title); InitWindows(); } CWaveDisPlay::~CWaveDisPlay() { delete ui; } void CWaveDisPlay::InitWindows() { m_pNetMgr = new NetMgr(this); ui->Btn_Timewave->setStyleSheet(" QPushButton{ border-image:url(:/image/Btn/timewave.png) 5; }\ QPushButton:hover{ border-image:url(:/image/Btn/timewave-p.png) 5; }\ QPushButton:pressed{border-image:url(:/image/Btn/timewave-p.png) 5; }\ QPushButton:checked{border-image:url(:/image/Btn/timewave-p.png) 5; } " ); ui->Btn_Timewave->setChecked(true); ui->pushButton_1S->setStyleSheet(" QPushButton{ border-image:url(:/image/Btn/normal_Btn.png); color:#1f5188 }\ QPushButton:hover{ border-image:url(:/image/Btn/normal_Btn_p.png); color:#ffffff }\ QPushButton:pressed{border-image:url(:/image/Btn/normal_Btn_p.png); color:#ffffff }\ QPushButton:checked{border-image:url(:/image/Btn/normal_Btn_p.png); color:#ffffff } " ); ui->pushButton_1S->setChecked(true); //绑定HTTP消息响应 connect(m_pNetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&))); on_comboBox_channel_2_currentTextChanged(""); tracer = new QCPItemTracer(ui->widget_wave); tracer->setStyle(QCPItemTracer::tsCrosshair); tracer->setPen(QPen(Qt::red)); tracer->setBrush(Qt::red); tracer->setSize(6); tracer->setVisible(false); tracer->setInterpolating(false); QFont font; font.setPixelSize(20); tracerLabel = new QCPItemText(ui->widget_wave); tracerLabel->setLayer("overlay"); tracerLabel->setPen(QPen(Qt::white)); tracerLabel->setBrush(QColor(64, 156, 225)); tracerLabel->setPadding(QMargins(2,2,2,2));//边界宽度 tracerLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignLeft);//文字布局:顶、左对齐 tracerLabel->position->setType(QCPItemPosition::ptAxisRectRatio);//位置类型(当前轴范围的比例为单位/实际坐标为单位) tracerLabel->position->setCoords(0, 0); tracerLabel->setFont(font); tracerLabel->setText(""); ui->comboBox_channel_2->clear(); ui->comboBox_channel_2->addItem("请选择通道..."); for (int i = 0; i < g_ChannelBaseInfo.size(); i++) { ui->comboBox_channel_2->addItem(g_ChannelBaseInfo[i].channelName); } ui->comboBox_channel_2->setView(new QListView()); ui->comboBox_channel_2->setCurrentText("请选择通道..."); Cursor(); } void CWaveDisPlay::on_Btn_Hand_clicked() { ui->widget_wave->setSelectionRectMode(QCP::SelectionRectMode::srmNone); if(ui->Btn_Hand->isChecked()){ //交互设置 ui->widget_wave->setInteractions(QCP::iRangeDrag | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iMultiSelect); ui->Btn_Zoom->setChecked(false); }else if(!ui->Btn_Hand->isChecked()){ ui->widget_wave->setInteractions(QCP::iNone); } } void CWaveDisPlay::on_Btn_Zoom_clicked() { if(ui->Btn_Zoom->isChecked()){ ui->Btn_Hand->setChecked(false); ui->widget_wave->setSelectionRectMode(QCP::SelectionRectMode::srmZoom); ui->widget_wave->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));//虚线 ui->widget_wave->selectionRect()->setBrush(QBrush(QColor(255, 222, 173,50)));//NavajoWhite } else if(!ui->Btn_Zoom->isChecked()){ ui->widget_wave->setSelectionRectMode(QCP::SelectionRectMode::srmNone); } } void CWaveDisPlay::on_Btn_Scales_clicked() { ui->widget_wave->setSelectionRectMode(QCP::SelectionRectMode::srmNone); ui->widget_wave->rescaleAxes(true); ui->widget_wave->replot(); } void CWaveDisPlay::on_comboBox_channel_currentTextChanged(const QString &arg1) { } void CWaveDisPlay::ParseDataTimeWave(QJsonObject & objContent) { int packageMax = objContent["packageMax"].toInt(); int package = objContent["package"].toInt(); if(packageMax > 1 && m_PackgNum < packageMax /*&& package + 1 <= packageMax*/ && !bFlag ){ QJsonObject allObj,cmdBody; allObj.insert("cmd", "30"); cmdBody.insert("package",m_PackgNum); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); m_PackgNum += 1; } QString data = objContent["Data"].toString(); if(package + 1 == packageMax){ m_WaveData = m_WaveData + data; qDebug() << "package" <widget_wave->xAxis->setLabel("Time(s)"); ui->widget_wave->yAxis->setLabel(m_ChannelUnit); QString str = QString("wave size = %1").arg(m_ListWaveData.size()); customLogMessageHandler(QtDebugMsg,str); double gap = 0.0; if(OneSecond){ ui->widget_wave->xAxis->setRange(0, 1); gap = (double)1/(double)m_ListWaveData.size(); }else{ ui->widget_wave->xAxis->setRange(0, 4); gap = (double)4/(double)m_ListWaveData.size(); } double f = 0.0; QVector x,y; QCPGraph* graph = ui->widget_wave->addGraph(ui->widget_wave->xAxis, ui->widget_wave->yAxis); for(int i = 0; i < m_ListWaveData.size();i++){ x.push_back(f); y.push_back(m_ListWaveData[i].toDouble()); f += gap; } QVector::iterator max = std::max_element(std::begin(y), std::end(y)); if(m_ChannelType == "PULSE_CURRENT"){ QVector::iterator min = std::min_element(std::begin(y), std::end(y)); double min_y = *min; if(min_y < 1){ double sum = std::accumulate(std::begin(y), std::end(y), 0.0); double size = y.size(); double mean = sum/size; for(int i = 0; i < size;i++){ y[i] = y[i] - mean; } } max = std::max_element(std::begin(y), std::end(y)); } double biggest = *max; qDebug() << "x" << x.size() << "y" << y.size() << endl; graph->setData(x,y); ui->widget_wave->yAxis->setRange(-1,biggest+2); on_Btn_Scales_clicked(); ui->widget_wave->replot(); if(ui->pushButton_1S->isChecked() && (m_ChannelType == "TACHOMETER" || (m_speedRefChannelId != "" && m_speedRefChannelId != "- -"))){ QJsonObject allObj,cmdBody; allObj.insert("cmd", "92");//获取键相 cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); } }else{ m_WaveData = m_WaveData + data + ","; } } void CWaveDisPlay::ParseDataFsWave(QJsonObject & objContent) { int packageMax = objContent["packageMax"].toInt(); int package = objContent["package"].toInt(); if(packageMax > 1 && m_PackgNum < packageMax && package + 1 <= packageMax && !bFlag ){ QJsonObject allObj,cmdBody; allObj.insert("cmd", "32"); cmdBody.insert("package",m_PackgNum); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); m_PackgNum += 1; } QString data = objContent["Data"].toString(); qDebug() << data << endl; m_WaveData = m_WaveData + data + ","; if(m_PackgNum == packageMax){ m_PackgNum = 1; bFlag = true; m_ListWaveData = m_WaveData.split(","); //qDebug() << m_ListWaveData << endl; ui->widget_wave->xAxis->setRange(0, 1); ui->widget_wave->xAxis->setLabel("Fs(Hz)"); ui->widget_wave->yAxis->setLabel(m_ChannelUnit); QVector x,y; //QCPGraph* graph = ui->widget_wave->addGraph(ui->widget_wave->xAxis, ui->widget_wave->yAxis); QCPBars *bars = new QCPBars(ui->widget_wave->xAxis, ui->widget_wave->yAxis); bars->setAntialiased(false); bars->setPen(QPen(QColor(31, 81, 136).lighter(130))); // 设置柱状图的边框颜色 bars->setBrush(QColor(31, 81, 136)); // 设置柱状图的画刷颜色 bars->setWidth(0.02); float gap = 0; for(int i = 0; i < m_ListWaveData.size();i++){ x.push_back(gap); y.push_back(qAbs(m_ListWaveData[i].toDouble())); if(g_strFre == "1") gap += 1; else if(g_strFre == "0.25") gap += 0.25; } y[0] = 0; //qDebug() << "x" <setData(x,y); bars->setData(x, y); ui->widget_wave->xAxis->setRange(0, y.size()); on_Btn_Scales_clicked(); ui->widget_wave->replot(); } } void CWaveDisPlay::ParseDataKeyPhase(QJsonObject& objContent) { m_ListKeyPhaseData.clear(); ui->widget_wave->plotLayout()->remove(m_title); m_title = NULL; QString data = objContent["Data"].toString(); double PhaseAngle = objContent["PhaseAngle"].toDouble(); double amp1x = objContent["1xamp"].toDouble(); double freq1x = objContent["1xfreq"].toDouble(); m_ListKeyPhaseData = data.split(","); qDebug() << "m_ListKeyPhaseData" << m_ListKeyPhaseData << endl; QString strAmp1x = QString::number(amp1x, 'f', 2); // 保留两位小数 QString strPhaseAngle = QString::number(PhaseAngle, 'f', 1); QString strTitle = QString("主频频率 %1 Hz,主频幅值 %2,相位角 %3").arg(freq1x).arg(strAmp1x).arg(strPhaseAngle); customLogMessageHandler(QtDebugMsg,strTitle); m_title = new QCPTextElement(ui->widget_wave, strTitle); ui->widget_wave->plotLayout()->addElement(0, 0, m_title); QVector x,y,x2,y2; double f = 0; double gap = (double)1/(double)m_ListWaveData.size(); for(int i = 0; i < m_ListWaveData.size();i++){ for(int j = 0; j < m_ListKeyPhaseData.size();j++){ if(m_ListKeyPhaseData[j].toInt() == i){ x2.push_back(f); y2.push_back(m_ListWaveData[i].toDouble()); } } f += gap; } QCPGraph* dwPoints = new QCPGraph(ui->widget_wave->xAxis, ui->widget_wave->yAxis); dwPoints->setAdaptiveSampling(false); dwPoints->setLineStyle(QCPGraph::lsNone); dwPoints->setScatterStyle(QCPScatterStyle::ssDot); dwPoints->setPen(QPen(QBrush(Qt::red), 5)); dwPoints->addData(x2, y2); ui->widget_wave->replot(); } void CWaveDisPlay::slotNetMgr(QString sAddr, const QVariant &msg) { QJsonObject objec = msg.value(); if(!objec["success"].toBool()){ QString strMessage = objec["message"].toString(); if(strMessage.isEmpty()) QMessageBox::information(this, QStringLiteral("提示"), "连接失败!"); else QMessageBox::information(this, QStringLiteral("提示"), strMessage); return; } if(objec.contains("cmd")) { QJsonValue arrays_value = objec.take("cmd"); qDebug()<<"cmd ="<widget_wave->clearGraphs(); ui->widget_wave->clearPlottables(); ui->widget_wave->replot(); m_WaveData = ""; m_ListWaveData.clear(); } void CWaveDisPlay::on_Btn_Timewave_clicked() { ui->Btn_Fswave->setChecked(false); if(!ui->Btn_Timewave->isChecked()){ InitGraph(); return; } InitGraph(); QJsonObject allObj,cmdBody; allObj.insert("cmd", "30"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); } void CWaveDisPlay::on_Btn_Fswave_clicked() { if(ui->pushButton_4S->isChecked()){ QMessageBox::information(this, QStringLiteral("提示"), "请切换到1S波形!"); ui->Btn_Fswave->setChecked(false); return; } ui->Btn_Timewave->setChecked(false); if(!ui->Btn_Fswave->isChecked()){ InitGraph(); return; } InitGraph(); QJsonObject allObj,cmdBody; allObj.insert("cmd", "32"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); } void CWaveDisPlay::mouseMoveEvent(QMouseEvent *event) { tracer->setVisible(true); tracerLabel->setVisible(true); if(ui->Btn_Timewave->isChecked()){ //将像素点转换成qcustomplot中的坐标值,并通过setGraphKey将锚点值设为真实数据值。tracer->setGraphKey(xAxis->pixelToCoord(event->pos().x())); int graphCount = ui->widget_wave->graphCount(); if(graphCount < 1) return; //获得鼠标位置处对应的横坐标数据x double x = ui->widget_wave->xAxis->pixelToCoord(event->pos().x()); //遍历曲线 //for (int i = 0; i < graphCount && tracer->visible(); ++i) { //显示锚点 QCPGraph *mGraph = ui->widget_wave->graph(0); tracer->setGraph(mGraph);//将锚点设置到被选中的曲线上 tracer->setGraphKey(x); //将游标横坐标设置成刚获得的横坐标数据x tracer->updatePosition(); //使得刚设置游标的横纵坐标位置生效 double xValue = tracer->position->key(); double yValue = tracer->position->value(); tracerLabel->setVisible(true); tracerLabel->setText(QString("X: %1 Y: %2").arg( QString::number(xValue)).arg(QString::number(yValue))); //显示tip框 QCPDataContainer::const_iterator coorPoint = ui->widget_wave->graph(0)->data().data()->findBegin(xValue, true);//true代表向左搜索 } }else if(ui->Btn_Fswave->isChecked()){ // 获取鼠标位置并转换为坐标 double x = ui->widget_wave->xAxis->pixelToCoord(event->pos().x()); // 找到最近的条形图数据点 double minDistance = std::numeric_limits::max(); double nearestBarKey = 0; double nearestBarValue = 0; for (int i = 0; i < ui->widget_wave->plottableCount();i++) { if (QCPBars *bars = qobject_cast(ui->widget_wave->plottable(i))) { for (int i = 0; i < bars->data()->size(); ++i) { double barKey = bars->data()->at(i)->key; double distance = std::abs(barKey - x); if (distance < minDistance) { minDistance = distance; nearestBarKey = barKey; nearestBarValue = bars->data()->at(i)->value; } } } } tracerLabel->setText(QString("X: %1 Y: %2").arg( QString::number(nearestBarKey)).arg(QString::number(nearestBarValue))); // 设置游标位置 tracer->position->setCoords(nearestBarKey, nearestBarValue); } //重绘 ui->widget_wave->replot(); } void CWaveDisPlay::Cursor() { tracer->setVisible(false); tracerLabel->setVisible(false); connect(ui->widget_wave, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMoveEvent(QMouseEvent*))); ui->widget_wave->replot(); } void CWaveDisPlay::on_pushButton_refresh_clicked() { on_comboBox_channel_2_currentTextChanged(""); } void CWaveDisPlay::on_comboBox_channel_2_currentTextChanged(const QString &arg1) { if(ui->comboBox_channel_2->count() < 1) return; m_PackgNum = 1; bFlag = false; ui->widget_wave->clearGraphs(); ui->widget_wave->clearPlottables(); ui->widget_wave->replot(); m_WaveData = ""; m_ListWaveData.clear(); QString strChannelName = ui->comboBox_channel_2->currentText(); if(strChannelName == "请选择通道..."){ return; } int i = 0; for (i = 0; i < g_ChannelBaseInfo.size(); i++) { if(strChannelName == g_ChannelBaseInfo[i].channelName){ break; } } m_ChannelID = g_ChannelBaseInfo[i].channelID; m_ChannelType = g_ChannelBaseInfo[i].channelType; m_speedRefChannelId = g_ChannelBaseInfo[i].speedRefChannelId; m_ChannelUnit = g_ChannelBaseInfo[i].sensorEngineeringUnit; if(ui->Btn_Timewave->isChecked()) { m_Times = 0; if(ui->pushButton_4S->isChecked()){ OneSecond = false; QJsonObject allObj,cmdBody; allObj.insert("cmd", "89"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); }else if(ui->pushButton_1S->isChecked()){ OneSecond = true; QJsonObject allObj,cmdBody; allObj.insert("cmd", "30"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); QString str = QString("当前通道 %1,通道类型 %2,转速参考通道 %3").arg(m_ChannelID).arg(m_ChannelType).arg(m_speedRefChannelId); customLogMessageHandler(QtDebugMsg,str); } } else if(ui->Btn_Fswave->isChecked()) { QJsonObject allObj,cmdBody; allObj.insert("cmd", "32"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); } } void CWaveDisPlay::timerEvent(QTimerEvent *ev) { if(ev->timerId() == id1) { QJsonObject allObj,cmdBody; allObj.insert("cmd", "30"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); m_Times = m_Times + 1; qDebug() << "m_Times" << m_Times << endl; if(m_Times == 4){ killTimer(id1); m_Times = 0; } } if(ev->timerId() == id2){ m_PackgNum = 1; bFlag = false; ui->widget_wave->clearGraphs(); ui->widget_wave->clearPlottables(); ui->widget_wave->replot(); m_WaveData = ""; m_ListWaveData.clear(); if(ui->Btn_Timewave->isChecked()){ OneSecond = true; QJsonObject allObj,cmdBody; allObj.insert("cmd", "30"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); }else if(ui->Btn_Fswave->isChecked()) { QJsonObject allObj,cmdBody; allObj.insert("cmd", "32"); cmdBody.insert("package",0); cmdBody.insert("channelId",m_ChannelID); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); m_pNetMgr->PostJson(req,allObj); } } } void CWaveDisPlay::on_pushButton_1S_clicked() { OneSecond = true; ui->pushButton_4S->setChecked(false); if(ui->pushButton_1S->isChecked()){ on_comboBox_channel_2_currentTextChanged(""); return; } } void CWaveDisPlay::on_pushButton_4S_clicked() { if(ui->Btn_Fswave->isChecked()){ QMessageBox::information(this, QStringLiteral("提示"), "请切换到时域图!"); ui->pushButton_4S->setChecked(false); return; } OneSecond = false; m_Times = 0; ui->pushButton_1S->setChecked(false); if(ui->pushButton_4S->isChecked()){ on_comboBox_channel_2_currentTextChanged(""); return; } } void CWaveDisPlay::on_pushButton_refreshauto_clicked() { if(ui->comboBox_channel_2->count() < 1) return; m_PackgNum = 1; bFlag = false; ui->widget_wave->clearGraphs(); ui->widget_wave->clearPlottables(); ui->widget_wave->replot(); m_WaveData = ""; m_ListWaveData.clear(); QString strChannelName = ui->comboBox_channel_2->currentText(); if(strChannelName == "请选择通道..."){ return; } if(ui->pushButton_4S->isChecked()){ QMessageBox::information(this, QStringLiteral("提示"), "请切换到1秒时域图!"); ui->pushButton_refreshauto->setChecked(false); return; } if(ui->pushButton_refreshauto->isChecked()){ ui->pushButton_refreshauto->setText("关闭"); id2 = startTimer(2000); //参数1 间隔 单位 毫秒 //定时器第二种方式 QTimer * timer = new QTimer(this); //启动定时器 timer->start(500); } if(!ui->pushButton_refreshauto->isChecked() && ui->pushButton_1S->isChecked()){ ui->pushButton_refreshauto->setText("自动刷新"); killTimer(id2); return; } }