This commit is contained in:
hm
2020-08-13 16:36:49 +08:00
parent 7491439a9b
commit 8b4142163a
5 changed files with 366 additions and 316 deletions
+276 -4
View File
@@ -1,5 +1,7 @@
#include "Chart.h" #include "Chart.h"
int Chart::ChannelIndex = 0;
Chart::Chart(QChart *chart, QWidget *parent) Chart::Chart(QChart *chart, QWidget *parent)
{ {
chart->legend()->setAlignment(Qt::AlignRight); chart->legend()->setAlignment(Qt::AlignRight);
@@ -20,12 +22,20 @@ Chart::Chart(QChart *chart, QWidget *parent)
setParent(parent); setParent(parent);
setChart(chart); setChart(chart);
//默认不使用,因为使用显卡加速图形会闪
setUseOpenGL(false);
updateTimer = new QTimer(this);
connect(updateTimer,SIGNAL(timeout()),
this,SLOT(timeout()));
updateTimer->start(50);//20hz刷新
} }
Chart::Chart(QWidget *parent) Chart::Chart(QWidget *parent)
{ {
setParent(parent); setParent(parent);
chart()->legend()->setAlignment(Qt::AlignRight); chart()->legend()->setAlignment(Qt::AlignRight);
chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle); chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle);
chart()->setTheme(QChart::ChartThemeLight); chart()->setTheme(QChart::ChartThemeLight);
@@ -42,14 +52,35 @@ Chart::Chart(QWidget *parent)
axisY->setLabelFormat("%.2f"); axisY->setLabelFormat("%.2f");
chart()->addAxis(axisY, Qt::AlignLeft); chart()->addAxis(axisY, Qt::AlignLeft);
//默认不使用,因为使用显卡加速图形会闪
setUseOpenGL(false);
updateTimer = new QTimer(this);
connect(updateTimer,SIGNAL(timeout()),
this,SLOT(timeout()));
updateTimer->start(50);//20hz刷新
} }
Chart::~Chart() Chart::~Chart()
{ {
if(updateTimer)
{
if(updateTimer->isActive())
{
updateTimer->stop();
}
delete updateTimer;
}
} }
void Chart::leaveEvent(QEvent *e)
{
isPress = false;
setCursor(Qt::ArrowCursor);
//QChartView::leaveEvent(e);
}
void Chart::mouseMoveEvent(QMouseEvent *e) void Chart::mouseMoveEvent(QMouseEvent *e)
{ {
@@ -60,12 +91,14 @@ void Chart::mouseMoveEvent(QMouseEvent *e)
lastPoint = curPos; lastPoint = curPos;
chart()->scroll(-offset.x(), offset.y()); chart()->scroll(-offset.x(), offset.y());
} }
//QChartView::mouseMoveEvent(e);
} }
void Chart::mouseReleaseEvent(QMouseEvent *e) void Chart::mouseReleaseEvent(QMouseEvent *e)
{ {
isPress = false; isPress = false;
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
//QChartView::mouseReleaseEvent(e);
} }
void Chart::mousePressEvent(QMouseEvent *e) void Chart::mousePressEvent(QMouseEvent *e)
@@ -76,11 +109,13 @@ void Chart::mousePressEvent(QMouseEvent *e)
isPress = true; isPress = true;
setCursor(Qt::ClosedHandCursor); setCursor(Qt::ClosedHandCursor);
} }
//QChartView::mousePressEvent(e);
} }
void Chart::mouseDoubleClickEvent(QMouseEvent *e) void Chart::mouseDoubleClickEvent(QMouseEvent *e)
{ {
qDebug() << "Chart double clicked" << e; qDebug() << "Chart double clicked" << e;
//QChartView::mouseDoubleClickEvent(e);
} }
void Chart::wheelEvent(QWheelEvent *e) void Chart::wheelEvent(QWheelEvent *e)
@@ -148,6 +183,7 @@ void Chart::wheelEvent(QWheelEvent *e)
} }
} }
} }
//QChartView::wheelEvent(e);
} }
void Chart::resizeEvent(QResizeEvent *e) void Chart::resizeEvent(QResizeEvent *e)
@@ -158,8 +194,244 @@ void Chart::resizeEvent(QResizeEvent *e)
chart()->setGeometry(this->geometry()); chart()->setGeometry(this->geometry());
} }
bool Chart::event(QEvent *event) void Chart::timeout(void)//100Hz
{ {
return QGraphicsView::event(event); timeseries += 0.01;
chartUpdate();
} }
void Chart::chartUpdate(void)
{
//读取新数据,如果没有,那就保持
//滚动显示器
//当到达最大时(0.95),开始滚动
if(isScroll() == true)
{
QValueAxis *axis = dynamic_cast<QValueAxis*>(chart()->axisX());
qreal dwidth = chart()->plotArea().width()/(axis->tickCount() - 1);
if(timeseries > (axis->min() + (axis->max() - axis->min()) * 0.95))
{
chart()->scroll(dwidth * 0.01,0);//每次增加0.01
}
}
}
void Chart::setUseOpenGL(const bool &value)
{
isOpenGL= value;
}
bool Chart::addSeries(QString name = tr("new"))
{
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chart()->series();
for(QAbstractSeries *serie:slist)
{
if(serie->name() == name)
{
isContain = true;
}
}
if(isContain == false)
{
QLineSeries *s = new QLineSeries();
s->setColor(ColorMap::Color[ChannelIndex]);
s->setName(name);
s->append(timeseries,0);
s->setUseOpenGL(isOpenGL);
chart()->addSeries(s);
chart()->setAxisX(chart()->axisX(),s);
chart()->setAxisY(chart()->axisY(),s);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
else
{
qInfo() << "this chart has contain a serie name by :" << name;
}
return isContain;
}
bool Chart::addSeries(QString name = tr("new"), int index = 0)
{
if(index >= 16)
{
int a = index/16;
ChannelIndex = index - a*16;
}
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chart()->series();
for(QAbstractSeries *serie:slist)
{
if(serie->name() == name)
{
isContain = true;
}
}
if(isContain == false)
{
QLineSeries *s = new QLineSeries();
s->setColor(ColorMap::Color[ChannelIndex]);
s->setName(name);
s->append(timeseries,0);
s->setUseOpenGL(isOpenGL);
chart()->addSeries(s);
chart()->setAxisX(chart()->axisX(),s);
chart()->setAxisY(chart()->axisY(),s);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
else
{
qInfo() << "this chart has contain a serie name by :" << name;
}
return isContain;
}
bool Chart::addSeries(QString name = tr("new"),QColor color = QColor("#FFFFFF"))
{
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chart()->series();
for(QAbstractSeries *serie:slist)
{
if(serie->name() == name)
{
isContain = true;
}
}
if(isContain == false)
{
QLineSeries *s = new QLineSeries();
s->setColor(color);
s->setName(name);
s->append(timeseries,0);
s->setUseOpenGL(isOpenGL);
chart()->addSeries(s);
chart()->setAxisX(chart()->axisX(),s);
chart()->setAxisY(chart()->axisY(),s);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
else
{
qInfo() << "this chart has contain a serie name by :" << name;
}
return isContain;
}
void Chart::setSerieData(QString name,QVariant data)
{
//出新了比以前更大或者更小的值,那么就设置一下量程
{
QValueAxis *axis = dynamic_cast<QValueAxis*>(chart()->axisY());
if(data.toDouble() > axis->max())
{
//设置最大值
const double Min = axis->min();
chart()->axisY()->setRange(Min, data.toDouble() * 1.1);
}
else if(data.toDouble() < axis->min())
{
//设置最小值
const double Max = axis->max();
chart()->axisY()->setRange(data.toDouble() * 1.1, Max);
}
}
//获取当前的时间撮
//series->append(timeseries,data.toFloat());
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chart()->series();
for(QAbstractSeries *serie:slist)
{
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
if(s->name() == name)
{
s->append(timeseries,data.toDouble());
isContain = true;
}
}
if(isContain == false)
{
addSeries(name,ChannelIndex);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
}
void Chart::removeSerie(QString name)
{
//找到,删除
QList<QAbstractSeries *> slist = chart()->series();
for(QAbstractSeries *serie:slist)
{
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
if(s)
{
if(s->name() == name)
{
chart()->removeSeries(s);
s->clear();
s->deleteLater();
delete s;
}
}
}
}
void Chart::removeAllSerie(void)
{
//找到,删除
QList<QAbstractSeries *> slist = chart()->series();
for(QAbstractSeries *serie:slist)
{
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
if(s)
{
chart()->removeSeries(s);
s->clear();
s->deleteLater();
delete s;
}
}
}
+63 -3
View File
@@ -14,8 +14,35 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QGraphicsSimpleTextItem> #include <QGraphicsSimpleTextItem>
#include "QTimer"
using namespace QtCharts; using namespace QtCharts;
namespace ColorMap{
static QColor Color[16] = {
QColor(165, 42, 42), //Channel1Color
QColor (255, 127, 80), //Channel2Color
QColor (30, 144, 255), //Channel3Color
QColor (218, 165, 32), //Channel4Color
QColor (255, 0, 255), //Channel5Color
QColor (147, 112, 219), //Channel6Color
QColor (0, 255, 255), //Channel7Color
QColor (0, 0, 128), //Channel8Color
QColor (128, 0, 128), //Channel9Color
QColor (46, 139, 87), //Channel10Color
QColor (152, 251, 152), //Channel11Color
QColor (0, 255, 127), //Channel12Color
QColor (0, 0, 255), //Channel13Color
QColor (135, 206, 250), //Channel14Color
QColor (255, 192, 203), //Channel15Color
QColor (127, 255, 0) //Channel16Color
};
}
class Chart : public QChartView class Chart : public QChartView
{ {
Q_OBJECT Q_OBJECT
@@ -25,10 +52,23 @@ public:
~Chart(); ~Chart();
static int ChannelIndex;
void setScroll(const bool &value)
{
isscroll = value;
}
bool isScroll(void)
{
return isscroll;
}
protected: protected:
void leaveEvent(QEvent *e);
bool event(QEvent *event);
void mouseMoveEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);
@@ -39,6 +79,18 @@ protected:
signals: signals:
public slots:
void timeout(void);
void chartUpdate(void);
void setUseOpenGL(const bool &value);
bool addSeries(QString name);
bool addSeries(QString name,int index);
bool addSeries(QString name, QColor color);
void removeSerie(QString name);
void removeAllSerie(void);
void setSerieData(QString name,QVariant data);
private: private:
@@ -53,6 +105,14 @@ private:
bool isPress = false; bool isPress = false;
QLineSeries *series;
QTimer *updateTimer = nullptr;
qreal timeseries = 0;//时间基准,注意时间长度,可能会出现时间反转的情况
bool isOpenGL = false;
bool isscroll = true;
}; };
+24 -261
View File
@@ -1,7 +1,7 @@
#include "Scope.h" #include "Scope.h"
#include "ui_Scope.h" #include "ui_Scope.h"
int Scope::ChannelIndex = 0;
//需要重新实现chart类,现在这种方式有局限,不方便操作 //需要重新实现chart类,现在这种方式有局限,不方便操作
@@ -20,19 +20,11 @@ Scope::Scope(QWidget *parent) :
this->setStyleSheet(stylesheet); this->setStyleSheet(stylesheet);
file.close(); file.close();
timeseries = 0;
chartView = new Chart(this);//new QChartView(this); chartView = new Chart(this);//new QChartView(this);
chartView->setRenderHint(QPainter::Antialiasing); chartView->setRenderHint(QPainter::Antialiasing);
chartView->setGeometry(ui->frame->geometry()); chartView->setGeometry(ui->frame->geometry());
//默认不使用,因为使用显卡加速图形会闪
setUseOpenGL(false);
updateTimer = new QTimer(this);
connect(updateTimer,SIGNAL(timeout()),
this,SLOT(timeout()));
updateTimer->start(50);//20hz刷新
qInfo() << "Scope inital ready"; qInfo() << "Scope inital ready";
} }
@@ -42,291 +34,62 @@ Scope::~Scope()
delete ui; delete ui;
} }
void Scope::mouseMoveEvent(QMouseEvent *e) void Scope::mouseMoveEvent(QMouseEvent *e)
{ {
QWidget::mouseMoveEvent(e); //QWidget::mouseMoveEvent(e);
} }
void Scope::mouseReleaseEvent(QMouseEvent *e) void Scope::mouseReleaseEvent(QMouseEvent *e)
{ {
QWidget::mouseReleaseEvent(e); //QWidget::mouseReleaseEvent(e);
} }
void Scope::mousePressEvent(QMouseEvent *e) void Scope::mousePressEvent(QMouseEvent *e)
{ {
QWidget::mousePressEvent(e); //QWidget::mousePressEvent(e);
} }
void Scope::mouseDoubleClickEvent(QMouseEvent *e) void Scope::mouseDoubleClickEvent(QMouseEvent *e)
{ {
QWidget::mouseDoubleClickEvent(e); //QWidget::mouseDoubleClickEvent(e);
} }
void Scope::wheelEvent(QWheelEvent *e) void Scope::wheelEvent(QWheelEvent *e)
{ {
QWidget::wheelEvent(e); //QWidget::wheelEvent(e);
} }
void Scope::resizeEvent(QResizeEvent *e) void Scope::resizeEvent(QResizeEvent *e)
{ {
chartView->setGeometry(ui->frame->geometry()); if(chartView)
QWidget::resizeEvent(e); {
chartView->setGeometry(ui->frame->geometry());
}
//QWidget::resizeEvent(e);
} }
void Scope::timeout(void)//100Hz
{
timeseries += 0.01;
chartUpdate();
}
void Scope::chartUpdate(void)
{
//读取新数据,如果没有,那就保持
//滚动显示器
//当到达最大时(0.95),开始滚动
if(isScroll == true)
{
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisX());
qreal dwidth = chartView->chart()->plotArea().width()/(axis->tickCount() - 1);
if(timeseries > (axis->min() + (axis->max() - axis->min()) * 0.95))
{
chartView->chart()->scroll(dwidth * 0.01,0);//每次增加0.01
}
}
}
void Scope::setUseOpenGL(const bool &value)
{
isOpenGL= value;
}
bool Scope::addSeries(QString name = tr("new"))
{
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chartView->chart()->series();
for(QAbstractSeries *serie:slist)
{
if(serie->name() == name)
{
isContain = true;
}
}
if(isContain == false)
{
QLineSeries *s = new QLineSeries();
s->setColor(ColorMap::Color[ChannelIndex]);
s->setName(name);
s->append(timeseries,0);
s->setUseOpenGL(isOpenGL);
chartView->chart()->addSeries(s);
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
else
{
qInfo() << "this chart has contain a serie name by :" << name;
}
return isContain;
}
bool Scope::addSeries(QString name = tr("new"), int index = 0)
{
if(index >= 16)
{
int a = index/16;
ChannelIndex = index - a*16;
}
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chartView->chart()->series();
for(QAbstractSeries *serie:slist)
{
if(serie->name() == name)
{
isContain = true;
}
}
if(isContain == false)
{
QLineSeries *s = new QLineSeries();
s->setColor(ColorMap::Color[ChannelIndex]);
s->setName(name);
s->append(timeseries,0);
s->setUseOpenGL(isOpenGL);
chartView->chart()->addSeries(s);
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
else
{
qInfo() << "this chart has contain a serie name by :" << name;
}
return isContain;
}
bool Scope::addSeries(QString name = tr("new"),QColor color = QColor("#FFFFFF"))
{
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chartView->chart()->series();
for(QAbstractSeries *serie:slist)
{
if(serie->name() == name)
{
isContain = true;
}
}
if(isContain == false)
{
QLineSeries *s = new QLineSeries();
s->setColor(color);
s->setName(name);
s->append(timeseries,0);
s->setUseOpenGL(isOpenGL);
chartView->chart()->addSeries(s);
chartView->chart()->setAxisX(chartView->chart()->axisX(),s);
chartView->chart()->setAxisY(chartView->chart()->axisY(),s);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
else
{
qInfo() << "this chart has contain a serie name by :" << name;
}
return isContain;
}
void Scope::setSerieData(QString name,QVariant data)
{
//出新了比以前更大或者更小的值,那么就设置一下量程
{
QValueAxis *axis = dynamic_cast<QValueAxis*>(chartView->chart()->axisY());
if(data.toDouble() > axis->max())
{
//设置最大值
const double Min = axis->min();
chartView->chart()->axisY()->setRange(Min, data.toDouble() * 1.1);
}
else if(data.toDouble() < axis->min())
{
//设置最小值
const double Max = axis->max();
chartView->chart()->axisY()->setRange(data.toDouble() * 1.1, Max);
}
//delete axis;
}
//获取当前的时间撮
//series->append(timeseries,data.toFloat());
//查找有没有这个名字的序列
bool isContain = false;
QList<QAbstractSeries *> slist = chartView->chart()->series();
for(QAbstractSeries *serie:slist)
{
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
if(s->name() == name)
{
s->append(timeseries,data.toDouble());
isContain = true;
}
}
if(isContain == false)
{
addSeries(name,ChannelIndex);
ChannelIndex ++;
if(ChannelIndex >= 16)
{
ChannelIndex = 0;
}
}
}
void Scope::removeSerie(QString name)
{
//找到,删除
QList<QAbstractSeries *> slist = chartView->chart()->series();
for(QAbstractSeries *serie:slist)
{
QLineSeries *s = qobject_cast<QLineSeries *>(serie);
if(s->name() == name)
{
chartView->chart()->removeSeries(s);
s->clear();
s->deleteLater();
delete s;
}
}
}
void Scope::on_pushButton_Pause_clicked() void Scope::on_pushButton_Pause_clicked()
{ {
if(isScroll == true) if(chartView)
{ {
ui->pushButton_Pause->setText(tr("Scroll")); if(chartView->isScroll() == true)
{
ui->pushButton_Pause->setText(tr("Scroll"));
}
else
{
ui->pushButton_Pause->setText(tr("Pause"));
}
chartView->setScroll(chartView->isScroll()?false:true);
} }
else
{
ui->pushButton_Pause->setText(tr("Pause"));
}
isScroll = isScroll?false:true;
} }
void Scope::on_pushButton_Clear_clicked() void Scope::on_pushButton_Clear_clicked()
{ {
timeseries = 0; if(chartView)
QList<QAbstractSeries *> slist = chartView->chart()->series();
for(QAbstractSeries *serie:slist)
{ {
QLineSeries *s = qobject_cast<QLineSeries *>(serie); chartView->removeAllSerie();
if(s)
{
chartView->chart()->removeSeries(s);
s->clear();
s->deleteLater();
delete s;
}
} }
emit clearall(); emit clearall();
} }
@@ -344,7 +107,7 @@ void Scope::on_pushButton_Close_clicked()
{ {
// delete ui; // delete ui;
emit willclose(); emit willclose();
this->hide(); this->hide();
// this->deleteLater(); // this->deleteLater();
// this->close(); // this->close();
+1 -46
View File
@@ -24,27 +24,6 @@ using namespace QtCharts;
namespace ColorMap{
static QColor Color[16] = {
QColor(165, 42, 42), //Channel1Color
QColor (255, 127, 80), //Channel2Color
QColor (30, 144, 255), //Channel3Color
QColor (218, 165, 32), //Channel4Color
QColor (255, 0, 255), //Channel5Color
QColor (147, 112, 219), //Channel6Color
QColor (0, 255, 255), //Channel7Color
QColor (0, 0, 128), //Channel8Color
QColor (128, 0, 128), //Channel9Color
QColor (46, 139, 87), //Channel10Color
QColor (152, 251, 152), //Channel11Color
QColor (0, 255, 127), //Channel12Color
QColor (0, 0, 255), //Channel13Color
QColor (135, 206, 250), //Channel14Color
QColor (255, 192, 203), //Channel15Color
QColor (127, 255, 0) //Channel16Color
};
}
namespace Ui { namespace Ui {
class Scope; class Scope;
@@ -59,7 +38,7 @@ public:
explicit Scope(QWidget *parent = nullptr); explicit Scope(QWidget *parent = nullptr);
~Scope(); ~Scope();
static int ChannelIndex; Chart *chartView;
protected: protected:
@@ -75,19 +54,8 @@ signals:
void willclose(void); void willclose(void);
public slots: public slots:
void setUseOpenGL(const bool &value);
bool addSeries(QString name);
bool addSeries(QString name,int index);
bool addSeries(QString name, QColor color);
void removeSerie(QString name);
void setSerieData(QString name,QVariant data);
private slots: private slots:
void timeout(void);
void chartUpdate(void);
void on_pushButton_Pause_clicked(); void on_pushButton_Pause_clicked();
@@ -102,20 +70,7 @@ private slots:
private: private:
Ui::Scope *ui; Ui::Scope *ui;
Chart *chartView;
QLineSeries *series;
QTimer *updateTimer = nullptr;
qreal timeseries = 0;//时间基准,注意时间长度,可能会出现时间反转的情况
bool isOpenGL = false;
bool isScroll = true;
double maxValue;
double minValue;
//Chart *mychart = nullptr;
}; };
@@ -737,7 +737,7 @@ void MAVLinkInspector::updateField(mavlink_message_t* msg, const mavlink_message
{ {
if(scope) if(scope)
{ {
scope->setSerieData(QString::number(msg->sysid) + "." + item->data(0,Qt::DisplayRole).toString(),item->data(1,Qt::DisplayRole)); scope->chartView->setSerieData(QString::number(msg->sysid) + "." + item->data(0,Qt::DisplayRole).toString(),item->data(1,Qt::DisplayRole));
} }
} }
} }
@@ -787,7 +787,7 @@ void MAVLinkInspector::on_treeWidget_itemChanged(QTreeWidgetItem *item, int colu
{ {
QString str = item->parent()->parent()->text(0); QString str = item->parent()->parent()->text(0);
str = str.mid(str.indexOf(' ') + 1); str = str.mid(str.indexOf(' ') + 1);
scope->removeSerie(str + "." + item->data(0,Qt::DisplayRole).toString()); scope->chartView->removeSerie(str + "." + item->data(0,Qt::DisplayRole).toString());
} }
} }
} }