Newer
Older
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* -*- 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
*****************************************************************************/
// vim: expandtab
#include <qpainter.h>
#include <qstyle.h>
#if QT_VERSION >= 0x040000
#include <qstyleoption.h>
#include <qpaintengine.h>
#ifdef Q_WS_X11
#include <qx11info_x11.h>
#endif
#endif
#include <qevent.h>
#include "qwt_painter.h"
#include "qwt_math.h"
#include "qwt_plot.h"
#include "qwt_paint_buffer.h"
#include "qwt_plot_canvas.h"
class QwtPlotCanvas::PrivateData
{
public:
PrivateData():
focusIndicator(NoFocusIndicator),
paintAttributes(0),
cache(NULL)
{
}
~PrivateData()
{
delete cache;
}
FocusIndicator focusIndicator;
int paintAttributes;
QPixmap *cache;
};
//! Sets a cross cursor, enables QwtPlotCanvas::PaintCached
QwtPlotCanvas::QwtPlotCanvas(QwtPlot *plot):
QFrame(plot)
{
d_data = new PrivateData;
#if QT_VERSION >= 0x040100
setAutoFillBackground(true);
#endif
#if QT_VERSION < 0x040000
setWFlags(Qt::WNoAutoErase);
#ifndef QT_NO_CURSOR
setCursor(Qt::crossCursor);
#endif
#else
#ifndef QT_NO_CURSOR
setCursor(Qt::CrossCursor);
#endif
#endif // >= 0x040000
setPaintAttribute(PaintCached, true);
setPaintAttribute(PaintPacked, true);
}
//! Destructor
QwtPlotCanvas::~QwtPlotCanvas()
{
delete d_data;
}
//! Return parent plot widget
QwtPlot *QwtPlotCanvas::plot()
{
QWidget *w = parentWidget();
if ( w && w->inherits("QwtPlot") )
return (QwtPlot *)w;
return NULL;
}
//! Return parent plot widget
const QwtPlot *QwtPlotCanvas::plot() const
{
const QWidget *w = parentWidget();
if ( w && w->inherits("QwtPlot") )
return (QwtPlot *)w;
return NULL;
}
/*!
\brief Changing the paint attributes
\param attribute Paint attribute
\param on On/Off
The default setting enables PaintCached and PaintPacked
\sa testPaintAttribute(), drawCanvas(), drawContents(), paintCache()
*/
void QwtPlotCanvas::setPaintAttribute(PaintAttribute attribute, bool on)
{
if ( bool(d_data->paintAttributes & attribute) == on )
return;
if ( on )
d_data->paintAttributes |= attribute;
else
d_data->paintAttributes &= ~attribute;
switch(attribute)
{
case PaintCached:
{
if ( on )
{
if ( d_data->cache == NULL )
d_data->cache = new QPixmap();
if ( isVisible() )
{
const QRect cr = contentsRect();
*d_data->cache = QPixmap::grabWidget(this,
cr.x(), cr.y(), cr.width(), cr.height() );
}
}
else
{
delete d_data->cache;
d_data->cache = NULL;
}
break;
}
case PaintPacked:
{
/*
If not visible, changing of the background mode
is delayed until it becomes visible. This tries to avoid
looking through the canvas when the canvas is shown the first
time.
*/
if ( on == false || isVisible() )
QwtPlotCanvas::setSystemBackground(!on);
break;
}
}
}
/*!
Test wether a paint attribute is enabled
\param attribute Paint attribute
\return true if the attribute is enabled
\sa setPaintAttribute()
*/
bool QwtPlotCanvas::testPaintAttribute(PaintAttribute attribute) const
{
return (d_data->paintAttributes & attribute) != 0;
}
//! Return the paint cache, might be null
QPixmap *QwtPlotCanvas::paintCache()
{
return d_data->cache;
}
//! Return the paint cache, might be null
const QPixmap *QwtPlotCanvas::paintCache() const
{
return d_data->cache;
}
//! Invalidate the internal paint cache
void QwtPlotCanvas::invalidatePaintCache()
{
if ( d_data->cache )
*d_data->cache = QPixmap();
}
/*!
Set the focus indicator
\sa FocusIndicator, focusIndicator
*/
void QwtPlotCanvas::setFocusIndicator(FocusIndicator focusIndicator)
{
d_data->focusIndicator = focusIndicator;
}
/*!
\return Focus indicator
\sa FocusIndicator, setFocusIndicator
*/
QwtPlotCanvas::FocusIndicator QwtPlotCanvas::focusIndicator() const
{
return d_data->focusIndicator;
}
void QwtPlotCanvas::hideEvent(QHideEvent *e)
{
QFrame::hideEvent(e);
if ( d_data->paintAttributes & PaintPacked )
{
// enable system background to avoid the "looking through
// the canvas" effect, for the next show
setSystemBackground(true);
}
}
//! Paint event
void QwtPlotCanvas::paintEvent(QPaintEvent *event)
{
#if QT_VERSION >= 0x040000
QPainter painter(this);
if ( !contentsRect().contains( event->rect() ) )
{
painter.save();
painter.setClipRegion( event->region() & frameRect() );
drawFrame( &painter );
painter.restore();
}
painter.setClipRegion(event->region() & contentsRect());
drawContents( &painter );
#else // QT_VERSION < 0x040000
QFrame::paintEvent(event);
#endif
if ( d_data->paintAttributes & PaintPacked )
setSystemBackground(false);
}
//! Redraw the canvas, and focus rect
void QwtPlotCanvas::drawContents(QPainter *painter)
{
if ( d_data->paintAttributes & PaintCached && d_data->cache
&& d_data->cache->size() == contentsRect().size() )
{
painter->drawPixmap(contentsRect().topLeft(), *d_data->cache);
}
else
{
QwtPlot *plot = ((QwtPlot *)parent());
const bool doAutoReplot = plot->autoReplot();
plot->setAutoReplot(false);
drawCanvas(painter);
plot->setAutoReplot(doAutoReplot);
}
if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
drawFocusIndicator(painter);
}
/*!
Draw the the canvas
Paints all plot items to the contentsRect(), using QwtPlot::drawCanvas
and updates the paint cache.
\sa QwtPlot::drawCanvas, setPaintAttributes(), testPaintAttributes()
*/
void QwtPlotCanvas::drawCanvas(QPainter *painter)
{
if ( !contentsRect().isValid() )
return;
QBrush bgBrush;
#if QT_VERSION >= 0x040000
bgBrush = palette().brush(backgroundRole());
#else
QColorGroup::ColorRole role =
QPalette::backgroundRoleFromMode( backgroundMode() );
bgBrush = colorGroup().brush( role );
#endif
if ( d_data->paintAttributes & PaintCached && d_data->cache )
{
*d_data->cache = QPixmap(contentsRect().size());
#ifdef Q_WS_X11
#if QT_VERSION >= 0x040000
if ( d_data->cache->x11Info().screen() != x11Info().screen() )
d_data->cache->x11SetScreen(x11Info().screen());
#else
if ( d_data->cache->x11Screen() != x11Screen() )
d_data->cache->x11SetScreen(x11Screen());
#endif
#endif
if ( d_data->paintAttributes & PaintPacked )
{
QPainter bgPainter(d_data->cache);
bgPainter.setPen(Qt::NoPen);
bgPainter.setBrush(bgBrush);
bgPainter.drawRect(d_data->cache->rect());
}
else
d_data->cache->fill(this, d_data->cache->rect().topLeft());
QPainter cachePainter(d_data->cache);
cachePainter.translate(-contentsRect().x(),
-contentsRect().y());
((QwtPlot *)parent())->drawCanvas(&cachePainter);
cachePainter.end();
painter->drawPixmap(contentsRect(), *d_data->cache);
}
else
{
#if QT_VERSION >= 0x040000
if ( d_data->paintAttributes & PaintPacked )
#endif
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(bgBrush);
painter->drawRect(contentsRect());
painter->restore();
}
((QwtPlot *)parent())->drawCanvas(painter);
}
}
//! Draw the focus indication
void QwtPlotCanvas::drawFocusIndicator(QPainter *painter)
{
const int margin = 1;
QRect focusRect = contentsRect();
focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
focusRect.width() - 2 * margin, focusRect.height() - 2 * margin);
QwtPainter::drawFocusRect(painter, this, focusRect);
}
void QwtPlotCanvas::setSystemBackground(bool on)
{
#if QT_VERSION < 0x040000
if ( backgroundMode() == Qt::NoBackground )
{
if ( on )
setBackgroundMode(Qt::PaletteBackground);
}
else
{
if ( !on )
setBackgroundMode(Qt::NoBackground);
}
#else
if ( testAttribute(Qt::WA_NoSystemBackground) == on )
setAttribute(Qt::WA_NoSystemBackground, !on);
#endif
}