##// END OF EJS Templates
Enabled legend by default....
Tero Ahola -
r2071:b22984824da1
parent child
Show More
@@ -1,541 +1,540
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qlegend.h"
22 22 #include "qlegend_p.h"
23 23 #include "qabstractseries.h"
24 24 #include "qabstractseries_p.h"
25 25 #include "qchart_p.h"
26 26 #include "legendlayout_p.h"
27 27 #include "legendmarker_p.h"
28 28 #include "qxyseries.h"
29 29 #include "qlineseries.h"
30 30 #include "qareaseries.h"
31 31 #include "qscatterseries.h"
32 32 #include "qsplineseries.h"
33 33 #include "qabstractbarseries.h"
34 34 #include "qstackedbarseries.h"
35 35 #include "qpercentbarseries.h"
36 36 #include "qbarset.h"
37 37 #include "qpieseries.h"
38 38 #include "qpieseries_p.h"
39 39 #include "qpieslice.h"
40 40 #include "chartpresenter_p.h"
41 41 #include <QPainter>
42 42 #include <QPen>
43 43 #include <QTimer>
44 44 #include <QGraphicsLayout>
45 45 #include <QGraphicsSceneEvent>
46 46
47 47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48 48
49 49 /*!
50 50 \class QLegend
51 51 \brief Legend object
52 52 \mainclass
53 53
54 54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
55 55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
56 56 handle the drawing manually.
57 57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
58 58
59 59 \image examples_percentbarchart_legend.png
60 60
61 61 \sa QChart
62 62 */
63 63 /*!
64 64 \qmlclass Legend QLegend
65 65 \brief Legend is part of QtCommercial Chart QML API.
66 66
67 67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
68 68 series have been changed. Legend is used via ChartView class. For example:
69 69 \code
70 70 ChartView {
71 71 legend.visible: true
72 72 legend.alignment: Qt.AlignBottom
73 73 // Add a few series...
74 74 }
75 75 \endcode
76 76
77 77 \image examples_percentbarchart_legend.png
78 78 */
79 79
80 80 /*!
81 81 \property QLegend::alignment
82 82 \brief The alignment of the legend.
83 83
84 84 Legend paints on the defined position in the chart. The following alignments are supported:
85 85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
86 86 */
87 87 /*!
88 88 \qmlproperty Qt.Alignment Legend::alignment
89 89 \brief The alignment of the legend.
90 90
91 91 Legend paints on the defined position in the chart. The following alignments are supported:
92 92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
93 93 */
94 94
95 95 /*!
96 96 \property QLegend::backgroundVisible
97 97 Whether the legend background is visible or not.
98 98 */
99 99 /*!
100 100 \qmlproperty bool Legend::backgroundVisible
101 101 Whether the legend background is visible or not.
102 102 */
103 103
104 104 /*!
105 105 \property QLegend::color
106 106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
107 107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
108 108 */
109 109 /*!
110 110 \qmlproperty color Legend::color
111 111 The color of the legend, i.e. the background (brush) color.
112 112 */
113 113
114 114 /*!
115 115 \property QLegend::borderColor
116 116 The border color of the legend, i.e. the line color.
117 117 */
118 118 /*!
119 119 \qmlproperty color Legend::borderColor
120 120 The border color of the legend, i.e. the line color.
121 121 */
122 122
123 123 /*!
124 124 \property QLegend::font
125 125 The font of markers used by legend
126 126 */
127 127 /*!
128 128 \qmlproperty color Legend::font
129 129 The font of markers used by legend
130 130 */
131 131
132 132 /*!
133 133 \property QLegend::labelColor
134 134 The color of brush used to draw labels.
135 135 */
136 136 /*!
137 137 \qmlproperty color QLegend::labelColor
138 138 The color of brush used to draw labels.
139 139 */
140 140
141 141 /*!
142 142 \fn void QLegend::backgroundVisibleChanged(bool)
143 143 The visibility of the legend background changed to \a visible.
144 144 */
145 145
146 146 /*!
147 147 \fn void QLegend::colorChanged(QColor)
148 148 The color of the legend background changed to \a color.
149 149 */
150 150
151 151 /*!
152 152 \fn void QLegend::borderColorChanged(QColor)
153 153 The border color of the legend background changed to \a color.
154 154 */
155 155
156 156 /*!
157 157 \fn void QLegend::fontChanged(QFont)
158 158 The font of markers of the legend changed to \a font.
159 159 */
160 160
161 161 /*!
162 162 \fn void QLegend::labelColorChanged(QColor color)
163 163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
164 164 */
165 165
166 166 /*!
167 167 Constructs the legend object and sets the parent to \a parent
168 168 */
169 169
170 170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
171 171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
172 172 {
173 173 setZValue(ChartPresenter::LegendZValue);
174 174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
175 175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
176 176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
177 177 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
178 178 setLayout(d_ptr->m_layout);
179 setVisible(false);
180 179 }
181 180
182 181 /*!
183 182 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
184 183 */
185 184 QLegend::~QLegend()
186 185 {
187 186 }
188 187
189 188 /*!
190 189 \internal
191 190 */
192 191 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
193 192 {
194 193 Q_UNUSED(option)
195 194 Q_UNUSED(widget)
196 195
197 196 if(!d_ptr->m_backgroundVisible) return;
198 197
199 198 painter->setOpacity(opacity());
200 199 painter->setPen(d_ptr->m_pen);
201 200 painter->setBrush(d_ptr->m_brush);
202 201 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
203 202
204 203 }
205 204
206 205
207 206 /*!
208 207 Sets the \a brush of legend. Brush affects the background of legend.
209 208 */
210 209 void QLegend::setBrush(const QBrush &brush)
211 210 {
212 211 if (d_ptr->m_brush != brush) {
213 212 d_ptr->m_brush = brush;
214 213 update();
215 214 emit colorChanged(brush.color());
216 215 }
217 216 }
218 217
219 218 /*!
220 219 Returns the brush used by legend.
221 220 */
222 221 QBrush QLegend::brush() const
223 222 {
224 223 return d_ptr->m_brush;
225 224 }
226 225
227 226 void QLegend::setColor(QColor color)
228 227 {
229 228 QBrush b = d_ptr->m_brush;
230 229 if (b.style() != Qt::SolidPattern || b.color() != color) {
231 230 b.setStyle(Qt::SolidPattern);
232 231 b.setColor(color);
233 232 setBrush(b);
234 233 }
235 234 }
236 235
237 236 QColor QLegend::color()
238 237 {
239 238 return d_ptr->m_brush.color();
240 239 }
241 240
242 241 /*!
243 242 Sets the \a pen of legend. Pen affects the legend borders.
244 243 */
245 244 void QLegend::setPen(const QPen &pen)
246 245 {
247 246 if (d_ptr->m_pen != pen) {
248 247 d_ptr->m_pen = pen;
249 248 update();
250 249 emit borderColorChanged(pen.color());
251 250 }
252 251 }
253 252
254 253 /*!
255 254 Returns the pen used by legend
256 255 */
257 256
258 257 QPen QLegend::pen() const
259 258 {
260 259 return d_ptr->m_pen;
261 260 }
262 261
263 262 void QLegend::setFont(const QFont &font)
264 263 {
265 264 if (d_ptr->m_font != font) {
266 265 d_ptr->m_font = font;
267 266
268 267 foreach (LegendMarker *marker, d_ptr->markers()) {
269 268 marker->setFont(d_ptr->m_font);
270 269 }
271 270 layout()->invalidate();
272 271 emit fontChanged(font);
273 272 }
274 273 }
275 274
276 275 QFont QLegend::font() const
277 276 {
278 277 return d_ptr->m_font;
279 278 }
280 279
281 280 void QLegend::setBorderColor(QColor color)
282 281 {
283 282 QPen p = d_ptr->m_pen;
284 283 if (p.color() != color) {
285 284 p.setColor(color);
286 285 setPen(p);
287 286 }
288 287 }
289 288
290 289 QColor QLegend::borderColor()
291 290 {
292 291 return d_ptr->m_pen.color();
293 292 }
294 293
295 294 /*!
296 295 Set brush used to draw labels to \a brush.
297 296 */
298 297 void QLegend::setLabelBrush(const QBrush &brush)
299 298 {
300 299 if (d_ptr->m_labelBrush != brush) {
301 300 d_ptr->m_labelBrush = brush;
302 301 foreach (LegendMarker *marker, d_ptr->markers()) {
303 302 marker->setLabelBrush(d_ptr->m_labelBrush);
304 303 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
305 304 // instead of mapping it from label brush color
306 305 marker->setPen(brush.color());
307 306 }
308 307 emit labelColorChanged(brush.color());
309 308 }
310 309 }
311 310
312 311 /*!
313 312 Brush used to draw labels.
314 313 */
315 314 QBrush QLegend::labelBrush() const
316 315 {
317 316 return d_ptr->m_labelBrush;
318 317 }
319 318
320 319 void QLegend::setLabelColor(QColor color)
321 320 {
322 321 QBrush b = d_ptr->m_labelBrush;
323 322 if (b.style() != Qt::SolidPattern || b.color() != color) {
324 323 b.setStyle(Qt::SolidPattern);
325 324 b.setColor(color);
326 325 setLabelBrush(b);
327 326 }
328 327 }
329 328
330 329 QColor QLegend::labelColor() const
331 330 {
332 331 return d_ptr->m_labelBrush.color();
333 332 }
334 333
335 334
336 335 void QLegend::setAlignment(Qt::Alignment alignment)
337 336 {
338 337 if(d_ptr->m_alignment!=alignment) {
339 338 d_ptr->m_alignment = alignment;
340 339 updateGeometry();
341 340 if(isAttachedToChart()){
342 341 d_ptr->m_presenter->layout()->invalidate();
343 342 }else{
344 343 layout()->invalidate();
345 344 }
346 345 }
347 346 }
348 347
349 348 Qt::Alignment QLegend::alignment() const
350 349 {
351 350 return d_ptr->m_alignment;
352 351 }
353 352
354 353 /*!
355 354 Detaches the legend from chart. Chart won't change layout of the legend.
356 355 */
357 356 void QLegend::detachFromChart()
358 357 {
359 358 d_ptr->m_attachedToChart = false;
360 359 d_ptr->m_layout->invalidate();
361 360 setParent(0);
362 361
363 362 }
364 363
365 364 /*!
366 365 Attaches the legend to chart. Chart may change layout of the legend.
367 366 */
368 367 void QLegend::attachToChart()
369 368 {
370 369 d_ptr->m_attachedToChart = true;
371 370 d_ptr->m_presenter->layout()->invalidate();
372 371 setParent(d_ptr->m_chart);
373 372 }
374 373
375 374 /*!
376 375 Returns true, if legend is attached to chart.
377 376 */
378 377 bool QLegend::isAttachedToChart()
379 378 {
380 379 return d_ptr->m_attachedToChart;
381 380 }
382 381
383 382 /*!
384 383 Sets the visibility of legend background to \a visible
385 384 */
386 385 void QLegend::setBackgroundVisible(bool visible)
387 386 {
388 387 if(d_ptr->m_backgroundVisible != visible) {
389 388 d_ptr->m_backgroundVisible = visible;
390 389 update();
391 390 emit backgroundVisibleChanged(visible);
392 391 }
393 392 }
394 393
395 394 /*!
396 395 Returns the visibility of legend background
397 396 */
398 397 bool QLegend::isBackgroundVisible() const
399 398 {
400 399 return d_ptr->m_backgroundVisible;
401 400 }
402 401
403 402 /*!
404 403 \internal \a event see QGraphicsWidget for details
405 404 */
406 405 void QLegend::hideEvent(QHideEvent *event)
407 406 {
408 407 d_ptr->m_presenter->layout()->invalidate();
409 408 QGraphicsWidget::hideEvent(event);
410 409 }
411 410
412 411 /*!
413 412 \internal \a event see QGraphicsWidget for details
414 413 */
415 414 void QLegend::showEvent(QShowEvent *event)
416 415 {
417 416 QGraphicsWidget::showEvent(event);
418 417 d_ptr->m_presenter->layout()->invalidate();
419 418 d_ptr->items()->setVisible(false);
420 419 //layout activation will show the items
421 420 }
422 421
423 422 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
424 423
425 424 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
426 425 q_ptr(q),
427 426 m_presenter(presenter),
428 427 m_layout(new LegendLayout(q)),
429 428 m_chart(chart),
430 429 m_items(new QGraphicsItemGroup(q)),
431 430 m_alignment(Qt::AlignTop),
432 431 m_brush(QBrush()),
433 432 m_pen(QPen()),
434 433 m_labelBrush(QBrush()),
435 434 m_diameter(5),
436 435 m_attachedToChart(true),
437 436 m_backgroundVisible(false)
438 437 {
439 438
440 439 }
441 440
442 441 QLegendPrivate::~QLegendPrivate()
443 442 {
444 443
445 444 }
446 445
447 446 void QLegendPrivate::setOffset(qreal x, qreal y)
448 447 {
449 448 m_layout->setOffset(x,y);
450 449 }
451 450
452 451 QPointF QLegendPrivate::offset() const
453 452 {
454 453 return m_layout->offset();
455 454 }
456 455
457 456 int QLegendPrivate::roundness(qreal size)
458 457 {
459 458 return 100*m_diameter/int(size);
460 459 }
461 460
462 461 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
463 462 {
464 463 Q_UNUSED(domain)
465 464
466 465 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
467 466
468 467 foreach(LegendMarker* marker, markers) {
469 468 marker->setFont(m_font);
470 469 marker->setLabelBrush(m_labelBrush);
471 470 marker->setVisible(series->isVisible());
472 471 m_items->addToGroup(marker);
473 472 m_markers<<marker;
474 473 }
475 474
476 475 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
477 476
478 477 if(series->type() == QAbstractSeries::SeriesTypePie) {
479 478 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
480 479 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
481 480 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
482 481 }
483 482
484 483 q_ptr->layout()->invalidate();
485 484 q_ptr->layout()->activate();
486 485 }
487 486
488 487 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
489 488 {
490 489 foreach (LegendMarker *marker, m_markers) {
491 490 if (marker->series() == series) {
492 491 delete marker;
493 492 m_markers.removeAll(marker);
494 493 }
495 494 }
496 495
497 496 if(series->type() == QAbstractSeries::SeriesTypePie)
498 497 {
499 498 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
500 499 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
501 500 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
502 501 }
503 502
504 503 q_ptr->layout()->invalidate();
505 504 }
506 505
507 506 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
508 507 {
509 508 // TODO: find out which markers are are added or removed. Update them
510 509 // TODO: better implementation
511 510 handleSeriesRemoved(series);
512 511 Domain domain;
513 512 handleSeriesAdded(series, &domain);
514 513 }
515 514
516 515 void QLegendPrivate::handleUpdatePieSeries()
517 516 {
518 517 //TODO: reimplement to be optimal
519 518 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
520 519 Q_ASSERT(series);
521 520 handleSeriesRemoved(series);
522 521 handleSeriesAdded(series, 0);
523 522 }
524 523
525 524 void QLegendPrivate::handleSeriesVisibleChanged()
526 525 {
527 526 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
528 527
529 528 foreach (LegendMarker* marker, m_markers) {
530 529 if (marker->series() == series) {
531 530 marker->setVisible(series->isVisible());
532 531 }
533 532 }
534 533
535 534 q_ptr->layout()->invalidate();
536 535 }
537 536
538 537 #include "moc_qlegend.cpp"
539 538 #include "moc_qlegend_p.cpp"
540 539
541 540 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now