Newer
Older
/* -*- 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():
cache.policy = QwtPlotRasterItem::NoCache;
}
int alpha;
QwtPlotRasterItem::CachePolicy policy;
QwtDoubleRect rect;
QSize size;
QImage image;
} cache;
};
static QImage toRgba(const QImage& image, int alpha)
{
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();
if ( image.depth() == 8 ) {
for ( int y = 0; y < h; y++ ) {
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;
}
} else if ( image.depth() == 32 ) {
for ( int y = 0; y < h; y++ ) {
QRgb* alphaLine = (QRgb*)alphaImage.scanLine(y);
const QRgb* line = (const QRgb*) image.scanLine(y);
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
control the transparency of the image. 0 represents a fully
All alpha values of the pixels returned by renderImage() will be set to
alpha, beside those with an alpha value of 0 (invalid pixels).
- 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;
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)
{
if ( d_data->cache.policy != policy ) {
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,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRect &canvasRect) const
{
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;
if ( painter->device()->devType() == QInternal::Printer
|| painter->device()->devType() == QInternal::Picture ) {
if ( !doCache || d_data->cache.policy == NoCache ) {
image = renderImage(xMap, yMap, area);
if ( d_data->alpha >= 0 && d_data->alpha < 255 )
image = toRgba(image, d_data->alpha);
} else if ( d_data->cache.policy == PaintCache ) {
if ( d_data->cache.image.isNull() || d_data->cache.rect != area
|| d_data->cache.size != paintRect.size() ) {
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);
} else if ( d_data->cache.policy == ScreenCache ) {
const QSize screenSize =
QApplication::desktop()->screenGeometry().size();
if ( paintRect.width() > screenSize.width() ||
paintRect.height() > screenSize.height() ) {
} else {
if ( d_data->cache.image.isNull() || d_data->cache.rect != area ) {
QwtScaleMap cacheXMap = xMap;
cacheXMap.setPaintInterval( 0, screenSize.width());
QwtScaleMap cacheYMap = yMap;
cacheYMap.setPaintInterval(screenSize.height(), 0);
d_data->cache.image = renderImage(