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 KCHARTLEGEND_P_H
21 #define KCHARTLEGEND_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 "KChartLegend.h"
35 
36 #include <KChartDiagramObserver.h>
37 #include "KChartAbstractAreaWidget_p.h"
38 #include <KChartTextAttributes.h>
39 #include <KChartMarkerAttributes.h>
40 #include "KChartMath_p.h"
41 
42 #include <QList>
43 #include <QAbstractTextDocumentLayout>
44 #include <QPainter>
45 #include <QVector>
46 
47 QT_BEGIN_NAMESPACE
48 class QGridLayout;
49 class KTextDocument;
50 class QTextDocument;
51 QT_END_NAMESPACE
52 
53 namespace KChart {
54 class AbstractDiagram;
55 struct DatasetItem;
56 class DiagramObserver;
57 class AbstractLayoutItem;
58 
59 struct HDatasetItem
60 {
61     HDatasetItem();
62     int height() const;
63 
64     AbstractLayoutItem *markerLine;
65     TextLayoutItem *label;
66     VerticalLineLayoutItem *separatorLine;
67     QSpacerItem *spacer;
68 };
69 
70 class DiagramsObserversList : public QList<DiagramObserver*> {};
71 
72 /**
73  * \internal
74  */
75 class Q_DECL_HIDDEN Legend::Private : public AbstractAreaWidget::Private
76 {
77     friend class Legend;
78 public:
79     Private();
80     ~Private();
81 
findObserverForDiagram(AbstractDiagram * diagram)82     DiagramObserver* findObserverForDiagram( AbstractDiagram* diagram )
83     {
84         for (int i = 0; i < observers.size(); ++i) {
85             DiagramObserver * obs = observers.at(i);
86             if ( obs->diagram() == diagram )
87                 return obs;
88         }
89         return nullptr;
90     }
91 
92     void fetchPaintOptions( Legend *q );
93     QSizeF markerSize( Legend *q, int dataset, qreal fontHeight ) const;
94     QSizeF maxMarkerSize( Legend *q, qreal fontHeight ) const;
95     void reflowHDatasetItems( Legend *q );
96     void flowHDatasetItems( Legend *q );
97     void destroyOldLayout();
98 
99 private:
100     // user-settable
101     const QWidget* referenceArea;
102     Position position;
103     Qt::Alignment alignment;
104     Qt::Alignment textAlignment;
105     Qt::Alignment legendLineSymbolAlignment;
106     RelativePosition relativePosition;
107     Qt::Orientation orientation;
108     Qt::SortOrder order;
109     bool showLines;
110     QMap<uint,QString> texts;
111     QMap<uint,QBrush> brushes;
112     QMap<uint,QPen> pens;
113     QMap<uint, MarkerAttributes> markerAttributes;
114     QList<uint> hiddenDatasets;
115     TextAttributes textAttributes;
116     QString titleText;
117     TextAttributes titleTextAttributes;
118     uint spacing;
119     bool useAutomaticMarkerSize;
120     LegendStyle legendStyle;
121 
122     // internal
123     mutable QStringList modelLabels;
124     mutable QList<QBrush> modelBrushes;
125     mutable QList<QPen> modelPens;
126     mutable QList< MarkerAttributes > modelMarkers;
127     mutable QSize cachedSizeHint;
128     QVector< AbstractLayoutItem* > paintItems;
129     QGridLayout* layout;
130     QList< HDatasetItem > hLayoutDatasets;
131     DiagramsObserversList observers;
132 };
133 
Legend(Private * p,QWidget * parent)134 inline Legend::Legend( Private* p, QWidget* parent )
135     : AbstractAreaWidget( p, parent ) { init(); }
136 
d_func()137 inline Legend::Private * Legend::d_func()
138 { return static_cast<Private*>( AbstractAreaWidget::d_func() ); }
139 
d_func()140 inline const Legend::Private * Legend::d_func() const
141 { return static_cast<const Private*>( AbstractAreaWidget::d_func() ); }
142 
143 }
144 
145 #endif /* KCHARTLEGEND_P_H */
146