[4271] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 2 | * Qwt Widget Library
|
---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
| 8 | *****************************************************************************/
|
---|
| 9 |
|
---|
| 10 | #include "qwt_plot_spectrocurve.h"
|
---|
| 11 | #include "qwt_color_map.h"
|
---|
| 12 | #include "qwt_scale_map.h"
|
---|
| 13 | #include "qwt_painter.h"
|
---|
| 14 | #include <qpainter.h>
|
---|
| 15 |
|
---|
| 16 | class QwtPlotSpectroCurve::PrivateData
|
---|
| 17 | {
|
---|
| 18 | public:
|
---|
| 19 | PrivateData():
|
---|
| 20 | colorRange( 0.0, 1000.0 ),
|
---|
| 21 | penWidth(0.0),
|
---|
| 22 | paintAttributes( QwtPlotSpectroCurve::ClipPoints )
|
---|
| 23 | {
|
---|
| 24 | colorMap = new QwtLinearColorMap();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | ~PrivateData()
|
---|
| 28 | {
|
---|
| 29 | delete colorMap;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | QwtColorMap *colorMap;
|
---|
| 33 | QwtInterval colorRange;
|
---|
| 34 | QVector<QRgb> colorTable;
|
---|
| 35 | double penWidth;
|
---|
| 36 | QwtPlotSpectroCurve::PaintAttributes paintAttributes;
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | /*!
|
---|
| 40 | Constructor
|
---|
| 41 | \param title Title of the curve
|
---|
| 42 | */
|
---|
| 43 | QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QwtText &title ):
|
---|
[8127] | 44 | QwtPlotSeriesItem( title )
|
---|
[4271] | 45 | {
|
---|
| 46 | init();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /*!
|
---|
| 50 | Constructor
|
---|
| 51 | \param title Title of the curve
|
---|
| 52 | */
|
---|
| 53 | QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QString &title ):
|
---|
[8127] | 54 | QwtPlotSeriesItem( QwtText( title ) )
|
---|
[4271] | 55 | {
|
---|
| 56 | init();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | //! Destructor
|
---|
| 60 | QwtPlotSpectroCurve::~QwtPlotSpectroCurve()
|
---|
| 61 | {
|
---|
| 62 | delete d_data;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /*!
|
---|
| 66 | \brief Initialize data members
|
---|
| 67 | */
|
---|
| 68 | void QwtPlotSpectroCurve::init()
|
---|
| 69 | {
|
---|
| 70 | setItemAttribute( QwtPlotItem::Legend );
|
---|
| 71 | setItemAttribute( QwtPlotItem::AutoScale );
|
---|
| 72 |
|
---|
| 73 | d_data = new PrivateData;
|
---|
[8127] | 74 | setData( new QwtPoint3DSeriesData() );
|
---|
[4271] | 75 |
|
---|
| 76 | setZ( 20.0 );
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | //! \return QwtPlotItem::Rtti_PlotSpectroCurve
|
---|
| 80 | int QwtPlotSpectroCurve::rtti() const
|
---|
| 81 | {
|
---|
| 82 | return QwtPlotItem::Rtti_PlotSpectroCurve;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /*!
|
---|
| 86 | Specify an attribute how to draw the curve
|
---|
| 87 |
|
---|
| 88 | \param attribute Paint attribute
|
---|
| 89 | \param on On/Off
|
---|
| 90 | /sa PaintAttribute, testPaintAttribute()
|
---|
| 91 | */
|
---|
| 92 | void QwtPlotSpectroCurve::setPaintAttribute( PaintAttribute attribute, bool on )
|
---|
| 93 | {
|
---|
| 94 | if ( on )
|
---|
| 95 | d_data->paintAttributes |= attribute;
|
---|
| 96 | else
|
---|
| 97 | d_data->paintAttributes &= ~attribute;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /*!
|
---|
[8127] | 101 | \return True, when attribute is enabled
|
---|
[4271] | 102 | \sa PaintAttribute, setPaintAttribute()
|
---|
| 103 | */
|
---|
| 104 | bool QwtPlotSpectroCurve::testPaintAttribute( PaintAttribute attribute ) const
|
---|
| 105 | {
|
---|
| 106 | return ( d_data->paintAttributes & attribute );
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /*!
|
---|
| 110 | Initialize data with an array of samples.
|
---|
| 111 | \param samples Vector of points
|
---|
| 112 | */
|
---|
| 113 | void QwtPlotSpectroCurve::setSamples( const QVector<QwtPoint3D> &samples )
|
---|
| 114 | {
|
---|
[8127] | 115 | setData( new QwtPoint3DSeriesData( samples ) );
|
---|
[4271] | 116 | }
|
---|
| 117 |
|
---|
| 118 | /*!
|
---|
[8127] | 119 | Assign a series of samples
|
---|
[9383] | 120 |
|
---|
[8127] | 121 | setSamples() is just a wrapper for setData() without any additional
|
---|
| 122 | value - beside that it is easier to find for the developer.
|
---|
[9383] | 123 |
|
---|
[8127] | 124 | \param data Data
|
---|
| 125 | \warning The item takes ownership of the data object, deleting
|
---|
[9383] | 126 | it when its not used anymore.
|
---|
[8127] | 127 | */
|
---|
| 128 | void QwtPlotSpectroCurve::setSamples(
|
---|
| 129 | QwtSeriesData<QwtPoint3D> *data )
|
---|
| 130 | {
|
---|
| 131 | setData( data );
|
---|
[9383] | 132 | }
|
---|
[8127] | 133 |
|
---|
| 134 | /*!
|
---|
[4271] | 135 | Change the color map
|
---|
| 136 |
|
---|
| 137 | Often it is useful to display the mapping between intensities and
|
---|
| 138 | colors as an additional plot axis, showing a color bar.
|
---|
| 139 |
|
---|
| 140 | \param colorMap Color Map
|
---|
| 141 |
|
---|
| 142 | \sa colorMap(), setColorRange(), QwtColorMap::color(),
|
---|
| 143 | QwtScaleWidget::setColorBarEnabled(), QwtScaleWidget::setColorMap()
|
---|
| 144 | */
|
---|
| 145 | void QwtPlotSpectroCurve::setColorMap( QwtColorMap *colorMap )
|
---|
| 146 | {
|
---|
| 147 | if ( colorMap != d_data->colorMap )
|
---|
| 148 | {
|
---|
| 149 | delete d_data->colorMap;
|
---|
| 150 | d_data->colorMap = colorMap;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[8127] | 153 | legendChanged();
|
---|
[4271] | 154 | itemChanged();
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /*!
|
---|
| 158 | \return Color Map used for mapping the intensity values to colors
|
---|
| 159 | \sa setColorMap(), setColorRange(), QwtColorMap::color()
|
---|
| 160 | */
|
---|
| 161 | const QwtColorMap *QwtPlotSpectroCurve::colorMap() const
|
---|
| 162 | {
|
---|
| 163 | return d_data->colorMap;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /*!
|
---|
| 167 | Set the value interval, that corresponds to the color map
|
---|
| 168 |
|
---|
| 169 | \param interval interval.minValue() corresponds to 0.0,
|
---|
| 170 | interval.maxValue() to 1.0 on the color map.
|
---|
| 171 |
|
---|
| 172 | \sa colorRange(), setColorMap(), QwtColorMap::color()
|
---|
| 173 | */
|
---|
| 174 | void QwtPlotSpectroCurve::setColorRange( const QwtInterval &interval )
|
---|
| 175 | {
|
---|
| 176 | if ( interval != d_data->colorRange )
|
---|
| 177 | {
|
---|
| 178 | d_data->colorRange = interval;
|
---|
[8127] | 179 |
|
---|
| 180 | legendChanged();
|
---|
[4271] | 181 | itemChanged();
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /*!
|
---|
| 186 | \return Value interval, that corresponds to the color map
|
---|
| 187 | \sa setColorRange(), setColorMap(), QwtColorMap::color()
|
---|
| 188 | */
|
---|
| 189 | QwtInterval &QwtPlotSpectroCurve::colorRange() const
|
---|
| 190 | {
|
---|
| 191 | return d_data->colorRange;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /*!
|
---|
| 195 | Assign a pen width
|
---|
| 196 |
|
---|
| 197 | \param penWidth New pen width
|
---|
| 198 | \sa penWidth()
|
---|
| 199 | */
|
---|
| 200 | void QwtPlotSpectroCurve::setPenWidth(double penWidth)
|
---|
| 201 | {
|
---|
| 202 | if ( penWidth < 0.0 )
|
---|
| 203 | penWidth = 0.0;
|
---|
| 204 |
|
---|
| 205 | if ( d_data->penWidth != penWidth )
|
---|
| 206 | {
|
---|
| 207 | d_data->penWidth = penWidth;
|
---|
[8127] | 208 |
|
---|
| 209 | legendChanged();
|
---|
[4271] | 210 | itemChanged();
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /*!
|
---|
| 215 | \return Pen width used to draw a dot
|
---|
| 216 | \sa setPenWidth()
|
---|
| 217 | */
|
---|
| 218 | double QwtPlotSpectroCurve::penWidth() const
|
---|
| 219 | {
|
---|
| 220 | return d_data->penWidth;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /*!
|
---|
| 224 | Draw a subset of the points
|
---|
| 225 |
|
---|
| 226 | \param painter Painter
|
---|
| 227 | \param xMap Maps x-values into pixel coordinates.
|
---|
| 228 | \param yMap Maps y-values into pixel coordinates.
|
---|
[8127] | 229 | \param canvasRect Contents rectangle of the canvas
|
---|
[4271] | 230 | \param from Index of the first sample to be painted
|
---|
| 231 | \param to Index of the last sample to be painted. If to < 0 the
|
---|
| 232 | series will be painted to its last sample.
|
---|
| 233 |
|
---|
| 234 | \sa drawDots()
|
---|
| 235 | */
|
---|
| 236 | void QwtPlotSpectroCurve::drawSeries( QPainter *painter,
|
---|
| 237 | const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
---|
| 238 | const QRectF &canvasRect, int from, int to ) const
|
---|
| 239 | {
|
---|
| 240 | if ( !painter || dataSize() <= 0 )
|
---|
| 241 | return;
|
---|
| 242 |
|
---|
| 243 | if ( to < 0 )
|
---|
| 244 | to = dataSize() - 1;
|
---|
| 245 |
|
---|
| 246 | if ( from < 0 )
|
---|
| 247 | from = 0;
|
---|
| 248 |
|
---|
[8127] | 249 | if ( from > to )
|
---|
[4271] | 250 | return;
|
---|
| 251 |
|
---|
| 252 | drawDots( painter, xMap, yMap, canvasRect, from, to );
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /*!
|
---|
| 256 | Draw a subset of the points
|
---|
| 257 |
|
---|
| 258 | \param painter Painter
|
---|
| 259 | \param xMap Maps x-values into pixel coordinates.
|
---|
| 260 | \param yMap Maps y-values into pixel coordinates.
|
---|
[8127] | 261 | \param canvasRect Contents rectangle of the canvas
|
---|
[4271] | 262 | \param from Index of the first sample to be painted
|
---|
| 263 | \param to Index of the last sample to be painted. If to < 0 the
|
---|
| 264 | series will be painted to its last sample.
|
---|
| 265 |
|
---|
| 266 | \sa drawSeries()
|
---|
| 267 | */
|
---|
| 268 | void QwtPlotSpectroCurve::drawDots( QPainter *painter,
|
---|
| 269 | const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
---|
| 270 | const QRectF &canvasRect, int from, int to ) const
|
---|
| 271 | {
|
---|
| 272 | if ( !d_data->colorRange.isValid() )
|
---|
| 273 | return;
|
---|
| 274 |
|
---|
| 275 | const bool doAlign = QwtPainter::roundingAlignment( painter );
|
---|
| 276 |
|
---|
| 277 | const QwtColorMap::Format format = d_data->colorMap->format();
|
---|
| 278 | if ( format == QwtColorMap::Indexed )
|
---|
| 279 | d_data->colorTable = d_data->colorMap->colorTable( d_data->colorRange );
|
---|
| 280 |
|
---|
[8127] | 281 | const QwtSeriesData<QwtPoint3D> *series = data();
|
---|
| 282 |
|
---|
[4271] | 283 | for ( int i = from; i <= to; i++ )
|
---|
| 284 | {
|
---|
[8127] | 285 | const QwtPoint3D sample = series->sample( i );
|
---|
[4271] | 286 |
|
---|
| 287 | double xi = xMap.transform( sample.x() );
|
---|
| 288 | double yi = yMap.transform( sample.y() );
|
---|
| 289 | if ( doAlign )
|
---|
| 290 | {
|
---|
| 291 | xi = qRound( xi );
|
---|
| 292 | yi = qRound( yi );
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | if ( d_data->paintAttributes & QwtPlotSpectroCurve::ClipPoints )
|
---|
| 296 | {
|
---|
| 297 | if ( !canvasRect.contains( xi, yi ) )
|
---|
| 298 | continue;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | if ( format == QwtColorMap::RGB )
|
---|
| 302 | {
|
---|
| 303 | const QRgb rgb = d_data->colorMap->rgb(
|
---|
| 304 | d_data->colorRange, sample.z() );
|
---|
| 305 |
|
---|
[8127] | 306 | painter->setPen( QPen( QColor::fromRgba( rgb ), d_data->penWidth ) );
|
---|
[4271] | 307 | }
|
---|
| 308 | else
|
---|
| 309 | {
|
---|
| 310 | const unsigned char index = d_data->colorMap->colorIndex(
|
---|
| 311 | d_data->colorRange, sample.z() );
|
---|
| 312 |
|
---|
[9383] | 313 | painter->setPen( QPen( QColor::fromRgba( d_data->colorTable[index] ),
|
---|
[4271] | 314 | d_data->penWidth ) );
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | QwtPainter::drawPoint( painter, QPointF( xi, yi ) );
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | d_data->colorTable.clear();
|
---|
| 321 | }
|
---|