##// END OF EJS Templates
legend refactoring.
sauimone -
r792:f33d64d30a1c
parent child
Show More
@@ -185,14 +185,13 QChart* ThemeWidget::createAreaChart() const
185 185 for (int i(0); i < m_dataTable.count(); i++) {
186 186 QLineSeries *series1 = new QLineSeries(chart);
187 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 188 foreach (Data data, m_dataTable[i]) {
192 189 series1->add(data.first);
193 190 series2->add(QPointF(data.first.x(), 0.0));
194 191 }
195 192 QAreaSeries *area = new QAreaSeries(series1, series2);
193 area->setName(name + QString::number(nameIndex));
194 nameIndex++;
196 195 chart->addSeries(area);
197 196 }
198 197 }
@@ -58,7 +58,6 void ChartPresenter::createConnections()
58 58
59 59 void ChartPresenter::handleGeometryChanged()
60 60 {
61 qDebug() << "legend h:" << m_chart->legend()->size().height();
62 61 QRectF rect(QPoint(0,0),m_chart->size());
63 62 rect.adjust(m_padding,
64 63 m_padding + m_chart->legend()->size().height(),
@@ -3,6 +3,7
3 3 #include <qpieslice.h>
4 4 #include <qbarset.h>
5 5 #include <qxyseries.h>
6 #include <qareaseries.h>
6 7 #include <QPainter>
7 8 #include <QGraphicsSceneEvent>
8 9 #include <QGraphicsSimpleTextItem>
@@ -17,7 +18,6 LegendMarker::LegendMarker(QSeries *series, QGraphicsItem *parent) : QGraphicsOb
17 18 m_series(series),
18 19 m_barset(0),
19 20 m_pieslice(0),
20 m_type(LegendMarkerTypeSeries),
21 21 m_textItem(new QGraphicsSimpleTextItem(this))
22 22 {
23 23 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
@@ -31,7 +31,6 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *pare
31 31 m_series(series),
32 32 m_barset(barset),
33 33 m_pieslice(0),
34 m_type(LegendMarkerTypeBarset),
35 34 m_textItem(new QGraphicsSimpleTextItem(this))
36 35 {
37 36 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
@@ -45,7 +44,6 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *
45 44 m_series(series),
46 45 m_barset(0),
47 46 m_pieslice(pieslice),
48 m_type(LegendMarkerTypePieslice),
49 47 m_textItem(new QGraphicsSimpleTextItem(this))
50 48 {
51 49 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
@@ -125,17 +123,21 void LegendMarker::layoutChanged()
125 123
126 124 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
127 125 {
128 switch (m_type)
129 {
130 case LegendMarkerTypeSeries: {
126 switch (m_series->type()) {
127 case QSeries::SeriesTypeLine:
128 case QSeries::SeriesTypeArea:
129 case QSeries::SeriesTypeScatter:
130 case QSeries::SeriesTypeSpline: {
131 131 emit clicked(m_series,event->button());
132 132 break;
133 133 }
134 case LegendMarkerTypeBarset: {
134 case QSeries::SeriesTypeBar:
135 case QSeries::SeriesTypeStackedBar:
136 case QSeries::SeriesTypePercentBar: {
135 137 emit clicked(m_barset,event->button());
136 138 break;
137 139 }
138 case LegendMarkerTypePieslice: {
140 case QSeries::SeriesTypePie: {
139 141 emit clicked(m_pieslice,event->button());
140 142 break;
141 143 }
@@ -147,26 +149,39 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
147 149
148 150 void LegendMarker::changed()
149 151 {
150 switch (m_type)
151 {
152 case LegendMarkerTypeSeries: {
153 QXYSeries* s = static_cast<QXYSeries*> (m_series);
152 switch (m_series->type()) {
153 case QSeries::SeriesTypeArea: {
154 QAreaSeries* s = static_cast<QAreaSeries*> (m_series);
154 155 setPen(s->pen());
155 156 setBrush(s->brush());
156 157 setName(s->name());
157 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 172 setPen(m_barset->pen());
161 173 setBrush(m_barset->brush());
162 174 setName(m_barset->name());
163 175 break;
164 }
165 case LegendMarkerTypePieslice: {
176 }
177 case QSeries::SeriesTypePie: {
166 178 setBrush(m_pieslice->brush());
167 179 setName(m_pieslice->label());
168 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 19 Q_OBJECT
20 20
21 enum LegendMarkerType {
22 LegendMarkerTypeSeries,
23 LegendMarkerTypeBarset,
24 LegendMarkerTypePieslice
25 };
26
27 21 public:
28 22 LegendMarker(QSeries *series, QGraphicsItem *parent = 0);
29 23 LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent = 0);
@@ -72,7 +66,6 private:
72 66 QBarSet *m_barset;
73 67 QPieSlice *m_pieslice;
74 68
75 LegendMarkerType m_type;
76 69 QGraphicsSimpleTextItem *m_textItem;
77 70
78 71 };
@@ -219,8 +219,55 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
219 219 {
220 220 Q_UNUSED(domain)
221 221
222 createMarkers(series);
223 connectSeries(series);
222 switch (series->type())
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 271 updateLayout();
225 272 }
226 273
@@ -229,15 +276,24 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
229 276 */
230 277 void QLegend::handleSeriesRemoved(QSeries *series)
231 278 {
232 disconnectSeries(series);
233
234 if (series->type() == QSeries::SeriesTypeArea) {
235 // This is special case. Area series has upper and lower series, which each have markers
236 QAreaSeries* s = static_cast<QAreaSeries *> (series);
237 deleteMarkers(s->upperSeries());
238 deleteMarkers(s->lowerSeries());
239 } else {
279 switch (series->type())
280 {
281 case QSeries::SeriesTypeArea: {
282 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
283 deleteMarkers(areaSeries);
284 break;
285 }
286 case QSeries::SeriesTypePie: {
287 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
288 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
240 289 deleteMarkers(series);
290 break;
291 }
292 default: {
293 // All other types
294 deleteMarkers(series);
295 break;
296 }
241 297 }
242 298
243 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.
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.
372 \internal Helper function. Appends markers from \a series to legend.
344 373 */
345 void QLegend::createMarkers(QSeries *series)
374 void QLegend::appendMarkers(QAreaSeries* series)
346 375 {
347 switch (series->type())
348 {
349 case QSeries::SeriesTypeLine: {
350 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
351 appendMarkers(lineSeries);
352 break;
353 }
354 case QSeries::SeriesTypeArea: {
355 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
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 }
376 LegendMarker* marker = new LegendMarker(series,this);
377 marker->setName(series->name());
378 marker->setPen(series->pen());
379 marker->setBrush(series->brush());
380 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
381 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
382 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
383 m_markers.append(marker);
384 childItems().append(marker);
396 385 }
397 386
398 387 /*!
@@ -406,12 +395,13 void QLegend::appendMarkers(QXYSeries* series)
406 395 marker->setBrush(series->brush());
407 396 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
408 397 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
398 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
409 399 m_markers.append(marker);
410 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 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 425 void QLegend::appendMarkers(QPieSeries *series)
436 426 {
@@ -15,6 +15,7 class QXYSeries;
15 15 class QBarSet;
16 16 class QBarSeries;
17 17 class QPieSeries;
18 class QAreaSeries;
18 19 class LegendScrollButton;
19 20 class QSeries;
20 21 class QChart;
@@ -75,10 +76,8 public Q_SLOTS:
75 76
76 77 private:
77 78 // PIMPL --->
78 void connectSeries(QSeries *series);
79 void disconnectSeries(QSeries *series);
80 void createMarkers(QSeries *series);
81 void appendMarkers(QXYSeries *series); // All line series are derived from QXYSeries, so this works for now
79 void appendMarkers(QAreaSeries *series);
80 void appendMarkers(QXYSeries *series);
82 81 void appendMarkers(QBarSeries *series);
83 82 void appendMarkers(QPieSeries *series);
84 83 void deleteMarkers(QSeries *series);
General Comments 0
You need to be logged in to leave comments. Login now