##// END OF EJS Templates
fixed changed singal name in legend
sauimone -
r670:27d565e40e33
parent child
Show More
@@ -1,491 +1,491
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 "chartpresenter_p.h"
16 #include "chartpresenter_p.h"
17 #include <QPainter>
17 #include <QPainter>
18 #include <QPen>
18 #include <QPen>
19
19
20 #include <QGraphicsSceneEvent>
20 #include <QGraphicsSceneEvent>
21
21
22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23
23
24 QLegend::QLegend(QGraphicsItem *parent)
24 QLegend::QLegend(QGraphicsItem *parent)
25 : QGraphicsObject(parent)
25 : QGraphicsObject(parent)
26 ,mPos(0,0)
26 ,mPos(0,0)
27 ,mSize(0,0)
27 ,mSize(0,0)
28 ,mMinimumSize(50,20) // TODO: magic numbers
28 ,mMinimumSize(50,20) // TODO: magic numbers
29 ,mMaximumSize(150,100)
29 ,mMaximumSize(150,100)
30 ,m_brush(Qt::darkGray) // TODO: from theme?
30 ,m_brush(Qt::darkGray) // TODO: from theme?
31 ,mPreferredLayout(QLegend::PreferredLayoutVertical)
31 ,mPreferredLayout(QLegend::PreferredLayoutVertical)
32 {
32 {
33 setVisible(false);
33 setVisible(false);
34 setZValue(ChartPresenter::LegendZValue);
34 setZValue(ChartPresenter::LegendZValue);
35 }
35 }
36
36
37 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
38 {
38 {
39 Q_UNUSED(option)
39 Q_UNUSED(option)
40 Q_UNUSED(widget)
40 Q_UNUSED(widget)
41
41
42 painter->setOpacity(0.5);
42 painter->setOpacity(0.5);
43 painter->setPen(m_pen);
43 painter->setPen(m_pen);
44 painter->setBrush(m_brush);
44 painter->setBrush(m_brush);
45 painter->drawRect(boundingRect());
45 painter->drawRect(boundingRect());
46 }
46 }
47
47
48 QRectF QLegend::boundingRect() const
48 QRectF QLegend::boundingRect() const
49 {
49 {
50 return QRectF(mPos,mSize);
50 return QRectF(mPos,mSize);
51 }
51 }
52
52
53 void QLegend::setBrush(const QBrush& brush)
53 void QLegend::setBrush(const QBrush& brush)
54 {
54 {
55 if(m_brush!=brush){
55 if(m_brush!=brush){
56 m_brush = brush;
56 m_brush = brush;
57 update();
57 update();
58 }
58 }
59 }
59 }
60
60
61 QBrush QLegend::brush() const
61 QBrush QLegend::brush() const
62 {
62 {
63 return m_brush;
63 return m_brush;
64 }
64 }
65
65
66 void QLegend::setPen(const QPen& pen)
66 void QLegend::setPen(const QPen& pen)
67 {
67 {
68 if(m_pen!=pen){
68 if(m_pen!=pen){
69 m_pen = pen;
69 m_pen = pen;
70 update();
70 update();
71 }
71 }
72 }
72 }
73
73
74 QPen QLegend::pen() const
74 QPen QLegend::pen() const
75 {
75 {
76 return m_pen;
76 return m_pen;
77 }
77 }
78
78
79 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
79 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
80 {
80 {
81 mPreferredLayout = preferred;
81 mPreferredLayout = preferred;
82 layoutChanged();
82 layoutChanged();
83 }
83 }
84
84
85 QSizeF QLegend::maximumSize() const
85 QSizeF QLegend::maximumSize() const
86 {
86 {
87 return mMaximumSize;
87 return mMaximumSize;
88 }
88 }
89
89
90 void QLegend::setMaximumSize(const QSizeF size)
90 void QLegend::setMaximumSize(const QSizeF size)
91 {
91 {
92 mMaximumSize = size;
92 mMaximumSize = size;
93 layoutChanged();
93 layoutChanged();
94 }
94 }
95
95
96 void QLegend::setSize(const QSizeF size)
96 void QLegend::setSize(const QSizeF size)
97 {
97 {
98 mSize = size;
98 mSize = size;
99 if (mSize.width() > mMaximumSize.width()) {
99 if (mSize.width() > mMaximumSize.width()) {
100 mSize.setWidth(mMaximumSize.width());
100 mSize.setWidth(mMaximumSize.width());
101 }
101 }
102 if (mSize.height() > mMaximumSize.height()) {
102 if (mSize.height() > mMaximumSize.height()) {
103 mSize.setHeight(mMaximumSize.height());
103 mSize.setHeight(mMaximumSize.height());
104 }
104 }
105 }
105 }
106
106
107 void QLegend::setPos(const QPointF &pos)
107 void QLegend::setPos(const QPointF &pos)
108 {
108 {
109 mPos = pos;
109 mPos = pos;
110 layoutChanged();
110 layoutChanged();
111 }
111 }
112
112
113 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
113 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
114 {
114 {
115 Q_UNUSED(domain)
115 Q_UNUSED(domain)
116
116
117 createMarkers(series);
117 createMarkers(series);
118 connectSeries(series);
118 connectSeries(series);
119 layoutChanged();
119 layoutChanged();
120 }
120 }
121
121
122 void QLegend::handleSeriesRemoved(QSeries* series)
122 void QLegend::handleSeriesRemoved(QSeries* series)
123 {
123 {
124 disconnectSeries(series);
124 disconnectSeries(series);
125
125
126 if (series->type() == QSeries::SeriesTypeArea)
126 if (series->type() == QSeries::SeriesTypeArea)
127 {
127 {
128 // This is special case. Area series has upper and lower series, which each have markers
128 // This is special case. Area series has upper and lower series, which each have markers
129 QAreaSeries* s = static_cast<QAreaSeries*> (series);
129 QAreaSeries* s = static_cast<QAreaSeries*> (series);
130 deleteMarkers(s->upperSeries());
130 deleteMarkers(s->upperSeries());
131 deleteMarkers(s->lowerSeries());
131 deleteMarkers(s->lowerSeries());
132 } else {
132 } else {
133 deleteMarkers(series);
133 deleteMarkers(series);
134 }
134 }
135
135
136 layoutChanged();
136 layoutChanged();
137 }
137 }
138
138
139 void QLegend::handleAdded(QList<QPieSlice*> slices)
139 void QLegend::handleAdded(QList<QPieSlice*> slices)
140 {
140 {
141 QPieSeries* series = static_cast<QPieSeries*> (sender());
141 QPieSeries* series = static_cast<QPieSeries*> (sender());
142 foreach(QPieSlice* s, slices) {
142 foreach(QPieSlice* s, slices) {
143 LegendMarker* marker = new LegendMarker(series,s,this);
143 LegendMarker* marker = new LegendMarker(series,s,this);
144 marker->setName(s->label());
144 marker->setName(s->label());
145 marker->setBrush(s->sliceBrush());
145 marker->setBrush(s->sliceBrush());
146 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
146 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
147 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
147 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
148 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
148 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
149 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
149 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
150 mMarkers.append(marker);
150 mMarkers.append(marker);
151 childItems().append(marker);
151 childItems().append(marker);
152 }
152 }
153 layoutChanged();
153 layoutChanged();
154 }
154 }
155
155
156 void QLegend::handleRemoved(QList<QPieSlice *> slices)
156 void QLegend::handleRemoved(QList<QPieSlice *> slices)
157 {
157 {
158 Q_UNUSED(slices)
158 Q_UNUSED(slices)
159 // Propably no need to listen for this, since removed slices are deleted and we listen destroyed signal
159 // Propably no need to listen for this, since removed slices are deleted and we listen destroyed signal
160 // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count();
160 // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count();
161 }
161 }
162
162
163
163
164 void QLegend::handleMarkerDestroyed()
164 void QLegend::handleMarkerDestroyed()
165 {
165 {
166 // TODO: what if more than one markers are destroyed and we update layout after first one?
166 // TODO: what if more than one markers are destroyed and we update layout after first one?
167 LegendMarker* m = static_cast<LegendMarker*> (sender());
167 LegendMarker* m = static_cast<LegendMarker*> (sender());
168 mMarkers.removeOne(m);
168 mMarkers.removeOne(m);
169 layoutChanged();
169 layoutChanged();
170 }
170 }
171
171
172 void QLegend::connectSeries(QSeries *series)
172 void QLegend::connectSeries(QSeries *series)
173 {
173 {
174 // Connect relevant signals from series
174 // Connect relevant signals from series
175 switch (series->type())
175 switch (series->type())
176 {
176 {
177 case QSeries::SeriesTypeLine: {
177 case QSeries::SeriesTypeLine: {
178 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
178 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
179 break;
179 break;
180 }
180 }
181 case QSeries::SeriesTypeArea: {
181 case QSeries::SeriesTypeArea: {
182 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
182 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
183 break;
183 break;
184 }
184 }
185 case QSeries::SeriesTypeBar: {
185 case QSeries::SeriesTypeBar: {
186 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
186 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
187 break;
187 break;
188 }
188 }
189 case QSeries::SeriesTypeStackedBar: {
189 case QSeries::SeriesTypeStackedBar: {
190 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
190 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
191 break;
191 break;
192 }
192 }
193 case QSeries::SeriesTypePercentBar: {
193 case QSeries::SeriesTypePercentBar: {
194 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
194 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
195 break;
195 break;
196 }
196 }
197 case QSeries::SeriesTypeScatter: {
197 case QSeries::SeriesTypeScatter: {
198 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
198 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
199 break;
199 break;
200 }
200 }
201 case QSeries::SeriesTypePie: {
201 case QSeries::SeriesTypePie: {
202 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
202 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
203 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
203 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
204 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
204 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
205 break;
205 break;
206 }
206 }
207 case QSeries::SeriesTypeSpline: {
207 case QSeries::SeriesTypeSpline: {
208 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
208 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
209 break;
209 break;
210 }
210 }
211 default: {
211 default: {
212 qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented.";
212 qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented.";
213 break;
213 break;
214 }
214 }
215 }
215 }
216 }
216 }
217
217
218 void QLegend::disconnectSeries(QSeries *series)
218 void QLegend::disconnectSeries(QSeries *series)
219 {
219 {
220 // Connect relevant signals from series
220 // Connect relevant signals from series
221 switch (series->type())
221 switch (series->type())
222 {
222 {
223 case QSeries::SeriesTypeLine: {
223 case QSeries::SeriesTypeLine: {
224 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
224 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
225 break;
225 break;
226 }
226 }
227 case QSeries::SeriesTypeArea: {
227 case QSeries::SeriesTypeArea: {
228 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
228 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
229 break;
229 break;
230 }
230 }
231 case QSeries::SeriesTypeBar: {
231 case QSeries::SeriesTypeBar: {
232 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
232 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
233 break;
233 break;
234 }
234 }
235 case QSeries::SeriesTypeStackedBar: {
235 case QSeries::SeriesTypeStackedBar: {
236 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
236 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
237 break;
237 break;
238 }
238 }
239 case QSeries::SeriesTypePercentBar: {
239 case QSeries::SeriesTypePercentBar: {
240 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
240 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
241 break;
241 break;
242 }
242 }
243 case QSeries::SeriesTypeScatter: {
243 case QSeries::SeriesTypeScatter: {
244 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
244 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
245 break;
245 break;
246 }
246 }
247 case QSeries::SeriesTypePie: {
247 case QSeries::SeriesTypePie: {
248 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
248 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
249 disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
249 disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
250 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
250 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
251 break;
251 break;
252 }
252 }
253 case QSeries::SeriesTypeSpline: {
253 case QSeries::SeriesTypeSpline: {
254 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
254 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
255 break;
255 break;
256 }
256 }
257 default: {
257 default: {
258 qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
258 qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
259 break;
259 break;
260 }
260 }
261 }
261 }
262 }
262 }
263
263
264 void QLegend::createMarkers(QSeries *series)
264 void QLegend::createMarkers(QSeries *series)
265 {
265 {
266 switch (series->type())
266 switch (series->type())
267 {
267 {
268 case QSeries::SeriesTypeLine: {
268 case QSeries::SeriesTypeLine: {
269 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
269 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
270 appendMarkers(lineSeries);
270 appendMarkers(lineSeries);
271 break;
271 break;
272 }
272 }
273 case QSeries::SeriesTypeArea: {
273 case QSeries::SeriesTypeArea: {
274 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
274 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
275 appendMarkers(areaSeries->upperSeries());
275 appendMarkers(areaSeries->upperSeries());
276 if(areaSeries->lowerSeries())
276 if(areaSeries->lowerSeries())
277 appendMarkers(areaSeries->lowerSeries());
277 appendMarkers(areaSeries->lowerSeries());
278 break;
278 break;
279 }
279 }
280
280
281 case QSeries::SeriesTypeBar: {
281 case QSeries::SeriesTypeBar: {
282 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
282 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
283 appendMarkers(barSeries);
283 appendMarkers(barSeries);
284 break;
284 break;
285 }
285 }
286
286
287 case QSeries::SeriesTypeStackedBar: {
287 case QSeries::SeriesTypeStackedBar: {
288 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
288 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
289 appendMarkers(stackedBarSeries);
289 appendMarkers(stackedBarSeries);
290 break;
290 break;
291 }
291 }
292
292
293 case QSeries::SeriesTypePercentBar: {
293 case QSeries::SeriesTypePercentBar: {
294 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
294 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
295 appendMarkers(percentBarSeries);
295 appendMarkers(percentBarSeries);
296 break;
296 break;
297 }
297 }
298
298
299 case QSeries::SeriesTypeScatter: {
299 case QSeries::SeriesTypeScatter: {
300 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
300 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
301 appendMarkers(scatterSeries);
301 appendMarkers(scatterSeries);
302 break;
302 break;
303 }
303 }
304
304
305 case QSeries::SeriesTypePie: {
305 case QSeries::SeriesTypePie: {
306 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
306 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
307 appendMarkers(pieSeries);
307 appendMarkers(pieSeries);
308 break;
308 break;
309 }
309 }
310
310
311 case QSeries::SeriesTypeSpline: {
311 case QSeries::SeriesTypeSpline: {
312 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
312 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
313 appendMarkers(splineSeries);
313 appendMarkers(splineSeries);
314 break;
314 break;
315 }
315 }
316 default: {
316 default: {
317 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
317 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
318 break;
318 break;
319 }
319 }
320 }
320 }
321 }
321 }
322
322
323 void QLegend::appendMarkers(QXYSeries* series)
323 void QLegend::appendMarkers(QXYSeries* series)
324 {
324 {
325 LegendMarker* marker = new LegendMarker(series,this);
325 LegendMarker* marker = new LegendMarker(series,this);
326 marker->setName(series->name());
326 marker->setName(series->name());
327 marker->setBrush(series->brush());
327 marker->setBrush(series->brush());
328 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
328 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
329 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
329 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
330 mMarkers.append(marker);
330 mMarkers.append(marker);
331 childItems().append(marker);
331 childItems().append(marker);
332 }
332 }
333
333
334 void QLegend::appendMarkers(QBarSeries *series)
334 void QLegend::appendMarkers(QBarSeries *series)
335 {
335 {
336 foreach(QBarSet* s, series->barSets()) {
336 foreach(QBarSet* s, series->barSets()) {
337 LegendMarker* marker = new LegendMarker(series,s,this);
337 LegendMarker* marker = new LegendMarker(series,s,this);
338 marker->setName(s->name());
338 marker->setName(s->name());
339 marker->setBrush(s->brush());
339 marker->setBrush(s->brush());
340 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
340 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
341 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
341 connect(s,SIGNAL(valueChanged()),marker,SLOT(changed()));
342 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
342 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
343 mMarkers.append(marker);
343 mMarkers.append(marker);
344 childItems().append(marker);
344 childItems().append(marker);
345 }
345 }
346 }
346 }
347
347
348 void QLegend::appendMarkers(QPieSeries *series)
348 void QLegend::appendMarkers(QPieSeries *series)
349 {
349 {
350 foreach(QPieSlice* s, series->slices()) {
350 foreach(QPieSlice* s, series->slices()) {
351 LegendMarker* marker = new LegendMarker(series,s,this);
351 LegendMarker* marker = new LegendMarker(series,s,this);
352 marker->setName(s->label());
352 marker->setName(s->label());
353 marker->setBrush(s->sliceBrush());
353 marker->setBrush(s->sliceBrush());
354 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
354 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
355 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
355 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
356 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
356 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
357 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
357 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
358 mMarkers.append(marker);
358 mMarkers.append(marker);
359 childItems().append(marker);
359 childItems().append(marker);
360 }
360 }
361 }
361 }
362
362
363 void QLegend::deleteMarkers(QSeries *series)
363 void QLegend::deleteMarkers(QSeries *series)
364 {
364 {
365 // Search all markers that belong to given series and delete them.
365 // Search all markers that belong to given series and delete them.
366 foreach (LegendMarker *m, mMarkers) {
366 foreach (LegendMarker *m, mMarkers) {
367 if (m->series() == series) {
367 if (m->series() == series) {
368 mMarkers.removeOne(m);
368 mMarkers.removeOne(m);
369 delete m;
369 delete m;
370 }
370 }
371 }
371 }
372 }
372 }
373
373
374 void QLegend::layoutChanged()
374 void QLegend::layoutChanged()
375 {
375 {
376 // Calculate layout for markers and text
376 // Calculate layout for markers and text
377 if (mMarkers.count() <= 0) {
377 if (mMarkers.count() <= 0) {
378 // Nothing to do
378 // Nothing to do
379 return;
379 return;
380 }
380 }
381
381
382 // Find out widest item.
382 // Find out widest item.
383 qreal itemMaxWidth = 0;
383 qreal itemMaxWidth = 0;
384 qreal itemMaxHeight = 0;
384 qreal itemMaxHeight = 0;
385 foreach (LegendMarker* m, mMarkers) {
385 foreach (LegendMarker* m, mMarkers) {
386 if (m->boundingRect().width() > itemMaxWidth) {
386 if (m->boundingRect().width() > itemMaxWidth) {
387 itemMaxWidth = m->boundingRect().width();
387 itemMaxWidth = m->boundingRect().width();
388 }
388 }
389 if (m->boundingRect().height() > itemMaxHeight) {
389 if (m->boundingRect().height() > itemMaxHeight) {
390 itemMaxHeight = m->boundingRect().height();
390 itemMaxHeight = m->boundingRect().height();
391 }
391 }
392 }
392 }
393
393
394 int maxHorizontalItems = boundingRect().width() / itemMaxWidth;
394 int maxHorizontalItems = boundingRect().width() / itemMaxWidth;
395 int maxVerticalItems = boundingRect().height() / itemMaxHeight;
395 int maxVerticalItems = boundingRect().height() / itemMaxHeight;
396
396
397 if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) {
397 if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) {
398 // TODO: overlapping layout
398 // TODO: overlapping layout
399 qDebug() << "Warning. Not enough space to layout all legend items properly.";
399 //qDebug() << "Warning. Not enough space to layout all legend items properly.";
400 }
400 }
401
401
402 qreal margin = 5;
402 qreal margin = 5;
403 qreal totalWidth = 0;
403 qreal totalWidth = 0;
404 qreal totalHeight = 0;
404 qreal totalHeight = 0;
405 switch (mPreferredLayout)
405 switch (mPreferredLayout)
406 {
406 {
407 case QLegend::PreferredLayoutHorizontal: {
407 case QLegend::PreferredLayoutHorizontal: {
408 /*
408 /*
409 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
409 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
410 if (xStep > itemMaxWidth) {
410 if (xStep > itemMaxWidth) {
411 xStep = itemMaxWidth;
411 xStep = itemMaxWidth;
412 }
412 }
413 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
413 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
414 if (yStep > itemMaxHeight) {
414 if (yStep > itemMaxHeight) {
415 yStep = itemMaxHeight;
415 yStep = itemMaxHeight;
416 }*/
416 }*/
417 qreal xStep = itemMaxWidth;
417 qreal xStep = itemMaxWidth;
418 qreal yStep = itemMaxHeight;
418 qreal yStep = itemMaxHeight;
419 qreal x = mPos.x() + margin;
419 qreal x = mPos.x() + margin;
420 qreal y = mPos.y() + margin;
420 qreal y = mPos.y() + margin;
421 int row = 1;
421 int row = 1;
422 int column = 0;
422 int column = 0;
423 int maxRows = 1;
423 int maxRows = 1;
424 int maxColumns = 1;
424 int maxColumns = 1;
425 foreach (LegendMarker* m, mMarkers) {
425 foreach (LegendMarker* m, mMarkers) {
426 maxRows = row;
426 maxRows = row;
427 m->setPos(x,y);
427 m->setPos(x,y);
428 x += xStep;
428 x += xStep;
429 column++;
429 column++;
430 if (column > maxColumns) {
430 if (column > maxColumns) {
431 maxColumns = column;
431 maxColumns = column;
432 }
432 }
433 if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) {
433 if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) {
434 x = mPos.x() + margin;
434 x = mPos.x() + margin;
435 y += yStep;
435 y += yStep;
436 row++;
436 row++;
437 column = 0;
437 column = 0;
438 }
438 }
439 }
439 }
440 totalWidth = maxColumns * itemMaxWidth + margin * 2;
440 totalWidth = maxColumns * itemMaxWidth + margin * 2;
441 totalHeight = maxRows * itemMaxHeight + margin * 2;
441 totalHeight = maxRows * itemMaxHeight + margin * 2;
442 break;
442 break;
443 }
443 }
444 case QLegend::PreferredLayoutVertical: {
444 case QLegend::PreferredLayoutVertical: {
445 /*
445 /*
446 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
446 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
447 if (xStep > itemMaxWidth) {
447 if (xStep > itemMaxWidth) {
448 xStep = itemMaxWidth;
448 xStep = itemMaxWidth;
449 }
449 }
450 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
450 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
451 if (yStep > itemMaxHeight) {
451 if (yStep > itemMaxHeight) {
452 yStep = itemMaxHeight;
452 yStep = itemMaxHeight;
453 }*/
453 }*/
454 qreal xStep = itemMaxWidth;
454 qreal xStep = itemMaxWidth;
455 qreal yStep = itemMaxHeight;
455 qreal yStep = itemMaxHeight;
456 qreal x = mPos.x() + margin;
456 qreal x = mPos.x() + margin;
457 qreal y = mPos.y() + margin;
457 qreal y = mPos.y() + margin;
458 int row = 0;
458 int row = 0;
459 int column = 1;
459 int column = 1;
460 int maxRows = 1;
460 int maxRows = 1;
461 int maxColumns = 1;
461 int maxColumns = 1;
462 foreach (LegendMarker* m, mMarkers) {
462 foreach (LegendMarker* m, mMarkers) {
463 maxColumns = column;
463 maxColumns = column;
464 m->setPos(x,y);
464 m->setPos(x,y);
465 y += yStep;
465 y += yStep;
466 row++;
466 row++;
467 if (row > maxRows) {
467 if (row > maxRows) {
468 maxRows = row;
468 maxRows = row;
469 }
469 }
470 if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) {
470 if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) {
471 y = mPos.y() + margin;
471 y = mPos.y() + margin;
472 x += xStep;
472 x += xStep;
473 column++;
473 column++;
474 row = 0;
474 row = 0;
475 }
475 }
476 }
476 }
477 totalWidth = maxColumns * itemMaxWidth + margin * 2;
477 totalWidth = maxColumns * itemMaxWidth + margin * 2;
478 totalHeight = maxRows * itemMaxHeight + margin * 2;
478 totalHeight = maxRows * itemMaxHeight + margin * 2;
479 break;
479 break;
480 }
480 }
481 default: {
481 default: {
482 break;
482 break;
483 }
483 }
484 }
484 }
485
485
486 mSize.setWidth(totalWidth);
486 mSize.setWidth(totalWidth);
487 mSize.setHeight(totalHeight);
487 mSize.setHeight(totalHeight);
488 }
488 }
489
489
490 #include "moc_qlegend.cpp"
490 #include "moc_qlegend.cpp"
491 QTCOMMERCIALCHART_END_NAMESPACE
491 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now