Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

# 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 #include <qwidget.h>
    00011 #include <qpainter.h>
    00012 #include "qwt_paint_buffer.h"
    00013
    00014 bool QwtPaintBuffer::d_enabled = TRUE;
    00015
    00017 QwtPaintBuffer::QwtPaintBuffer():
    00018     d_device(0),
    00019     d_painter(0),
    00020     d_devicePainter(0)
    00021 {
    00022 }
    00023
    00034 QwtPaintBuffer::QwtPaintBuffer(QPaintDevice *device,
    00035         const QRect &rect, QPainter *painter):
    00036     d_device(0),
    00037     d_painter(0),
    00038     d_devicePainter(0)
    00039 {
    00040     open(device, rect, painter);
    00041 }
    00042
    00047 QwtPaintBuffer::~QwtPaintBuffer()
    00048 {
    00049     close();
    00050 }
    00051
    00058 QPainter *QwtPaintBuffer::painter()
    00059 {
    00060     return d_painter;
    00061 }
    00062
    00066 const QPaintDevice *QwtPaintBuffer::device()
    00067 {
    00068     return d_device;
    00069 }
    00070
    00076 void QwtPaintBuffer::setEnabled(bool enable)
    00077 {
    00078     d_enabled = enable;
    00079 }
    00080
    00084 bool QwtPaintBuffer::isEnabled()
    00085 {
    00086     return d_enabled;
    00087 }
    00088
    00097 void QwtPaintBuffer::open(QPaintDevice *device,
    00098         const QRect &rect, QPainter *painter)
    00099 {
    00100     close();
    00101
    00102     if ( device == 0 || !rect.isValid() )
    00103         return;
    00104
    00105     d_device = device;
    00106     d_devicePainter = painter;
    00107     d_rect = rect;
    00108
    00109     if ( isEnabled() )
    00110     {
    00111 #if QT_VERSION >= 300
    00112 #ifdef Q_WS_X11
    00113         if ( d_pixBuffer.x11Screen() != d_device->x11Screen() )
    00114             d_pixBuffer.x11SetScreen(d_device->x11Screen());
    00115 #endif
    00116 #endif
    00117         d_pixBuffer.resize(d_rect.size());
    00118
    00119         d_painter = new QPainter();
    00120         if ( d_device->devType() == QInternal::Widget )
    00121         {
    00122             QWidget *w = (QWidget *)d_device;
    00123             d_pixBuffer.fill(w, d_rect.topLeft());
    00124             d_painter->begin(&d_pixBuffer, w);
    00125             d_painter->translate(-d_rect.x(), -d_rect.y());
    00126         }
    00127         else
    00128         {
    00129             d_painter->begin(&d_pixBuffer);
    00130         }
    00131     }
    00132     else
    00133     {
    00134         if ( d_devicePainter )
    00135             d_painter = d_devicePainter;
    00136         else
    00137             d_painter = new QPainter(d_device);
    00138
    00139         if ( d_device->devType() == QInternal::Widget )
    00140         {
    00141             QWidget *w = (QWidget *)d_device;
    00142             if ( w->testWFlags( Qt::WRepaintNoErase | Qt::WResizeNoErase) )
    00143                 d_painter->eraseRect(d_rect);
    00144         }
    00145     }
    00146 }
    00147
    00151 void QwtPaintBuffer::flush()
    00152 {
    00153     if ( d_enabled && d_device != 0 && d_rect.isValid())
    00154     {
    00155         // We need a painter to find out if
    00156         // there is a painter redirection for d_device.
    00157
    00158         QPainter *p;
    00159         if ( d_devicePainter == 0 )
    00160             p = new QPainter(d_device);
    00161         else
    00162             p = d_devicePainter;
    00163
    00164         QPaintDevice *device = p->device();
    00165         if ( device->isExtDev() )
    00166             d_devicePainter->drawPixmap(d_rect.topLeft(), d_pixBuffer);
    00167         else
    00168             bitBlt(device, d_rect.topLeft(), &d_pixBuffer );
    00169
    00170         if ( d_devicePainter == 0 )
    00171             delete p;
    00172     }
    00173 }
    00174
    00178 void QwtPaintBuffer::close()
    00179 {
    00180     flush();
    00181
    00182     if ( d_painter )
    00183     {
    00184         if ( d_painter->isActive() )
    00185             d_painter->end();
    00186
    00187         if ( d_painter != d_devicePainter )
    00188             delete d_painter;
    00189     }
    00190
    00191     if ( !d_pixBuffer.isNull() )
    00192         d_pixBuffer = QPixmap();
    00193
    00194     d_device = 0;
    00195     d_painter = 0;
    00196     d_devicePainter = 0;
    00197 }