qwt_plot_seriesitem.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_PLOT_SERIES_ITEM_H
  10. #define QWT_PLOT_SERIES_ITEM_H
  11. #include "qwt_global.h"
  12. #include "qwt_plot_item.h"
  13. #include "qwt_scale_div.h"
  14. #include "qwt_series_data.h"
  15. /*!
  16. \brief Base class for plot items reprsenting a series of samples
  17. */
  18. class QWT_EXPORT QwtPlotAbstractSeriesItem: public QwtPlotItem
  19. {
  20. public:
  21. explicit QwtPlotAbstractSeriesItem( const QString &title = QString::null );
  22. explicit QwtPlotAbstractSeriesItem( const QwtText &title );
  23. virtual ~QwtPlotAbstractSeriesItem();
  24. void setOrientation( Qt::Orientation );
  25. Qt::Orientation orientation() const;
  26. virtual void draw( QPainter *p,
  27. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  28. const QRectF & ) const;
  29. /*!
  30. Draw a subset of the samples
  31. \param painter Painter
  32. \param xMap Maps x-values into pixel coordinates.
  33. \param yMap Maps y-values into pixel coordinates.
  34. \param canvasRect Contents rect of the canvas
  35. \param from Index of the first point to be painted
  36. \param to Index of the last point to be painted. If to < 0 the
  37. curve will be painted to its last point.
  38. */
  39. virtual void drawSeries( QPainter *painter,
  40. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  41. const QRectF &canvasRect, int from, int to ) const = 0;
  42. private:
  43. class PrivateData;
  44. PrivateData *d_data;
  45. };
  46. /*!
  47. \brief Class template for plot items reprsenting a series of samples
  48. */
  49. template <typename T>
  50. class QwtPlotSeriesItem: public QwtPlotAbstractSeriesItem
  51. {
  52. public:
  53. explicit QwtPlotSeriesItem<T>( const QString &title = QString::null );
  54. explicit QwtPlotSeriesItem<T>( const QwtText &title );
  55. virtual ~QwtPlotSeriesItem<T>();
  56. void setData( QwtSeriesData<T> * );
  57. QwtSeriesData<T> *data();
  58. const QwtSeriesData<T> *data() const;
  59. size_t dataSize() const;
  60. T sample( int i ) const;
  61. virtual QRectF boundingRect() const;
  62. virtual void updateScaleDiv( const QwtScaleDiv &,
  63. const QwtScaleDiv & );
  64. protected:
  65. //! Series
  66. QwtSeriesData<T> *d_series;
  67. };
  68. /*!
  69. Constructor
  70. \param title Title of the series item
  71. */
  72. template <typename T>
  73. QwtPlotSeriesItem<T>::QwtPlotSeriesItem( const QString &title ):
  74. QwtPlotAbstractSeriesItem( QwtText( title ) ),
  75. d_series( NULL )
  76. {
  77. }
  78. /*!
  79. Constructor
  80. \param title Title of the series item
  81. */
  82. template <typename T>
  83. QwtPlotSeriesItem<T>::QwtPlotSeriesItem( const QwtText &title ):
  84. QwtPlotAbstractSeriesItem( title ),
  85. d_series( NULL )
  86. {
  87. }
  88. //! Destructor
  89. template <typename T>
  90. QwtPlotSeriesItem<T>::~QwtPlotSeriesItem()
  91. {
  92. delete d_series;
  93. }
  94. //! \return the the curve data
  95. template <typename T>
  96. inline QwtSeriesData<T> *QwtPlotSeriesItem<T>::data()
  97. {
  98. return d_series;
  99. }
  100. //! \return the the curve data
  101. template <typename T>
  102. inline const QwtSeriesData<T> *QwtPlotSeriesItem<T>::data() const
  103. {
  104. return d_series;
  105. }
  106. /*!
  107. \param index Index
  108. \return Sample at position index
  109. */
  110. template <typename T>
  111. inline T QwtPlotSeriesItem<T>::sample( int index ) const
  112. {
  113. return d_series ? d_series->sample( index ) : T();
  114. }
  115. /*!
  116. Assign a series of samples
  117. \param data Data
  118. \warning The item takes ownership of the data object, deleting
  119. it when its not used anymore.
  120. */
  121. template <typename T>
  122. void QwtPlotSeriesItem<T>::setData( QwtSeriesData<T> *data )
  123. {
  124. if ( d_series != data )
  125. {
  126. delete d_series;
  127. d_series = data;
  128. itemChanged();
  129. }
  130. }
  131. /*!
  132. Return the size of the data arrays
  133. \sa setData()
  134. */
  135. template <typename T>
  136. size_t QwtPlotSeriesItem<T>::dataSize() const
  137. {
  138. if ( d_series == NULL )
  139. return 0;
  140. return d_series->size();
  141. }
  142. /*!
  143. \return Bounding rectangle of the data.
  144. If there is no bounding rect, like for empty data the rectangle is invalid.
  145. \sa QwtSeriesData<T>::boundingRect(), QRectF::isValid()
  146. */
  147. template <typename T>
  148. QRectF QwtPlotSeriesItem<T>::boundingRect() const
  149. {
  150. if ( d_series == NULL )
  151. return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
  152. return d_series->boundingRect();
  153. }
  154. /*!
  155. Update the rect of interest according to the current scale ranges
  156. \param xScaleDiv Scale division of the x-axis
  157. \param yScaleDiv Scale division of the y-axis
  158. \sa QwtSeriesData<T>::setRectOfInterest()
  159. */
  160. template <typename T>
  161. void QwtPlotSeriesItem<T>::updateScaleDiv(
  162. const QwtScaleDiv &xScaleDiv, const QwtScaleDiv &yScaleDiv )
  163. {
  164. const QRectF rect = QRectF(
  165. xScaleDiv.lowerBound(), yScaleDiv.lowerBound(),
  166. xScaleDiv.range(), yScaleDiv.range() );
  167. d_series->setRectOfInterest( rect );
  168. }
  169. #endif