qwt_series_store.h 4.7 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_SERIES_STORE_H
#define QWT_SERIES_STORE_H

#include "qwt_global.h"
#include "qwt_series_data.h"

/*!
  \brief Bridge between QwtSeriesStore and QwtPlotSeriesItem

  QwtAbstractSeriesStore is an abstract interface only
  to make it possible to isolate the template based methods ( QwtSeriesStore )
  from the regular methods ( QwtPlotSeriesItem ) to make it possible
  to derive from QwtPlotSeriesItem without any hassle with templates.
*/
class QwtAbstractSeriesStore
{
protected:
    //! Destructor
    virtual ~QwtAbstractSeriesStore() {}

    //! dataChanged() indicates, that the series has been changed.
    virtual void dataChanged() = 0;

    /*!
      Set a the "rectangle of interest" for the stored series
      \sa QwtSeriesData<T>::setRectOfInterest()
     */
    virtual void setRectOfInterest( const QRectF & ) = 0;

    //! \return Bounding rectangle of the stored series
    virtual QRectF dataRect() const = 0;

    //! \return Number of samples
    virtual size_t dataSize() const = 0;
};

/*!
  \brief Class storing a QwtSeriesData object

  QwtSeriesStore and QwtPlotSeriesItem are intended as base classes for all
  plot items iterating over a series of samples. Both classes share
  a virtual base class ( QwtAbstractSeriesStore ) to bridge between them.

  QwtSeriesStore offers the template based part for the plot item API, so
  that QwtPlotSeriesItem can be derived without any hassle with templates.
 */
template <typename T>
class QwtSeriesStore: public virtual QwtAbstractSeriesStore
{
public:
    /*!
      \brief Constructor
      The store contains no series
    */
    explicit QwtSeriesStore<T>();

    //! Destructor
    ~QwtSeriesStore<T>();

    /*!
      Assign a series of samples

      \param series Data
      \warning The item takes ownership of the data object, deleting
               it when its not used anymore.
    */
    void setData( QwtSeriesData<T> *series );

    //! \return the the series data
    QwtSeriesData<T> *data();

    //! \return the the series data
    const QwtSeriesData<T> *data() const;

    /*!
        \param index Index
        \return Sample at position index
    */
    T sample( int index ) const;

    /*!
      \return Number of samples of the series
      \sa setData(), QwtSeriesData<T>::size()
    */
    virtual size_t dataSize() const;

    /*!
      \return Bounding rectangle of the series
              or an invalid rectangle, when no series is stored

      \sa QwtSeriesData<T>::boundingRect()
    */
    virtual QRectF dataRect() const;

    /*!
      Set a the "rect of interest" for the series

      \param rect Rectangle of interest
      \sa QwtSeriesData<T>::setRectOfInterest()
    */
    virtual void setRectOfInterest( const QRectF &rect );

    /*!
      Replace a series without deleting the previous one

      \param series New series
      \return Previously assigned series
     */
    QwtSeriesData<T> *swapData( QwtSeriesData<T> *series );

private:
    QwtSeriesData<T> *d_series;
};

template <typename T>
QwtSeriesStore<T>::QwtSeriesStore():
    d_series( NULL )
{
}

template <typename T>
QwtSeriesStore<T>::~QwtSeriesStore()
{
    delete d_series;
}

template <typename T>
inline QwtSeriesData<T> *QwtSeriesStore<T>::data()
{
    return d_series;
}

template <typename T>
inline const QwtSeriesData<T> *QwtSeriesStore<T>::data() const
{
    return d_series;
}

template <typename T>
inline T QwtSeriesStore<T>::sample( int index ) const
{
    return d_series ? d_series->sample( index ) : T();
}

template <typename T>
void QwtSeriesStore<T>::setData( QwtSeriesData<T> *series )
{
    if ( d_series != series )
    {
        delete d_series;
        d_series = series;
        dataChanged();
    }
}

template <typename T>
size_t QwtSeriesStore<T>::dataSize() const
{
    if ( d_series == NULL )
        return 0;

    return d_series->size();
}

template <typename T>
QRectF QwtSeriesStore<T>::dataRect() const
{
    if ( d_series == NULL )
        return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid

    return d_series->boundingRect();
}

template <typename T>
void QwtSeriesStore<T>::setRectOfInterest( const QRectF &rect )
{
    if ( d_series )
        d_series->setRectOfInterest( rect );
}

template <typename T>
QwtSeriesData<T>* QwtSeriesStore<T>::swapData( QwtSeriesData<T> *series )
{
    QwtSeriesData<T> * swappedSeries = d_series;
    d_series = series;

    return swappedSeries;
}

#endif