| 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_interval.h" | 
|---|
| 15 | #include "qwt_point_3d.h" | 
|---|
| 16 | #include "qwt_point_polar.h" | 
|---|
| 17 | #include <qvector.h> | 
|---|
| 18 | #include <qrect.h> | 
|---|
| 19 |  | 
|---|
| 20 | //! \brief A sample of the types (x1-x2, y) or (x, y1-y2) | 
|---|
| 21 | class QWT_EXPORT QwtIntervalSample | 
|---|
| 22 | { | 
|---|
| 23 | public: | 
|---|
| 24 | QwtIntervalSample(); | 
|---|
| 25 | QwtIntervalSample( double, const QwtInterval & ); | 
|---|
| 26 | QwtIntervalSample( double value, double min, double max ); | 
|---|
| 27 |  | 
|---|
| 28 | bool operator==( const QwtIntervalSample & ) const; | 
|---|
| 29 | bool operator!=( const QwtIntervalSample & ) const; | 
|---|
| 30 |  | 
|---|
| 31 | //! Value | 
|---|
| 32 | double value; | 
|---|
| 33 |  | 
|---|
| 34 | //! Interval | 
|---|
| 35 | QwtInterval interval; | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | /*! | 
|---|
| 39 | Constructor | 
|---|
| 40 | The value is set to 0.0, the interval is invalid | 
|---|
| 41 | */ | 
|---|
| 42 | inline QwtIntervalSample::QwtIntervalSample(): | 
|---|
| 43 | value( 0.0 ) | 
|---|
| 44 | { | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | //! Constructor | 
|---|
| 48 | inline QwtIntervalSample::QwtIntervalSample( | 
|---|
| 49 | double v, const QwtInterval &intv ): | 
|---|
| 50 | value( v ), | 
|---|
| 51 | interval( intv ) | 
|---|
| 52 | { | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | //! Constructor | 
|---|
| 56 | inline QwtIntervalSample::QwtIntervalSample( | 
|---|
| 57 | double v, double min, double max ): | 
|---|
| 58 | value( v ), | 
|---|
| 59 | interval( min, max ) | 
|---|
| 60 | { | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | //! Compare operator | 
|---|
| 64 | inline bool QwtIntervalSample::operator==( | 
|---|
| 65 | const QwtIntervalSample &other ) const | 
|---|
| 66 | { | 
|---|
| 67 | return value == other.value && interval == other.interval; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | //! Compare operator | 
|---|
| 71 | inline bool QwtIntervalSample::operator!=( | 
|---|
| 72 | const QwtIntervalSample &other ) const | 
|---|
| 73 | { | 
|---|
| 74 | return !( *this == other ); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | //! \brief A sample of the types (x1...xn, y) or (x, y1..yn) | 
|---|
| 78 | class QWT_EXPORT QwtSetSample | 
|---|
| 79 | { | 
|---|
| 80 | public: | 
|---|
| 81 | QwtSetSample(); | 
|---|
| 82 | bool operator==( const QwtSetSample &other ) const; | 
|---|
| 83 | bool operator!=( const QwtSetSample &other ) const; | 
|---|
| 84 |  | 
|---|
| 85 | //! value | 
|---|
| 86 | double value; | 
|---|
| 87 |  | 
|---|
| 88 | //! Vector of values associated to value | 
|---|
| 89 | QVector<double> set; | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | /*! | 
|---|
| 93 | Constructor | 
|---|
| 94 | The value is set to 0.0 | 
|---|
| 95 | */ | 
|---|
| 96 | inline QwtSetSample::QwtSetSample(): | 
|---|
| 97 | value( 0.0 ) | 
|---|
| 98 | { | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | //! Compare operator | 
|---|
| 102 | inline bool QwtSetSample::operator==( const QwtSetSample &other ) const | 
|---|
| 103 | { | 
|---|
| 104 | return value == other.value && set == other.set; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | //! Compare operator | 
|---|
| 108 | inline bool QwtSetSample::operator!=( const QwtSetSample &other ) const | 
|---|
| 109 | { | 
|---|
| 110 | return !( *this == other ); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | /*! | 
|---|
| 114 | \brief Abstract interface for iterating over samples | 
|---|
| 115 |  | 
|---|
| 116 | Qwt offers several implementations of the QwtSeriesData API, | 
|---|
| 117 | but in situations, where data of an application specific format | 
|---|
| 118 | needs to be displayed, without having to copy it, it is recommended | 
|---|
| 119 | to implement an individual data access. | 
|---|
| 120 | */ | 
|---|
| 121 | template <typename T> | 
|---|
| 122 | class QwtSeriesData | 
|---|
| 123 | { | 
|---|
| 124 | public: | 
|---|
| 125 | QwtSeriesData(); | 
|---|
| 126 | virtual ~QwtSeriesData(); | 
|---|
| 127 |  | 
|---|
| 128 | //! \return Number of samples | 
|---|
| 129 | virtual size_t size() const = 0; | 
|---|
| 130 |  | 
|---|
| 131 | /*! | 
|---|
| 132 | Return a sample | 
|---|
| 133 | \param i Index | 
|---|
| 134 | \return Sample at position i | 
|---|
| 135 | */ | 
|---|
| 136 | virtual T sample( size_t i ) const = 0; | 
|---|
| 137 |  | 
|---|
| 138 | /*! | 
|---|
| 139 | Calculate the bounding rect of all samples | 
|---|
| 140 |  | 
|---|
| 141 | The bounding rect is necessary for autoscaling and can be used | 
|---|
| 142 | for a couple of painting optimizations. | 
|---|
| 143 |  | 
|---|
| 144 | qwtBoundingRect(...) offers slow implementations iterating | 
|---|
| 145 | over the samples. For large sets it is recommended to implement | 
|---|
| 146 | something faster f.e. by caching the bounding rect. | 
|---|
| 147 | */ | 
|---|
| 148 | virtual QRectF boundingRect() const = 0; | 
|---|
| 149 |  | 
|---|
| 150 | virtual void setRectOfInterest( const QRectF & ); | 
|---|
| 151 |  | 
|---|
| 152 | protected: | 
|---|
| 153 | //! Can be used to cache a calculated bounding rectangle | 
|---|
| 154 | mutable QRectF d_boundingRect; | 
|---|
| 155 |  | 
|---|
| 156 | private: | 
|---|
| 157 | QwtSeriesData<T> &operator=( const QwtSeriesData<T> & ); | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 | //! Constructor | 
|---|
| 161 | template <typename T> | 
|---|
| 162 | QwtSeriesData<T>::QwtSeriesData(): | 
|---|
| 163 | d_boundingRect( 0.0, 0.0, -1.0, -1.0 ) | 
|---|
| 164 | { | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | //! Destructor | 
|---|
| 168 | template <typename T> | 
|---|
| 169 | QwtSeriesData<T>::~QwtSeriesData() | 
|---|
| 170 | { | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | /*! | 
|---|
| 174 | Set a the "rect of interest" | 
|---|
| 175 |  | 
|---|
| 176 | QwtPlotSeriesItem defines the current area of the plot canvas | 
|---|
| 177 | as "rect of interest" ( QwtPlotSeriesItem::updateScaleDiv() ). | 
|---|
| 178 | It can be used to implement different levels of details. | 
|---|
| 179 |  | 
|---|
| 180 | The default implementation does nothing. | 
|---|
| 181 | */ | 
|---|
| 182 | template <typename T> | 
|---|
| 183 | void QwtSeriesData<T>::setRectOfInterest( const QRectF & ) | 
|---|
| 184 | { | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /*! | 
|---|
| 188 | \brief Template class for data, that is organized as QVector | 
|---|
| 189 |  | 
|---|
| 190 | QVector uses implicit data sharing and can be | 
|---|
| 191 | passed around as argument efficiently. | 
|---|
| 192 | */ | 
|---|
| 193 | template <typename T> | 
|---|
| 194 | class QwtArraySeriesData: public QwtSeriesData<T> | 
|---|
| 195 | { | 
|---|
| 196 | public: | 
|---|
| 197 | QwtArraySeriesData(); | 
|---|
| 198 | QwtArraySeriesData( const QVector<T> & ); | 
|---|
| 199 |  | 
|---|
| 200 | void setSamples( const QVector<T> & ); | 
|---|
| 201 | const QVector<T> samples() const; | 
|---|
| 202 |  | 
|---|
| 203 | virtual size_t size() const; | 
|---|
| 204 | virtual T sample( size_t ) const; | 
|---|
| 205 |  | 
|---|
| 206 | protected: | 
|---|
| 207 | //! Vector of samples | 
|---|
| 208 | QVector<T> d_samples; | 
|---|
| 209 | }; | 
|---|
| 210 |  | 
|---|
| 211 | //! Constructor | 
|---|
| 212 | template <typename T> | 
|---|
| 213 | QwtArraySeriesData<T>::QwtArraySeriesData() | 
|---|
| 214 | { | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | /*! | 
|---|
| 218 | Constructor | 
|---|
| 219 | \param samples Array of samples | 
|---|
| 220 | */ | 
|---|
| 221 | template <typename T> | 
|---|
| 222 | QwtArraySeriesData<T>::QwtArraySeriesData( const QVector<T> &samples ): | 
|---|
| 223 | d_samples( samples ) | 
|---|
| 224 | { | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | /*! | 
|---|
| 228 | Assign an array of samples | 
|---|
| 229 | \param samples Array of samples | 
|---|
| 230 | */ | 
|---|
| 231 | template <typename T> | 
|---|
| 232 | void QwtArraySeriesData<T>::setSamples( const QVector<T> &samples ) | 
|---|
| 233 | { | 
|---|
| 234 | QwtSeriesData<T>::d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 ); | 
|---|
| 235 | d_samples = samples; | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | //! \return Array of samples | 
|---|
| 239 | template <typename T> | 
|---|
| 240 | const QVector<T> QwtArraySeriesData<T>::samples() const | 
|---|
| 241 | { | 
|---|
| 242 | return d_samples; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | //! \return Number of samples | 
|---|
| 246 | template <typename T> | 
|---|
| 247 | size_t QwtArraySeriesData<T>::size() const | 
|---|
| 248 | { | 
|---|
| 249 | return d_samples.size(); | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | /*! | 
|---|
| 253 | Return a sample | 
|---|
| 254 | \param i Index | 
|---|
| 255 | \return Sample at position i | 
|---|
| 256 | */ | 
|---|
| 257 | template <typename T> | 
|---|
| 258 | T QwtArraySeriesData<T>::sample( size_t i ) const | 
|---|
| 259 | { | 
|---|
| 260 | return d_samples[i]; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | //! Interface for iterating over an array of points | 
|---|
| 264 | class QWT_EXPORT QwtPointSeriesData: public QwtArraySeriesData<QPointF> | 
|---|
| 265 | { | 
|---|
| 266 | public: | 
|---|
| 267 | QwtPointSeriesData( | 
|---|
| 268 | const QVector<QPointF> & = QVector<QPointF>() ); | 
|---|
| 269 |  | 
|---|
| 270 | virtual QRectF boundingRect() const; | 
|---|
| 271 | }; | 
|---|
| 272 |  | 
|---|
| 273 | //! Interface for iterating over an array of 3D points | 
|---|
| 274 | class QWT_EXPORT QwtPoint3DSeriesData: public QwtArraySeriesData<QwtPoint3D> | 
|---|
| 275 | { | 
|---|
| 276 | public: | 
|---|
| 277 | QwtPoint3DSeriesData( | 
|---|
| 278 | const QVector<QwtPoint3D> & = QVector<QwtPoint3D>() ); | 
|---|
| 279 | virtual QRectF boundingRect() const; | 
|---|
| 280 | }; | 
|---|
| 281 |  | 
|---|
| 282 | //! Interface for iterating over an array of intervals | 
|---|
| 283 | class QWT_EXPORT QwtIntervalSeriesData: public QwtArraySeriesData<QwtIntervalSample> | 
|---|
| 284 | { | 
|---|
| 285 | public: | 
|---|
| 286 | QwtIntervalSeriesData( | 
|---|
| 287 | const QVector<QwtIntervalSample> & = QVector<QwtIntervalSample>() ); | 
|---|
| 288 |  | 
|---|
| 289 | virtual QRectF boundingRect() const; | 
|---|
| 290 | }; | 
|---|
| 291 |  | 
|---|
| 292 | //! Interface for iterating over an array of samples | 
|---|
| 293 | class QWT_EXPORT QwtSetSeriesData: public QwtArraySeriesData<QwtSetSample> | 
|---|
| 294 | { | 
|---|
| 295 | public: | 
|---|
| 296 | QwtSetSeriesData( | 
|---|
| 297 | const QVector<QwtSetSample> & = QVector<QwtSetSample>() ); | 
|---|
| 298 |  | 
|---|
| 299 | virtual QRectF boundingRect() const; | 
|---|
| 300 | }; | 
|---|
| 301 |  | 
|---|
| 302 | /*! | 
|---|
| 303 | \brief Interface for iterating over two QVector<double> objects. | 
|---|
| 304 | */ | 
|---|
| 305 | class QWT_EXPORT QwtPointArrayData: public QwtSeriesData<QPointF> | 
|---|
| 306 | { | 
|---|
| 307 | public: | 
|---|
| 308 | QwtPointArrayData( const QVector<double> &x, const QVector<double> &y ); | 
|---|
| 309 | QwtPointArrayData( const double *x, const double *y, size_t size ); | 
|---|
| 310 |  | 
|---|
| 311 | virtual QRectF boundingRect() const; | 
|---|
| 312 |  | 
|---|
| 313 | virtual size_t size() const; | 
|---|
| 314 | virtual QPointF sample( size_t i ) const; | 
|---|
| 315 |  | 
|---|
| 316 | const QVector<double> &xData() const; | 
|---|
| 317 | const QVector<double> &yData() const; | 
|---|
| 318 |  | 
|---|
| 319 | private: | 
|---|
| 320 | QVector<double> d_x; | 
|---|
| 321 | QVector<double> d_y; | 
|---|
| 322 | }; | 
|---|
| 323 |  | 
|---|
| 324 | /*! | 
|---|
| 325 | \brief Data class containing two pointers to memory blocks of doubles. | 
|---|
| 326 | */ | 
|---|
| 327 | class QWT_EXPORT QwtCPointerData: public QwtSeriesData<QPointF> | 
|---|
| 328 | { | 
|---|
| 329 | public: | 
|---|
| 330 | QwtCPointerData( const double *x, const double *y, size_t size ); | 
|---|
| 331 |  | 
|---|
| 332 | virtual QRectF boundingRect() const; | 
|---|
| 333 | virtual size_t size() const; | 
|---|
| 334 | virtual QPointF sample( size_t i ) const; | 
|---|
| 335 |  | 
|---|
| 336 | const double *xData() const; | 
|---|
| 337 | const double *yData() const; | 
|---|
| 338 |  | 
|---|
| 339 | private: | 
|---|
| 340 | const double *d_x; | 
|---|
| 341 | const double *d_y; | 
|---|
| 342 | size_t d_size; | 
|---|
| 343 | }; | 
|---|
| 344 |  | 
|---|
| 345 | /*! | 
|---|
| 346 | \brief Synthetic point data | 
|---|
| 347 |  | 
|---|
| 348 | QwtSyntheticPointData provides a fixed number of points for an interval. | 
|---|
| 349 | The points are calculated in equidistant steps in x-direction. | 
|---|
| 350 |  | 
|---|
| 351 | If the interval is invalid, the points are calculated for | 
|---|
| 352 | the "rect of interest", what normally is the displayed area on the | 
|---|
| 353 | plot canvas. In this mode you get different levels of detail, when | 
|---|
| 354 | zooming in/out. | 
|---|
| 355 |  | 
|---|
| 356 | \par Example | 
|---|
| 357 |  | 
|---|
| 358 | The following example shows how to implement a sinus curve. | 
|---|
| 359 |  | 
|---|
| 360 | \verbatim | 
|---|
| 361 | #include <cmath> | 
|---|
| 362 | #include <qwt_series_data.h> | 
|---|
| 363 | #include <qwt_plot_curve.h> | 
|---|
| 364 | #include <qwt_plot.h> | 
|---|
| 365 | #include <qapplication.h> | 
|---|
| 366 |  | 
|---|
| 367 | class SinusData: public QwtSyntheticPointData | 
|---|
| 368 | { | 
|---|
| 369 | public: | 
|---|
| 370 | SinusData(): | 
|---|
| 371 | QwtSyntheticPointData(100) | 
|---|
| 372 | { | 
|---|
| 373 | } | 
|---|
| 374 | virtual double y(double x) const | 
|---|
| 375 | { | 
|---|
| 376 | return qSin(x); | 
|---|
| 377 | } | 
|---|
| 378 | }; | 
|---|
| 379 |  | 
|---|
| 380 | int main(int argc, char **argv) | 
|---|
| 381 | { | 
|---|
| 382 | QApplication a(argc, argv); | 
|---|
| 383 |  | 
|---|
| 384 | QwtPlot plot; | 
|---|
| 385 | plot.setAxisScale(QwtPlot::xBottom, 0.0, 10.0); | 
|---|
| 386 | plot.setAxisScale(QwtPlot::yLeft, -1.0, 1.0); | 
|---|
| 387 |  | 
|---|
| 388 | QwtPlotCurve *curve = new QwtPlotCurve("y = sin(x)"); | 
|---|
| 389 | curve->setData(SinusData()); | 
|---|
| 390 | curve->attach(&plot); | 
|---|
| 391 |  | 
|---|
| 392 | plot.show(); | 
|---|
| 393 | return a.exec(); | 
|---|
| 394 | } | 
|---|
| 395 | \endverbatim | 
|---|
| 396 | */ | 
|---|
| 397 | class QWT_EXPORT QwtSyntheticPointData: public QwtSeriesData<QPointF> | 
|---|
| 398 | { | 
|---|
| 399 | public: | 
|---|
| 400 | QwtSyntheticPointData( size_t size, | 
|---|
| 401 | const QwtInterval & = QwtInterval() ); | 
|---|
| 402 |  | 
|---|
| 403 | void setSize( size_t size ); | 
|---|
| 404 | size_t size() const; | 
|---|
| 405 |  | 
|---|
| 406 | void setInterval( const QwtInterval& ); | 
|---|
| 407 | QwtInterval interval() const; | 
|---|
| 408 |  | 
|---|
| 409 | virtual QRectF boundingRect() const; | 
|---|
| 410 | virtual QPointF sample( size_t i ) const; | 
|---|
| 411 |  | 
|---|
| 412 | /*! | 
|---|
| 413 | Calculate a y value for a x value | 
|---|
| 414 |  | 
|---|
| 415 | \param x x value | 
|---|
| 416 | \return Corresponding y value | 
|---|
| 417 | */ | 
|---|
| 418 | virtual double y( double x ) const = 0; | 
|---|
| 419 | virtual double x( uint index ) const; | 
|---|
| 420 |  | 
|---|
| 421 | virtual void setRectOfInterest( const QRectF & ); | 
|---|
| 422 | QRectF rectOfInterest() const; | 
|---|
| 423 |  | 
|---|
| 424 | private: | 
|---|
| 425 | size_t d_size; | 
|---|
| 426 | QwtInterval d_interval; | 
|---|
| 427 | QRectF d_rectOfInterest; | 
|---|
| 428 | QwtInterval d_intervalOfInterest; | 
|---|
| 429 | }; | 
|---|
| 430 |  | 
|---|
| 431 | QWT_EXPORT QRectF qwtBoundingRect( | 
|---|
| 432 | const QwtSeriesData<QPointF> &, int from = 0, int to = -1 ); | 
|---|
| 433 | QWT_EXPORT QRectF qwtBoundingRect( | 
|---|
| 434 | const QwtSeriesData<QwtPoint3D> &, int from = 0, int to = -1 ); | 
|---|
| 435 | QWT_EXPORT QRectF qwtBoundingRect( | 
|---|
| 436 | const QwtSeriesData<QwtPointPolar> &, int from = 0, int to = -1 ); | 
|---|
| 437 | QWT_EXPORT QRectF qwtBoundingRect( | 
|---|
| 438 | const QwtSeriesData<QwtIntervalSample> &, int from = 0, int to = -1 ); | 
|---|
| 439 | QWT_EXPORT QRectF qwtBoundingRect( | 
|---|
| 440 | const QwtSeriesData<QwtSetSample> &, int from = 0, int to = -1 ); | 
|---|
| 441 |  | 
|---|
| 442 | #endif | 
|---|