File indexing completed on 2024-05-12 04:20:35

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KCHARTLEGEND_P_H
0010 #define KCHARTLEGEND_P_H
0011 
0012 //
0013 //  W A R N I N G
0014 //  -------------
0015 //
0016 // This file is not part of the KD Chart API.  It exists purely as an
0017 // implementation detail.  This header file may change from version to
0018 // version without notice, or even be removed.
0019 //
0020 // We mean it.
0021 //
0022 
0023 #include "KChartLegend.h"
0024 
0025 #include <KChartDiagramObserver.h>
0026 #include "KChartAbstractAreaWidget_p.h"
0027 #include <KChartTextAttributes.h>
0028 #include <KChartMarkerAttributes.h>
0029 #include "KChartMath_p.h"
0030 
0031 #include <QList>
0032 #include <QAbstractTextDocumentLayout>
0033 #include <QPainter>
0034 #include <QVector>
0035 
0036 QT_BEGIN_NAMESPACE
0037 class QGridLayout;
0038 class KTextDocument;
0039 class QTextDocument;
0040 QT_END_NAMESPACE
0041 
0042 namespace KChart {
0043 class AbstractDiagram;
0044 struct DatasetItem;
0045 class DiagramObserver;
0046 class AbstractLayoutItem;
0047 
0048 struct HDatasetItem
0049 {
0050     HDatasetItem();
0051     int height() const;
0052 
0053     AbstractLayoutItem *markerLine;
0054     TextLayoutItem *label;
0055     VerticalLineLayoutItem *separatorLine;
0056     QSpacerItem *spacer;
0057 };
0058 
0059 class DiagramsObserversList : public QList<DiagramObserver*> {};
0060 
0061 /**
0062  * \internal
0063  */
0064 class Q_DECL_HIDDEN Legend::Private : public AbstractAreaWidget::Private
0065 {
0066     friend class Legend;
0067 public:
0068     Private();
0069     ~Private() override;
0070 
0071     DiagramObserver* findObserverForDiagram( AbstractDiagram* diagram )
0072     {
0073         for (int i = 0; i < observers.size(); ++i) {
0074             DiagramObserver * obs = observers.at(i);
0075             if ( obs->diagram() == diagram )
0076                 return obs;
0077         }
0078         return nullptr;
0079     }
0080 
0081     void fetchPaintOptions( Legend *q );
0082     QSizeF markerSize( Legend *q, int dataset, qreal fontHeight ) const;
0083     QSizeF maxMarkerSize( Legend *q, qreal fontHeight ) const;
0084     void reflowHDatasetItems( Legend *q );
0085     void flowHDatasetItems( Legend *q );
0086     void destroyOldLayout();
0087 
0088 private:
0089     // user-settable
0090     const QWidget* referenceArea;
0091     Position position;
0092     Qt::Alignment alignment;
0093     Qt::Alignment textAlignment;
0094     Qt::Alignment legendLineSymbolAlignment;
0095     RelativePosition relativePosition;
0096     Qt::Orientation orientation;
0097     Qt::SortOrder order;
0098     bool showLines;
0099     QMap<uint,QString> texts;
0100     QMap<uint,QBrush> brushes;
0101     QMap<uint,QPen> pens;
0102     QMap<uint, MarkerAttributes> markerAttributes;
0103     QList<uint> hiddenDatasets;
0104     TextAttributes textAttributes;
0105     QString titleText;
0106     TextAttributes titleTextAttributes;
0107     uint spacing;
0108     bool useAutomaticMarkerSize;
0109     LegendStyle legendStyle;
0110 
0111     // internal
0112     mutable QStringList modelLabels;
0113     mutable QList<QBrush> modelBrushes;
0114     mutable QList<QPen> modelPens;
0115     mutable QList< MarkerAttributes > modelMarkers;
0116     mutable QSize cachedSizeHint;
0117     QVector< AbstractLayoutItem* > paintItems;
0118     QGridLayout* layout;
0119     QList< HDatasetItem > hLayoutDatasets;
0120     DiagramsObserversList observers;
0121 };
0122 
0123 inline Legend::Legend( Private* p, QWidget* parent )
0124     : AbstractAreaWidget( p, parent ) { init(); }
0125 
0126 inline Legend::Private * Legend::d_func()
0127 { return static_cast<Private*>( AbstractAreaWidget::d_func() ); }
0128 
0129 inline const Legend::Private * Legend::d_func() const
0130 { return static_cast<const Private*>( AbstractAreaWidget::d_func() ); }
0131 
0132 }
0133 
0134 #endif /* KCHARTLEGEND_P_H */