1 Star 0 Fork 0

houmingzhang/CovAnalysisTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
QDataTrendWidget.cpp 17.12 KB
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2021-10-05 19:06 +08:00 . 协相关分析工具
#include <QtGui>
#include "QDataTrendWidget.h"
//#include "ui_QDataTrendCurve.h"
#include "ui_CustomPlot.h"
#include "AnalysisTask.h"
QDataTrendWidget::QDataTrendWidget(AnalysisTask* ptask,const QString& taskName,QString& XAxisName,QString& YAxisName,QWidget *parent) : m_ptaskOwner(ptask), QMainWindow(parent),
m_taskname(taskName),m_XAxisName(XAxisName),m_YAxisName(YAxisName),ui(new Ui::CustomPlot)
{
ui->setupUi(this);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
QCP::iSelectLegend | QCP::iSelectPlottables);
InitPlotor(m_taskname,m_XAxisName,m_YAxisName);
// connect slot that ties some axis selections together (especially opposite axes):
connect(ui->customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(ui->customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
// make bottom and left axes transfer their ranges to top and right axes:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// connect some interaction slots:
//connect(ui->customPlot, SIGNAL(titleDoubleClick(QMouseEvent*,QCPPlotTitle*)), this, SLOT(titleDoubleClick(QMouseEvent*,QCPPlotTitle*)));
connect(ui->customPlot, SIGNAL(axisDoubleClick(QCPAxis*,QCPAxis::SelectablePart,QMouseEvent*)), this, SLOT(axisLabelDoubleClick(QCPAxis*,QCPAxis::SelectablePart)));
connect(ui->customPlot, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this, SLOT(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*)));
// connect slot that shows a message in the status bar when a graph is clicked:
connect(ui->customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)), this, SLOT(graphClicked(QCPAbstractPlottable*,int)));
connect(ui->customPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
}
double QDataTrendWidget::GetFloor(double value,int step)
{
int count=(int)value/step;
count-=1;
return (double)(count*step);
}
double QDataTrendWidget::GetCeiling(double value,int step)
{
int count=(int)value/step;
count+=1;
return (double)(count*step);
}
void QDataTrendWidget::SlotNewChannelDataRecvied()
{
DataChannel *pchannel = qobject_cast<DataChannel*>(sender());
//qDebug()<<"pchannel:"<<pchannel;
if(pchannel!=NULL)
{
if(pchannel->GetGraph()==NULL)
{
QCPGraph * ptr2=ui->customPlot->addGraph();
pchannel->SetGraph(ptr2);
QPen graphPen;
graphPen.setColor(QColor(GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(0,255)));
graphPen.setWidthF(rand()/(double)RAND_MAX*2+1);
pchannel->GetGraph()->setPen(graphPen);
pchannel->GetGraph()->setName(pchannel->GetChannelDesc());
//pchannel->GetGraph()->setLineStyle(QCPGraph::lsLine);
pchannel->GetGraph()->setLineStyle((QCPGraph::LineStyle)pchannel->GetLineStyle());
pchannel->GetGraph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
//UpdateYRange(3000,10000);
}
QVector<double> tvar;
QVector<double> yvar;
QString graphname;
pchannel->GetRealTimeDataVector(tvar,yvar,graphname);
//qDebug()<<"floor:"<<GetFloor(pchannel->GetMinimumValue(),10)<<",ceiling:"<<GetCeiling(pchannel->GetMaximumValue(),100);
//UpdateYRange(GetFloor(pchannel->GetMinimumValue(),500),GetCeiling(pchannel->GetMaximumValue(),5000));
UpdateYRange(GetFloor(pchannel->GetMinimumValue(),10),GetCeiling(pchannel->GetMaximumValue(),100));
//UpdateYRange(-5,100);
ui->customPlot->xAxis->setRange(tvar.last(), 30, Qt::AlignHCenter);
/*qDebug()<<"========================tvar========================";
QVectorIterator<double> i(tvar);
while (i.hasNext()) {
qDebug() <<i.next();
}
qDebug()<<"========================yvar========================";
QVectorIterator<double> j(yvar);
while (j.hasNext()) {
qDebug() <<j.next();
}*/
pchannel->GetGraph()->setData(tvar, yvar);
ui->customPlot->replot();
}else{
//qDebug()<<"convert sender() to DataChannel failed";
}
#if 0
//有新的数据更新
std::list<DataChannel* >::iterator it= m_ptaskOwner->m_datachannels.begin();
for(;it!=m_ptaskOwner->m_datachannels.end();it++)
{
DataChannel* & pChannel=*(it);
QVector<QTime> tvar;
QVector<double> yvar;
QString graphname;
pChannel->GetRealTimeDataVector(tvar,yvar,graphname);
/*qDebug()<<"========================xvar========================";
QVectorIterator<double> i(xvar);
while (i.hasNext()) {
qDebug() <<i.next();
}
qDebug()<<"========================yvar========================";
QVectorIterator<double> j(yvar);
while (j.hasNext()) {
qDebug() <<j.next();
}*/
//UpdateXRange(GetFloor(xvar.first(),1000),GetCeiling(xvar.last(),1000));
UpdateXRange(0,20);
UpdateYRange(GetFloor(yvar.first(),100),GetCeiling(yvar.last(),100));
addCustomGraph(tvar,yvar,graphname);
}
#endif
}
QDataTrendWidget::~QDataTrendWidget()
{
delete ui;
}
void QDataTrendWidget::InitPlotor(QString& newtitle,QString& xAxisName,QString& yAxisName,int x1,int x2,int y1,int y2)
{
removeAllGraphs();
bool flag=false;
if(ui->customPlot->plotLayout()->hasElement(0,0))
{
QCPTextElement *pt = qobject_cast<QCPTextElement*>(ui->customPlot->plotLayout()->element(0,0));
if(pt!=NULL){
pt->setText(newtitle);
flag=true;
}
}
if(!flag)
{
ui->customPlot->plotLayout()->insertRow(0);
QCPTextElement *title = new QCPTextElement(ui->customPlot,newtitle, QFont("sans", 17, QFont::Bold));
ui->customPlot->plotLayout()->addElement(0, 0, title);
connect(title, SIGNAL(doubleClicked(QMouseEvent*)), this, SLOT(titleDoubleClick(QMouseEvent*)));
}
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
ui->customPlot->xAxis->setTicker(timeTicker);
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->xAxis->setLabel(xAxisName);
ui->customPlot->yAxis->setLabel(yAxisName);
ui->customPlot->xAxis->setRange(x1, x2);
ui->customPlot->yAxis->setRange(y1, y2);
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->legend->setVisible(true);
QFont legendFont = font();
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->legend->setSelectedFont(legendFont);
ui->customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
// setup policy and connect slot for context menu popup:
ui->customPlot->setContextMenuPolicy(Qt::CustomContextMenu);
}
void QDataTrendWidget::InitPlotor(QString& newtitle,QString& xAxisName,QString& yAxisName)
{
removeAllGraphs();
bool flag=false;
if(ui->customPlot->plotLayout()->hasElement(0,0))
{
QCPTextElement *pt = qobject_cast<QCPTextElement*>(ui->customPlot->plotLayout()->element(0,0));
if(pt!=NULL){
pt->setText(newtitle);
flag=true;
}
}
if(!flag)
{
ui->customPlot->plotLayout()->insertRow(0);
QCPTextElement *title = new QCPTextElement(ui->customPlot,newtitle, QFont("sans", 17, QFont::Bold));
ui->customPlot->plotLayout()->addElement(0, 0, title);
connect(title, SIGNAL(doubleClicked(QMouseEvent*)), this, SLOT(titleDoubleClick(QMouseEvent*)));
}
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
ui->customPlot->xAxis->setTicker(timeTicker);
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->xAxis->setLabel(xAxisName);
ui->customPlot->yAxis->setLabel(yAxisName);
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->legend->setVisible(true);
QFont legendFont = font();
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->legend->setSelectedFont(legendFont);
ui->customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
// setup policy and connect slot for context menu popup:
ui->customPlot->setContextMenuPolicy(Qt::CustomContextMenu);
ui->customPlot->rescaleAxes();
}
void QDataTrendWidget::UpdateXRange(int x1,int x2)
{
ui->customPlot->xAxis->setRange(x1, x2);
}
void QDataTrendWidget::UpdateYRange(int y1,int y2)
{
ui->customPlot->yAxis->setRange(y1, y2);
}
QString QDataTrendWidget::GetTaskName()
{
return m_taskname;
}
void QDataTrendWidget::titleDoubleClick(QMouseEvent* event)
{
Q_UNUSED(event)
if (QCPTextElement *title = qobject_cast<QCPTextElement*>(sender()))
{
// Set the plot title by double clicking on it
bool ok;
QString newTitle = QInputDialog::getText(this, "QCustomPlot example", "New plot title:", QLineEdit::Normal, title->text(), &ok);
if (ok)
{
title->setText(newTitle);
ui->customPlot->replot();
}
}
}
void QDataTrendWidget::axisLabelDoubleClick(QCPAxis *axis, QCPAxis::SelectablePart part)
{
// Set an axis label by double clicking on it
if (part == QCPAxis::spAxisLabel) // only react when the actual axis label is clicked, not tick label or axis backbone
{
bool ok;
QString newLabel = QInputDialog::getText(this, "QCustomPlot example", "New axis label:", QLineEdit::Normal, axis->label(), &ok);
if (ok)
{
axis->setLabel(newLabel);
ui->customPlot->replot();
}
}
}
void QDataTrendWidget::legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item)
{
// Rename a graph by double clicking on its legend item
Q_UNUSED(legend)
if (item) // only react if item was clicked (user could have clicked on border padding of legend where there is no item, then item is 0)
{
QCPPlottableLegendItem *plItem = qobject_cast<QCPPlottableLegendItem*>(item);
bool ok;
QString newName = QInputDialog::getText(this, "QCustomPlot example", "New graph name:", QLineEdit::Normal, plItem->plottable()->name(), &ok);
if (ok)
{
plItem->plottable()->setName(newName);
ui->customPlot->replot();
}
}
}
void QDataTrendWidget::selectionChanged()
{
/*
normally, axis base line, axis tick labels and axis labels are selectable separately, but we want
the user only to be able to select the axis as a whole, so we tie the selected states of the tick labels
and the axis base line together. However, the axis label shall be selectable individually.
The selection state of the left and right axes shall be synchronized as well as the state of the
bottom and top axes.
Further, we want to synchronize the selection of the graphs with the selection state of the respective
legend item belonging to that graph. So the user can select a graph by either clicking on the graph itself
or on its legend item.
*/
// make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object:
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
{
ui->customPlot->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
ui->customPlot->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
}
// make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object:
if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
{
ui->customPlot->yAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
ui->customPlot->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
}
// synchronize selection of graphs with selection of corresponding legend items:
for (int i=0; i<ui->customPlot->graphCount(); ++i)
{
QCPGraph *graph = ui->customPlot->graph(i);
QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(graph);
if (item->selected() || graph->selected())
{
item->setSelected(true);
graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
}
}
}
void QDataTrendWidget::mousePress()
{
// if an axis is selected, only allow the direction of that axis to be dragged
// if no axis is selected, both directions may be dragged
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->yAxis->orientation());
else
ui->customPlot->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
}
void QDataTrendWidget::mouseWheel()
{
// if an axis is selected, only allow the direction of that axis to be zoomed
// if no axis is selected, both directions may be zoomed
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->yAxis->orientation());
else
ui->customPlot->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
}
void QDataTrendWidget::addCustomGraph(QVector<double>& xvar,QVector<double>& yvar,QString& graphname)
{
removeAllGraphs();
ui->customPlot->addGraph();
ui->customPlot->graph()->setName(graphname);
ui->customPlot->graph()->setData(xvar, yvar);
//ui->customPlot->graph()->setLineStyle((QCPGraph::LineStyle)(rand()%5+1));
//if (rand()%100 > 75)
// ui->customPlot->graph()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(rand()%9+1)));
QPen graphPen;
graphPen.setColor(QColor(rand()%245+10, rand()%245+10, rand()%245+10));
//graphPen.setWidthF(rand()/(double)RAND_MAX*2+1);
ui->customPlot->graph()->setPen(graphPen);
ui->customPlot->replot();
}
void QDataTrendWidget::removeSelectedGraph()
{
if (ui->customPlot->selectedGraphs().size() > 0)
{
ui->customPlot->removeGraph(ui->customPlot->selectedGraphs().first());
ui->customPlot->replot();
}
}
void QDataTrendWidget::removeAllGraphs()
{
ui->customPlot->clearGraphs();
ui->customPlot->replot();
}
void QDataTrendWidget::contextMenuRequest(QPoint pos)
{
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
if (ui->customPlot->legend->selectTest(pos, false) >= 0) // context menu on legend requested
{
menu->addAction("Move to top left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignLeft));
menu->addAction("Move to top center", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignHCenter));
menu->addAction("Move to top right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignRight));
menu->addAction("Move to bottom right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom|Qt::AlignRight));
menu->addAction("Move to bottom left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom|Qt::AlignLeft));
} else // general context menu on graphs requested
{
menu->addAction("Add random graph", this, SLOT(addCustomGraph()));
if (ui->customPlot->selectedGraphs().size() > 0)
menu->addAction("Remove selected graph", this, SLOT(removeSelectedGraph()));
if (ui->customPlot->graphCount() > 0)
menu->addAction("Remove all graphs", this, SLOT(removeAllGraphs()));
}
menu->popup(ui->customPlot->mapToGlobal(pos));
}
void QDataTrendWidget::moveLegend()
{
if (QAction* contextAction = qobject_cast<QAction*>(sender())) // make sure this slot is really called by a context menu action, so it carries the data we need
{
bool ok;
int dataInt = contextAction->data().toInt(&ok);
if (ok)
{
ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, (Qt::Alignment)dataInt);
ui->customPlot->replot();
}
}
}
void QDataTrendWidget::graphClicked(QCPAbstractPlottable *plottable, int dataIndex)
{
// since we know we only have QCPGraphs in the plot, we can immediately access interface1D()
// usually it's better to first check whether interface1D() returns non-zero, and only then use it.
double dataValue = plottable->interface1D()->dataMainValue(dataIndex);
QString message = QString("Clicked on graph '%1' at data point #%2 with value %3.").arg(plottable->name()).arg(dataIndex).arg(dataValue);
ui->statusBar->showMessage(message, 2500);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dtrylzhm/cov-analysis-tool.git
git@gitee.com:dtrylzhm/cov-analysis-tool.git
dtrylzhm
cov-analysis-tool
CovAnalysisTool
master

搜索帮助