KD Chart 2  [rev.2.5]
KDChartPlotterDiagramCompressor.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2012 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #ifndef PLOTTERDIAGRAMCOMPRESSOR_H
00024 #define PLOTTERDIAGRAMCOMPRESSOR_H
00025 
00026 #include <QtCore/QObject>
00027 #include <QtCore/QAbstractItemModel>
00028 #include <QtCore/QWeakPointer>
00029 #include <QtCore/QVector>
00030 #include <QtCore/QDateTime>
00031 
00032 #include <cmath>
00033 #include <limits>
00034 
00035 namespace KDChart
00036 {
00037 
00038 
00039 class PlotterDiagramCompressor : public QObject
00040 {
00041     Q_OBJECT
00042     Q_ENUMS( CompressionMode )
00043 public:
00044 
00045     enum CompressionMode{ SLOPE = 0, DISTANCE, BOTH };
00046     class DataPoint {
00047     public:
00048         DataPoint()
00049             : key( std::numeric_limits< qreal >::quiet_NaN() ),
00050               value( std::numeric_limits< qreal >::quiet_NaN() ),
00051               hidden( false )
00052               {}
00053         inline qreal distance( const DataPoint &other )
00054         {
00055             const qreal dx = key - other.key;
00056             const qreal dy = value - other.value;
00057             return std::sqrt( dx * dx + dy * dy );
00058         }
00059 
00060         inline bool operator==( const DataPoint &other )
00061         {
00062             return key == other.key && value == other.value;
00063         }
00064 
00065         inline bool operator!=( const DataPoint &other )
00066         {
00067             return !( *this == other );
00068         }
00069 
00070         qreal key;
00071         qreal value;
00072         bool hidden;
00073         QModelIndex index;
00074     };
00075 
00076     class Iterator
00077     {
00078         friend class PlotterDiagramCompressor;
00079     public:
00080         Iterator( int dataSet, PlotterDiagramCompressor *parent );
00081         ~Iterator();
00082         bool isValid() const;
00083         Iterator& operator++();
00084         Iterator operator++( int );
00085         Iterator& operator += ( int value );
00086         Iterator& operator--();
00087         Iterator operator--( int );
00088         Iterator& operator-=( int value );
00089         DataPoint operator*();
00090         bool operator==( const Iterator &other ) const;
00091         bool operator!=( const Iterator &other ) const;
00092         void invalidate();
00093     protected:
00094         Iterator( int dataSet, PlotterDiagramCompressor *parent, QVector< DataPoint > buffer );
00095     private:
00096         void handleSlopeForward( const DataPoint &dp );
00097         QWeakPointer< PlotterDiagramCompressor > m_parent;
00098         QVector< DataPoint > m_buffer;
00099         int m_index;
00100         int m_dataset;
00101         int m_bufferIndex;
00102         int m_rebuffer;
00103         QDateTime m_timeOfCreation;
00104     };
00105 
00106     typedef QVector<DataPoint> DataPointVector;
00107     class CachePosition {
00108     public:
00109         CachePosition()
00110             : first( -1 ),
00111               second( -1 )
00112               {}
00113         CachePosition( int first, int second )
00114             : first( first ),
00115               second( second )
00116               {}
00117         int first;
00118         int second;
00119 
00120         bool operator==( const CachePosition& rhs ) const
00121         {
00122             return first == rhs.first &&
00123                    second == rhs.second;
00124         }
00125     };
00126     explicit PlotterDiagramCompressor(QObject *parent = 0);
00127     Iterator begin( int dataSet );
00128     Iterator end( int dataSet );    
00129     void setMergeRadius( qreal radius );
00130     void setMergeRadiusPercentage( qreal radius );
00131     void setModel( QAbstractItemModel *model );
00132     QAbstractItemModel* model() const;
00133     DataPoint data( const CachePosition& pos ) const;
00134     int rowCount() const;
00135     int datasetCount() const;
00136     void setCompressionModel( CompressionMode value );
00137     void setMaxSlopeChange( qreal value );
00138     qreal maxSlopeChange() const;
00139     void cleanCache();
00140     QPair< QPointF, QPointF > dataBoundaries() const;
00141     void setForcedDataBoundaries( const QPair< qreal, qreal > &bounds, Qt::Orientation direction );
00142 Q_SIGNALS:
00143     void boundariesChanged();
00144     void rowCountChanged();
00145 
00146 private:
00147     class Private;
00148     Private *d;
00149 };
00150 
00151 }
00152 
00153 #endif // PLOTTERDIAGRAMCOMPRESSOR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/