add Zoom in Scope

This commit is contained in:
Ryan Tang 2023-02-09 19:05:30 -05:00
parent 1579e38322
commit 95a1a6ed6b
2 changed files with 87 additions and 5 deletions

View File

@ -3,11 +3,9 @@
#include <QValueAxis>
#include <QRandomGenerator>
#include <QGroupBox>
#include <QChartView>
#include <QStandardItemModel>
#include <QLabel>
Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDataThread, QMainWindow *parent) : QMainWindow(parent){
this->digi = digi;
this->nDigi = nDigi;
@ -19,11 +17,11 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
allowChange = false;
plot = new QChart();
plot = new Trace();
for( int i = 0; i < 6; i++) {
dataTrace[i] = new QLineSeries();
dataTrace[i]->setName("Trace " + QString::number(i));
for(int j = 0; j < 100; j ++) dataTrace[i]->append(j, QRandomGenerator::global()->bounded(8000) + 8000);
for(int j = 0; j < 100; j ++) dataTrace[i]->append(40*j, QRandomGenerator::global()->bounded(8000) + 8000);
plot->addSeries(dataTrace[i]);
}
@ -34,6 +32,7 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
dataTrace[4]->setPen(QPen(Qt::darkGreen, 1));
dataTrace[5]->setPen(QPen(Qt::darkBlue, 1));
plot->setAnimationOptions(QChart::SeriesAnimations);
plot->createDefaultAxes(); /// this must be after addSeries();
/// this must be after createDefaultAxes();
QValueAxis * yaxis = qobject_cast<QValueAxis*> (plot->axes(Qt::Vertical).first());
@ -320,7 +319,7 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
//------------ plot view
rowID ++;
QChartView * plotView = new QChartView(plot);
TraceView * plotView = new TraceView(plot);
plotView->setRenderHints(QPainter::Antialiasing);
layout->addWidget(plotView, rowID, 0, 1, 6);

83
scope.h
View File

@ -4,16 +4,99 @@
#include <QMainWindow>
#include <QChart>
#include <QChartView>
#include <QSpinBox>
#include <QPushButton>
#include <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QLineSeries>
#include <QRubberBand>
#include <QMouseEvent>
#include <QGestureEvent>
#include "ClassDigitizer2Gen.h"
#include "manyThread.h"
class Trace : public QChart{
public:
explicit Trace(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = {})
: QChart(QChart::ChartTypeCartesian, parent, wFlags){
grabGesture(Qt::PanGesture);
grabGesture(Qt::PinchGesture);
}
~Trace(){}
protected:
bool sceneEvent(QEvent *event){
if (event->type() == QEvent::Gesture) return gestureEvent(static_cast<QGestureEvent *>(event));
return QChart::event(event);
}
private:
bool gestureEvent(QGestureEvent *event){
if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
QPanGesture *pan = static_cast<QPanGesture *>(gesture);
QChart::scroll(-(pan->delta().x()), pan->delta().y());
}
if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) QChart::zoom(pinch->scaleFactor());
}
return true;
}
private:
};
class TraceView : public QChartView{
public:
TraceView(QChart * chart, QWidget * parent = nullptr): QChartView(chart, parent){
m_isTouching = false;
this->setRubberBand(QChartView::RectangleRubberBand);
}
protected:
bool viewportEvent(QEvent *event){
if (event->type() == QEvent::TouchBegin) {
m_isTouching = true;
chart()->setAnimationOptions(QChart::NoAnimation);
}
return QChartView::viewportEvent(event);
}
void mousePressEvent(QMouseEvent *event){
if (m_isTouching) return;
QChartView::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event){
if (m_isTouching) return;
QChartView::mouseMoveEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event){
if (m_isTouching) m_isTouching = false;
chart()->setAnimationOptions(QChart::SeriesAnimations);
QChartView::mouseReleaseEvent(event);
}
void keyPressEvent(QKeyEvent *event){
switch (event->key()) {
case Qt::Key_Plus: chart()->zoomIn(); break;
case Qt::Key_Minus: chart()->zoomOut(); break;
case Qt::Key_Left: chart()->scroll(-10, 0); break;
case Qt::Key_Right: chart()->scroll(10, 0); break;
case Qt::Key_Up: chart()->scroll(0, 10); break;
case Qt::Key_Down: chart()->scroll(0, -10); break;
case Qt::Key_R : chart()->axes(Qt::Vertical).first()->setRange(-16384, 65536); break;
default: QGraphicsView::keyPressEvent(event); break;
}
}
private:
bool m_isTouching;
};
class Scope : public QMainWindow{
Q_OBJECT