File indexing completed on 2024-05-12 15:54:12

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef KCHARTABSTRACTDIAGRAM_P_H
0021 #define KCHARTABSTRACTDIAGRAM_P_H
0022 
0023 //
0024 //  W A R N I N G
0025 //  -------------
0026 //
0027 // This file is not part of the KD Chart API.  It exists purely as an
0028 // implementation detail.  This header file may change from version to
0029 // version without notice, or even be removed.
0030 //
0031 // We mean it.
0032 //
0033 
0034 #include "KChartAbstractDiagram.h"
0035 #include "KChartAbstractCoordinatePlane.h"
0036 #include "KChartDataValueAttributes.h"
0037 #include "KChartBackgroundAttributes.h"
0038 #include "KChartRelativePosition.h"
0039 #include "KChartPosition.h"
0040 #include "KChartPaintContext.h"
0041 #include "KChartPrintingParameters.h"
0042 #include "KChartChart.h"
0043 #include <KChartCartesianDiagramDataCompressor_p.h>
0044 #include "ReverseMapper.h"
0045 
0046 #include <QMap>
0047 #include <QPoint>
0048 #include <QPointer>
0049 #include <QFont>
0050 #include <QFontMetrics>
0051 #include <QPaintDevice>
0052 #include <QPainterPath>
0053 #include <QModelIndex>
0054 #include <QPainterPath>
0055 
0056 
0057 namespace KChart {
0058     class LabelPaintInfo {
0059     public:
0060         LabelPaintInfo();
0061         LabelPaintInfo( const QModelIndex& _index, const DataValueAttributes& _attrs,
0062                         const QPainterPath& _labelArea, const QPointF& _markerPos,
0063                         bool _isValuePositive, const QString& _value );
0064         LabelPaintInfo( const LabelPaintInfo& other );
0065         QModelIndex index;
0066         DataValueAttributes attrs;
0067         QPainterPath labelArea;
0068         QPointF markerPos;
0069         bool isValuePositive;
0070         // could (ab)use attrs.dataLabel() instead
0071         QString value;
0072     };
0073 
0074     class LabelPaintCache
0075     {
0076     public:
0077         LabelPaintCache() {}
0078         ~LabelPaintCache()
0079         {
0080             clear();
0081         }
0082 
0083         void clear()
0084         {
0085             paintReplay.clear();
0086         }
0087 
0088         QVector<LabelPaintInfo> paintReplay;
0089     private:
0090         LabelPaintCache( LabelPaintCache& other ); // no copies
0091     };
0092 
0093 
0094 /**
0095  * \internal
0096  */
0097     class AttributesModel;
0098 
0099     class Q_DECL_HIDDEN KChart::AbstractDiagram::Private
0100     {
0101         friend class AbstractDiagram;
0102     public:
0103         explicit Private();
0104         virtual ~Private();
0105 
0106         Private( const Private& rhs );
0107 
0108         void setAttributesModel( AttributesModel* );
0109 
0110         bool usesExternalAttributesModel() const;
0111 
0112         // FIXME: Optimize if necessary
0113         virtual qreal calcPercentValue( const QModelIndex & index ) const;
0114 
0115         // this should possibly be virtual so it can be overridden
0116         void addLabel( LabelPaintCache* cache,
0117                        const QModelIndex& index,
0118                        const CartesianDiagramDataCompressor::CachePosition* position,
0119                        const PositionPoints& points, const Position& autoPositionPositive,
0120                        const Position& autoPositionNegative, const qreal value,
0121                        qreal favoriteAngle = 0.0 );
0122 
0123         const QFontMetrics* cachedFontMetrics( const QFont& font, const QPaintDevice* paintDevice) const;
0124         const QFontMetrics cachedFontMetrics() const;
0125 
0126         QString formatNumber( qreal value, int decimalDigits ) const;
0127         QString formatDataValueText( const DataValueAttributes &dva,
0128                                      const QModelIndex& index, qreal value ) const;
0129 
0130         void forgetAlreadyPaintedDataValues();
0131 
0132         void paintDataValueTextsAndMarkers( PaintContext* ctx,
0133                                             const LabelPaintCache& cache,
0134                                             bool paintMarkers,
0135                                             bool justCalculateRect=false,
0136                                             QRectF* cumulatedBoundingRect = nullptr );
0137 
0138         void paintDataValueText( QPainter* painter,
0139                                  const QModelIndex& index,
0140                                  const QPointF& pos,
0141                                  qreal value,
0142                                  bool justCalculateRect=false,
0143                                  QRectF* cumulatedBoundingRect = nullptr );
0144 
0145         void paintDataValueText( QPainter* painter,
0146                                  const DataValueAttributes& attrs,
0147                                  const QPointF& pos,
0148                                  bool valueIsPositive,
0149                                  const QString& text,
0150                                  bool justCalculateRect=false,
0151                                  QRectF* cumulatedBoundingRect = nullptr );
0152 
0153         inline int datasetCount() const
0154         {
0155             return attributesModel->columnCount( attributesModelRootIndex ) / datasetDimension;
0156         }
0157 
0158         virtual QModelIndex indexAt( const QPoint& point ) const;
0159 
0160         QModelIndexList indexesAt( const QPoint& point ) const;
0161 
0162         QModelIndexList indexesIn( const QRect& rect ) const;
0163 
0164         virtual CartesianDiagramDataCompressor::AggregatedDataValueAttributes aggregatedAttrs(
0165                 const QModelIndex & index,
0166                 const CartesianDiagramDataCompressor::CachePosition * position ) const;
0167 
0168         /**
0169          * Sets arbitrary attributes of a data set.
0170          */
0171         void setDatasetAttrs( int dataset, const QVariant& data, int role );
0172 
0173         /**
0174          * Retrieves arbitrary attributes of a data set.
0175          */
0176         QVariant datasetAttrs( int dataset, int role ) const;
0177 
0178         /**
0179          * Resets an attribute of a dataset back to its default.
0180          */
0181         void resetDatasetAttrs( int dataset, int role );
0182 
0183         /**
0184          * Whether the diagram is transposed (X and Y swapped), which has the same effect as rotating
0185          * the diagram 90° clockwise and inverting the (then vertical) X coordinate.
0186          */
0187         bool isTransposed() const;
0188 
0189         static Private* get( AbstractDiagram *diagram ) { return diagram->_d; }
0190 
0191         AbstractDiagram* diagram;
0192         ReverseMapper reverseMapper;
0193         /// The size of the diagram set by AbstractDiagram::resize()
0194         QSizeF diagramSize;
0195         bool doDumpPaintTime; // for use in performance testing code
0196 
0197     protected:
0198         void init();
0199         void init( AbstractCoordinatePlane* plane );
0200 
0201         QPointer<AbstractCoordinatePlane> plane;
0202         mutable QModelIndex attributesModelRootIndex;
0203         QPointer<AttributesModel> attributesModel;
0204         bool allowOverlappingDataValueTexts;
0205         bool antiAliasing;
0206         bool percent;
0207         int datasetDimension;
0208         mutable QPair<QPointF,QPointF> databoundaries;
0209         mutable bool databoundariesDirty;
0210 
0211         QMap< Qt::Orientation, QString > unitSuffix;
0212         QMap< Qt::Orientation, QString > unitPrefix;
0213         QMap< int, QMap< Qt::Orientation, QString > > unitSuffixMap;
0214         QMap< int, QMap< Qt::Orientation, QString > > unitPrefixMap;
0215         QList< QPainterPath > alreadyDrawnDataValueTexts;
0216 
0217     private:
0218         QString prevPaintedDataValueText;
0219         mutable QFontMetrics mCachedFontMetrics;
0220         mutable QFont mCachedFont;
0221         mutable QPaintDevice* mCachedPaintDevice;
0222     };
0223 
0224     inline AbstractDiagram::AbstractDiagram( Private * p ) : _d( p )
0225     {
0226         init();
0227     }
0228     inline AbstractDiagram::AbstractDiagram(
0229         Private * p, QWidget* parent, AbstractCoordinatePlane* plane )
0230         : QAbstractItemView( parent ), _d( p )
0231     {
0232         _d->init( plane );
0233         init();
0234     }
0235 
0236 
0237     class LineAttributesInfo {
0238     public:
0239         LineAttributesInfo();
0240         LineAttributesInfo( const QModelIndex& _index, const QPointF& _value, const QPointF& _nextValue );
0241 
0242         QModelIndex index;
0243         QPointF value;
0244         QPointF nextValue;
0245     };
0246 
0247     typedef QVector<LineAttributesInfo> LineAttributesInfoList;
0248     typedef QVectorIterator<LineAttributesInfo> LineAttributesInfoListIterator;
0249 
0250 }
0251 #endif /* KCHARTDIAGRAM_P_H */