Qwt User's Guide: qwt_plot_spectrocurve.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_spectrocurve.cpp
1 /******************************************************************************
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 "qwt_text.h"
15
16 #include <qpainter.h>
17
18 class QwtPlotSpectroCurve::PrivateData
19 {
20 public:
21 PrivateData()
22 : colorRange( 0.0, 1000.0 )
23 , penWidth( 0.0 )
24 , paintAttributes( QwtPlotSpectroCurve::ClipPoints )
25 {
26 colorMap = new QwtLinearColorMap();
27 }
28
29 ~PrivateData()
30 {
31 delete colorMap;
32 }
33
34 QwtColorMap* colorMap;
35 QwtInterval colorRange;
36 QVector< QRgb > colorTable;
37 double penWidth;
38 QwtPlotSpectroCurve::PaintAttributes paintAttributes;
39 };
40
45 QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QwtText& title )
46 : QwtPlotSeriesItem( title )
47 {
48 init();
49 }
50
55 QwtPlotSpectroCurve::QwtPlotSpectroCurve( const QString& title )
56 : QwtPlotSeriesItem( QwtText( title ) )
57 {
58 init();
59 }
60
62 QwtPlotSpectroCurve::~QwtPlotSpectroCurve()
63 {
64 delete m_data;
65 }
66
70 void QwtPlotSpectroCurve::init()
71 {
72 setItemAttribute( QwtPlotItem::Legend );
73 setItemAttribute( QwtPlotItem::AutoScale );
74
75 m_data = new PrivateData;
76 setData( new QwtPoint3DSeriesData() );
77
78 setZ( 20.0 );
79 }
80
82 int QwtPlotSpectroCurve::rtti() const
83 {
84 return QwtPlotItem::Rtti_PlotSpectroCurve;
85 }
86
94 void QwtPlotSpectroCurve::setPaintAttribute( PaintAttribute attribute, bool on )
95 {
96 if ( on )
97 m_data->paintAttributes |= attribute;
98 else
99 m_data->paintAttributes &= ~attribute;
100 }
101
106 bool QwtPlotSpectroCurve::testPaintAttribute( PaintAttribute attribute ) const
107 {
108 return ( m_data->paintAttributes & attribute );
109 }
110
115 void QwtPlotSpectroCurve::setSamples( const QVector< QwtPoint3D >& samples )
116 {
117 setData( new QwtPoint3DSeriesData( samples ) );
118 }
119
130 void QwtPlotSpectroCurve::setSamples(
131 QwtSeriesData< QwtPoint3D >* data )
132 {
133 setData( data );
134 }
135
147 void QwtPlotSpectroCurve::setColorMap( QwtColorMap* colorMap )
148 {
149 if ( colorMap != m_data->colorMap )
150 {
151 delete m_data->colorMap;
152 m_data->colorMap = colorMap;
153 }
154
155 legendChanged();
156 itemChanged();
157 }
158
163 const QwtColorMap* QwtPlotSpectroCurve::colorMap() const
164 {
165 return m_data->colorMap;
166 }
167
176 void QwtPlotSpectroCurve::setColorRange( const QwtInterval& interval )
177 {
178 if ( interval != m_data->colorRange )
179 {
180 m_data->colorRange = interval;
181
182 legendChanged();
183 itemChanged();
184 }
185 }
186
191 QwtInterval& QwtPlotSpectroCurve::colorRange() const
192 {
193 return m_data->colorRange;
194 }
195
202 void QwtPlotSpectroCurve::setPenWidth(double penWidth)
203 {
204 if ( penWidth < 0.0 )
205 penWidth = 0.0;
206
207 if ( m_data->penWidth != penWidth )
208 {
209 m_data->penWidth = penWidth;
210
211 legendChanged();
212 itemChanged();
213 }
214 }
215
220 double QwtPlotSpectroCurve::penWidth() const
221 {
222 return m_data->penWidth;
223 }
224
238 void QwtPlotSpectroCurve::drawSeries( QPainter* painter,
239 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
240 const QRectF& canvasRect, int from, int to ) const
241 {
242 if ( !painter || dataSize() <= 0 )
243 return;
244
245 if ( to < 0 )
246 to = dataSize() - 1;
247
248 if ( from < 0 )
249 from = 0;
250
251 if ( from > to )
252 return;
253
254 drawDots( painter, xMap, yMap, canvasRect, from, to );
255 }
256
270 void QwtPlotSpectroCurve::drawDots( QPainter* painter,
271 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
272 const QRectF& canvasRect, int from, int to ) const
273 {
274 if ( !m_data->colorRange.isValid() )
275 return;
276
277 const bool doAlign = QwtPainter::roundingAlignment( painter );
278
279 const QwtColorMap::Format format = m_data->colorMap->format();
280 if ( format == QwtColorMap::Indexed )
281 m_data->colorTable = m_data->colorMap->colorTable256();
282
283 const QwtSeriesData< QwtPoint3D >* series = data();
284
285 for ( int i = from; i <= to; i++ )
286 {
287 const QwtPoint3D sample = series->sample( i );
288
289 double xi = xMap.transform( sample.x() );
290 double yi = yMap.transform( sample.y() );
291 if ( doAlign )
292 {
293 xi = qRound( xi );
294 yi = qRound( yi );
295 }
296
297 if ( m_data->paintAttributes & QwtPlotSpectroCurve::ClipPoints )
298 {
299 if ( !canvasRect.contains( xi, yi ) )
300 continue;
301 }
302
303 if ( format == QwtColorMap::RGB )
304 {
305 const QRgb rgb = m_data->colorMap->rgb(
306 m_data->colorRange, sample.z() );
307
308 painter->setPen( QPen( QColor::fromRgba( rgb ), m_data->penWidth ) );
309 }
310 else
311 {
312 const unsigned char index = m_data->colorMap->colorIndex(
313 256, m_data->colorRange, sample.z() );
314
315 painter->setPen( QPen( QColor::fromRgba( m_data->colorTable[index] ),
316 m_data->penWidth ) );
317 }
318
319 QwtPainter::drawPoint( painter, QPointF( xi, yi ) );
320 }
321
322 m_data->colorTable.clear();
323 }
QVector< QRgb >
QwtColorMap
QwtColorMap is used to map values into colors.
Definition: qwt_color_map.h:38
QwtColorMap::Format
Format
Definition: qwt_color_map.h:46
QwtColorMap::Indexed
@ Indexed
Definition: qwt_color_map.h:60
QwtColorMap::RGB
@ RGB
The map is intended to map into RGB values.
Definition: qwt_color_map.h:48
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:23
QwtLinearColorMap
QwtLinearColorMap builds a color map from color stops.
Definition: qwt_color_map.h:99
QwtPainter::drawPoint
static void drawPoint(QPainter *, const QPoint &)
Wrapper for QPainter::drawPoint()
Definition: qwt_painter.cpp:691
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition: qwt_plot_item.cpp:228
QwtPlotItem::Rtti_PlotSpectroCurve
@ Rtti_PlotSpectroCurve
For QwtPlotSpectroCurve.
Definition: qwt_plot_item.h:96
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPlotItem::AutoScale
@ AutoScale
Definition: qwt_plot_item.h:157
QwtPlotSeriesItem
Base class for plot items representing a series of samples.
Definition: qwt_plot_seriesitem.h:26
QwtPlotSpectroCurve
Curve that displays 3D points as dots, where the z coordinate is mapped to a color.
Definition: qwt_plot_spectrocurve.h:25
QwtPlotSpectroCurve::setColorRange
void setColorRange(const QwtInterval &)
Definition: qwt_plot_spectrocurve.cpp:176
QwtPlotSpectroCurve::drawSeries
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const override
Definition: qwt_plot_spectrocurve.cpp:238
QwtPlotSpectroCurve::colorMap
const QwtColorMap * colorMap() const
Definition: qwt_plot_spectrocurve.cpp:163
QwtPlotSpectroCurve::setPaintAttribute
void setPaintAttribute(PaintAttribute, bool on=true)
Definition: qwt_plot_spectrocurve.cpp:94
QwtPlotSpectroCurve::setSamples
void setSamples(const QVector< QwtPoint3D > &)
Definition: qwt_plot_spectrocurve.cpp:115
QwtPlotSpectroCurve::setColorMap
void setColorMap(QwtColorMap *)
Definition: qwt_plot_spectrocurve.cpp:147
QwtPlotSpectroCurve::~QwtPlotSpectroCurve
virtual ~QwtPlotSpectroCurve()
Destructor.
Definition: qwt_plot_spectrocurve.cpp:62
QwtPlotSpectroCurve::drawDots
virtual void drawDots(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
Definition: qwt_plot_spectrocurve.cpp:270
QwtPlotSpectroCurve::penWidth
double penWidth() const
Definition: qwt_plot_spectrocurve.cpp:220
QwtPlotSpectroCurve::colorRange
QwtInterval & colorRange() const
Definition: qwt_plot_spectrocurve.cpp:191
QwtPlotSpectroCurve::QwtPlotSpectroCurve
QwtPlotSpectroCurve(const QString &title=QString())
Definition: qwt_plot_spectrocurve.cpp:55
QwtPlotSpectroCurve::setPenWidth
void setPenWidth(double)
Definition: qwt_plot_spectrocurve.cpp:202
QwtPlotSpectroCurve::rtti
virtual int rtti() const override
Definition: qwt_plot_spectrocurve.cpp:82
QwtPlotSpectroCurve::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_plot_spectrocurve.cpp:106
QwtPlotSpectroCurve::PaintAttribute
PaintAttribute
Paint attributes.
Definition: qwt_plot_spectrocurve.h:29
QwtPlotSpectroCurve::ClipPoints
@ ClipPoints
Clip points outside the canvas rectangle.
Definition: qwt_plot_spectrocurve.h:31
QwtPlotSpectroCurve::PaintAttributes
QFlags< PaintAttribute > PaintAttributes
Definition: qwt_plot_spectrocurve.h:34
QwtPoint3D
QwtPoint3D class defines a 3D point in double coordinates.
Definition: qwt_point_3d.h:23
QwtPoint3D::z
double z() const
Definition: qwt_point_3d.h:115
QwtPoint3D::y
double y() const
Definition: qwt_point_3d.h:109
QwtPoint3D::x
double x() const
Definition: qwt_point_3d.h:103
QwtPoint3DSeriesData
Interface for iterating over an array of 3D points.
Definition: qwt_series_data.h:220
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtSeriesData
Abstract interface for iterating over samples.
Definition: qwt_series_data.h:50
QwtSeriesData::sample
virtual T sample(size_t i) const =0
QwtSeriesStore< QwtPoint3D >::sample
QwtPoint3D sample(int index) const
Definition: qwt_series_store.h:158
QwtSeriesStore< QwtPoint3D >::dataSize
virtual size_t dataSize() const override
Definition: qwt_series_store.h:175
QwtSeriesStore< QwtPoint3D >::data
QwtSeriesData< QwtPoint3D > * data()
Definition: qwt_series_store.h:146
QwtSeriesStore< QwtPoint3D >::setData
void setData(QwtSeriesData< QwtPoint3D > *series)
Definition: qwt_series_store.h:164
QwtText
A class representing a text.
Definition: qwt_text.h:52