qwt_plot_rasteritem.cpp 7.4 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
/* -*- 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
 *****************************************************************************/

#include <qapplication.h>
#include <qdesktopwidget.h>
#include <qpaintdevice.h>
#include <qpainter.h>
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_scale_map.h"
#include "qwt_plot_rasteritem.h"

class QwtPlotRasterItem::PrivateData
{
public:
    PrivateData():
23
        alpha(-1) {
pixhawk's avatar
pixhawk committed
24 25 26 27 28
        cache.policy = QwtPlotRasterItem::NoCache;
    }

    int alpha;

29
    struct ImageCache {
pixhawk's avatar
pixhawk committed
30 31 32 33 34 35 36 37 38
        QwtPlotRasterItem::CachePolicy policy;
        QwtDoubleRect rect;
        QSize size;
        QImage image;
    } cache;
};

static QImage toRgba(const QImage& image, int alpha)
{
39
    if ( alpha < 0 || alpha >= 255 )
pixhawk's avatar
pixhawk committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        return image;

#if QT_VERSION < 0x040000
    QImage alphaImage(image.size(), 32);
    alphaImage.setAlphaBuffer(true);
#else
    QImage alphaImage(image.size(), QImage::Format_ARGB32);
#endif

    const QRgb mask1 = qRgba(0, 0, 0, alpha);
    const QRgb mask2 = qRgba(255, 255, 255, 0);
    const QRgb mask3 = qRgba(0, 0, 0, 255);

    const int w = image.size().width();
    const int h = image.size().height();

56 57
    if ( image.depth() == 8 ) {
        for ( int y = 0; y < h; y++ ) {
pixhawk's avatar
pixhawk committed
58 59 60 61 62 63
            QRgb* alphaLine = (QRgb*)alphaImage.scanLine(y);
            const unsigned char *line = image.scanLine(y);

            for ( int x = 0; x < w; x++ )
                *alphaLine++ = (image.color(*line++) & mask2) | mask1;
        }
64 65
    } else if ( image.depth() == 32 ) {
        for ( int y = 0; y < h; y++ ) {
pixhawk's avatar
pixhawk committed
66 67 68
            QRgb* alphaLine = (QRgb*)alphaImage.scanLine(y);
            const QRgb* line = (const QRgb*) image.scanLine(y);

69
            for ( int x = 0; x < w; x++ ) {
pixhawk's avatar
pixhawk committed
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 118 119
                const QRgb rgb = *line++;
                if ( rgb & mask3 ) // alpha != 0
                    *alphaLine++ = (rgb & mask2) | mask1;
                else
                    *alphaLine++ = rgb;
            }
        }
    }

    return alphaImage;
}

//! Constructor
QwtPlotRasterItem::QwtPlotRasterItem(const QString& title):
    QwtPlotItem(QwtText(title))
{
    init();
}

//! Constructor
QwtPlotRasterItem::QwtPlotRasterItem(const QwtText& title):
    QwtPlotItem(title)
{
    init();
}

//! Destructor
QwtPlotRasterItem::~QwtPlotRasterItem()
{
    delete d_data;
}

void QwtPlotRasterItem::init()
{
    d_data = new PrivateData();

    setItemAttribute(QwtPlotItem::AutoScale, true);
    setItemAttribute(QwtPlotItem::Legend, false);

    setZ(8.0);
}

/*!
   \brief Set an alpha value for the raster data

   Often a plot has several types of raster data organized in layers.
   ( f.e a geographical map, with weather statistics ).
   Using setAlpha() raster items can be stacked easily.

   The alpha value is a value [0, 255] to
120
   control the transparency of the image. 0 represents a fully
pixhawk's avatar
pixhawk committed
121
   transparent color, while 255 represents a fully opaque color.
122

pixhawk's avatar
pixhawk committed
123 124 125
   \param alpha Alpha value

   - alpha >= 0\n
126 127
     All alpha values of the pixels returned by renderImage() will be set to
     alpha, beside those with an alpha value of 0 (invalid pixels).
pixhawk's avatar
pixhawk committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
   - alpha < 0
     The alpha values returned by renderImage() are not changed.

   The default alpha value is -1.

   \sa alpha()
*/
void QwtPlotRasterItem::setAlpha(int alpha)
{
    if ( alpha < 0 )
        alpha = -1;

    if ( alpha > 255 )
        alpha = 255;

143
    if ( alpha != d_data->alpha ) {
pixhawk's avatar
pixhawk committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        d_data->alpha = alpha;

        itemChanged();
    }
}

/*!
  \return Alpha value of the raster item
  \sa setAlpha()
*/
int QwtPlotRasterItem::alpha() const
{
    return d_data->alpha;
}

/*!
  Change the cache policy

  The default policy is NoCache

  \param policy Cache policy
  \sa CachePolicy, cachePolicy()
*/
void QwtPlotRasterItem::setCachePolicy(
    QwtPlotRasterItem::CachePolicy policy)
{
170
    if ( d_data->cache.policy != policy ) {
pixhawk's avatar
pixhawk committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        d_data->cache.policy = policy;

        invalidateCache();
        itemChanged();
    }
}

/*!
  \return Cache policy
  \sa CachePolicy, setCachePolicy()
*/
QwtPlotRasterItem::CachePolicy QwtPlotRasterItem::cachePolicy() const
{
    return d_data->cache.policy;
}

/*!
   Invalidate the paint cache
   \sa setCachePolicy
*/
void QwtPlotRasterItem::invalidateCache()
{
    d_data->cache.image = QImage();
    d_data->cache.rect = QwtDoubleRect();
    d_data->cache.size = QSize();
}

/*!
   \brief Returns the recommended raster for a given rect.

   F.e the raster hint can be used to limit the resolution of
   the image that is rendered.

   The default implementation returns an invalid size (QSize()),
   what means: no hint.
*/
QSize QwtPlotRasterItem::rasterHint(const QwtDoubleRect &) const
{
    return QSize();
}

/*!
  \brief Draw the raster data
  \param painter Painter
  \param xMap X-Scale Map
  \param yMap Y-Scale Map
  \param canvasRect Contents rect of the plot canvas
*/
void QwtPlotRasterItem::draw(QPainter *painter,
220 221
                             const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                             const QRect &canvasRect) const
pixhawk's avatar
pixhawk committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
{
    if ( canvasRect.isEmpty() || d_data->alpha == 0 )
        return;

    QwtDoubleRect area = invTransform(xMap, yMap, canvasRect);
    if ( boundingRect().isValid() )
        area &= boundingRect();

    const QRect paintRect = transform(xMap, yMap, area);
    if ( !paintRect.isValid() )
        return;

    QImage image;

    bool doCache = true;
237 238
    if ( painter->device()->devType() == QInternal::Printer
            || painter->device()->devType() == QInternal::Picture ) {
pixhawk's avatar
pixhawk committed
239 240 241
        doCache = false;
    }

242
    if ( !doCache || d_data->cache.policy == NoCache ) {
pixhawk's avatar
pixhawk committed
243 244 245 246
        image = renderImage(xMap, yMap, area);
        if ( d_data->alpha >= 0 && d_data->alpha < 255 )
            image = toRgba(image, d_data->alpha);

247
    } else if ( d_data->cache.policy == PaintCache ) {
pixhawk's avatar
pixhawk committed
248
        if ( d_data->cache.image.isNull() || d_data->cache.rect != area
249
                || d_data->cache.size != paintRect.size() ) {
pixhawk's avatar
pixhawk committed
250 251 252 253 254 255 256 257
            d_data->cache.image = renderImage(xMap, yMap, area);
            d_data->cache.rect = area;
            d_data->cache.size = paintRect.size();
        }

        image = d_data->cache.image;
        if ( d_data->alpha >= 0 && d_data->alpha < 255 )
            image = toRgba(image, d_data->alpha);
258
    } else if ( d_data->cache.policy == ScreenCache ) {
pixhawk's avatar
pixhawk committed
259 260 261 262
        const QSize screenSize =
            QApplication::desktop()->screenGeometry().size();

        if ( paintRect.width() > screenSize.width() ||
263
                paintRect.height() > screenSize.height() ) {
pixhawk's avatar
pixhawk committed
264
            image = renderImage(xMap, yMap, area);
265 266
        } else {
            if ( d_data->cache.image.isNull() || d_data->cache.rect != area ) {
pixhawk's avatar
pixhawk committed
267 268 269 270 271 272 273
                QwtScaleMap cacheXMap = xMap;
                cacheXMap.setPaintInterval( 0, screenSize.width());

                QwtScaleMap cacheYMap = yMap;
                cacheYMap.setPaintInterval(screenSize.height(), 0);

                d_data->cache.image = renderImage(
274
                                          cacheXMap, cacheYMap, area);
pixhawk's avatar
pixhawk committed
275 276 277 278 279 280 281 282 283 284 285
                d_data->cache.rect = area;
                d_data->cache.size = paintRect.size();
            }

            image = d_data->cache.image;
        }
        image = toRgba(image, d_data->alpha);
    }

    painter->drawImage(paintRect, image);
}