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
00090 void QwtPlotCanvas::setPaintAttribute(PaintAttribute attribute, bool on)
00091 {
00092 if ( bool(d_data->paintAttributes & attribute) == on )
00093 return;
00094
00095 if ( on )
00096 d_data->paintAttributes |= attribute;
00097 else
00098 d_data->paintAttributes &= ~attribute;
00099
00100 switch(attribute)
00101 {
00102 case PaintCached:
00103 {
00104 if ( on )
00105 {
00106 if ( d_data->cache == NULL )
00107 d_data->cache = new QPixmap();
00108
00109 if ( isVisible() )
00110 {
00111 const QRect cr = contentsRect();
00112 *d_data->cache = QPixmap::grabWidget(this,
00113 cr.x(), cr.y(), cr.width(), cr.height() );
00114 }
00115 }
00116 else
00117 {
00118 delete d_data->cache;
00119 d_data->cache = NULL;
00120 }
00121 break;
00122 }
00123 case PaintPacked:
00124 {
00125
00126
00127
00128
00129
00130
00131
00132 if ( on == false || isVisible() )
00133 QwtPlotCanvas::setSystemBackground(!on);
00134
00135 break;
00136 }
00137 }
00138 }
00139
00146 bool QwtPlotCanvas::testPaintAttribute(PaintAttribute attribute) const
00147 {
00148 return (d_data->paintAttributes & attribute) != 0;
00149 }
00150
00152 QPixmap *QwtPlotCanvas::paintCache()
00153 {
00154 return d_data->cache;
00155 }
00156
00158 const QPixmap *QwtPlotCanvas::paintCache() const
00159 {
00160 return d_data->cache;
00161 }
00162
00164 void QwtPlotCanvas::invalidatePaintCache()
00165 {
00166 if ( d_data->cache )
00167 *d_data->cache = QPixmap();
00168 }
00169
00175 void QwtPlotCanvas::setFocusIndicator(FocusIndicator focusIndicator)
00176 {
00177 d_data->focusIndicator = focusIndicator;
00178 }
00179
00185 QwtPlotCanvas::FocusIndicator QwtPlotCanvas::focusIndicator() const
00186 {
00187 return d_data->focusIndicator;
00188 }
00189
00190 void QwtPlotCanvas::hideEvent(QHideEvent *e)
00191 {
00192 QFrame::hideEvent(e);
00193
00194 if ( d_data->paintAttributes & PaintPacked )
00195 {
00196
00197
00198
00199 setSystemBackground(true);
00200 }
00201 }
00202
00203 void QwtPlotCanvas::paintEvent(QPaintEvent *event)
00204 {
00205 #if QT_VERSION >= 0x040000
00206 QPainter painter(this);
00207
00208 if ( !contentsRect().contains( event->rect() ) )
00209 {
00210 painter.save();
00211 painter.setClipRegion( event->region() & frameRect() );
00212 drawFrame( &painter );
00213 painter.restore();
00214 }
00215
00216 painter.setClipRegion(event->region() & contentsRect());
00217
00218 drawContents( &painter );
00219 #else // QT_VERSION < 0x040000
00220 QFrame::paintEvent(event);
00221 #endif
00222
00223 if ( d_data->paintAttributes & PaintPacked )
00224 setSystemBackground(false);
00225 }
00226
00228 void QwtPlotCanvas::drawContents(QPainter *painter)
00229 {
00230 if ( d_data->paintAttributes & PaintCached && d_data->cache
00231 && d_data->cache->size() == contentsRect().size() )
00232 {
00233 painter->drawPixmap(contentsRect().topLeft(), *d_data->cache);
00234 }
00235 else
00236 drawCanvas(painter);
00237
00238 if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
00239 drawFocusIndicator(painter);
00240 }
00241
00251 void QwtPlotCanvas::drawCanvas(QPainter *painter)
00252 {
00253 if ( !contentsRect().isValid() )
00254 return;
00255
00256 QBrush bgBrush;
00257 #if QT_VERSION >= 0x040000
00258 bgBrush = palette().brush(backgroundRole());
00259 #else
00260 QColorGroup::ColorRole role =
00261 QPalette::backgroundRoleFromMode( backgroundMode() );
00262 bgBrush = colorGroup().brush( role );
00263 #endif
00264
00265 if ( d_data->paintAttributes & PaintCached && d_data->cache )
00266 {
00267 *d_data->cache = QPixmap(contentsRect().size());
00268
00269 #ifdef Q_WS_X11
00270 #if QT_VERSION >= 0x040000
00271 if ( d_data->cache->x11Info().screen() != x11Info().screen() )
00272 d_data->cache->x11SetScreen(x11Info().screen());
00273 #else
00274 if ( d_data->cache->x11Screen() != x11Screen() )
00275 d_data->cache->x11SetScreen(x11Screen());
00276 #endif
00277 #endif
00278
00279 if ( d_data->paintAttributes & PaintPacked )
00280 {
00281 QPainter bgPainter(d_data->cache);
00282 bgPainter.setPen(Qt::NoPen);
00283
00284 bgPainter.setBrush(bgBrush);
00285 bgPainter.drawRect(d_data->cache->rect());
00286 }
00287 else
00288 d_data->cache->fill(this, d_data->cache->rect().topLeft());
00289
00290 QPainter cachePainter(d_data->cache);
00291 cachePainter.translate(-contentsRect().x(),
00292 -contentsRect().y());
00293
00294 ((QwtPlot *)parent())->drawCanvas(&cachePainter);
00295
00296 cachePainter.end();
00297
00298 painter->drawPixmap(contentsRect(), *d_data->cache);
00299 }
00300 else
00301 {
00302 #if QT_VERSION >= 0x040000
00303 if ( d_data->paintAttributes & PaintPacked )
00304 #endif
00305 {
00306 painter->save();
00307
00308 painter->setPen(Qt::NoPen);
00309 painter->setBrush(bgBrush);
00310 painter->drawRect(contentsRect());
00311
00312 painter->restore();
00313 }
00314
00315 ((QwtPlot *)parent())->drawCanvas(painter);
00316 }
00317 }
00318
00320 void QwtPlotCanvas::drawFocusIndicator(QPainter *painter)
00321 {
00322 const int margin = 1;
00323
00324 QRect focusRect = contentsRect();
00325 focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
00326 focusRect.width() - 2 * margin, focusRect.height() - 2 * margin);
00327
00328 QwtPainter::drawFocusRect(painter, this, focusRect);
00329 }
00330
00331 void QwtPlotCanvas::setSystemBackground(bool on)
00332 {
00333 #if QT_VERSION < 0x040000
00334 if ( backgroundMode() == Qt::NoBackground )
00335 {
00336 if ( on )
00337 setBackgroundMode(Qt::PaletteBackground);
00338 }
00339 else
00340 {
00341 if ( !on )
00342 setBackgroundMode(Qt::NoBackground);
00343 }
00344 #else
00345 if ( testAttribute(Qt::WA_NoSystemBackground) == on )
00346 setAttribute(Qt::WA_NoSystemBackground, !on);
00347 #endif
00348 }