1 /* 2 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 3 * 4 * This file is part of the KD Chart library. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef PLOTTERDIAGRAMCOMPRESSOR_H 21 #define PLOTTERDIAGRAMCOMPRESSOR_H 22 23 #include <QObject> 24 #include <QAbstractItemModel> 25 #include <QPointer> 26 #include <QVector> 27 #include <QDateTime> 28 29 #include <cmath> 30 #include <limits> 31 32 namespace KChart 33 { 34 35 36 class PlotterDiagramCompressor : public QObject 37 { 38 Q_OBJECT 39 public: 40 41 enum CompressionMode{ SLOPE = 0, DISTANCE, BOTH }; Q_ENUM(CompressionMode)42 Q_ENUM( CompressionMode ) 43 44 class DataPoint { 45 public: 46 DataPoint() 47 : key( std::numeric_limits< qreal >::quiet_NaN() ), 48 value( std::numeric_limits< qreal >::quiet_NaN() ), 49 hidden( false ) 50 {} 51 inline qreal distance( const DataPoint &other ) 52 { 53 const qreal dx = key - other.key; 54 const qreal dy = value - other.value; 55 return std::sqrt( dx * dx + dy * dy ); 56 } 57 58 inline bool operator==( const DataPoint &other ) 59 { 60 return key == other.key && value == other.value; 61 } 62 63 inline bool operator!=( const DataPoint &other ) 64 { 65 return !( *this == other ); 66 } 67 68 qreal key; 69 qreal value; 70 bool hidden; 71 QModelIndex index; 72 }; 73 74 class Iterator 75 { 76 friend class PlotterDiagramCompressor; 77 public: 78 Iterator( int dataSet, PlotterDiagramCompressor *parent ); 79 ~Iterator(); 80 bool isValid() const; 81 Iterator& operator++(); 82 Iterator operator++( int ); 83 Iterator& operator += ( int value ); 84 Iterator& operator--(); 85 Iterator operator--( int ); 86 Iterator& operator-=( int value ); 87 DataPoint operator*(); 88 bool operator==( const Iterator &other ) const; 89 bool operator!=( const Iterator &other ) const; 90 void invalidate(); 91 protected: 92 Iterator( int dataSet, PlotterDiagramCompressor *parent, QVector< DataPoint > buffer ); 93 private: 94 void handleSlopeForward( const DataPoint &dp ); 95 QPointer< PlotterDiagramCompressor > m_parent; 96 QVector< DataPoint > m_buffer; 97 int m_index; 98 int m_dataset; 99 int m_bufferIndex; 100 int m_rebuffer; 101 QDateTime m_timeOfCreation; 102 }; 103 104 typedef QVector<DataPoint> DataPointVector; 105 class CachePosition { 106 public: CachePosition()107 CachePosition() 108 : first( -1 ), 109 second( -1 ) 110 {} CachePosition(int first,int second)111 CachePosition( int first, int second ) 112 : first( first ), 113 second( second ) 114 {} 115 int first; 116 int second; 117 118 bool operator==( const CachePosition& rhs ) const 119 { 120 return first == rhs.first && 121 second == rhs.second; 122 } 123 }; 124 explicit PlotterDiagramCompressor(QObject *parent = nullptr); 125 ~PlotterDiagramCompressor(); 126 Iterator begin( int dataSet ); 127 Iterator end( int dataSet ); 128 void setMergeRadius( qreal radius ); 129 void setMergeRadiusPercentage( qreal radius ); 130 void setModel( QAbstractItemModel *model ); 131 QAbstractItemModel* model() const; 132 DataPoint data( const CachePosition& pos ) const; 133 int rowCount() const; 134 int datasetCount() const; 135 void setCompressionModel( CompressionMode value ); 136 void setMaxSlopeChange( qreal value ); 137 qreal maxSlopeChange() const; 138 void cleanCache(); 139 QPair< QPointF, QPointF > dataBoundaries() const; 140 void setForcedDataBoundaries( const QPair< qreal, qreal > &bounds, Qt::Orientation direction ); 141 Q_SIGNALS: 142 void boundariesChanged(); 143 void rowCountChanged(); 144 145 private: 146 class Private; 147 Private *d; 148 }; 149 150 } 151 152 #endif // PLOTTERDIAGRAMCOMPRESSOR_H 153