##// END OF EJS Templates
legend refactoring.
sauimone -
r792:f33d64d30a1c
parent child
Show More
@@ -185,14 +185,13 QChart* ThemeWidget::createAreaChart() const
185 for (int i(0); i < m_dataTable.count(); i++) {
185 for (int i(0); i < m_dataTable.count(); i++) {
186 QLineSeries *series1 = new QLineSeries(chart);
186 QLineSeries *series1 = new QLineSeries(chart);
187 QLineSeries *series2 = new QLineSeries(chart);
187 QLineSeries *series2 = new QLineSeries(chart);
188 series1->setName(name + QString::number(nameIndex) + QString(" lo"));
189 series2->setName(name + QString::number(nameIndex) + QString(" hi"));
190 nameIndex++;
191 foreach (Data data, m_dataTable[i]) {
188 foreach (Data data, m_dataTable[i]) {
192 series1->add(data.first);
189 series1->add(data.first);
193 series2->add(QPointF(data.first.x(), 0.0));
190 series2->add(QPointF(data.first.x(), 0.0));
194 }
191 }
195 QAreaSeries *area = new QAreaSeries(series1, series2);
192 QAreaSeries *area = new QAreaSeries(series1, series2);
193 area->setName(name + QString::number(nameIndex));
194 nameIndex++;
196 chart->addSeries(area);
195 chart->addSeries(area);
197 }
196 }
198 }
197 }
@@ -58,7 +58,6 void ChartPresenter::createConnections()
58
58
59 void ChartPresenter::handleGeometryChanged()
59 void ChartPresenter::handleGeometryChanged()
60 {
60 {
61 qDebug() << "legend h:" << m_chart->legend()->size().height();
62 QRectF rect(QPoint(0,0),m_chart->size());
61 QRectF rect(QPoint(0,0),m_chart->size());
63 rect.adjust(m_padding,
62 rect.adjust(m_padding,
64 m_padding + m_chart->legend()->size().height(),
63 m_padding + m_chart->legend()->size().height(),
@@ -3,6 +3,7
3 #include <qpieslice.h>
3 #include <qpieslice.h>
4 #include <qbarset.h>
4 #include <qbarset.h>
5 #include <qxyseries.h>
5 #include <qxyseries.h>
6 #include <qareaseries.h>
6 #include <QPainter>
7 #include <QPainter>
7 #include <QGraphicsSceneEvent>
8 #include <QGraphicsSceneEvent>
8 #include <QGraphicsSimpleTextItem>
9 #include <QGraphicsSimpleTextItem>
@@ -17,7 +18,6 LegendMarker::LegendMarker(QSeries *series, QGraphicsItem *parent) : QGraphicsOb
17 m_series(series),
18 m_series(series),
18 m_barset(0),
19 m_barset(0),
19 m_pieslice(0),
20 m_pieslice(0),
20 m_type(LegendMarkerTypeSeries),
21 m_textItem(new QGraphicsSimpleTextItem(this))
21 m_textItem(new QGraphicsSimpleTextItem(this))
22 {
22 {
23 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
23 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
@@ -31,7 +31,6 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *pare
31 m_series(series),
31 m_series(series),
32 m_barset(barset),
32 m_barset(barset),
33 m_pieslice(0),
33 m_pieslice(0),
34 m_type(LegendMarkerTypeBarset),
35 m_textItem(new QGraphicsSimpleTextItem(this))
34 m_textItem(new QGraphicsSimpleTextItem(this))
36 {
35 {
37 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
36 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
@@ -45,7 +44,6 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *
45 m_series(series),
44 m_series(series),
46 m_barset(0),
45 m_barset(0),
47 m_pieslice(pieslice),
46 m_pieslice(pieslice),
48 m_type(LegendMarkerTypePieslice),
49 m_textItem(new QGraphicsSimpleTextItem(this))
47 m_textItem(new QGraphicsSimpleTextItem(this))
50 {
48 {
51 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
49 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
@@ -125,17 +123,21 void LegendMarker::layoutChanged()
125
123
126 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
124 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
127 {
125 {
128 switch (m_type)
126 switch (m_series->type()) {
129 {
127 case QSeries::SeriesTypeLine:
130 case LegendMarkerTypeSeries: {
128 case QSeries::SeriesTypeArea:
129 case QSeries::SeriesTypeScatter:
130 case QSeries::SeriesTypeSpline: {
131 emit clicked(m_series,event->button());
131 emit clicked(m_series,event->button());
132 break;
132 break;
133 }
133 }
134 case LegendMarkerTypeBarset: {
134 case QSeries::SeriesTypeBar:
135 case QSeries::SeriesTypeStackedBar:
136 case QSeries::SeriesTypePercentBar: {
135 emit clicked(m_barset,event->button());
137 emit clicked(m_barset,event->button());
136 break;
138 break;
137 }
139 }
138 case LegendMarkerTypePieslice: {
140 case QSeries::SeriesTypePie: {
139 emit clicked(m_pieslice,event->button());
141 emit clicked(m_pieslice,event->button());
140 break;
142 break;
141 }
143 }
@@ -147,26 +149,39 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
147
149
148 void LegendMarker::changed()
150 void LegendMarker::changed()
149 {
151 {
150 switch (m_type)
152 switch (m_series->type()) {
151 {
153 case QSeries::SeriesTypeArea: {
152 case LegendMarkerTypeSeries: {
154 QAreaSeries* s = static_cast<QAreaSeries*> (m_series);
153 QXYSeries* s = static_cast<QXYSeries*> (m_series);
154 setPen(s->pen());
155 setPen(s->pen());
155 setBrush(s->brush());
156 setBrush(s->brush());
156 setName(s->name());
157 setName(s->name());
157 break;
158 break;
158 }
159 }
159 case LegendMarkerTypeBarset: {
160 case QSeries::SeriesTypeLine:
161 case QSeries::SeriesTypeScatter:
162 case QSeries::SeriesTypeSpline: {
163 QXYSeries* s = static_cast<QXYSeries*> (m_series);
164 setPen(s->pen());
165 setBrush(s->brush());
166 setName(s->name());
167 break;
168 }
169 case QSeries::SeriesTypeBar:
170 case QSeries::SeriesTypeStackedBar:
171 case QSeries::SeriesTypePercentBar: {
160 setPen(m_barset->pen());
172 setPen(m_barset->pen());
161 setBrush(m_barset->brush());
173 setBrush(m_barset->brush());
162 setName(m_barset->name());
174 setName(m_barset->name());
163 break;
175 break;
164 }
176 }
165 case LegendMarkerTypePieslice: {
177 case QSeries::SeriesTypePie: {
166 setBrush(m_pieslice->brush());
178 setBrush(m_pieslice->brush());
167 setName(m_pieslice->label());
179 setName(m_pieslice->label());
168 break;
180 break;
169 }
181 }
182 default: {
183 break;
184 }
170 }
185 }
171 }
186 }
172
187
@@ -18,12 +18,6 class LegendMarker : public QGraphicsObject
18 {
18 {
19 Q_OBJECT
19 Q_OBJECT
20
20
21 enum LegendMarkerType {
22 LegendMarkerTypeSeries,
23 LegendMarkerTypeBarset,
24 LegendMarkerTypePieslice
25 };
26
27 public:
21 public:
28 LegendMarker(QSeries *series, QGraphicsItem *parent = 0);
22 LegendMarker(QSeries *series, QGraphicsItem *parent = 0);
29 LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent = 0);
23 LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent = 0);
@@ -72,7 +66,6 private:
72 QBarSet *m_barset;
66 QBarSet *m_barset;
73 QPieSlice *m_pieslice;
67 QPieSlice *m_pieslice;
74
68
75 LegendMarkerType m_type;
76 QGraphicsSimpleTextItem *m_textItem;
69 QGraphicsSimpleTextItem *m_textItem;
77
70
78 };
71 };
@@ -219,8 +219,55 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
219 {
219 {
220 Q_UNUSED(domain)
220 Q_UNUSED(domain)
221
221
222 createMarkers(series);
222 switch (series->type())
223 connectSeries(series);
223 {
224 case QSeries::SeriesTypeLine: {
225 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
226 appendMarkers(lineSeries);
227 break;
228 }
229 case QSeries::SeriesTypeArea: {
230 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
231 appendMarkers(areaSeries);
232 break;
233 }
234 case QSeries::SeriesTypeBar: {
235 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
236 appendMarkers(barSeries);
237 break;
238 }
239 case QSeries::SeriesTypeStackedBar: {
240 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
241 appendMarkers(stackedBarSeries);
242 break;
243 }
244 case QSeries::SeriesTypePercentBar: {
245 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
246 appendMarkers(percentBarSeries);
247 break;
248 }
249 case QSeries::SeriesTypeScatter: {
250 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
251 appendMarkers(scatterSeries);
252 break;
253 }
254 case QSeries::SeriesTypePie: {
255 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
256 appendMarkers(pieSeries);
257 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
258 break;
259 }
260 case QSeries::SeriesTypeSpline: {
261 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
262 appendMarkers(splineSeries);
263 break;
264 }
265 default: {
266 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
267 break;
268 }
269 }
270
224 updateLayout();
271 updateLayout();
225 }
272 }
226
273
@@ -229,15 +276,24 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
229 */
276 */
230 void QLegend::handleSeriesRemoved(QSeries *series)
277 void QLegend::handleSeriesRemoved(QSeries *series)
231 {
278 {
232 disconnectSeries(series);
279 switch (series->type())
233
280 {
234 if (series->type() == QSeries::SeriesTypeArea) {
281 case QSeries::SeriesTypeArea: {
235 // This is special case. Area series has upper and lower series, which each have markers
282 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
236 QAreaSeries* s = static_cast<QAreaSeries *> (series);
283 deleteMarkers(areaSeries);
237 deleteMarkers(s->upperSeries());
284 break;
238 deleteMarkers(s->lowerSeries());
285 }
239 } else {
286 case QSeries::SeriesTypePie: {
287 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
288 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
240 deleteMarkers(series);
289 deleteMarkers(series);
290 break;
291 }
292 default: {
293 // All other types
294 deleteMarkers(series);
295 break;
296 }
241 }
297 }
242
298
243 updateLayout();
299 updateLayout();
@@ -313,86 +369,19 void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton)
313 }
369 }
314
370
315 /*!
371 /*!
316 \internal Connects the \a series to legend. Legend listens changes in series, for example pie slices added / removed.
372 \internal Helper function. Appends markers from \a series to legend.
317 Not all series notify about events
318 */
319 void QLegend::connectSeries(QSeries *series)
320 {
321 // Connect relevant signals from series. Currently only pie series has interesting signals
322 // TODO: bar chart may have
323 if (series->type() == QSeries::SeriesTypePie) {
324 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
325 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
326 }
327 }
328
329 /*!
330 \internal Disconnects \a series from legend. No more status updates from series to legend.
331 */
332 void QLegend::disconnectSeries(QSeries *series)
333 {
334 if (series->type() == QSeries::SeriesTypePie) {
335 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
336 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
337 }
338 }
339
340 /*!
341 \internal Creates new markers for \a series. Marker contains the colored rectangle and series name.
342 With pie chart, created markers depend on pie slices.
343 With bar chart, created markers depend on bar sets.
344 */
373 */
345 void QLegend::createMarkers(QSeries *series)
374 void QLegend::appendMarkers(QAreaSeries* series)
346 {
375 {
347 switch (series->type())
376 LegendMarker* marker = new LegendMarker(series,this);
348 {
377 marker->setName(series->name());
349 case QSeries::SeriesTypeLine: {
378 marker->setPen(series->pen());
350 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
379 marker->setBrush(series->brush());
351 appendMarkers(lineSeries);
380 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
352 break;
381 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
353 }
382 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
354 case QSeries::SeriesTypeArea: {
383 m_markers.append(marker);
355 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
384 childItems().append(marker);
356 appendMarkers(areaSeries->upperSeries());
357 if(areaSeries->lowerSeries())
358 appendMarkers(areaSeries->lowerSeries());
359 break;
360 }
361 case QSeries::SeriesTypeBar: {
362 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
363 appendMarkers(barSeries);
364 break;
365 }
366 case QSeries::SeriesTypeStackedBar: {
367 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
368 appendMarkers(stackedBarSeries);
369 break;
370 }
371 case QSeries::SeriesTypePercentBar: {
372 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
373 appendMarkers(percentBarSeries);
374 break;
375 }
376 case QSeries::SeriesTypeScatter: {
377 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
378 appendMarkers(scatterSeries);
379 break;
380 }
381 case QSeries::SeriesTypePie: {
382 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
383 appendMarkers(pieSeries);
384 break;
385 }
386 case QSeries::SeriesTypeSpline: {
387 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
388 appendMarkers(splineSeries);
389 break;
390 }
391 default: {
392 qWarning()<< "QLegend::createMarkers" << series->type() << "unknown series type.";
393 break;
394 }
395 }
396 }
385 }
397
386
398 /*!
387 /*!
@@ -406,12 +395,13 void QLegend::appendMarkers(QXYSeries* series)
406 marker->setBrush(series->brush());
395 marker->setBrush(series->brush());
407 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
396 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
408 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
397 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
398 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
409 m_markers.append(marker);
399 m_markers.append(marker);
410 childItems().append(marker);
400 childItems().append(marker);
411 }
401 }
412
402
413 /*!
403 /*!
414 \internal Helper function. Appends markers from \a series to legend.
404 \internal Helper function. Appends markers from \a series to legend.
415 */
405 */
416 void QLegend::appendMarkers(QBarSeries *series)
406 void QLegend::appendMarkers(QBarSeries *series)
417 {
407 {
@@ -430,7 +420,7 void QLegend::appendMarkers(QBarSeries *series)
430 }
420 }
431
421
432 /*!
422 /*!
433 \internal Helper function. Appends markers from \a series to legend.
423 \internal Helper function. Appends markers from \a series to legend.
434 */
424 */
435 void QLegend::appendMarkers(QPieSeries *series)
425 void QLegend::appendMarkers(QPieSeries *series)
436 {
426 {
@@ -15,6 +15,7 class QXYSeries;
15 class QBarSet;
15 class QBarSet;
16 class QBarSeries;
16 class QBarSeries;
17 class QPieSeries;
17 class QPieSeries;
18 class QAreaSeries;
18 class LegendScrollButton;
19 class LegendScrollButton;
19 class QSeries;
20 class QSeries;
20 class QChart;
21 class QChart;
@@ -75,10 +76,8 public Q_SLOTS:
75
76
76 private:
77 private:
77 // PIMPL --->
78 // PIMPL --->
78 void connectSeries(QSeries *series);
79 void appendMarkers(QAreaSeries *series);
79 void disconnectSeries(QSeries *series);
80 void appendMarkers(QXYSeries *series);
80 void createMarkers(QSeries *series);
81 void appendMarkers(QXYSeries *series); // All line series are derived from QXYSeries, so this works for now
82 void appendMarkers(QBarSeries *series);
81 void appendMarkers(QBarSeries *series);
83 void appendMarkers(QPieSeries *series);
82 void appendMarkers(QPieSeries *series);
84 void deleteMarkers(QSeries *series);
83 void deleteMarkers(QSeries *series);
General Comments 0
You need to be logged in to leave comments. Login now