Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_plot_spectrocurve.h"
00011 #include "qwt_color_map.h"
00012 #include "qwt_scale_map.h"
00013 #include "qwt_painter.h"
00014 #include <qpainter.h>
00015
00016 class QwtPlotSpectroCurve::PrivateData
00017 {
00018 public:
00019 PrivateData():
00020 colorRange( 0.0, 1000.0 ),
00021 penWidth(0.0),
00022 paintAttributes( QwtPlotSpectroCurve::ClipPoints )
00023 {
00024 colorMap = new QwtLinearColorMap();
00025 }
00026
00027 ~PrivateData()
00028 {
00029 delete colorMap;
00030 }
00031
00032 QwtColorMap *colorMap;
00033 QwtInterval colorRange;
00034 QVector<QRgb> colorTable;
00035 double penWidth;
00036 QwtPlotSpectroCurve::PaintAttributes paintAttributes;
00037 };
00038
00043 QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QwtText &title ):
00044 QwtPlotSeriesItem( title )
00045 {
00046 init();
00047 }
00048
00053 QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QString &title ):
00054 QwtPlotSeriesItem( QwtText( title ) )
00055 {
00056 init();
00057 }
00058
00060 QwtPlotSpectroCurve::~QwtPlotSpectroCurve()
00061 {
00062 delete d_data;
00063 }
00064
00068 void QwtPlotSpectroCurve::init()
00069 {
00070 setItemAttribute( QwtPlotItem::Legend );
00071 setItemAttribute( QwtPlotItem::AutoScale );
00072
00073 d_data = new PrivateData;
00074 setData( new QwtPoint3DSeriesData() );
00075
00076 setZ( 20.0 );
00077 }
00078
00080 int QwtPlotSpectroCurve::rtti() const
00081 {
00082 return QwtPlotItem::Rtti_PlotSpectroCurve;
00083 }
00084
00092 void QwtPlotSpectroCurve::setPaintAttribute( PaintAttribute attribute, bool on )
00093 {
00094 if ( on )
00095 d_data->paintAttributes |= attribute;
00096 else
00097 d_data->paintAttributes &= ~attribute;
00098 }
00099
00104 bool QwtPlotSpectroCurve::testPaintAttribute( PaintAttribute attribute ) const
00105 {
00106 return ( d_data->paintAttributes & attribute );
00107 }
00108
00113 void QwtPlotSpectroCurve::setSamples( const QVector<QwtPoint3D> &samples )
00114 {
00115 setData( new QwtPoint3DSeriesData( samples ) );
00116 }
00117
00128 void QwtPlotSpectroCurve::setSamples(
00129 QwtSeriesData<QwtPoint3D> *data )
00130 {
00131 setData( data );
00132 }
00133
00145 void QwtPlotSpectroCurve::setColorMap( QwtColorMap *colorMap )
00146 {
00147 if ( colorMap != d_data->colorMap )
00148 {
00149 delete d_data->colorMap;
00150 d_data->colorMap = colorMap;
00151 }
00152
00153 legendChanged();
00154 itemChanged();
00155 }
00156
00161 const QwtColorMap *QwtPlotSpectroCurve::colorMap() const
00162 {
00163 return d_data->colorMap;
00164 }
00165
00174 void QwtPlotSpectroCurve::setColorRange( const QwtInterval &interval )
00175 {
00176 if ( interval != d_data->colorRange )
00177 {
00178 d_data->colorRange = interval;
00179
00180 legendChanged();
00181 itemChanged();
00182 }
00183 }
00184
00189 QwtInterval &QwtPlotSpectroCurve::colorRange() const
00190 {
00191 return d_data->colorRange;
00192 }
00193
00200 void QwtPlotSpectroCurve::setPenWidth(double penWidth)
00201 {
00202 if ( penWidth < 0.0 )
00203 penWidth = 0.0;
00204
00205 if ( d_data->penWidth != penWidth )
00206 {
00207 d_data->penWidth = penWidth;
00208
00209 legendChanged();
00210 itemChanged();
00211 }
00212 }
00213
00218 double QwtPlotSpectroCurve::penWidth() const
00219 {
00220 return d_data->penWidth;
00221 }
00222
00236 void QwtPlotSpectroCurve::drawSeries( QPainter *painter,
00237 const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00238 const QRectF &canvasRect, int from, int to ) const
00239 {
00240 if ( !painter || dataSize() <= 0 )
00241 return;
00242
00243 if ( to < 0 )
00244 to = dataSize() - 1;
00245
00246 if ( from < 0 )
00247 from = 0;
00248
00249 if ( from > to )
00250 return;
00251
00252 drawDots( painter, xMap, yMap, canvasRect, from, to );
00253 }
00254
00268 void QwtPlotSpectroCurve::drawDots( QPainter *painter,
00269 const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00270 const QRectF &canvasRect, int from, int to ) const
00271 {
00272 if ( !d_data->colorRange.isValid() )
00273 return;
00274
00275 const bool doAlign = QwtPainter::roundingAlignment( painter );
00276
00277 const QwtColorMap::Format format = d_data->colorMap->format();
00278 if ( format == QwtColorMap::Indexed )
00279 d_data->colorTable = d_data->colorMap->colorTable256();
00280
00281 const QwtSeriesData<QwtPoint3D> *series = data();
00282
00283 for ( int i = from; i <= to; i++ )
00284 {
00285 const QwtPoint3D sample = series->sample( i );
00286
00287 double xi = xMap.transform( sample.x() );
00288 double yi = yMap.transform( sample.y() );
00289 if ( doAlign )
00290 {
00291 xi = qRound( xi );
00292 yi = qRound( yi );
00293 }
00294
00295 if ( d_data->paintAttributes & QwtPlotSpectroCurve::ClipPoints )
00296 {
00297 if ( !canvasRect.contains( xi, yi ) )
00298 continue;
00299 }
00300
00301 if ( format == QwtColorMap::RGB )
00302 {
00303 const QRgb rgb = d_data->colorMap->rgb(
00304 d_data->colorRange, sample.z() );
00305
00306 painter->setPen( QPen( QColor::fromRgba( rgb ), d_data->penWidth ) );
00307 }
00308 else
00309 {
00310 const unsigned char index = d_data->colorMap->colorIndex(
00311 256, d_data->colorRange, sample.z() );
00312
00313 painter->setPen( QPen( QColor::fromRgba( d_data->colorTable[index] ),
00314 d_data->penWidth ) );
00315 }
00316
00317 QwtPainter::drawPoint( painter, QPointF( xi, yi ) );
00318 }
00319
00320 d_data->colorTable.clear();
00321 }