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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_directpainter.h"
#include "qwt_scale_map.h"
#include "qwt_plot.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_seriesitem.h"
#include <qpainter.h>
#include <qevent.h>
#include <qapplication.h>
#include <qpixmap.h>
static inline void qwtRenderItem(
QPainter *painter, const QRect &canvasRect,
QwtPlotSeriesItem *seriesItem, int from, int to )
{
// A minor performance improvement is possible
// with caching the maps. TODO ...
QwtPlot *plot = seriesItem->plot();
const QwtScaleMap xMap = plot->canvasMap( seriesItem->xAxis() );
const QwtScaleMap yMap = plot->canvasMap( seriesItem->yAxis() );
painter->setRenderHint( QPainter::Antialiasing,
seriesItem->testRenderHint( QwtPlotItem::RenderAntialiased ) );
seriesItem->drawSeries( painter, xMap, yMap, canvasRect, from, to );
}
static inline bool qwtHasBackingStore( const QwtPlotCanvas *canvas )
{
return canvas->testPaintAttribute( QwtPlotCanvas::BackingStore )
&& canvas->backingStore() && !canvas->backingStore()->isNull();
}
class QwtPlotDirectPainter::PrivateData
{
public:
PrivateData():
attributes( 0 ),
hasClipping(false),
seriesItem( NULL )
{
}
QwtPlotDirectPainter::Attributes attributes;
bool hasClipping;
QRegion clipRegion;
QPainter painter;
QwtPlotSeriesItem *seriesItem;
int from;
int to;
};
//! Constructor
QwtPlotDirectPainter::QwtPlotDirectPainter( QObject *parent ):
QObject( parent )
{
d_data = new PrivateData;
}
//! Destructor
QwtPlotDirectPainter::~QwtPlotDirectPainter()
{
delete d_data;
}
/*!
Change an attribute
\param attribute Attribute to change
\param on On/Off
\sa Attribute, testAttribute()
*/
void QwtPlotDirectPainter::setAttribute( Attribute attribute, bool on )
{
if ( bool( d_data->attributes & attribute ) != on )
{
if ( on )
d_data->attributes |= attribute;
else
d_data->attributes &= ~attribute;
if ( ( attribute == AtomicPainter ) && on )
reset();
}
}
/*!
\return True, when attribute is enabled
\param attribute Attribute to be tested
\sa Attribute, setAttribute()
*/
bool QwtPlotDirectPainter::testAttribute( Attribute attribute ) const
{
return d_data->attributes & attribute;
}
/*!
En/Disables clipping
\param enable Enables clipping is true, disable it otherwise
\sa hasClipping(), clipRegion(), setClipRegion()
*/
void QwtPlotDirectPainter::setClipping( bool enable )
{
d_data->hasClipping = enable;
}
/*!
\return true, when clipping is enabled
\sa setClipping(), clipRegion(), setClipRegion()
*/
bool QwtPlotDirectPainter::hasClipping() const
{
return d_data->hasClipping;
}
/*!
\brief Assign a clip region and enable clipping
Depending on the environment setting a proper clip region might improve
the performance heavily. F.e. on Qt embedded only the clipped part of
the backing store will be copied to a ( maybe unaccelerated ) frame buffer
device.
\param region Clip region
\sa clipRegion(), hasClipping(), setClipping()
*/
void QwtPlotDirectPainter::setClipRegion( const QRegion ®ion )
{
d_data->clipRegion = region;
d_data->hasClipping = true;
}
/*!
\return Currently set clip region.
\sa setClipRegion(), setClipping(), hasClipping()
*/
QRegion QwtPlotDirectPainter::clipRegion() const
{
return d_data->clipRegion;
}
/*!
\brief Draw a set of points of a seriesItem.
When observing an measurement while it is running, new points have to be
added to an existing seriesItem. drawSeries() can be used to display them avoiding
a complete redraw of the canvas.
Setting plot()->canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
will result in faster painting, if the paint engine of the canvas widget
supports this feature.
\param seriesItem Item to be painted
\param from Index of the first point to be painted
\param to Index of the last point to be painted. If to < 0 the
series will be painted to its last point.
*/
void QwtPlotDirectPainter::drawSeries(
QwtPlotSeriesItem *seriesItem, int from, int to )
{
if ( seriesItem == NULL || seriesItem->plot() == NULL )
return;
QWidget *canvas = seriesItem->plot()->canvas();
const QRect canvasRect = canvas->contentsRect();
QwtPlotCanvas *plotCanvas = qobject_cast<QwtPlotCanvas *>( canvas );
if ( plotCanvas && qwtHasBackingStore( plotCanvas ) )
{
QPainter painter( const_cast<QPixmap *>( plotCanvas->backingStore() ) );
if ( d_data->hasClipping )
painter.setClipRegion( d_data->clipRegion );
qwtRenderItem( &painter, canvasRect, seriesItem, from, to );
if ( testAttribute( QwtPlotDirectPainter::FullRepaint ) )
{
plotCanvas->repaint();
return;
}
}
bool immediatePaint = true;
if ( !canvas->testAttribute( Qt::WA_WState_InPaintEvent ) )
{
#if QT_VERSION < 0x050000
if ( !canvas->testAttribute( Qt::WA_PaintOutsidePaintEvent ) )
#endif
immediatePaint = false;
}
if ( immediatePaint )
{
if ( !d_data->painter.isActive() )
{
reset();
d_data->painter.begin( canvas );
canvas->installEventFilter( this );
}
if ( d_data->hasClipping )
{
d_data->painter.setClipRegion(
QRegion( canvasRect ) & d_data->clipRegion );
}
else
{
if ( !d_data->painter.hasClipping() )
d_data->painter.setClipRect( canvasRect );
}
qwtRenderItem( &d_data->painter, canvasRect, seriesItem, from, to );
if ( d_data->attributes & QwtPlotDirectPainter::AtomicPainter )
{
reset();
}
else
{
if ( d_data->hasClipping )
d_data->painter.setClipping( false );
}
}
else
{
reset();
d_data->seriesItem = seriesItem;
d_data->from = from;
d_data->to = to;
QRegion clipRegion = canvasRect;
if ( d_data->hasClipping )
clipRegion &= d_data->clipRegion;
canvas->installEventFilter( this );
canvas->repaint(clipRegion);
canvas->removeEventFilter( this );
d_data->seriesItem = NULL;
}
}
//! Close the internal QPainter
void QwtPlotDirectPainter::reset()
{
if ( d_data->painter.isActive() )
{
QWidget *w = static_cast<QWidget *>( d_data->painter.device() );
if ( w )
w->removeEventFilter( this );
d_data->painter.end();
}
}
//! Event filter
bool QwtPlotDirectPainter::eventFilter( QObject *, QEvent *event )
{
if ( event->type() == QEvent::Paint )
{
reset();
if ( d_data->seriesItem )
{
const QPaintEvent *pe = static_cast< QPaintEvent *>( event );
QWidget *canvas = d_data->seriesItem->plot()->canvas();
QPainter painter( canvas );
painter.setClipRegion( pe->region() );
bool doCopyCache = testAttribute( CopyBackingStore );
if ( doCopyCache )
{
QwtPlotCanvas *plotCanvas =
qobject_cast<QwtPlotCanvas *>( canvas );
if ( plotCanvas )
{
doCopyCache = qwtHasBackingStore( plotCanvas );
if ( doCopyCache )
{
painter.drawPixmap( plotCanvas->contentsRect().topLeft(),
*plotCanvas->backingStore() );
}
}
}
if ( !doCopyCache )
{
qwtRenderItem( &painter, canvas->contentsRect(),
d_data->seriesItem, d_data->from, d_data->to );
}
return true; // don't call QwtPlotCanvas::paintEvent()
}
}
return false;
}