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 KCHARTABSTRACTDIAGRAM_P_H
21 #define KCHARTABSTRACTDIAGRAM_P_H
22 
23 //
24 //  W A R N I N G
25 //  -------------
26 //
27 // This file is not part of the KD Chart API.  It exists purely as an
28 // implementation detail.  This header file may change from version to
29 // version without notice, or even be removed.
30 //
31 // We mean it.
32 //
33 
34 #include "KChartAbstractDiagram.h"
35 #include "KChartAbstractCoordinatePlane.h"
36 #include "KChartDataValueAttributes.h"
37 #include "KChartBackgroundAttributes.h"
38 #include "KChartRelativePosition.h"
39 #include "KChartPosition.h"
40 #include "KChartPaintContext.h"
41 #include "KChartPrintingParameters.h"
42 #include "KChartChart.h"
43 #include <KChartCartesianDiagramDataCompressor_p.h>
44 #include "ReverseMapper.h"
45 
46 #include <QMap>
47 #include <QPoint>
48 #include <QPointer>
49 #include <QFont>
50 #include <QFontMetrics>
51 #include <QPaintDevice>
52 #include <QPainterPath>
53 #include <QModelIndex>
54 #include <QPainterPath>
55 
56 
57 namespace KChart {
58     class LabelPaintInfo {
59     public:
60         LabelPaintInfo();
61         LabelPaintInfo( const QModelIndex& _index, const DataValueAttributes& _attrs,
62                         const QPainterPath& _labelArea, const QPointF& _markerPos,
63                         bool _isValuePositive, const QString& _value );
64         LabelPaintInfo( const LabelPaintInfo& other );
65         QModelIndex index;
66         DataValueAttributes attrs;
67         QPainterPath labelArea;
68         QPointF markerPos;
69         bool isValuePositive;
70         // could (ab)use attrs.dataLabel() instead
71         QString value;
72     };
73 
74     class LabelPaintCache
75     {
76     public:
LabelPaintCache()77         LabelPaintCache() {}
~LabelPaintCache()78         ~LabelPaintCache()
79         {
80             clear();
81         }
82 
clear()83         void clear()
84         {
85             paintReplay.clear();
86         }
87 
88         QVector<LabelPaintInfo> paintReplay;
89     private:
90         LabelPaintCache( LabelPaintCache& other ); // no copies
91     };
92 
93 
94 /**
95  * \internal
96  */
97     class AttributesModel;
98 
99     class Q_DECL_HIDDEN KChart::AbstractDiagram::Private
100     {
101         friend class AbstractDiagram;
102     public:
103         explicit Private();
104         virtual ~Private();
105 
106         Private( const Private& rhs );
107 
108         void setAttributesModel( AttributesModel* );
109 
110         bool usesExternalAttributesModel() const;
111 
112         // FIXME: Optimize if necessary
113         virtual qreal calcPercentValue( const QModelIndex & index ) const;
114 
115         // this should possibly be virtual so it can be overridden
116         void addLabel( LabelPaintCache* cache,
117                        const QModelIndex& index,
118                        const CartesianDiagramDataCompressor::CachePosition* position,
119                        const PositionPoints& points, const Position& autoPositionPositive,
120                        const Position& autoPositionNegative, const qreal value,
121                        qreal favoriteAngle = 0.0 );
122 
123         const QFontMetrics* cachedFontMetrics( const QFont& font, const QPaintDevice* paintDevice) const;
124         const QFontMetrics cachedFontMetrics() const;
125 
126         QString formatNumber( qreal value, int decimalDigits ) const;
127         QString formatDataValueText( const DataValueAttributes &dva,
128                                      const QModelIndex& index, qreal value ) const;
129 
130         void forgetAlreadyPaintedDataValues();
131 
132         void paintDataValueTextsAndMarkers( PaintContext* ctx,
133                                             const LabelPaintCache& cache,
134                                             bool paintMarkers,
135                                             bool justCalculateRect=false,
136                                             QRectF* cumulatedBoundingRect = nullptr );
137 
138         void paintDataValueText( QPainter* painter,
139                                  const QModelIndex& index,
140                                  const QPointF& pos,
141                                  qreal value,
142                                  bool justCalculateRect=false,
143                                  QRectF* cumulatedBoundingRect = nullptr );
144 
145         void paintDataValueText( QPainter* painter,
146                                  const DataValueAttributes& attrs,
147                                  const QPointF& pos,
148                                  bool valueIsPositive,
149                                  const QString& text,
150                                  bool justCalculateRect=false,
151                                  QRectF* cumulatedBoundingRect = nullptr );
152 
datasetCount()153         inline int datasetCount() const
154         {
155             return attributesModel->columnCount( attributesModelRootIndex ) / datasetDimension;
156         }
157 
158         virtual QModelIndex indexAt( const QPoint& point ) const;
159 
160         QModelIndexList indexesAt( const QPoint& point ) const;
161 
162         QModelIndexList indexesIn( const QRect& rect ) const;
163 
164         virtual CartesianDiagramDataCompressor::AggregatedDataValueAttributes aggregatedAttrs(
165                 const QModelIndex & index,
166                 const CartesianDiagramDataCompressor::CachePosition * position ) const;
167 
168         /**
169          * Sets arbitrary attributes of a data set.
170          */
171         void setDatasetAttrs( int dataset, const QVariant& data, int role );
172 
173         /**
174          * Retrieves arbitrary attributes of a data set.
175          */
176         QVariant datasetAttrs( int dataset, int role ) const;
177 
178         /**
179          * Resets an attribute of a dataset back to its default.
180          */
181         void resetDatasetAttrs( int dataset, int role );
182 
183         /**
184          * Whether the diagram is transposed (X and Y swapped), which has the same effect as rotating
185          * the diagram 90° clockwise and inverting the (then vertical) X coordinate.
186          */
187         bool isTransposed() const;
188 
get(AbstractDiagram * diagram)189         static Private* get( AbstractDiagram *diagram ) { return diagram->_d; }
190 
191         AbstractDiagram* diagram;
192         ReverseMapper reverseMapper;
193         /// The size of the diagram set by AbstractDiagram::resize()
194         QSizeF diagramSize;
195         bool doDumpPaintTime; // for use in performance testing code
196 
197     protected:
198         void init();
199         void init( AbstractCoordinatePlane* plane );
200 
201         QPointer<AbstractCoordinatePlane> plane;
202         mutable QModelIndex attributesModelRootIndex;
203         QPointer<AttributesModel> attributesModel;
204         bool allowOverlappingDataValueTexts;
205         bool antiAliasing;
206         bool percent;
207         int datasetDimension;
208         mutable QPair<QPointF,QPointF> databoundaries;
209         mutable bool databoundariesDirty;
210 
211         QMap< Qt::Orientation, QString > unitSuffix;
212         QMap< Qt::Orientation, QString > unitPrefix;
213         QMap< int, QMap< Qt::Orientation, QString > > unitSuffixMap;
214         QMap< int, QMap< Qt::Orientation, QString > > unitPrefixMap;
215         QList< QPainterPath > alreadyDrawnDataValueTexts;
216 
217     private:
218         QString prevPaintedDataValueText;
219         mutable QFontMetrics mCachedFontMetrics;
220         mutable QFont mCachedFont;
221         mutable QPaintDevice* mCachedPaintDevice;
222     };
223 
AbstractDiagram(Private * p)224     inline AbstractDiagram::AbstractDiagram( Private * p ) : _d( p )
225     {
226         init();
227     }
AbstractDiagram(Private * p,QWidget * parent,AbstractCoordinatePlane * plane)228     inline AbstractDiagram::AbstractDiagram(
229         Private * p, QWidget* parent, AbstractCoordinatePlane* plane )
230         : QAbstractItemView( parent ), _d( p )
231     {
232         _d->init( plane );
233         init();
234     }
235 
236 
237     class LineAttributesInfo {
238     public:
239         LineAttributesInfo();
240         LineAttributesInfo( const QModelIndex& _index, const QPointF& _value, const QPointF& _nextValue );
241 
242         QModelIndex index;
243         QPointF value;
244         QPointF nextValue;
245     };
246 
247     typedef QVector<LineAttributesInfo> LineAttributesInfoList;
248     typedef QVectorIterator<LineAttributesInfo> LineAttributesInfoListIterator;
249 
250 }
251 #endif /* KCHARTDIAGRAM_P_H */
252