RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
incrementalplot.cpp
Go to the documentation of this file.
1 #include "incrementalplot.h"
2 #include <qwt_plot.h>
3 #include <qwt_plot_canvas.h>
4 #include <qwt_plot_curve.h>
5 #if QT_VERSION >= 0x040000
6 #include <QPaintEngine>
7 #endif
8 
10  : d_count(0)
11 {
12 }
13 
14 void
15 CurveData::append(double* x, double* y, int count)
16 {
17  int newSize = ((d_count + count) / 1000 + 1) * 1000;
18  if (newSize > size()) {
19  d_x.resize(newSize);
20  d_y.resize(newSize);
21  }
22 
23  for (register int i = 0; i < count; i++) {
24  d_x[d_count + i] = x[i];
25  d_y[d_count + i] = y[i];
26  }
27  d_count += count;
28 }
29 
30 int
32 {
33  return d_count;
34 }
35 
36 int
38 {
39  return d_x.size();
40 }
41 
42 const double*
43 CurveData::x() const
44 {
45  return d_x.data();
46 }
47 
48 const double*
49 CurveData::y() const
50 {
51  return d_y.data();
52 }
53 
55  : BasicPlot(parent)
56  , d_data(NULL)
57  , d_curve(NULL)
58  , l_data(NULL)
59  , l_curve(NULL)
60 {
61  setAutoReplot(false);
62 }
63 
65 {
66  delete d_data;
67 }
68 
69 const double*
71 {
72  return d_data->x();
73 }
74 
75 const double*
77 {
78  return d_data->y();
79 }
80 
81 const uint
83 {
84  return d_data->count();
85 }
86 
87 bool
89 {
90  if (d_data == NULL)
91  return false;
92  else
93  return true;
94 }
95 
96 void
97 IncrementalPlot::appendData(double x, double y)
98 {
99  appendData(&x, &y, 1);
100 }
101 
102 void
103 IncrementalPlot::appendData(double x, double y, QwtSymbol::Style s)
104 {
105  appendData(&x, &y, 1, s);
106 }
107 
108 void
109 IncrementalPlot::appendData(double* x, double* y, int size)
110 {
111  if (d_data == NULL)
112  d_data = new CurveData;
113 
114  if (d_curve == NULL) {
115  d_curve = new QwtPlotCurve("Data");
116  d_curve->setStyle(QwtPlotCurve::NoCurve);
117  d_curve->setPaintAttribute(QwtPlotCurve::FilterPoints, true);
118  const QColor& c = Qt::white;
119  QwtSymbol* symbol = new QwtSymbol(QwtSymbol::Ellipse);
120  symbol->setBrush(QBrush(c));
121  symbol->setPen(QPen(c));
122  symbol->setSize(QSize(6, 6));
123  d_curve->setSymbol(symbol);
124  d_curve->attach(this);
125  }
126 
127  d_data->append(x, y, size);
128  d_curve->setRawSamples(d_data->x(), d_data->y(), d_data->count());
129 #ifdef __GNUC__
130 #warning better use QwtData
131 #endif
132 
133  const bool cacheMode =
134  qobject_cast<QwtPlotCanvas*>(canvas())->testPaintAttribute(
135  QwtPlotCanvas::BackingStore);
136 
137 #if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
138  // Even if not recommended by TrollTech, Qt::WA_PaintOutsidePaintEvent
139  // works on X11. This has an tremendous effect on the performance..
140 
141  canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
142 #endif
143 
144  QwtPlotCanvas* plotCanvas = qobject_cast<QwtPlotCanvas*>(canvas());
145  plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, false);
146  QPainter painter;
147  QwtScaleMap xMap = plotCanvas->plot()->canvasMap(d_curve->xAxis());
148  QwtScaleMap yMap = plotCanvas->plot()->canvasMap(d_curve->yAxis());
149  d_curve->drawSeries(&painter, xMap, yMap, (d_curve->paintRect(xMap, yMap)),
150  d_curve->dataSize() - size, d_curve->dataSize() - 1);
151  plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, cacheMode);
152 
153 #if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
154  canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, false);
155 #endif
156 }
157 
158 void
159 IncrementalPlot::appendData(double* x, double* y, int size, QwtSymbol::Style s)
160 {
161  if (d_data == NULL)
162  d_data = new CurveData;
163 
164  if (d_curve == NULL) {
165  d_curve = new QwtPlotCurve("Data");
166  d_curve->setStyle(QwtPlotCurve::NoCurve);
167  d_curve->setPaintAttribute(QwtPlotCurve::FilterPoints);
168  const QColor& c = Qt::white;
169  QwtSymbol* symbol = new QwtSymbol(s, QBrush(c), QPen(c), QSize(6, 6));
170  d_curve->setSymbol(symbol);
171  d_curve->attach(this);
172  }
173 
174  d_data->append(x, y, size);
175  d_curve->setRawSamples(d_data->x(), d_data->y(), d_data->count());
176 #ifdef __GNUC__
177 #warning better use QwtData
178 #endif
179 
180  const bool cacheMode =
181  qobject_cast<QwtPlotCanvas*>(canvas())->testPaintAttribute(
182  QwtPlotCanvas::BackingStore);
183 
184 #if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
185  // Even if not recommended by TrollTech, Qt::WA_PaintOutsidePaintEvent
186  // works on X11. This has an tremendous effect on the performance..
187 
188  canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
189 #endif
190 
191  QwtPlotCanvas* plotCanvas = qobject_cast<QwtPlotCanvas*>(canvas());
192  plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, false);
193  QPainter painter;
194  QwtScaleMap xMap = plotCanvas->plot()->canvasMap(d_curve->xAxis());
195  QwtScaleMap yMap = plotCanvas->plot()->canvasMap(d_curve->yAxis());
196  d_curve->drawSeries(&painter, xMap, yMap, (d_curve->paintRect(xMap, yMap)),
197  d_curve->dataSize() - size, d_curve->dataSize() - 1);
198  plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, cacheMode);
199 
200 #if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
201  canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, false);
202 #endif
203 }
204 
205 void
206 IncrementalPlot::appendLine(double* x, double* y, int size)
207 {
208  if (l_data == NULL)
209  l_data = new CurveData;
210 
211  if (l_curve == NULL) {
212  l_curve = new QwtPlotCurve("Line");
213  l_curve->setStyle(QwtPlotCurve::Lines);
214  l_curve->setPaintAttribute(QwtPlotCurve::FilterPoints);
215  l_curve->setPen(QColor(Qt::white));
216  const QColor& c = Qt::white;
217  QwtSymbol* symbol =
218  new QwtSymbol(QwtSymbol::NoSymbol, QBrush(c), QPen(c), QSize(6, 6));
219  l_curve->setSymbol(symbol);
220  l_curve->attach(this);
221  }
222 
223  l_data->append(x, y, size);
224  l_curve->setRawSamples(l_data->x(), l_data->y(), l_data->count());
225 #ifdef __GNUC__
226 #warning better use QwtData
227 #endif
228 
229  const bool cacheMode =
230  qobject_cast<QwtPlotCanvas*>(canvas())->testPaintAttribute(
231  QwtPlotCanvas::BackingStore);
232 
233 #if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
234  // Even if not recommended by TrollTech, Qt::WA_PaintOutsidePaintEvent
235  // works on X11. This has an tremendous effect on the performance..
236 
237  canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
238 #endif
239 
240  QwtPlotCanvas* plotCanvas = qobject_cast<QwtPlotCanvas*>(canvas());
241  plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, false);
242  QPainter painter;
243  QwtScaleMap xMap = plotCanvas->plot()->canvasMap(l_curve->xAxis());
244  QwtScaleMap yMap = plotCanvas->plot()->canvasMap(l_curve->yAxis());
245  l_curve->drawSeries(&painter, xMap, yMap, (l_curve->paintRect(xMap, yMap)),
246  l_curve->dataSize() - size, l_curve->dataSize() - 1);
247  plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, cacheMode);
248 
249 #if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
250  canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, false);
251 #endif
252 }
253 
254 void
256 {
257  delete d_curve;
258  d_curve = NULL;
259  delete l_curve;
260  l_curve = NULL;
261 
262  delete d_data;
263  d_data = NULL;
264  delete l_data;
265  l_data = NULL;
266  replot();
267 }
268 
269 void
271 {
272  int n = d_curve->symbol()->style() + 1;
273  if (n > 14)
274  n = 0;
275 
276  d_curve->setSymbol(new QwtSymbol((QwtSymbol::Style)n, QBrush(Qt::white),
277  QPen(Qt::white), QSize(6, 6)));
278 }
int count() const
void appendLine(double *x, double *y, int size)
QwtPlotCurve * d_curve
CurveData * d_data
IncrementalPlot(QWidget *parent=NULL)
void appendData(double x, double y)
virtual ~IncrementalPlot()
const uint dataSize()
QwtPlotCurve * l_curve
QwtArray< double > d_x
const double * yData()
const double * y() const
void append(double *x, double *y, int count)
const double * x() const
CurveData * l_data
const double * xData()
QwtArray< double > d_y
int size() const