qwt_paint_buffer.cpp

00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 
00011 #include <qglobal.h>
00012 #if QT_VERSION < 0x040000
00013 
00014 #include <qwidget.h>
00015 #include <qpainter.h>
00016 #include "qwt_paint_buffer.h"
00017 
00018 bool QwtPaintBuffer::d_enabled = true;
00019 
00021 QwtPaintBuffer::QwtPaintBuffer():
00022     d_device(0),
00023     d_painter(0),
00024     d_devicePainter(0)
00025 {
00026 }
00027 
00038 QwtPaintBuffer::QwtPaintBuffer(QPaintDevice *device, 
00039         const QRect &rect, QPainter *painter):
00040     d_device(0),
00041     d_painter(0),
00042     d_devicePainter(0)
00043 {
00044     open(device, rect, painter);
00045 }
00046 
00051 QwtPaintBuffer::~QwtPaintBuffer()
00052 {
00053     close();
00054 }
00055 
00062 QPainter *QwtPaintBuffer::painter() 
00063 { 
00064     return d_painter; 
00065 }
00066 
00070 const QPaintDevice *QwtPaintBuffer::device() 
00071 { 
00072     return d_device; 
00073 }
00074 
00080 void QwtPaintBuffer::setEnabled(bool enable) 
00081 { 
00082     d_enabled = enable; 
00083 }
00084 
00088 bool QwtPaintBuffer::isEnabled() 
00089 { 
00090     return d_enabled; 
00091 }
00092 
00101 void QwtPaintBuffer::open(QPaintDevice *device, 
00102         const QRect &rect, QPainter *painter)
00103 {
00104     close();
00105 
00106     if ( device == 0 || !rect.isValid() )
00107         return;
00108 
00109     d_device = device;
00110     d_devicePainter = painter;
00111     d_rect = rect;
00112 
00113     if ( isEnabled() )
00114     {
00115 #ifdef Q_WS_X11
00116         if ( d_pixBuffer.x11Screen() != d_device->x11Screen() )
00117             d_pixBuffer.x11SetScreen(d_device->x11Screen());
00118 #endif
00119         d_pixBuffer.resize(d_rect.size());
00120 
00121         d_painter = new QPainter();
00122         if ( d_device->devType() == QInternal::Widget )
00123         {
00124             QWidget *w = (QWidget *)d_device;
00125             d_pixBuffer.fill(w, d_rect.topLeft());
00126             d_painter->begin(&d_pixBuffer, w);
00127             d_painter->translate(-d_rect.x(), -d_rect.y());
00128         }
00129         else
00130         {
00131             d_painter->begin(&d_pixBuffer);
00132         }
00133     }
00134     else
00135     {
00136         if ( d_devicePainter )
00137             d_painter = d_devicePainter;
00138         else
00139             d_painter = new QPainter(d_device);
00140 
00141         if ( d_device->devType() == QInternal::Widget )
00142         {
00143             QWidget *w = (QWidget *)d_device;
00144             if ( w->testWFlags( Qt::WNoAutoErase ) )
00145                 d_painter->eraseRect(d_rect);
00146         }
00147     }
00148 }
00149 
00153 void QwtPaintBuffer::flush()
00154 {
00155     if ( d_enabled && d_device != 0 && d_rect.isValid())
00156     {
00157         // We need a painter to find out if
00158         // there is a painter redirection for d_device.
00159 
00160         QPainter *p;
00161         if ( d_devicePainter == 0 )
00162             p = new QPainter(d_device);
00163         else 
00164             p = d_devicePainter;
00165 
00166         QPaintDevice *device = p->device();
00167         if ( device->isExtDev() )
00168             d_devicePainter->drawPixmap(d_rect.topLeft(), d_pixBuffer);
00169         else
00170             bitBlt(device, d_rect.topLeft(), &d_pixBuffer );
00171 
00172         if ( d_devicePainter == 0 )
00173             delete p;
00174     }
00175 }
00176 
00180 void QwtPaintBuffer::close()
00181 {
00182     flush();
00183 
00184     if ( d_painter )
00185     {
00186         if ( d_painter->isActive() )
00187             d_painter->end();
00188 
00189         if ( d_painter != d_devicePainter )
00190             delete d_painter;
00191     }
00192 
00193     if ( !d_pixBuffer.isNull() )
00194         d_pixBuffer = QPixmap();
00195 
00196     d_device = 0;
00197     d_painter = 0;
00198     d_devicePainter = 0;
00199 } 
00200 
00201 #endif // QT_VERSION < 0x040000

Generated on Mon Feb 26 21:22:37 2007 for Qwt User's Guide by  doxygen 1.4.6