##// END OF EJS Templates
Warnings treated as errors; fixed one
Tero Ahola -
r647:adb69112ecad
parent child
Show More
@@ -1,491 +1,492
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 }
93 }
94
94
95 void QLegend::setSize(const QSizeF size)
95 void QLegend::setSize(const QSizeF size)
96 {
96 {
97 mSize = size;
97 mSize = size;
98 if (mSize.width() > mMaximumSize.width()) {
98 if (mSize.width() > mMaximumSize.width()) {
99 mSize.setWidth(mMaximumSize.width());
99 mSize.setWidth(mMaximumSize.width());
100 }
100 }
101 if (mSize.height() > mMaximumSize.height()) {
101 if (mSize.height() > mMaximumSize.height()) {
102 mSize.setHeight(mMaximumSize.height());
102 mSize.setHeight(mMaximumSize.height());
103 }
103 }
104 }
104 }
105
105
106 void QLegend::setPos(const QPointF &pos)
106 void QLegend::setPos(const QPointF &pos)
107 {
107 {
108 mPos = pos;
108 mPos = pos;
109 }
109 }
110
110
111 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
111 void QLegend::handleSeriesAdded(QSeries* series, Domain* domain)
112 {
112 {
113 Q_UNUSED(domain)
113 Q_UNUSED(domain)
114
114
115 // mSeriesList.append(series);
115 // mSeriesList.append(series);
116 createMarkers(series);
116 createMarkers(series);
117 connectSeries(series);
117 connectSeries(series);
118 layoutChanged();
118 layoutChanged();
119 }
119 }
120
120
121 void QLegend::handleSeriesRemoved(QSeries* series)
121 void QLegend::handleSeriesRemoved(QSeries* series)
122 {
122 {
123 disconnectSeries(series);
123 disconnectSeries(series);
124
124
125 if (series->type() == QSeries::SeriesTypeArea)
125 if (series->type() == QSeries::SeriesTypeArea)
126 {
126 {
127 // This is special case. Area series has upper and lower series, which each have markers
127 // This is special case. Area series has upper and lower series, which each have markers
128 QAreaSeries* s = static_cast<QAreaSeries*> (series);
128 QAreaSeries* s = static_cast<QAreaSeries*> (series);
129 deleteMarkers(s->upperSeries());
129 deleteMarkers(s->upperSeries());
130 deleteMarkers(s->lowerSeries());
130 deleteMarkers(s->lowerSeries());
131 } else {
131 } else {
132 deleteMarkers(series);
132 deleteMarkers(series);
133 }
133 }
134
134
135 // mSeriesList.removeOne(series);
135 // mSeriesList.removeOne(series);
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 // 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
159 // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count();
160 // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count();
160 }
161 }
161
162
162
163
163 void QLegend::handleMarkerDestroyed()
164 void QLegend::handleMarkerDestroyed()
164 {
165 {
165 // 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?
166 LegendMarker* m = static_cast<LegendMarker*> (sender());
167 LegendMarker* m = static_cast<LegendMarker*> (sender());
167 mMarkers.removeOne(m);
168 mMarkers.removeOne(m);
168 layoutChanged();
169 layoutChanged();
169 }
170 }
170
171
171 void QLegend::connectSeries(QSeries *series)
172 void QLegend::connectSeries(QSeries *series)
172 {
173 {
173 // Connect relevant signals from series
174 // Connect relevant signals from series
174 switch (series->type())
175 switch (series->type())
175 {
176 {
176 case QSeries::SeriesTypeLine: {
177 case QSeries::SeriesTypeLine: {
177 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
178 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
178 break;
179 break;
179 }
180 }
180 case QSeries::SeriesTypeArea: {
181 case QSeries::SeriesTypeArea: {
181 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
182 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
182 break;
183 break;
183 }
184 }
184 case QSeries::SeriesTypeBar: {
185 case QSeries::SeriesTypeBar: {
185 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
186 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
186 break;
187 break;
187 }
188 }
188 case QSeries::SeriesTypeStackedBar: {
189 case QSeries::SeriesTypeStackedBar: {
189 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
190 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
190 break;
191 break;
191 }
192 }
192 case QSeries::SeriesTypePercentBar: {
193 case QSeries::SeriesTypePercentBar: {
193 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
194 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
194 break;
195 break;
195 }
196 }
196 case QSeries::SeriesTypeScatter: {
197 case QSeries::SeriesTypeScatter: {
197 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
198 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
198 break;
199 break;
199 }
200 }
200 case QSeries::SeriesTypePie: {
201 case QSeries::SeriesTypePie: {
201 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
202 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
202 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
203 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
203 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
204 // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
204 break;
205 break;
205 }
206 }
206 case QSeries::SeriesTypeSpline: {
207 case QSeries::SeriesTypeSpline: {
207 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
208 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
208 break;
209 break;
209 }
210 }
210 default: {
211 default: {
211 qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented.";
212 qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented.";
212 break;
213 break;
213 }
214 }
214 }
215 }
215 }
216 }
216
217
217 void QLegend::disconnectSeries(QSeries *series)
218 void QLegend::disconnectSeries(QSeries *series)
218 {
219 {
219 // Connect relevant signals from series
220 // Connect relevant signals from series
220 switch (series->type())
221 switch (series->type())
221 {
222 {
222 case QSeries::SeriesTypeLine: {
223 case QSeries::SeriesTypeLine: {
223 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
224 // QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
224 break;
225 break;
225 }
226 }
226 case QSeries::SeriesTypeArea: {
227 case QSeries::SeriesTypeArea: {
227 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
228 // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
228 break;
229 break;
229 }
230 }
230 case QSeries::SeriesTypeBar: {
231 case QSeries::SeriesTypeBar: {
231 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
232 // QBarSeries* barSeries = static_cast<QBarSeries*>(series);
232 break;
233 break;
233 }
234 }
234 case QSeries::SeriesTypeStackedBar: {
235 case QSeries::SeriesTypeStackedBar: {
235 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
236 // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
236 break;
237 break;
237 }
238 }
238 case QSeries::SeriesTypePercentBar: {
239 case QSeries::SeriesTypePercentBar: {
239 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
240 // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
240 break;
241 break;
241 }
242 }
242 case QSeries::SeriesTypeScatter: {
243 case QSeries::SeriesTypeScatter: {
243 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
244 // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
244 break;
245 break;
245 }
246 }
246 case QSeries::SeriesTypePie: {
247 case QSeries::SeriesTypePie: {
247 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
248 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
248 disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
249 disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
249 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
250 // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>)));
250 break;
251 break;
251 }
252 }
252 case QSeries::SeriesTypeSpline: {
253 case QSeries::SeriesTypeSpline: {
253 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
254 // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
254 break;
255 break;
255 }
256 }
256 default: {
257 default: {
257 qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
258 qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented.";
258 break;
259 break;
259 }
260 }
260 }
261 }
261 }
262 }
262
263
263 void QLegend::createMarkers(QSeries *series)
264 void QLegend::createMarkers(QSeries *series)
264 {
265 {
265 switch (series->type())
266 switch (series->type())
266 {
267 {
267 case QSeries::SeriesTypeLine: {
268 case QSeries::SeriesTypeLine: {
268 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
269 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
269 appendMarkers(lineSeries);
270 appendMarkers(lineSeries);
270 break;
271 break;
271 }
272 }
272 case QSeries::SeriesTypeArea: {
273 case QSeries::SeriesTypeArea: {
273 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
274 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
274 appendMarkers(areaSeries->upperSeries());
275 appendMarkers(areaSeries->upperSeries());
275 if(areaSeries->lowerSeries())
276 if(areaSeries->lowerSeries())
276 appendMarkers(areaSeries->lowerSeries());
277 appendMarkers(areaSeries->lowerSeries());
277 break;
278 break;
278 }
279 }
279
280
280 case QSeries::SeriesTypeBar: {
281 case QSeries::SeriesTypeBar: {
281 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
282 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
282 appendMarkers(barSeries);
283 appendMarkers(barSeries);
283 break;
284 break;
284 }
285 }
285
286
286 case QSeries::SeriesTypeStackedBar: {
287 case QSeries::SeriesTypeStackedBar: {
287 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
288 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
288 appendMarkers(stackedBarSeries);
289 appendMarkers(stackedBarSeries);
289 break;
290 break;
290 }
291 }
291
292
292 case QSeries::SeriesTypePercentBar: {
293 case QSeries::SeriesTypePercentBar: {
293 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
294 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
294 appendMarkers(percentBarSeries);
295 appendMarkers(percentBarSeries);
295 break;
296 break;
296 }
297 }
297
298
298 case QSeries::SeriesTypeScatter: {
299 case QSeries::SeriesTypeScatter: {
299 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
300 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
300 appendMarkers(scatterSeries);
301 appendMarkers(scatterSeries);
301 break;
302 break;
302 }
303 }
303
304
304 case QSeries::SeriesTypePie: {
305 case QSeries::SeriesTypePie: {
305 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
306 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
306 appendMarkers(pieSeries);
307 appendMarkers(pieSeries);
307 break;
308 break;
308 }
309 }
309
310
310 case QSeries::SeriesTypeSpline: {
311 case QSeries::SeriesTypeSpline: {
311 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
312 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
312 appendMarkers(splineSeries);
313 appendMarkers(splineSeries);
313 break;
314 break;
314 }
315 }
315 default: {
316 default: {
316 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
317 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
317 break;
318 break;
318 }
319 }
319 }
320 }
320 }
321 }
321
322
322 void QLegend::appendMarkers(QXYSeries* series)
323 void QLegend::appendMarkers(QXYSeries* series)
323 {
324 {
324 LegendMarker* marker = new LegendMarker(series,this);
325 LegendMarker* marker = new LegendMarker(series,this);
325 marker->setName(series->name());
326 marker->setName(series->name());
326 marker->setBrush(series->brush());
327 marker->setBrush(series->brush());
327 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)));
328 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
329 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
329 mMarkers.append(marker);
330 mMarkers.append(marker);
330 childItems().append(marker);
331 childItems().append(marker);
331 }
332 }
332
333
333 void QLegend::appendMarkers(QBarSeries *series)
334 void QLegend::appendMarkers(QBarSeries *series)
334 {
335 {
335 foreach(QBarSet* s, series->barSets()) {
336 foreach(QBarSet* s, series->barSets()) {
336 LegendMarker* marker = new LegendMarker(series,s,this);
337 LegendMarker* marker = new LegendMarker(series,s,this);
337 marker->setName(s->name());
338 marker->setName(s->name());
338 marker->setBrush(s->brush());
339 marker->setBrush(s->brush());
339 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)));
340 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
341 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
341 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
342 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
342 mMarkers.append(marker);
343 mMarkers.append(marker);
343 childItems().append(marker);
344 childItems().append(marker);
344 }
345 }
345 }
346 }
346
347
347 void QLegend::appendMarkers(QPieSeries *series)
348 void QLegend::appendMarkers(QPieSeries *series)
348 {
349 {
349 foreach(QPieSlice* s, series->slices()) {
350 foreach(QPieSlice* s, series->slices()) {
350 LegendMarker* marker = new LegendMarker(series,s,this);
351 LegendMarker* marker = new LegendMarker(series,s,this);
351 marker->setName(s->label());
352 marker->setName(s->label());
352 marker->setBrush(s->sliceBrush());
353 marker->setBrush(s->sliceBrush());
353 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)));
354 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
355 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
355 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
356 connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater()));
356 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
357 connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed()));
357 mMarkers.append(marker);
358 mMarkers.append(marker);
358 childItems().append(marker);
359 childItems().append(marker);
359 }
360 }
360 }
361 }
361
362
362 void QLegend::deleteMarkers(QSeries *series)
363 void QLegend::deleteMarkers(QSeries *series)
363 {
364 {
364 // Search all markers that belong to given series and delete them.
365 // Search all markers that belong to given series and delete them.
365 foreach (LegendMarker *m, mMarkers) {
366 foreach (LegendMarker *m, mMarkers) {
366 if (m->series() == series) {
367 if (m->series() == series) {
367 mMarkers.removeOne(m);
368 mMarkers.removeOne(m);
368 delete m;
369 delete m;
369 }
370 }
370 }
371 }
371 }
372 }
372
373
373 void QLegend::layoutChanged()
374 void QLegend::layoutChanged()
374 {
375 {
375 // Calculate layout for markers and text
376 // Calculate layout for markers and text
376 qDebug() << "Marker count:" << mMarkers.count();
377 qDebug() << "Marker count:" << mMarkers.count();
377 if (mMarkers.count() <= 0) {
378 if (mMarkers.count() <= 0) {
378 // Nothing to do
379 // Nothing to do
379 return;
380 return;
380 }
381 }
381
382
382 // Find out widest item.
383 // Find out widest item.
383 qreal itemMaxWidth = 0;
384 qreal itemMaxWidth = 0;
384 qreal itemMaxHeight = 0;
385 qreal itemMaxHeight = 0;
385 foreach (LegendMarker* m, mMarkers) {
386 foreach (LegendMarker* m, mMarkers) {
386 if (m->boundingRect().width() > itemMaxWidth) {
387 if (m->boundingRect().width() > itemMaxWidth) {
387 itemMaxWidth = m->boundingRect().width();
388 itemMaxWidth = m->boundingRect().width();
388 }
389 }
389 if (m->boundingRect().height() > itemMaxHeight) {
390 if (m->boundingRect().height() > itemMaxHeight) {
390 itemMaxHeight = m->boundingRect().height();
391 itemMaxHeight = m->boundingRect().height();
391 }
392 }
392 }
393 }
393
394
394 int maxHorizontalItems = boundingRect().width() / itemMaxWidth;
395 int maxHorizontalItems = boundingRect().width() / itemMaxWidth;
395 int maxVerticalItems = boundingRect().height() / itemMaxHeight;
396 int maxVerticalItems = boundingRect().height() / itemMaxHeight;
396
397
397 if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) {
398 if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) {
398 // TODO: overlapping layout
399 // TODO: overlapping layout
399 qDebug() << "Warning. Not enough space to layout all legend items properly.";
400 qDebug() << "Warning. Not enough space to layout all legend items properly.";
400 }
401 }
401
402
402 qreal margin = 5;
403 qreal margin = 5;
403 qreal totalWidth = 0;
404 qreal totalWidth = 0;
404 qreal totalHeight = 0;
405 qreal totalHeight = 0;
405 switch (mPreferredLayout)
406 switch (mPreferredLayout)
406 {
407 {
407 case QLegend::PreferredLayoutHorizontal: {
408 case QLegend::PreferredLayoutHorizontal: {
408 /*
409 /*
409 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
410 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
410 if (xStep > itemMaxWidth) {
411 if (xStep > itemMaxWidth) {
411 xStep = itemMaxWidth;
412 xStep = itemMaxWidth;
412 }
413 }
413 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
414 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
414 if (yStep > itemMaxHeight) {
415 if (yStep > itemMaxHeight) {
415 yStep = itemMaxHeight;
416 yStep = itemMaxHeight;
416 }*/
417 }*/
417 qreal xStep = itemMaxWidth;
418 qreal xStep = itemMaxWidth;
418 qreal yStep = itemMaxHeight;
419 qreal yStep = itemMaxHeight;
419 qreal x = mPos.x() + margin;
420 qreal x = mPos.x() + margin;
420 qreal y = mPos.y() + margin;
421 qreal y = mPos.y() + margin;
421 int row = 1;
422 int row = 1;
422 int column = 0;
423 int column = 0;
423 int maxRows = 1;
424 int maxRows = 1;
424 int maxColumns = 1;
425 int maxColumns = 1;
425 foreach (LegendMarker* m, mMarkers) {
426 foreach (LegendMarker* m, mMarkers) {
426 maxRows = row;
427 maxRows = row;
427 m->setPos(x,y);
428 m->setPos(x,y);
428 x += xStep;
429 x += xStep;
429 column++;
430 column++;
430 if (column > maxColumns) {
431 if (column > maxColumns) {
431 maxColumns = column;
432 maxColumns = column;
432 }
433 }
433 if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) {
434 if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) {
434 x = mPos.x() + margin;
435 x = mPos.x() + margin;
435 y += yStep;
436 y += yStep;
436 row++;
437 row++;
437 column = 0;
438 column = 0;
438 }
439 }
439 }
440 }
440 totalWidth = maxColumns * itemMaxWidth + margin * 2;
441 totalWidth = maxColumns * itemMaxWidth + margin * 2;
441 totalHeight = maxRows * itemMaxHeight + margin * 2;
442 totalHeight = maxRows * itemMaxHeight + margin * 2;
442 break;
443 break;
443 }
444 }
444 case QLegend::PreferredLayoutVertical: {
445 case QLegend::PreferredLayoutVertical: {
445 /*
446 /*
446 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
447 qreal xStep = mMaximumSize.width() / (mMarkers.count()+1);
447 if (xStep > itemMaxWidth) {
448 if (xStep > itemMaxWidth) {
448 xStep = itemMaxWidth;
449 xStep = itemMaxWidth;
449 }
450 }
450 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
451 qreal yStep = mMaximumSize.height() / (mMarkers.count()+1);
451 if (yStep > itemMaxHeight) {
452 if (yStep > itemMaxHeight) {
452 yStep = itemMaxHeight;
453 yStep = itemMaxHeight;
453 }*/
454 }*/
454 qreal xStep = itemMaxWidth;
455 qreal xStep = itemMaxWidth;
455 qreal yStep = itemMaxHeight;
456 qreal yStep = itemMaxHeight;
456 qreal x = mPos.x() + margin;
457 qreal x = mPos.x() + margin;
457 qreal y = mPos.y() + margin;
458 qreal y = mPos.y() + margin;
458 int row = 0;
459 int row = 0;
459 int column = 1;
460 int column = 1;
460 int maxRows = 1;
461 int maxRows = 1;
461 int maxColumns = 1;
462 int maxColumns = 1;
462 foreach (LegendMarker* m, mMarkers) {
463 foreach (LegendMarker* m, mMarkers) {
463 maxColumns = column;
464 maxColumns = column;
464 m->setPos(x,y);
465 m->setPos(x,y);
465 y += yStep;
466 y += yStep;
466 row++;
467 row++;
467 if (row > maxRows) {
468 if (row > maxRows) {
468 maxRows = row;
469 maxRows = row;
469 }
470 }
470 if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) {
471 if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) {
471 y = mPos.y() + margin;
472 y = mPos.y() + margin;
472 x += xStep;
473 x += xStep;
473 column++;
474 column++;
474 row = 0;
475 row = 0;
475 }
476 }
476 }
477 }
477 totalWidth = maxColumns * itemMaxWidth + margin * 2;
478 totalWidth = maxColumns * itemMaxWidth + margin * 2;
478 totalHeight = maxRows * itemMaxHeight + margin * 2;
479 totalHeight = maxRows * itemMaxHeight + margin * 2;
479 break;
480 break;
480 }
481 }
481 default: {
482 default: {
482 break;
483 break;
483 }
484 }
484 }
485 }
485
486
486 mSize.setWidth(totalWidth);
487 mSize.setWidth(totalWidth);
487 mSize.setHeight(totalHeight);
488 mSize.setHeight(totalHeight);
488 }
489 }
489
490
490 #include "moc_qlegend.cpp"
491 #include "moc_qlegend.cpp"
491 QTCOMMERCIALCHART_END_NAMESPACE
492 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now