qwt_plot_canvas.cpp 8.53 KB
Newer Older
pixhawk's avatar
pixhawk committed
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
/* -*- 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),
34
        cache(NULL) {
pixhawk's avatar
pixhawk committed
35 36
    }

37
    ~PrivateData() {
pixhawk's avatar
pixhawk committed
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
        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;

118 119 120 121 122
    switch(attribute) {
    case PaintCached: {
        if ( on ) {
            if ( d_data->cache == NULL )
                d_data->cache = new QPixmap();
pixhawk's avatar
pixhawk committed
123

124 125 126 127 128 129 130 131
            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;
pixhawk's avatar
pixhawk committed
132
        }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        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;
    }
pixhawk's avatar
pixhawk committed
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
    }
}

/*!
  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
194

pixhawk's avatar
pixhawk committed
195 196 197 198 199 200 201 202 203 204 205
  \sa FocusIndicator, setFocusIndicator
*/
QwtPlotCanvas::FocusIndicator QwtPlotCanvas::focusIndicator() const
{
    return d_data->focusIndicator;
}

void QwtPlotCanvas::hideEvent(QHideEvent *e)
{
    QFrame::hideEvent(e);

206
    if ( d_data->paintAttributes & PaintPacked ) {
pixhawk's avatar
pixhawk committed
207 208 209 210 211 212 213 214 215 216 217 218
        // 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);
219 220

    if ( !contentsRect().contains( event->rect() ) ) {
pixhawk's avatar
pixhawk committed
221 222 223
        painter.save();
        painter.setClipRegion( event->region() & frameRect() );
        drawFrame( &painter );
224
        painter.restore();
pixhawk's avatar
pixhawk committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    }

    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)
{
241 242
    if ( d_data->paintAttributes & PaintCached && d_data->cache
            && d_data->cache->size() == contentsRect().size() ) {
pixhawk's avatar
pixhawk committed
243
        painter->drawPixmap(contentsRect().topLeft(), *d_data->cache);
244
    } else {
pixhawk's avatar
pixhawk committed
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
        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
274
    bgBrush = palette().brush(backgroundRole());
pixhawk's avatar
pixhawk committed
275
#else
276
    QColorGroup::ColorRole role =
pixhawk's avatar
pixhawk committed
277 278 279 280
        QPalette::backgroundRoleFromMode( backgroundMode() );
    bgBrush = colorGroup().brush( role );
#endif

281
    if ( d_data->paintAttributes & PaintCached && d_data->cache ) {
pixhawk's avatar
pixhawk committed
282 283 284 285 286 287 288 289 290 291 292 293
        *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

294
        if ( d_data->paintAttributes & PaintPacked ) {
pixhawk's avatar
pixhawk committed
295 296 297 298 299
            QPainter bgPainter(d_data->cache);
            bgPainter.setPen(Qt::NoPen);

            bgPainter.setBrush(bgBrush);
            bgPainter.drawRect(d_data->cache->rect());
300
        } else
pixhawk's avatar
pixhawk committed
301 302 303 304
            d_data->cache->fill(this, d_data->cache->rect().topLeft());

        QPainter cachePainter(d_data->cache);
        cachePainter.translate(-contentsRect().x(),
305
                               -contentsRect().y());
pixhawk's avatar
pixhawk committed
306 307 308 309 310 311

        ((QwtPlot *)parent())->drawCanvas(&cachePainter);

        cachePainter.end();

        painter->drawPixmap(contentsRect(), *d_data->cache);
312
    } else {
pixhawk's avatar
pixhawk committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#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,
337
                      focusRect.width() - 2 * margin, focusRect.height() - 2 * margin);
pixhawk's avatar
pixhawk committed
338 339 340 341 342 343 344

    QwtPainter::drawFocusRect(painter, this, focusRect);
}

void QwtPlotCanvas::setSystemBackground(bool on)
{
#if QT_VERSION < 0x040000
345
    if ( backgroundMode() == Qt::NoBackground ) {
pixhawk's avatar
pixhawk committed
346 347
        if ( on )
            setBackgroundMode(Qt::PaletteBackground);
348
    } else {
pixhawk's avatar
pixhawk committed
349 350 351 352 353 354 355 356
        if ( !on )
            setBackgroundMode(Qt::NoBackground);
    }
#else
    if ( testAttribute(Qt::WA_NoSystemBackground) == on )
        setAttribute(Qt::WA_NoSystemBackground, !on);
#endif
}