-
Notifications
You must be signed in to change notification settings - Fork 2
/
qwtquick2.cpp
141 lines (110 loc) · 3.41 KB
/
qwtquick2.cpp
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
134
135
136
137
138
139
140
141
#include "plotdata.h"
#include "qwtquick2.h"
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_renderer.h>
#include <QDebug>
QwtQuick2Plot::QwtQuick2Plot(QQuickItem* parent) : QQuickPaintedItem(parent)
, m_qwtPlot(nullptr), m_timerId(0)
{
setFlag(QQuickItem::ItemHasContents, true);
setAcceptedMouseButtons(Qt::AllButtons);
connect(this, &QQuickPaintedItem::widthChanged, this, &QwtQuick2Plot::updatePlotSize);
connect(this, &QQuickPaintedItem::heightChanged, this, &QwtQuick2Plot::updatePlotSize);
}
QwtQuick2Plot::~QwtQuick2Plot()
{
delete m_qwtPlot;
m_qwtPlot = nullptr;
if (m_timerId != 0) {
killTimer(m_timerId);
}
}
void QwtQuick2Plot::replotAndUpdate()
{
m_qwtPlot->replot();
update();
}
void QwtQuick2Plot::initQwtPlot()
{
m_qwtPlot = new QwtPlot();
// after replot() we need to call update() - so disable auto replot
m_qwtPlot->setAutoReplot(false);
m_qwtPlot->setStyleSheet("background: white");
updatePlotSize();
m_curve1 = new QwtPlotCurve("Curve 1");
m_curve1->setPen(QPen(Qt::red));
m_curve1->setStyle(QwtPlotCurve::Lines);
m_curve1->setRenderHint(QwtPlotItem::RenderAntialiased);
m_curve1->setData(new PlotData(&m_curve1_data));
m_qwtPlot->setAxisTitle(m_qwtPlot->xBottom, tr("t"));
m_qwtPlot->setAxisTitle(m_qwtPlot->yLeft, tr("S"));
m_curve1->attach(m_qwtPlot);
startTimer(500);
replotAndUpdate();
}
void QwtQuick2Plot::paint(QPainter* painter)
{
if (m_qwtPlot) {
QPixmap picture(boundingRect().size().toSize());
QwtPlotRenderer renderer;
renderer.renderTo(m_qwtPlot, picture);
painter->drawPixmap(QPoint(), picture);
}
}
void QwtQuick2Plot::mousePressEvent(QMouseEvent* event)
{
qDebug() << Q_FUNC_INFO;
routeMouseEvents(event);
}
void QwtQuick2Plot::mouseReleaseEvent(QMouseEvent* event)
{
qDebug() << Q_FUNC_INFO;
routeMouseEvents(event);
}
void QwtQuick2Plot::mouseMoveEvent(QMouseEvent* event)
{
routeMouseEvents(event);
}
void QwtQuick2Plot::mouseDoubleClickEvent(QMouseEvent* event)
{
qDebug() << Q_FUNC_INFO;
routeMouseEvents(event);
}
void QwtQuick2Plot::wheelEvent(QWheelEvent* event)
{
routeWheelEvents(event);
}
void QwtQuick2Plot::timerEvent(QTimerEvent* /*event*/)
{
static double t, U;
U = (static_cast<double>(qrand()) / RAND_MAX) * 5;
m_curve1_data.append(QPointF(t, U));
qDebug() << Q_FUNC_INFO << QString("Adding dot t = %1, S = %2").arg(t).arg(U);
t++;
replotAndUpdate();
}
void QwtQuick2Plot::routeMouseEvents(QMouseEvent* event)
{
if (m_qwtPlot) {
QMouseEvent* newEvent = new QMouseEvent(event->type(), event->localPos(),
event->button(), event->buttons(),
event->modifiers());
QCoreApplication::postEvent(m_qwtPlot, newEvent);
}
}
void QwtQuick2Plot::routeWheelEvents(QWheelEvent* event)
{
if (m_qwtPlot) {
QWheelEvent* newEvent = new QWheelEvent(event->pos(), event->delta(),
event->buttons(), event->modifiers(),
event->orientation());
QCoreApplication::postEvent(m_qwtPlot, newEvent);
}
}
void QwtQuick2Plot::updatePlotSize()
{
if (m_qwtPlot) {
m_qwtPlot->setGeometry(0, 0, static_cast<int>(width()), static_cast<int>(height()));
}
}