|
- #include "CurveWidget.h"
-
- CurveWidget::CurveWidget(QWidget* parent)
- : QWidget(parent)
- , m_iWgtWidth(0)
- , m_iWgtHeight(0)
- , m_pPlotTitle(nullptr)
- , m_pRubberBand(nullptr)
- , m_pCPGraph(nullptr)
- , m_dYMax(0.0)
- , m_dYMin(0.0)
- , m_uiParseStatus(0)
- , m_bRefTime(false)
- {
- ui.setupUi(this);
- setWindowFlags(Qt::FramelessWindowHint);
- initCurvePlot();
- // connect(ui.pb_changeCoord,&QPushButton::clicked,this,&CurveWidget::changeCoord); //坐标切换
- connect(ui.pb_zoomCoordY, &QPushButton::clicked, this, &CurveWidget::zoomCoordY); // Y轴缩放
- connect(ui.pb_resetCoord, &QPushButton::clicked, this, &CurveWidget::resetCoord); // 坐标重置
- connect(ui.pb_hide, &QPushButton::clicked, this, &CurveWidget::hideWindow); // 隐藏窗口
-
- connect(&m_timer, &QTimer::timeout, this, &CurveWidget::refreshCurve);
- ui.le_curValue->setEnabled(false); // 当前值窗口
-
- // ui.pb_changeCoord->setToolTip("坐标转换:\n实时解析时改变X轴显示方式");
- ui.pb_zoomCoordY->setToolTip("Y轴缩放:\n点击按钮,利用Ctrl+鼠标滚轮进行Y轴缩放");
- ui.pb_resetCoord->setToolTip("坐标重置");
- ui.pb_hide->setToolTip("关闭窗口");
-
- // ui.pb_changeCoord->setEnabled(false); //坐标切换
- ui.pb_zoomCoordY->setEnabled(false); // Y轴缩放
- ui.pb_resetCoord->setEnabled(false); // 坐标重置
- }
-
- CurveWidget::~CurveWidget()
- {
- delete m_pCurveInfo;
- m_pCurveInfo = nullptr;
- }
-
- void CurveWidget::initCurvePlot()
- {
- m_axisRange.axisMinX = 0.0;
- m_axisRange.axisMaxX = 100.0;
- m_axisRange.axisMinY = 0.0;
- m_axisRange.axisMaxY = 100.0;
- m_pPlotTitle = new QCPPlotTitle(ui.wgt_curvePlot);
- m_pPlotTitle->setFont(QFont("sans", 12, QFont::Bold));
- ui.wgt_curvePlot->xAxis->setRange(m_axisRange.axisMinX, m_axisRange.axisMaxX);
- ui.wgt_curvePlot->yAxis->setRange(m_axisRange.axisMinY, m_axisRange.axisMaxY);
- ui.wgt_curvePlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); // 支持拖拽和缩放
- ui.wgt_curvePlot->axisRect()->setRangeZoom(Qt::Horizontal); // 水平缩放
- ui.wgt_curvePlot->xAxis->setLabel("时间(ms)");
- ui.wgt_curvePlot->plotLayout()->insertRow(0);
- ui.wgt_curvePlot->plotLayout()->addElement(0, 0, m_pPlotTitle);
- m_pRubberBand = new QRubberBand(QRubberBand::Rectangle, ui.wgt_curvePlot);
- connect(ui.wgt_curvePlot, &QCustomPlot::mousePress, this, &CurveWidget::mousePressEvent);
- connect(ui.wgt_curvePlot, &QCustomPlot::mouseMove, this, &CurveWidget::mouseMoveEvent);
- connect(ui.wgt_curvePlot, &QCustomPlot::mouseRelease, this, &CurveWidget::mouseReleaseEvent);
- }
-
- void CurveWidget::setParam(CurveInfo*& curveInfo)
- {
- m_pCurveInfo = curveInfo;
- strTitle = curveInfo->m_strTitle;
- m_usModel = curveInfo->m_usCurveModel;
- m_count = curveInfo->m_curveCount;
- ui.lb_curveName->setText(strTitle);
- ui.wgt_curvePlot->yAxis->setLabel(strTitle);
- // 添加曲线
- m_pCPGraph = ui.wgt_curvePlot->addGraph();
- QPen pen;
- pen.setColor(curveInfo->m_curveColor); // 曲线颜色
- m_pCPGraph->setPen(pen);
- m_pCPGraph->setVisible(true);
- if (m_usModel == 4) { // 状态量
- m_pCPGraph->setLineStyle((QCPGraph::lsStepLeft)); // 阶梯型曲线
- }
- curveInfo->m_pGPGraph = m_pCPGraph;
- }
-
- void CurveWidget::keyPressEvent(QKeyEvent* event)
- {
- if (event->key() == Qt::Key_Control) {
- ui.wgt_curvePlot->axisRect()->setRangeZoom(Qt::Vertical);
- }
- }
- void CurveWidget::keyReleaseEvent(QKeyEvent* event)
- {
- if (event->key() == Qt::Key_Control) {
- ui.wgt_curvePlot->axisRect()->setRangeZoom(Qt::Horizontal);
- }
- }
-
- void CurveWidget::resizeEvent(QResizeEvent* event)
- {
- QSize size = event->size();
- m_iWgtWidth = size.width();
- m_iWgtHeight = size.height();
- resizeSubControl(m_iWgtWidth, m_iWgtHeight);
- }
-
- void CurveWidget::resizeSubControl(int width, int height)
- {
- ui.wgt_background->setGeometry(0, 0, width, height);
- // 曲线信息
- int iHeight = ui.lb_curveInfo->height();
- ui.lb_curveInfo->setGeometry(0, 0, width, iHeight);
- ui.lb_curveInfo->setStyleSheet("background-color:rgb(113,113,113)");
- // 关闭按钮
- QRect rect = ui.pb_hide->geometry();
- ui.pb_hide->setGeometry(width - 5 - rect.width(), rect.y(), rect.width(), rect.height());
- // 重置按钮
- rect = ui.pb_resetCoord->geometry();
- ui.pb_resetCoord->setGeometry(width - (5 + rect.width()) * 2, rect.y(), rect.width(),
- rect.height());
- // Y轴缩放
- rect = ui.pb_zoomCoordY->geometry();
- ui.pb_zoomCoordY->setGeometry(width - (5 + rect.width()) * 3, rect.y(), rect.width(),
- rect.height());
- // 坐标切换
- // rect = ui.pb_changeCoord->geometry();
- // ui.pb_changeCoord->setGeometry(width - (5 + rect.width()) * 4, rect.y(), rect.width(),
- // rect.height()); 曲线控件
- ui.wgt_curvePlot->setGeometry(0, iHeight, width, height - iHeight);
- }
-
- void CurveWidget::mousePressEvent(QMouseEvent* event)
- {
- if (event->button() == Qt::RightButton) {
- m_originPoint = event->pos(); // 鼠标原始位置
- m_pRubberBand->setGeometry(QRect(m_originPoint, QSize()));
- m_pRubberBand->show();
- }
- }
- void CurveWidget::mouseMoveEvent(QMouseEvent* event)
- {
- if (event->buttons() && Qt::RightButton) {
- if (m_pRubberBand->isVisible()) {
- QPoint point = event->pos();
- m_pRubberBand->setGeometry(QRect(m_originPoint, event->pos()).normalized());
- }
- }
- int axisX = event->pos().x();
- int axisY = event->pos().y();
- double coordX = ui.wgt_curvePlot->xAxis->pixelToCoord((double)axisX);
- double coordY = ui.wgt_curvePlot->yAxis->pixelToCoord((double)axisY);
- QString strTip = QString("x:%1,y:%2")
- .arg(QString::number(coordX, 10, 1))
- .arg(QString::number(coordY, 10, 3));
- QToolTip::showText(cursor().pos(), strTip, ui.wgt_curvePlot);
- }
- void CurveWidget::mouseReleaseEvent(QMouseEvent* event)
- {
- if (m_pRubberBand->isVisible()) {
- const QRect zoomRect = m_pRubberBand->geometry();
- int xp1, yp1, xp2, yp2;
- zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);
- double x1 = 0.0, y1 = 0.0, x2 = 0.0, y2 = 0.0;
- // 把鼠标坐标点 转换为QCustomPlot内部坐标值(pixelToCoord 函数)
- // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
- x1 = ui.wgt_curvePlot->xAxis->pixelToCoord(xp1);
- x2 = ui.wgt_curvePlot->xAxis->pixelToCoord(xp2);
- y1 = ui.wgt_curvePlot->yAxis->pixelToCoord(yp1);
- y2 = ui.wgt_curvePlot->yAxis->pixelToCoord(yp2);
- ui.wgt_curvePlot->xAxis->setRange(x1, x2);
- ui.wgt_curvePlot->yAxis->setRange(y1, y2);
- m_pRubberBand->hide();
- ui.wgt_curvePlot->replot();
- }
- }
- /*
- //坐标切换
- void CurveWidget::changeCoord() {
- if (m_pCurveInfo->m_bIsZeroX == false) { //false:固定间距显示
- m_pCurveInfo->m_bIsZeroX = true;
- }
- else if (m_pCurveInfo->m_bIsZeroX == true) { //true:从零开始显示
- m_pCurveInfo->m_bIsZeroX = false;
- }
- }*/
- // Y轴缩放
- void CurveWidget::zoomCoordY()
- {
- ui.wgt_curvePlot->replot();
- }
- // 坐标重置
- void CurveWidget::resetCoord()
- {
- // 修改坐标轴范围
- ui.wgt_curvePlot->xAxis->setRange(m_axisRange.axisMinX, m_axisRange.axisMaxX);
- ui.wgt_curvePlot->yAxis->setRange(m_dYMin, m_dYMax);
- ui.wgt_curvePlot->replot();
- }
- // 隐藏窗口
- void CurveWidget::hideWindow()
- {
- emit hideCurveWidget(m_count);
- }
-
- void CurveWidget::startRefresh(bool status)
- {
- if (status == true) {
- m_timer.start(500);
- } else if (status == false) {
- m_timer.stop();
- }
- }
-
- void CurveWidget::refreshCurve()
- {
- int iSize = m_pCurveInfo->m_vecAxisY.size();
- if (iSize == 0) {
- return;
- }
- m_mutex.lock();
- if (m_uiParseStatus == 2) { // 在线解析
- double dValue = (double)m_pCurveInfo->m_vecAxisY.at(iSize - 1);
- QString strValue = QString::number(dValue, 'f', 3);
- ui.le_curValue->setText(strValue); // 当前值窗口
-
- if (m_pCurveInfo->m_vecAxisX.size() > 3000) {
- m_pCurveInfo->m_vecAxisX.erase(m_pCurveInfo->m_vecAxisX.begin(),
- m_pCurveInfo->m_vecAxisX.end() - 2000);
- }
- if (m_pCurveInfo->m_vecAxisY.size() > 3000) {
- m_pCurveInfo->m_vecAxisY.erase(m_pCurveInfo->m_vecAxisY.begin(),
- m_pCurveInfo->m_vecAxisY.end() - 2000);
- }
- }
- m_pCurveInfo->m_pGPGraph->setData(m_pCurveInfo->m_vecAxisX, m_pCurveInfo->m_vecAxisY);
- m_axisRange.axisMinX = m_pCurveInfo->minX;
- m_axisRange.axisMaxX = m_pCurveInfo->maxX;
- m_axisRange.axisMaxY = m_pCurveInfo->maxY;
- m_axisRange.axisMinY = m_pCurveInfo->minY;
- ui.wgt_curvePlot->xAxis->setRange(m_axisRange.axisMinX, m_axisRange.axisMaxX);
- m_dYMin = m_axisRange.axisMinY - (m_axisRange.axisMaxY - m_axisRange.axisMinY) / 2.0;
- m_dYMax = m_axisRange.axisMaxY + (m_axisRange.axisMaxY - m_axisRange.axisMinY) / 2.0;
- ui.wgt_curvePlot->yAxis->setRange(m_dYMin, m_dYMax);
- ui.wgt_curvePlot->replot();
- m_mutex.unlock();
- }
-
- void CurveWidget::clearWidget()
- {
- m_pCurveInfo->m_vecAxisX.clear();
- m_pCurveInfo->m_vecAxisY.clear();
- m_pCurveInfo->m_pGPGraph->clearData();
- m_axisRange.axisMinX = 0.0;
- m_axisRange.axisMaxX = 100.0;
- m_axisRange.axisMinY = 0.0;
- m_axisRange.axisMaxY = 100.0;
- ui.wgt_curvePlot->xAxis->setLabel("时间(ms)");
- ui.wgt_curvePlot->xAxis->setRange(m_axisRange.axisMinX, m_axisRange.axisMaxX);
- ui.wgt_curvePlot->yAxis->setRange(m_axisRange.axisMinY, m_axisRange.axisMaxY);
- ui.wgt_curvePlot->replot();
- }
-
- void CurveWidget::parseFinished()
- {
- refreshCurve();
- }
-
- void CurveWidget::setParseStatus(unsigned int status)
- {
- m_uiParseStatus = status;
- if (m_uiParseStatus == 0 || m_uiParseStatus == 1) { // 无任务解析或离线解析
- // ui.pb_changeCoord->setEnabled(false); //坐标切换
- ui.pb_zoomCoordY->setEnabled(true); // Y轴缩放
- ui.pb_resetCoord->setEnabled(true); // 坐标重置
- } else if (m_uiParseStatus == 2) { // 实时解析
- // ui.pb_changeCoord->setEnabled(true); //坐标切换
- ui.pb_zoomCoordY->setEnabled(false); // Y轴缩放
- ui.pb_resetCoord->setEnabled(false); // 坐标重置
- }
- }
|