qwt_series_store.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifndef QWT_SERIES_STORE_H
  10. #define QWT_SERIES_STORE_H
  11. #include "qwt_global.h"
  12. #include "qwt_series_data.h"
  13. /*!
  14. \brief Bridge between QwtSeriesStore and QwtPlotSeriesItem
  15. QwtAbstractSeriesStore is an abstract interface only
  16. to make it possible to isolate the template based methods ( QwtSeriesStore )
  17. from the regular methods ( QwtPlotSeriesItem ) to make it possible
  18. to derive from QwtPlotSeriesItem without any hassle with templates.
  19. */
  20. class QwtAbstractSeriesStore
  21. {
  22. protected:
  23. //! Destructor
  24. virtual ~QwtAbstractSeriesStore() {}
  25. #ifndef QWT_PYTHON_WRAPPER
  26. //! dataChanged() indicates, that the series has been changed.
  27. virtual void dataChanged() = 0;
  28. /*!
  29. Set a the "rectangle of interest" for the stored series
  30. \sa QwtSeriesData<T>::setRectOfInterest()
  31. */
  32. virtual void setRectOfInterest( const QRectF & ) = 0;
  33. //! \return Bounding rectangle of the stored series
  34. virtual QRectF dataRect() const = 0;
  35. //! \return Number of samples
  36. virtual size_t dataSize() const = 0;
  37. #else
  38. // Needed for generating the python bindings, but not for using them !
  39. virtual void dataChanged() {}
  40. virtual void setRectOfInterest( const QRectF & ) {}
  41. virtual QRectF dataRect() const { return QRectF( 0.0, 0.0, -1.0, -1.0 ); }
  42. virtual size_t dataSize() const { return 0; }
  43. #endif
  44. };
  45. /*!
  46. \brief Class storing a QwtSeriesData object
  47. QwtSeriesStore and QwtPlotSeriesItem are intended as base classes for all
  48. plot items iterating over a series of samples. Both classes share
  49. a virtual base class ( QwtAbstractSeriesStore ) to bridge between them.
  50. QwtSeriesStore offers the template based part for the plot item API, so
  51. that QwtPlotSeriesItem can be derived without any hassle with templates.
  52. */
  53. template <typename T>
  54. class QwtSeriesStore: public virtual QwtAbstractSeriesStore
  55. {
  56. public:
  57. /*!
  58. \brief Constructor
  59. The store contains no series
  60. */
  61. explicit QwtSeriesStore<T>();
  62. //! Destructor
  63. ~QwtSeriesStore<T>();
  64. /*!
  65. Assign a series of samples
  66. \param series Data
  67. \warning The item takes ownership of the data object, deleting
  68. it when its not used anymore.
  69. */
  70. void setData( QwtSeriesData<T> *series );
  71. //! \return the the series data
  72. QwtSeriesData<T> *data();
  73. //! \return the the series data
  74. const QwtSeriesData<T> *data() const;
  75. /*!
  76. \param index Index
  77. \return Sample at position index
  78. */
  79. T sample( int index ) const;
  80. /*!
  81. \return Number of samples of the series
  82. \sa setData(), QwtSeriesData<T>::size()
  83. */
  84. virtual size_t dataSize() const;
  85. /*!
  86. \return Bounding rectangle of the series
  87. or an invalid rectangle, when no series is stored
  88. \sa QwtSeriesData<T>::boundingRect()
  89. */
  90. virtual QRectF dataRect() const;
  91. /*!
  92. Set a the "rect of interest" for the series
  93. \param rect Rectangle of interest
  94. \sa QwtSeriesData<T>::setRectOfInterest()
  95. */
  96. virtual void setRectOfInterest( const QRectF &rect );
  97. /*!
  98. Replace a series without deleting the previous one
  99. \param series New series
  100. \return Previously assigned series
  101. */
  102. QwtSeriesData<T> *swapData( QwtSeriesData<T> *series );
  103. private:
  104. QwtSeriesData<T> *d_series;
  105. };
  106. template <typename T>
  107. QwtSeriesStore<T>::QwtSeriesStore():
  108. d_series( NULL )
  109. {
  110. }
  111. template <typename T>
  112. QwtSeriesStore<T>::~QwtSeriesStore()
  113. {
  114. delete d_series;
  115. }
  116. template <typename T>
  117. inline QwtSeriesData<T> *QwtSeriesStore<T>::data()
  118. {
  119. return d_series;
  120. }
  121. template <typename T>
  122. inline const QwtSeriesData<T> *QwtSeriesStore<T>::data() const
  123. {
  124. return d_series;
  125. }
  126. template <typename T>
  127. inline T QwtSeriesStore<T>::sample( int index ) const
  128. {
  129. return d_series ? d_series->sample( index ) : T();
  130. }
  131. template <typename T>
  132. void QwtSeriesStore<T>::setData( QwtSeriesData<T> *series )
  133. {
  134. if ( d_series != series )
  135. {
  136. delete d_series;
  137. d_series = series;
  138. dataChanged();
  139. }
  140. }
  141. template <typename T>
  142. size_t QwtSeriesStore<T>::dataSize() const
  143. {
  144. if ( d_series == NULL )
  145. return 0;
  146. return d_series->size();
  147. }
  148. template <typename T>
  149. QRectF QwtSeriesStore<T>::dataRect() const
  150. {
  151. if ( d_series == NULL )
  152. return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
  153. return d_series->boundingRect();
  154. }
  155. template <typename T>
  156. void QwtSeriesStore<T>::setRectOfInterest( const QRectF &rect )
  157. {
  158. if ( d_series )
  159. d_series->setRectOfInterest( rect );
  160. }
  161. template <typename T>
  162. QwtSeriesData<T>* QwtSeriesStore<T>::swapData( QwtSeriesData<T> *series )
  163. {
  164. QwtSeriesData<T> * swappedSeries = d_series;
  165. d_series = series;
  166. return swappedSeries;
  167. }
  168. #endif