Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qpainter.h>
00013 #include <qstyle.h>
00014 #if QT_VERSION >= 0x040000
00015 #include <qstyleoption.h>
00016 #include <qpaintengine.h>
00017 #ifdef Q_WS_X11
00018 #include <qx11info_x11.h>
00019 #endif
00020 #endif
00021 #include <qevent.h>
00022 #include "qwt_painter.h"
00023 #include "qwt_math.h"
00024 #include "qwt_plot.h"
00025 #include "qwt_paint_buffer.h"
00026 #include "qwt_plot_canvas.h"
00027
00028 class QwtPlotCanvas::PrivateData
00029 {
00030 public:
00031 PrivateData():
00032 focusIndicator(NoFocusIndicator),
00033 paintAttributes(0),
00034 cache(NULL)
00035 {
00036 }
00037
00038 ~PrivateData()
00039 {
00040 delete cache;
00041 }
00042
00043 FocusIndicator focusIndicator;
00044 int paintAttributes;
00045 QPixmap *cache;
00046 };
00047
00049
00050 QwtPlotCanvas::QwtPlotCanvas(QwtPlot *plot):
00051 QFrame(plot)
00052 {
00053 d_data = new PrivateData;
00054
00055 #if QT_VERSION >= 0x040100
00056 setAutoFillBackground(true);
00057 #endif
00058
00059 #if QT_VERSION < 0x040000
00060 setWFlags(Qt::WNoAutoErase);
00061 #ifndef QT_NO_CURSOR
00062 setCursor(Qt::crossCursor);
00063 #endif
00064 #else
00065 #ifndef QT_NO_CURSOR
00066 setCursor(Qt::CrossCursor);
00067 #endif
00068 #endif // >= 0x040000
00069
00070 setPaintAttribute(PaintCached, true);
00071 setPaintAttribute(PaintPacked, true);
00072 }
00073
00075 QwtPlotCanvas::~QwtPlotCanvas()
00076 {
00077 delete d_data;
00078 }
00079
00081 QwtPlot *QwtPlotCanvas::plot()
00082 {
00083 QWidget *w = parentWidget();
00084 if ( w && w->inherits("QwtPlot") )
00085 return (QwtPlot *)w;
00086
00087 return NULL;
00088 }
00089
00091 const QwtPlot *QwtPlotCanvas::plot() const
00092 {
00093 const QWidget *w = parentWidget();
00094 if ( w && w->inherits("QwtPlot") )
00095 return (QwtPlot *)w;
00096
00097 return NULL;
00098 }
00099
00110 void QwtPlotCanvas::setPaintAttribute(PaintAttribute attribute, bool on)
00111 {
00112 if ( bool(d_data->paintAttributes & attribute) == on )
00113 return;
00114
00115 if ( on )
00116 d_data->paintAttributes |= attribute;
00117 else
00118 d_data->paintAttributes &= ~attribute;
00119
00120 switch(attribute)
00121 {
00122 case PaintCached:
00123 {
00124 if ( on )
00125 {
00126 if ( d_data->cache == NULL )
00127 d_data->cache = new QPixmap();
00128
00129 if ( isVisible() )
00130 {
00131 const QRect cr = contentsRect();
00132 *d_data->cache = QPixmap::grabWidget(this,
00133 cr.x(), cr.y(), cr.width(), cr.height() );
00134 }
00135 }
00136 else
00137 {
00138 delete d_data->cache;
00139 d_data->cache = NULL;
00140 }
00141 break;
00142 }
00143 case PaintPacked:
00144 {
00145
00146
00147
00148
00149
00150
00151
00152 if ( on == false || isVisible() )
00153 QwtPlotCanvas::setSystemBackground(!on);
00154
00155 break;
00156 }
00157 }
00158 }
00159
00167 bool QwtPlotCanvas::testPaintAttribute(PaintAttribute attribute) const
00168 {
00169 return (d_data->paintAttributes & attribute) != 0;
00170 }
00171
00173 QPixmap *QwtPlotCanvas::paintCache()
00174 {
00175 return d_data->cache;
00176 }
00177
00179 const QPixmap *QwtPlotCanvas::paintCache() const
00180 {
00181 return d_data->cache;
00182 }
00183
00185 void QwtPlotCanvas::invalidatePaintCache()
00186 {
00187 if ( d_data->cache )
00188 *d_data->cache = QPixmap();
00189 }
00190
00196 void QwtPlotCanvas::setFocusIndicator(FocusIndicator focusIndicator)
00197 {
00198 d_data->focusIndicator = focusIndicator;
00199 }
00200
00206 QwtPlotCanvas::FocusIndicator QwtPlotCanvas::focusIndicator() const
00207 {
00208 return d_data->focusIndicator;
00209 }
00210
00215 void QwtPlotCanvas::hideEvent(QHideEvent *event)
00216 {
00217 QFrame::hideEvent(event);
00218
00219 if ( d_data->paintAttributes & PaintPacked )
00220 {
00221
00222
00223
00224 setSystemBackground(true);
00225 }
00226 }
00227
00232 void QwtPlotCanvas::paintEvent(QPaintEvent *event)
00233 {
00234 #if QT_VERSION >= 0x040000
00235 QPainter painter(this);
00236
00237 if ( !contentsRect().contains( event->rect() ) )
00238 {
00239 painter.save();
00240 painter.setClipRegion( event->region() & frameRect() );
00241 drawFrame( &painter );
00242 painter.restore();
00243 }
00244
00245 painter.setClipRegion(event->region() & contentsRect());
00246
00247 drawContents( &painter );
00248 #else // QT_VERSION < 0x040000
00249 QFrame::paintEvent(event);
00250 #endif
00251
00252 if ( d_data->paintAttributes & PaintPacked )
00253 setSystemBackground(false);
00254 }
00255
00260 void QwtPlotCanvas::drawContents(QPainter *painter)
00261 {
00262 if ( d_data->paintAttributes & PaintCached && d_data->cache
00263 && d_data->cache->size() == contentsRect().size() )
00264 {
00265 painter->drawPixmap(contentsRect().topLeft(), *d_data->cache);
00266 }
00267 else
00268 {
00269 QwtPlot *plot = ((QwtPlot *)parent());
00270 const bool doAutoReplot = plot->autoReplot();
00271 plot->setAutoReplot(false);
00272
00273 drawCanvas(painter);
00274
00275 plot->setAutoReplot(doAutoReplot);
00276 }
00277
00278 if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
00279 drawFocusIndicator(painter);
00280 }
00281
00292 void QwtPlotCanvas::drawCanvas(QPainter *painter)
00293 {
00294 if ( !contentsRect().isValid() )
00295 return;
00296
00297 QBrush bgBrush;
00298 #if QT_VERSION >= 0x040000
00299 bgBrush = palette().brush(backgroundRole());
00300 #else
00301 QColorGroup::ColorRole role =
00302 QPalette::backgroundRoleFromMode( backgroundMode() );
00303 bgBrush = colorGroup().brush( role );
00304 #endif
00305
00306 if ( d_data->paintAttributes & PaintCached && d_data->cache )
00307 {
00308 *d_data->cache = QPixmap(contentsRect().size());
00309
00310 #ifdef Q_WS_X11
00311 #if QT_VERSION >= 0x040000
00312 if ( d_data->cache->x11Info().screen() != x11Info().screen() )
00313 d_data->cache->x11SetScreen(x11Info().screen());
00314 #else
00315 if ( d_data->cache->x11Screen() != x11Screen() )
00316 d_data->cache->x11SetScreen(x11Screen());
00317 #endif
00318 #endif
00319
00320 if ( d_data->paintAttributes & PaintPacked )
00321 {
00322 QPainter bgPainter(d_data->cache);
00323 bgPainter.setPen(Qt::NoPen);
00324
00325 bgPainter.setBrush(bgBrush);
00326 bgPainter.drawRect(d_data->cache->rect());
00327 }
00328 else
00329 d_data->cache->fill(this, d_data->cache->rect().topLeft());
00330
00331 QPainter cachePainter(d_data->cache);
00332 cachePainter.translate(-contentsRect().x(),
00333 -contentsRect().y());
00334
00335 ((QwtPlot *)parent())->drawCanvas(&cachePainter);
00336
00337 cachePainter.end();
00338
00339 painter->drawPixmap(contentsRect(), *d_data->cache);
00340 }
00341 else
00342 {
00343 #if QT_VERSION >= 0x040000
00344 if ( d_data->paintAttributes & PaintPacked )
00345 #endif
00346 {
00347 painter->save();
00348
00349 painter->setPen(Qt::NoPen);
00350 painter->setBrush(bgBrush);
00351 painter->drawRect(contentsRect());
00352
00353 painter->restore();
00354 }
00355
00356 ((QwtPlot *)parent())->drawCanvas(painter);
00357 }
00358 }
00359
00364 void QwtPlotCanvas::drawFocusIndicator(QPainter *painter)
00365 {
00366 const int margin = 1;
00367
00368 QRect focusRect = contentsRect();
00369 focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
00370 focusRect.width() - 2 * margin, focusRect.height() - 2 * margin);
00371
00372 QwtPainter::drawFocusRect(painter, this, focusRect);
00373 }
00374
00375 void QwtPlotCanvas::setSystemBackground(bool on)
00376 {
00377 #if QT_VERSION < 0x040000
00378 if ( backgroundMode() == Qt::NoBackground )
00379 {
00380 if ( on )
00381 setBackgroundMode(Qt::PaletteBackground);
00382 }
00383 else
00384 {
00385 if ( !on )
00386 setBackgroundMode(Qt::NoBackground);
00387 }
00388 #else
00389 if ( testAttribute(Qt::WA_NoSystemBackground) == on )
00390 setAttribute(Qt::WA_NoSystemBackground, !on);
00391 #endif
00392 }
00393
00398 void QwtPlotCanvas::replot()
00399 {
00400 invalidatePaintCache();
00401
00402
00403
00404
00405
00406 const bool erase =
00407 !testPaintAttribute(QwtPlotCanvas::PaintPacked)
00408 && !testPaintAttribute(QwtPlotCanvas::PaintCached);
00409
00410 #if QT_VERSION >= 0x040000
00411 const bool noBackgroundMode = testAttribute(Qt::WA_NoBackground);
00412 if ( !erase && !noBackgroundMode )
00413 setAttribute(Qt::WA_NoBackground, true);
00414
00415 repaint(contentsRect());
00416
00417 if ( !erase && !noBackgroundMode )
00418 setAttribute(Qt::WA_NoBackground, false);
00419 #else
00420 repaint(contentsRect(), erase);
00421 #endif
00422 }