-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathqmlplot.cpp
More file actions
133 lines (109 loc) · 3.39 KB
/
qmlplot.cpp
File metadata and controls
133 lines (109 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "qmlplot.h"
#include "qcustomplot.h"
#include <QDebug>
CustomPlotItem::CustomPlotItem( QQuickItem* parent ) : QQuickPaintedItem( parent )
, m_CustomPlot( nullptr ), m_timerId( 0 )
{
setFlag( QQuickItem::ItemHasContents, true );
setAcceptedMouseButtons( Qt::AllButtons );
connect( this, &QQuickPaintedItem::widthChanged, this, &CustomPlotItem::updateCustomPlotSize );
connect( this, &QQuickPaintedItem::heightChanged, this, &CustomPlotItem::updateCustomPlotSize );
}
CustomPlotItem::~CustomPlotItem()
{
delete m_CustomPlot;
m_CustomPlot = nullptr;
if(m_timerId != 0) {
killTimer(m_timerId);
}
}
void CustomPlotItem::initCustomPlot()
{
m_CustomPlot = new QCustomPlot();
updateCustomPlotSize();
m_CustomPlot->addGraph();
m_CustomPlot->graph( 0 )->setPen( QPen( Qt::red ) );
m_CustomPlot->xAxis->setLabel( "t" );
m_CustomPlot->yAxis->setLabel( "S" );
m_CustomPlot->xAxis->setRange( 0, 10 );
m_CustomPlot->yAxis->setRange( 0, 5 );
m_CustomPlot ->setInteractions( QCP::iRangeDrag | QCP::iRangeZoom );
startTimer(500);
connect( m_CustomPlot, &QCustomPlot::afterReplot, this, &CustomPlotItem::onCustomReplot );
m_CustomPlot->replot();
}
void CustomPlotItem::paint( QPainter* painter )
{
if (m_CustomPlot)
{
QPixmap picture( boundingRect().size().toSize() );
QCPPainter qcpPainter( &picture );
m_CustomPlot->toPainter( &qcpPainter );
painter->drawPixmap( QPoint(), picture );
}
}
void CustomPlotItem::mousePressEvent( QMouseEvent* event )
{
qDebug() << Q_FUNC_INFO;
routeMouseEvents( event );
}
void CustomPlotItem::mouseReleaseEvent( QMouseEvent* event )
{
qDebug() << Q_FUNC_INFO;
routeMouseEvents( event );
}
void CustomPlotItem::mouseMoveEvent( QMouseEvent* event )
{
routeMouseEvents( event );
}
void CustomPlotItem::mouseDoubleClickEvent( QMouseEvent* event )
{
qDebug() << Q_FUNC_INFO;
routeMouseEvents( event );
}
void CustomPlotItem::wheelEvent( QWheelEvent *event )
{
routeWheelEvents( event );
}
void CustomPlotItem::timerEvent(QTimerEvent *event)
{
static double t, U;
U = ((double)rand() / RAND_MAX) * 5;
m_CustomPlot->graph(0)->addData(t, U);
qDebug() << Q_FUNC_INFO << QString("Adding dot t = %1, S = %2").arg(t).arg(U);
t++;
m_CustomPlot->replot();
}
void CustomPlotItem::graphClicked( QCPAbstractPlottable* plottable )
{
qDebug() << Q_FUNC_INFO << QString( "Clicked on graph '%1 " ).arg( plottable->name() );
}
void CustomPlotItem::routeMouseEvents( QMouseEvent* event )
{
if (m_CustomPlot)
{
QMouseEvent* newEvent = new QMouseEvent( event->type(), event->localPos(), event->button(), event->buttons(), event->modifiers() );
QCoreApplication::postEvent( m_CustomPlot, newEvent );
}
}
void CustomPlotItem::routeWheelEvents( QWheelEvent* event )
{
if (m_CustomPlot)
{
QWheelEvent* newEvent = new QWheelEvent( event->pos(), event->delta(), event->buttons(), event->modifiers(), event->orientation() );
QCoreApplication::postEvent( m_CustomPlot, newEvent );
}
}
void CustomPlotItem::updateCustomPlotSize()
{
if (m_CustomPlot)
{
m_CustomPlot->setGeometry(0, 0, (int)width(), (int)height());
m_CustomPlot->setViewport(QRect(0, 0, (int)width(), (int)height()));
}
}
void CustomPlotItem::onCustomReplot()
{
qDebug() << Q_FUNC_INFO;
update();
}