source: ntrip/trunk/BNC/qwt/qwt_series_data.h@ 8450

Last change on this file since 8450 was 8127, checked in by stoecker, 8 years ago

update qwt and qwtpolar, many QT5 fixes (unfinished)

File size: 8.9 KB
Line 
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#ifndef QWT_SERIES_DATA_H
11#define QWT_SERIES_DATA_H 1
12
13#include "qwt_global.h"
14#include "qwt_samples.h"
15#include "qwt_point_3d.h"
16#include "qwt_point_polar.h"
17#include <qvector.h>
18#include <qrect.h>
19
20/*!
21 \brief Abstract interface for iterating over samples
22
23 Qwt offers several implementations of the QwtSeriesData API,
24 but in situations, where data of an application specific format
25 needs to be displayed, without having to copy it, it is recommended
26 to implement an individual data access.
27
28 A subclass of QwtSeriesData<QPointF> must implement:
29
30 - size()\n
31 Should return number of data points.
32
33 - sample()\n
34 Should return values x and y values of the sample at specific position
35 as QPointF object.
36
37 - boundingRect()\n
38 Should return the bounding rectangle of the data series.
39 It is used for autoscaling and might help certain algorithms for displaying
40 the data. You can use qwtBoundingRect() for an implementation
41 but often it is possible to implement a more efficient algorithm
42 depending on the characteristics of the series.
43 The member d_boundingRect is intended for caching the calculated rectangle.
44
45*/
46template <typename T>
47class QwtSeriesData
48{
49public:
50 //! Constructor
51 QwtSeriesData();
52
53 //! Destructor
54 virtual ~QwtSeriesData();
55
56 //! \return Number of samples
57 virtual size_t size() const = 0;
58
59 /*!
60 Return a sample
61 \param i Index
62 \return Sample at position i
63 */
64 virtual T sample( size_t i ) const = 0;
65
66 /*!
67 Calculate the bounding rect of all samples
68
69 The bounding rect is necessary for autoscaling and can be used
70 for a couple of painting optimizations.
71
72 qwtBoundingRect(...) offers slow implementations iterating
73 over the samples. For large sets it is recommended to implement
74 something faster f.e. by caching the bounding rectangle.
75
76 \return Bounding rectangle
77 */
78 virtual QRectF boundingRect() const = 0;
79
80 /*!
81 Set a the "rect of interest"
82
83 QwtPlotSeriesItem defines the current area of the plot canvas
84 as "rectangle of interest" ( QwtPlotSeriesItem::updateScaleDiv() ).
85 It can be used to implement different levels of details.
86
87 The default implementation does nothing.
88
89 \param rect Rectangle of interest
90 */
91 virtual void setRectOfInterest( const QRectF &rect );
92
93protected:
94 //! Can be used to cache a calculated bounding rectangle
95 mutable QRectF d_boundingRect;
96
97private:
98 QwtSeriesData<T> &operator=( const QwtSeriesData<T> & );
99};
100
101template <typename T>
102QwtSeriesData<T>::QwtSeriesData():
103 d_boundingRect( 0.0, 0.0, -1.0, -1.0 )
104{
105}
106
107template <typename T>
108QwtSeriesData<T>::~QwtSeriesData()
109{
110}
111
112template <typename T>
113void QwtSeriesData<T>::setRectOfInterest( const QRectF & )
114{
115}
116
117/*!
118 \brief Template class for data, that is organized as QVector
119
120 QVector uses implicit data sharing and can be
121 passed around as argument efficiently.
122*/
123template <typename T>
124class QwtArraySeriesData: public QwtSeriesData<T>
125{
126public:
127 //! Constructor
128 QwtArraySeriesData();
129
130 /*!
131 Constructor
132 \param samples Array of samples
133 */
134 QwtArraySeriesData( const QVector<T> &samples );
135
136 /*!
137 Assign an array of samples
138 \param samples Array of samples
139 */
140 void setSamples( const QVector<T> &samples );
141
142 //! \return Array of samples
143 const QVector<T> samples() const;
144
145 //! \return Number of samples
146 virtual size_t size() const;
147
148 /*!
149 \return Sample at a specific position
150
151 \param index Index
152 \return Sample at position index
153 */
154 virtual T sample( size_t index ) const;
155
156protected:
157 //! Vector of samples
158 QVector<T> d_samples;
159};
160
161template <typename T>
162QwtArraySeriesData<T>::QwtArraySeriesData()
163{
164}
165
166template <typename T>
167QwtArraySeriesData<T>::QwtArraySeriesData( const QVector<T> &samples ):
168 d_samples( samples )
169{
170}
171
172template <typename T>
173void QwtArraySeriesData<T>::setSamples( const QVector<T> &samples )
174{
175 QwtSeriesData<T>::d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
176 d_samples = samples;
177}
178
179template <typename T>
180const QVector<T> QwtArraySeriesData<T>::samples() const
181{
182 return d_samples;
183}
184
185template <typename T>
186size_t QwtArraySeriesData<T>::size() const
187{
188 return d_samples.size();
189}
190
191template <typename T>
192T QwtArraySeriesData<T>::sample( size_t i ) const
193{
194 return d_samples[ static_cast<int>( i ) ];
195}
196
197//! Interface for iterating over an array of points
198class QWT_EXPORT QwtPointSeriesData: public QwtArraySeriesData<QPointF>
199{
200public:
201 QwtPointSeriesData(
202 const QVector<QPointF> & = QVector<QPointF>() );
203
204 virtual QRectF boundingRect() const;
205};
206
207//! Interface for iterating over an array of 3D points
208class QWT_EXPORT QwtPoint3DSeriesData: public QwtArraySeriesData<QwtPoint3D>
209{
210public:
211 QwtPoint3DSeriesData(
212 const QVector<QwtPoint3D> & = QVector<QwtPoint3D>() );
213 virtual QRectF boundingRect() const;
214};
215
216//! Interface for iterating over an array of intervals
217class QWT_EXPORT QwtIntervalSeriesData: public QwtArraySeriesData<QwtIntervalSample>
218{
219public:
220 QwtIntervalSeriesData(
221 const QVector<QwtIntervalSample> & = QVector<QwtIntervalSample>() );
222
223 virtual QRectF boundingRect() const;
224};
225
226//! Interface for iterating over an array of samples
227class QWT_EXPORT QwtSetSeriesData: public QwtArraySeriesData<QwtSetSample>
228{
229public:
230 QwtSetSeriesData(
231 const QVector<QwtSetSample> & = QVector<QwtSetSample>() );
232
233 virtual QRectF boundingRect() const;
234};
235
236/*!
237 Interface for iterating over an array of OHLC samples
238*/
239class QWT_EXPORT QwtTradingChartData: public QwtArraySeriesData<QwtOHLCSample>
240{
241public:
242 QwtTradingChartData(
243 const QVector<QwtOHLCSample> & = QVector<QwtOHLCSample>() );
244
245 virtual QRectF boundingRect() const;
246};
247
248QWT_EXPORT QRectF qwtBoundingRect(
249 const QwtSeriesData<QPointF> &, int from = 0, int to = -1 );
250
251QWT_EXPORT QRectF qwtBoundingRect(
252 const QwtSeriesData<QwtPoint3D> &, int from = 0, int to = -1 );
253
254QWT_EXPORT QRectF qwtBoundingRect(
255 const QwtSeriesData<QwtPointPolar> &, int from = 0, int to = -1 );
256
257QWT_EXPORT QRectF qwtBoundingRect(
258 const QwtSeriesData<QwtIntervalSample> &, int from = 0, int to = -1 );
259
260QWT_EXPORT QRectF qwtBoundingRect(
261 const QwtSeriesData<QwtSetSample> &, int from = 0, int to = -1 );
262
263QWT_EXPORT QRectF qwtBoundingRect(
264 const QwtSeriesData<QwtOHLCSample> &, int from = 0, int to = -1 );
265
266/*!
267 Binary search for a sorted series of samples
268
269 qwtUpperSampleIndex returns the index of sample that is the upper bound
270 of value. Is the the value smaller than the smallest value the return
271 value will be 0. Is the value greater or equal than the largest
272 value the return value will be -1.
273
274 \par Example
275 The following example shows finds a point of curve from an x
276 coordinate
277
278 \verbatim
279#include <qwt_series_data.h>
280#include <qwt_plot_curve.h>
281
282struct compareX
283{
284 inline bool operator()( const double x, const QPointF &pos ) const
285 {
286 return ( x < pos.x() );
287 }
288};
289
290QLineF curveLineAt( const QwtPlotCurve *curve, double x )
291{
292 int index = qwtUpperSampleIndex<QPointF>(
293 *curve->data(), x, compareX() );
294
295 if ( index == -1 &&
296 x == curve->sample( curve->dataSize() - 1 ).x() )
297 {
298 // the last sample is excluded from qwtUpperSampleIndex
299 index = curve->dataSize() - 1;
300 }
301
302 QLineF line; // invalid
303 if ( index > 0 )
304 {
305 line.setP1( curve->sample( index - 1 ) );
306 line.setP2( curve->sample( index ) );
307 }
308
309 return line;
310}
311
312\endverbatim
313
314
315 \param series Series of samples
316 \param value Value
317 \param lessThan Compare operation
318
319 \note The samples must be sorted according to the order specified
320 by the lessThan object
321
322of the range [begin, end) and returns the position of the one-past-the-last occurrence of value. If no such item is found, returns the position where the item should be inserted.
323 */
324template <typename T, typename LessThan>
325inline int qwtUpperSampleIndex( const QwtSeriesData<T> &series,
326 double value, LessThan lessThan )
327{
328 const int indexMax = series.size() - 1;
329
330 if ( indexMax < 0 || !lessThan( value, series.sample( indexMax ) ) )
331 return -1;
332
333 int indexMin = 0;
334 int n = indexMax;
335
336 while ( n > 0 )
337 {
338 const int half = n >> 1;
339 const int indexMid = indexMin + half;
340
341 if ( lessThan( value, series.sample( indexMid ) ) )
342 {
343 n = half;
344 }
345 else
346 {
347 indexMin = indexMid + 1;
348 n -= half + 1;
349 }
350 }
351
352 return indexMin;
353}
354
355#endif
Note: See TracBrowser for help on using the repository browser.