##// END OF EJS Templates
handleThemeChanged slot to legend.
sauimone -
r586:306855f16f30
parent child
Show More
@@ -1,219 +1,224
1 1 #include "qchartglobal.h"
2 2 #include "qlegend.h"
3 3 #include "qseries.h"
4 4 #include "legendmarker_p.h"
5 5 #include "qxyseries.h"
6 6 #include "qlineseries.h"
7 7 #include "qareaseries.h"
8 8 #include "qscatterseries.h"
9 9 #include "qsplineseries.h"
10 10 #include "qbarseries.h"
11 11 #include "qstackedbarseries.h"
12 12 #include "qpercentbarseries.h"
13 13 #include "qbarset.h"
14 14 #include "qpieseries.h"
15 15 #include "qpieslice.h"
16 16 #include <QPainter>
17 17 #include <QPen>
18 18
19 19 #include <QGraphicsSceneEvent>
20 20
21 21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22 22
23 23 QLegend::QLegend(QGraphicsItem *parent)
24 24 : QGraphicsObject(parent)
25 25 ,mBoundingRect(0,0,1,1)
26 26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
27 27 ,mMinimumSize(50,20) // TODO: magic numbers
28 28 {
29 29 }
30 30
31 31 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 32 {
33 33 painter->setBrush(mBackgroundBrush);
34 34 painter->drawRect(mBoundingRect);
35 35 }
36 36
37 37 QRectF QLegend::boundingRect() const
38 38 {
39 39 return mBoundingRect;
40 40 }
41 41
42 42 void QLegend::setBackgroundBrush(const QBrush& brush)
43 43 {
44 44 mBackgroundBrush = brush;
45 45 }
46 46
47 47 QBrush QLegend::backgroundBrush() const
48 48 {
49 49 return mBackgroundBrush;
50 50 }
51 51
52 52 QSizeF QLegend::minimumSize() const
53 53 {
54 54 return mMinimumSize;
55 55 }
56 56
57 57 void QLegend::setMinimumSize(const QSizeF size)
58 58 {
59 59 mMinimumSize = size;
60 60 }
61 61
62 62 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
63 63 {
64 64 mSeriesList.append(series);
65 createMarkers(series);
66 layoutChanged();
67 }
68
69 void QLegend::handleSeriesRemoved(QSeries* series)
70 {
71 if (series->type() == QSeries::SeriesTypeArea)
72 {
73 // This is special case. Area series has upper and lower series, which each have markers
74 QAreaSeries* s = static_cast<QAreaSeries*> (series);
75 deleteMarkers(s->upperSeries());
76 deleteMarkers(s->lowerSeries());
77 } else {
78 deleteMarkers(series);
79 }
80
81 mSeriesList.removeOne(series);
82 layoutChanged();
83 }
84
85 void QLegend::handleGeometryChanged(const QRectF& size)
86 {
87 mBoundingRect = size;
88 layoutChanged();
89 }
65 90
91 void QLegend::handleThemeChanged()
92 {
93 foreach(QSeries *s, mSeriesList) {
94 deleteMarkers(s);
95 }
96 foreach(QSeries *s, mSeriesList) {
97 createMarkers(s);
98 }
99 }
100
101 void QLegend::createMarkers(QSeries *series)
102 {
66 103 switch (series->type())
67 104 {
68 105 case QSeries::SeriesTypeLine: {
69
70 106 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
71 createMarker(lineSeries);
107 appendMarkers(lineSeries);
72 108 break;
73 109 }
74 110 case QSeries::SeriesTypeArea: {
75
76 111 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
77 createMarker(areaSeries->upperSeries());
112 appendMarkers(areaSeries->upperSeries());
78 113 if(areaSeries->lowerSeries())
79 createMarker(areaSeries->lowerSeries());
114 appendMarkers(areaSeries->lowerSeries());
80 115 break;
81 116 }
82 117
83 118 case QSeries::SeriesTypeBar: {
84
85 119 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
86 createMarkers(barSeries);
120 appendMarkers(barSeries);
87 121 break;
88 122 }
89 123
90 124 case QSeries::SeriesTypeStackedBar: {
91
92 125 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
93 createMarkers(stackedBarSeries);
126 appendMarkers(stackedBarSeries);
94 127 break;
95 128 }
96 129
97 130 case QSeries::SeriesTypePercentBar: {
98
99 131 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
100 createMarkers(percentBarSeries);
132 appendMarkers(percentBarSeries);
101 133 break;
102 134 }
103 135
104 136 case QSeries::SeriesTypeScatter: {
105
106 137 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
107 createMarker(scatterSeries);
138 appendMarkers(scatterSeries);
108 139 break;
109 140 }
110 141
111 142 case QSeries::SeriesTypePie: {
112
113 143 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
114 createMarkers(pieSeries);
144 appendMarkers(pieSeries);
115 145 break;
116 146 }
117 147
118 148 case QSeries::SeriesTypeSpline: {
119
120 149 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
121 createMarker(splineSeries);
150 appendMarkers(splineSeries);
122 151 break;
123 152 }
124 153 default: {
125 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
154 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
126 155 break;
127 156 }
128 157 }
129
130 layoutChanged();
131 }
132
133 void QLegend::handleSeriesRemoved(QSeries* series)
134 {
135 if (series->type() == QSeries::SeriesTypeArea)
136 {
137 // This is special case. Area series has upper and lower series, which each have markers
138 QAreaSeries* s = static_cast<QAreaSeries*> (series);
139 deleteMarkers(s->upperSeries());
140 deleteMarkers(s->lowerSeries());
141 } else {
142 deleteMarkers(series);
143 }
144
145 mSeriesList.removeOne(series);
146 layoutChanged();
147 }
148
149 void QLegend::handleGeometryChanged(const QRectF& size)
150 {
151 mBoundingRect = size;
152 layoutChanged();
153 }
154
155 void QLegend::deleteMarkers(QSeries *series)
156 {
157 // Search all markers that belong to given series and delete them.
158 foreach (LegendMarker *m, mMarkers) {
159 if (m->series() == series) {
160 mMarkers.removeOne(m);
161 delete m;
162 }
163 }
164 158 }
165 159
166 void QLegend::createMarker(QXYSeries* series)
160 void QLegend::appendMarkers(QXYSeries* series)
167 161 {
168 162 LegendMarker* marker = new LegendMarker(series,this);
169 163 marker->setName(series->name());
170 164 marker->setBrush(series->brush());
171 165 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
172 166 mMarkers.append(marker);
173 167 childItems().append(marker);
174 168 }
175 169
176 void QLegend::createMarkers(QBarSeries *series)
170 void QLegend::appendMarkers(QBarSeries *series)
177 171 {
178 172 foreach(QBarSet* s, series->barSets()) {
179 173 LegendMarker* marker = new LegendMarker(series,s,this);
180 174 marker->setName(s->name());
181 175 marker->setBrush(s->brush());
182 176 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
183 177 mMarkers.append(marker);
184 178 childItems().append(marker);
185 179 }
186 180 }
187 181
188 void QLegend::createMarkers(QPieSeries *series)
182 void QLegend::appendMarkers(QPieSeries *series)
189 183 {
190 184 foreach(QPieSlice* s, series->slices()) {
191 185 LegendMarker* marker = new LegendMarker(series,s,this);
192 186 marker->setName(s->label());
193 187 marker->setBrush(s->sliceBrush());
194 188 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
195 189 mMarkers.append(marker);
196 190 childItems().append(marker);
197 191 }
198 192 }
199 193
194 void QLegend::deleteMarkers(QSeries *series)
195 {
196 // Search all markers that belong to given series and delete them.
197 foreach (LegendMarker *m, mMarkers) {
198 if (m->series() == series) {
199 mMarkers.removeOne(m);
200 delete m;
201 }
202 }
203 }
204
200 205 void QLegend::layoutChanged()
201 206 {
202 207 // Calculate layout for markers and text
203 208 if (mMarkers.count() <= 0) {
204 209 // Nothing to do
205 210 return;
206 211 }
207 212
208 213 qreal steps = mMarkers.count();
209 214 qreal xStep = mBoundingRect.width() / steps;
210 215 qreal x=mBoundingRect.x();
211 216 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
212 217 foreach (LegendMarker* m, mMarkers) {
213 218 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
214 219 x += xStep;
215 220 }
216 221 }
217 222
218 223 #include "moc_qlegend.cpp"
219 224 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,65 +1,71
1 1 #ifndef QLEGEND_H
2 2 #define QLEGEND_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qseries.h"
6 6 #include <QGraphicsObject>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class Domain;
11 11 class LegendMarker;
12 12 class QPieSlice;
13 13 class QXYSeries;
14 14 class QBarSet;
15 15 class QBarSeries;
16 16 class QPieSeries;
17 17
18 18 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
19 19 {
20 20 Q_OBJECT
21 21 public:
22 22
23 23 explicit QLegend(QGraphicsItem *parent = 0);
24 24
25 25 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
26 26 QRectF boundingRect() const;
27 27
28 28 void setBackgroundBrush(const QBrush& brush);
29 29 QBrush backgroundBrush() const;
30 30
31 31 QSizeF minimumSize() const;
32 32 void setMinimumSize(const QSizeF size);
33 33
34 34 signals:
35 35 // for interactions.
36 36 void clicked(QSeries* series, Qt::MouseButton button);
37 37 void clicked(QBarSet* barset, Qt::MouseButton button);
38 38 void clicked(QPieSlice* slice, Qt::MouseButton button);
39 39
40 40 public slots:
41 41 void handleSeriesAdded(QSeries* series,Domain* domain);
42 42 void handleSeriesRemoved(QSeries* series);
43 43 void handleGeometryChanged(const QRectF& size);
44 44
45 // PIMPL --->
46 // Internal slot. Legend needs to know when theme has changed (or color of some series, if user changes it)
47 void handleThemeChanged();
48 // <--- PIMPL
49
45 50 private:
46 51 // PIMPL --->
47 void createMarker(QXYSeries* series);
48 void createMarkers(QBarSeries* series);
49 void createMarkers(QPieSeries* series);
52 void createMarkers(QSeries* series);
53 void appendMarkers(QXYSeries* series);
54 void appendMarkers(QBarSeries* series);
55 void appendMarkers(QPieSeries* series);
50 56 void deleteMarkers(QSeries* series);
51 57 void layoutChanged();
52 58 // <--- PIMPL
53 59
54 60
55 61 QRectF mBoundingRect;
56 62 QList<QSeries*> mSeriesList;
57 63 QList<LegendMarker*> mMarkers;
58 64
59 65 QBrush mBackgroundBrush;
60 66 QSizeF mMinimumSize;
61 67 };
62 68
63 69 QTCOMMERCIALCHART_END_NAMESPACE
64 70
65 71 #endif // QLEGEND_H
General Comments 0
You need to be logged in to leave comments. Login now