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