[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 ):
|
---|
| 44 | QwtPlotSeriesItem<QwtPoint3D>( title )
|
---|
| 45 | {
|
---|
| 46 | init();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /*!
|
---|
| 50 | Constructor
|
---|
| 51 | \param title Title of the curve
|
---|
| 52 | */
|
---|
| 53 | QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QString &title ):
|
---|
| 54 | QwtPlotSeriesItem<QwtPoint3D>( QwtText( title ) )
|
---|
| 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;
|
---|
| 74 | d_series = new QwtPoint3DSeriesData();
|
---|
| 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 | /*!
|
---|
| 101 | \brief Return the current paint attributes
|
---|
| 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 | {
|
---|
| 115 | delete d_series;
|
---|
| 116 | d_series = new QwtPoint3DSeriesData( samples );
|
---|
| 117 | itemChanged();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /*!
|
---|
| 121 | Change the color map
|
---|
| 122 |
|
---|
| 123 | Often it is useful to display the mapping between intensities and
|
---|
| 124 | colors as an additional plot axis, showing a color bar.
|
---|
| 125 |
|
---|
| 126 | \param colorMap Color Map
|
---|
| 127 |
|
---|
| 128 | \sa colorMap(), setColorRange(), QwtColorMap::color(),
|
---|
| 129 | QwtScaleWidget::setColorBarEnabled(), QwtScaleWidget::setColorMap()
|
---|
| 130 | */
|
---|
| 131 | void QwtPlotSpectroCurve::setColorMap( QwtColorMap *colorMap )
|
---|
| 132 | {
|
---|
| 133 | if ( colorMap != d_data->colorMap )
|
---|
| 134 | {
|
---|
| 135 | delete d_data->colorMap;
|
---|
| 136 | d_data->colorMap = colorMap;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | itemChanged();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /*!
|
---|
| 143 | \return Color Map used for mapping the intensity values to colors
|
---|
| 144 | \sa setColorMap(), setColorRange(), QwtColorMap::color()
|
---|
| 145 | */
|
---|
| 146 | const QwtColorMap *QwtPlotSpectroCurve::colorMap() const
|
---|
| 147 | {
|
---|
| 148 | return d_data->colorMap;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /*!
|
---|
| 152 | Set the value interval, that corresponds to the color map
|
---|
| 153 |
|
---|
| 154 | \param interval interval.minValue() corresponds to 0.0,
|
---|
| 155 | interval.maxValue() to 1.0 on the color map.
|
---|
| 156 |
|
---|
| 157 | \sa colorRange(), setColorMap(), QwtColorMap::color()
|
---|
| 158 | */
|
---|
| 159 | void QwtPlotSpectroCurve::setColorRange( const QwtInterval &interval )
|
---|
| 160 | {
|
---|
| 161 | if ( interval != d_data->colorRange )
|
---|
| 162 | {
|
---|
| 163 | d_data->colorRange = interval;
|
---|
| 164 | itemChanged();
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /*!
|
---|
| 169 | \return Value interval, that corresponds to the color map
|
---|
| 170 | \sa setColorRange(), setColorMap(), QwtColorMap::color()
|
---|
| 171 | */
|
---|
| 172 | QwtInterval &QwtPlotSpectroCurve::colorRange() const
|
---|
| 173 | {
|
---|
| 174 | return d_data->colorRange;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /*!
|
---|
| 178 | Assign a pen width
|
---|
| 179 |
|
---|
| 180 | \param penWidth New pen width
|
---|
| 181 | \sa penWidth()
|
---|
| 182 | */
|
---|
| 183 | void QwtPlotSpectroCurve::setPenWidth(double penWidth)
|
---|
| 184 | {
|
---|
| 185 | if ( penWidth < 0.0 )
|
---|
| 186 | penWidth = 0.0;
|
---|
| 187 |
|
---|
| 188 | if ( d_data->penWidth != penWidth )
|
---|
| 189 | {
|
---|
| 190 | d_data->penWidth = penWidth;
|
---|
| 191 | itemChanged();
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /*!
|
---|
| 196 | \return Pen width used to draw a dot
|
---|
| 197 | \sa setPenWidth()
|
---|
| 198 | */
|
---|
| 199 | double QwtPlotSpectroCurve::penWidth() const
|
---|
| 200 | {
|
---|
| 201 | return d_data->penWidth;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /*!
|
---|
| 205 | Draw a subset of the points
|
---|
| 206 |
|
---|
| 207 | \param painter Painter
|
---|
| 208 | \param xMap Maps x-values into pixel coordinates.
|
---|
| 209 | \param yMap Maps y-values into pixel coordinates.
|
---|
| 210 | \param canvasRect Contents rect of the canvas
|
---|
| 211 | \param from Index of the first sample to be painted
|
---|
| 212 | \param to Index of the last sample to be painted. If to < 0 the
|
---|
| 213 | series will be painted to its last sample.
|
---|
| 214 |
|
---|
| 215 | \sa drawDots()
|
---|
| 216 | */
|
---|
| 217 | void QwtPlotSpectroCurve::drawSeries( QPainter *painter,
|
---|
| 218 | const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
---|
| 219 | const QRectF &canvasRect, int from, int to ) const
|
---|
| 220 | {
|
---|
| 221 | if ( !painter || dataSize() <= 0 )
|
---|
| 222 | return;
|
---|
| 223 |
|
---|
| 224 | if ( to < 0 )
|
---|
| 225 | to = dataSize() - 1;
|
---|
| 226 |
|
---|
| 227 | if ( from < 0 )
|
---|
| 228 | from = 0;
|
---|
| 229 |
|
---|
| 230 | if ( from >= to )
|
---|
| 231 | return;
|
---|
| 232 |
|
---|
| 233 | drawDots( painter, xMap, yMap, canvasRect, from, to );
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /*!
|
---|
| 237 | Draw a subset of the points
|
---|
| 238 |
|
---|
| 239 | \param painter Painter
|
---|
| 240 | \param xMap Maps x-values into pixel coordinates.
|
---|
| 241 | \param yMap Maps y-values into pixel coordinates.
|
---|
| 242 | \param canvasRect Contents rect of the canvas
|
---|
| 243 | \param from Index of the first sample to be painted
|
---|
| 244 | \param to Index of the last sample to be painted. If to < 0 the
|
---|
| 245 | series will be painted to its last sample.
|
---|
| 246 |
|
---|
| 247 | \sa drawSeries()
|
---|
| 248 | */
|
---|
| 249 | void QwtPlotSpectroCurve::drawDots( QPainter *painter,
|
---|
| 250 | const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
---|
| 251 | const QRectF &canvasRect, int from, int to ) const
|
---|
| 252 | {
|
---|
| 253 | if ( !d_data->colorRange.isValid() )
|
---|
| 254 | return;
|
---|
| 255 |
|
---|
| 256 | const bool doAlign = QwtPainter::roundingAlignment( painter );
|
---|
| 257 |
|
---|
| 258 | const QwtColorMap::Format format = d_data->colorMap->format();
|
---|
| 259 | if ( format == QwtColorMap::Indexed )
|
---|
| 260 | d_data->colorTable = d_data->colorMap->colorTable( d_data->colorRange );
|
---|
| 261 |
|
---|
| 262 | for ( int i = from; i <= to; i++ )
|
---|
| 263 | {
|
---|
| 264 | const QwtPoint3D sample = d_series->sample( i );
|
---|
| 265 |
|
---|
| 266 | double xi = xMap.transform( sample.x() );
|
---|
| 267 | double yi = yMap.transform( sample.y() );
|
---|
| 268 | if ( doAlign )
|
---|
| 269 | {
|
---|
| 270 | xi = qRound( xi );
|
---|
| 271 | yi = qRound( yi );
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | if ( d_data->paintAttributes & QwtPlotSpectroCurve::ClipPoints )
|
---|
| 275 | {
|
---|
| 276 | if ( !canvasRect.contains( xi, yi ) )
|
---|
| 277 | continue;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | if ( format == QwtColorMap::RGB )
|
---|
| 281 | {
|
---|
| 282 | const QRgb rgb = d_data->colorMap->rgb(
|
---|
| 283 | d_data->colorRange, sample.z() );
|
---|
| 284 |
|
---|
| 285 | painter->setPen( QPen( QColor( rgb ), d_data->penWidth ) );
|
---|
| 286 | }
|
---|
| 287 | else
|
---|
| 288 | {
|
---|
| 289 | const unsigned char index = d_data->colorMap->colorIndex(
|
---|
| 290 | d_data->colorRange, sample.z() );
|
---|
| 291 |
|
---|
| 292 | painter->setPen( QPen( QColor( d_data->colorTable[index] ),
|
---|
| 293 | d_data->penWidth ) );
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | QwtPainter::drawPoint( painter, QPointF( xi, yi ) );
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | d_data->colorTable.clear();
|
---|
| 300 | }
|
---|