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

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 KCHARTLEGEND_P_H
0021 #define KCHARTLEGEND_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 "KChartLegend.h"
0035 
0036 #include <KChartDiagramObserver.h>
0037 #include "KChartAbstractAreaWidget_p.h"
0038 #include <KChartTextAttributes.h>
0039 #include <KChartMarkerAttributes.h>
0040 #include "KChartMath_p.h"
0041 
0042 #include <QList>
0043 #include <QAbstractTextDocumentLayout>
0044 #include <QPainter>
0045 #include <QVector>
0046 
0047 QT_BEGIN_NAMESPACE
0048 class QGridLayout;
0049 class KTextDocument;
0050 class QTextDocument;
0051 QT_END_NAMESPACE
0052 
0053 namespace KChart {
0054 class AbstractDiagram;
0055 struct DatasetItem;
0056 class DiagramObserver;
0057 class AbstractLayoutItem;
0058 
0059 struct HDatasetItem
0060 {
0061     HDatasetItem();
0062     int height() const;
0063 
0064     AbstractLayoutItem *markerLine;
0065     TextLayoutItem *label;
0066     VerticalLineLayoutItem *separatorLine;
0067     QSpacerItem *spacer;
0068 };
0069 
0070 class DiagramsObserversList : public QList<DiagramObserver*> {};
0071 
0072 /**
0073  * \internal
0074  */
0075 class Q_DECL_HIDDEN Legend::Private : public AbstractAreaWidget::Private
0076 {
0077     friend class Legend;
0078 public:
0079     Private();
0080     ~Private();
0081 
0082     DiagramObserver* findObserverForDiagram( AbstractDiagram* diagram )
0083     {
0084         for (int i = 0; i < observers.size(); ++i) {
0085             DiagramObserver * obs = observers.at(i);
0086             if ( obs->diagram() == diagram )
0087                 return obs;
0088         }
0089         return nullptr;
0090     }
0091 
0092     void fetchPaintOptions( Legend *q );
0093     QSizeF markerSize( Legend *q, int dataset, qreal fontHeight ) const;
0094     QSizeF maxMarkerSize( Legend *q, qreal fontHeight ) const;
0095     void reflowHDatasetItems( Legend *q );
0096     void flowHDatasetItems( Legend *q );
0097     void destroyOldLayout();
0098 
0099 private:
0100     // user-settable
0101     const QWidget* referenceArea;
0102     Position position;
0103     Qt::Alignment alignment;
0104     Qt::Alignment textAlignment;
0105     Qt::Alignment legendLineSymbolAlignment;
0106     RelativePosition relativePosition;
0107     Qt::Orientation orientation;
0108     Qt::SortOrder order;
0109     bool showLines;
0110     QMap<uint,QString> texts;
0111     QMap<uint,QBrush> brushes;
0112     QMap<uint,QPen> pens;
0113     QMap<uint, MarkerAttributes> markerAttributes;
0114     QList<uint> hiddenDatasets;
0115     TextAttributes textAttributes;
0116     QString titleText;
0117     TextAttributes titleTextAttributes;
0118     uint spacing;
0119     bool useAutomaticMarkerSize;
0120     LegendStyle legendStyle;
0121 
0122     // internal
0123     mutable QStringList modelLabels;
0124     mutable QList<QBrush> modelBrushes;
0125     mutable QList<QPen> modelPens;
0126     mutable QList< MarkerAttributes > modelMarkers;
0127     mutable QSize cachedSizeHint;
0128     QVector< AbstractLayoutItem* > paintItems;
0129     QGridLayout* layout;
0130     QList< HDatasetItem > hLayoutDatasets;
0131     DiagramsObserversList observers;
0132 };
0133 
0134 inline Legend::Legend( Private* p, QWidget* parent )
0135     : AbstractAreaWidget( p, parent ) { init(); }
0136 
0137 inline Legend::Private * Legend::d_func()
0138 { return static_cast<Private*>( AbstractAreaWidget::d_func() ); }
0139 
0140 inline const Legend::Private * Legend::d_func() const
0141 { return static_cast<const Private*>( AbstractAreaWidget::d_func() ); }
0142 
0143 }
0144 
0145 #endif /* KCHARTLEGEND_P_H */