##// END OF EJS Templates
Fix color setting...
Titta Heikkala -
r2852:5f71da41b84f
parent child
Show More
@@ -1,1093 +1,1093
1 1 /******************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd.
4 4 ** Contact: http://www.qt.io/licensing/
5 5 **
6 6 ** This file is part of the Qt Charts module.
7 7 **
8 8 ** $QT_BEGIN_LICENSE:COMM$
9 9 **
10 10 ** Commercial License Usage
11 11 ** Licensees holding valid commercial Qt licenses may use this file in
12 12 ** accordance with the commercial license agreement provided with the
13 13 ** Software or, alternatively, in accordance with the terms contained in
14 14 ** a written agreement between you and The Qt Company. For licensing terms
15 15 ** and conditions see http://www.qt.io/terms-conditions. For further
16 16 ** information use the contact form at http://www.qt.io/contact-us.
17 17 **
18 18 ** $QT_END_LICENSE$
19 19 **
20 20 ******************************************************************************/
21 21
22 22 #include <QtCharts/QAbstractAxis>
23 23 #include <private/qabstractaxis_p.h>
24 24 #include <private/chartdataset_p.h>
25 25 #include <private/charttheme_p.h>
26 26 #include <private/qchart_p.h>
27 27
28 28 QT_CHARTS_BEGIN_NAMESPACE
29 29
30 30 /*!
31 31 \class QAbstractAxis
32 32 \inmodule Qt Charts
33 33 \brief The QAbstractAxis class is used for manipulating chart's axis.
34 34
35 35 Each series can be bound to one or more horizontal and vertical axes, but mixing axis types
36 36 that would result in different domains is not supported, such as specifying
37 37 QValueAxis and QLogValueAxis on the same orientation.
38 38
39 39 Properties and visibility of various axis elements such as axis line, title, labels, grid lines,
40 40 and shades can be individually controlled.
41 41 */
42 42 /*!
43 43 \qmltype AbstractAxis
44 44 \instantiates QAbstractAxis
45 45 \inqmlmodule QtCharts
46 46
47 47 \brief The AbstractAxis is a base element used for specialized axis elements.
48 48
49 49 Each series can be bound to only one horizontal and vertical axis.
50 50
51 51 Properties and visibility of various axis elements such as axis line, title, labels, grid lines,
52 52 and shades can be individually controlled.
53 53 */
54 54
55 55 /*!
56 56 \enum QAbstractAxis::AxisType
57 57
58 58 The type of the axis object.
59 59
60 60 \value AxisTypeNoAxis
61 61 \value AxisTypeValue
62 62 \value AxisTypeBarCategory
63 63 \value AxisTypeCategory
64 64 \value AxisTypeDateTime
65 65 \value AxisTypeLogValue
66 66 */
67 67
68 68 /*!
69 69 *\fn void QAbstractAxis::type() const
70 70 Returns the type of the axis
71 71 */
72 72
73 73 /*!
74 74 \property QAbstractAxis::lineVisible
75 75 The visibility of the axis line
76 76 */
77 77 /*!
78 78 \qmlproperty bool AbstractAxis::lineVisible
79 79 The visibility of the axis line
80 80 */
81 81
82 82 /*!
83 83 \property QAbstractAxis::linePen
84 84 The pen of the line.
85 85 */
86 86
87 87 /*!
88 88 \property QAbstractAxis::labelsVisible
89 89 Defines if axis labels are visible.
90 90 */
91 91 /*!
92 92 \qmlproperty bool AbstractAxis::labelsVisible
93 93 Defines if axis labels are visible.
94 94 */
95 95
96 96 /*!
97 97 \property QAbstractAxis::labelsBrush
98 98 The brush of the labels. Only the color of the brush is relevant.
99 99 */
100 100
101 101 /*!
102 102 \property QAbstractAxis::visible
103 103 The visibility of the axis.
104 104 */
105 105 /*!
106 106 \qmlproperty bool AbstractAxis::visible
107 107 The visibility of the axis.
108 108 */
109 109
110 110 /*!
111 111 \property QAbstractAxis::gridVisible
112 112 The visibility of the grid lines.
113 113 */
114 114 /*!
115 115 \qmlproperty bool AbstractAxis::gridVisible
116 116 The visibility of the grid lines.
117 117 */
118 118
119 119 /*!
120 120 \property QAbstractAxis::minorGridVisible
121 121 The visibility of the minor grid lines. Applies only to QValueAxis.
122 122 */
123 123 /*!
124 124 \qmlproperty bool AbstractAxis::minorGridVisible
125 125 The visibility of the minor grid lines. Applies only to QValueAxis.
126 126 */
127 127
128 128 /*!
129 129 \property QAbstractAxis::color
130 130 The color of the axis and ticks.
131 131 */
132 132 /*!
133 133 \qmlproperty color AbstractAxis::color
134 134 The color of the axis and ticks.
135 135 */
136 136
137 137 /*!
138 138 \property QAbstractAxis::gridLinePen
139 139 The pen of the grid line.
140 140 */
141 141
142 142 /*!
143 143 \property QAbstractAxis::minorGridLinePen
144 144 The pen of the minor grid line. Applies only to QValueAxis.
145 145 */
146 146
147 147 /*!
148 148 \property QAbstractAxis::gridLineColor
149 149 The color of the grid line.
150 150 */
151 151
152 152 /*!
153 153 \property QAbstractAxis::minorGridLineColor
154 154 The color of the minor grid line. Applies only to QValueAxis.
155 155 */
156 156
157 157 /*!
158 158 \property QAbstractAxis::labelsFont
159 159 The font of the axis labels.
160 160 */
161 161
162 162 /*!
163 163 \qmlproperty Font AbstractAxis::labelsFont
164 164 The font of the axis labels.
165 165
166 166 See the Qt documentation for more details of Font.
167 167 */
168 168
169 169 /*!
170 170 \property QAbstractAxis::labelsColor
171 171 The color of the axis labels.
172 172 */
173 173 /*!
174 174 \qmlproperty color AbstractAxis::labelsColor
175 175 The color of the axis labels.
176 176 */
177 177
178 178 /*!
179 179 \property QAbstractAxis::labelsAngle
180 180 The angle of the axis labels in degrees.
181 181 */
182 182 /*!
183 183 \qmlproperty int AbstractAxis::labelsAngle
184 184 The angle of the axis labels in degrees.
185 185 */
186 186
187 187 /*!
188 188 \property QAbstractAxis::shadesVisible
189 189 The visibility of the axis shades.
190 190 */
191 191 /*!
192 192 \qmlproperty bool AbstractAxis::shadesVisible
193 193 The visibility of the axis shades.
194 194 */
195 195
196 196 /*!
197 197 \property QAbstractAxis::shadesColor
198 198 The fill (brush) color of the axis shades.
199 199 */
200 200 /*!
201 201 \qmlproperty color AbstractAxis::shadesColor
202 202 The fill (brush) color of the axis shades.
203 203 */
204 204
205 205 /*!
206 206 \property QAbstractAxis::shadesBorderColor
207 207 The border (pen) color of the axis shades.
208 208 */
209 209 /*!
210 210 \qmlproperty color AbstractAxis::shadesBorderColor
211 211 The border (pen) color of the axis shades.
212 212 */
213 213
214 214 /*!
215 215 \property QAbstractAxis::shadesPen
216 216 The pen of the axis shades (area between grid lines).
217 217 */
218 218
219 219 /*!
220 220 \property QAbstractAxis::shadesBrush
221 221 The brush of the axis shades (area between grid lines).
222 222 */
223 223
224 224 /*!
225 225 \property QAbstractAxis::titleVisible
226 226 The visibility of the axis title. By default the value is true.
227 227 */
228 228 /*!
229 229 \qmlproperty bool AbstractAxis::titleVisible
230 230 The visibility of the axis title. By default the value is true.
231 231 */
232 232
233 233 /*!
234 234 \property QAbstractAxis::titleText
235 235 The title of the axis. Empty by default. Axis titles support html formatting.
236 236 */
237 237 /*!
238 238 \qmlproperty String AbstractAxis::titleText
239 239 The title of the axis. Empty by default. Axis titles support html formatting.
240 240 */
241 241
242 242 /*!
243 243 \property QAbstractAxis::titleBrush
244 244 The brush of the title text. Only the color of the brush is relevant.
245 245 */
246 246
247 247 /*!
248 248 \property QAbstractAxis::titleFont
249 249 The font of the title of the axis.
250 250 */
251 251 /*!
252 252 \qmlproperty Font AbstractAxis::titleFont
253 253 The font of the title of the axis.
254 254 */
255 255
256 256 /*!
257 257 \property QAbstractAxis::orientation
258 258 The orientation of the axis. Fixed to either Qt::Horizontal or Qt::Vertical when you add the axis to a chart.
259 259 */
260 260 /*!
261 261 \qmlproperty Qt.Orientation AbstractAxis::orientation
262 262 The orientation of the axis. Fixed to either Qt.Horizontal or Qt.Vertical when the axis is set to a series.
263 263 */
264 264
265 265 /*!
266 266 \property QAbstractAxis::alignment
267 267 The alignment of the axis. Can be Qt::AlignLeft, Qt::AlignRight, Qt::AlignBottom, or Qt::AlignTop.
268 268 */
269 269 /*!
270 270 \qmlproperty alignment AbstractAxis::alignment
271 271 The alignment of the axis. Can be Qt.AlignLeft, Qt.AlignRight, Qt.AlignBottom, or Qt.AlignTop.
272 272 */
273 273
274 274 /*!
275 275 \property QAbstractAxis::reverse
276 276 The reverse property defines if reverse axis is used. By default the value is false.
277 277
278 278 Reverse axis is supported with line, spline, scatter and area series with cartesian chart.
279 279 All axes of the same orientation attached to same series must be reversed if one is reversed or
280 280 the behavior is undefined.
281 281 */
282 282 /*!
283 283 \qmlproperty alignment AbstractAxis::reverse
284 284 The reverse property defines if reverse axis is used. By default the value is false.
285 285
286 286 Reverse axis is supported with line, spline, scatter and area series with cartesian chart.
287 287 All axes of the same orientation attached to same series must be reversed if one is reversed or
288 288 the behavior is undefined.
289 289 */
290 290
291 291 /*!
292 292 \fn void QAbstractAxis::visibleChanged(bool visible)
293 293 Visibility of the axis has changed to \a visible.
294 294 */
295 295 /*!
296 296 \qmlsignal AbstractAxis::onVisibleChanged(bool visible)
297 297 Visibility of the axis has changed to \a visible.
298 298 */
299 299
300 300 /*!
301 301 \fn void QAbstractAxis::linePenChanged(const QPen& pen)
302 302 The pen of the line of the axis has changed to \a pen.
303 303 */
304 304
305 305 /*!
306 306 \fn void QAbstractAxis::lineVisibleChanged(bool visible)
307 307 Visibility of the axis line has changed to \a visible.
308 308 */
309 309 /*!
310 310 \qmlsignal AbstractAxis::onLineVisibleChanged(bool visible)
311 311 Visibility of the axis line has changed to \a visible.
312 312 */
313 313
314 314 /*!
315 315 \fn void QAbstractAxis::labelsVisibleChanged(bool visible)
316 316 Visibility of the labels of the axis has changed to \a visible.
317 317 */
318 318 /*!
319 319 \qmlsignal AbstractAxis::onLabelsVisibleChanged(bool visible)
320 320 Visibility of the labels of the axis has changed to \a visible.
321 321 */
322 322
323 323 /*!
324 324 \fn void QAbstractAxis::labelsFontChanged(const QFont& font)
325 325 The font of the axis labels has changed to \a font.
326 326 */
327 327 /*!
328 328 \qmlsignal AbstractAxis::onLabelsFontChanged(Font font)
329 329 The font of the axis labels has changed to \a font.
330 330 */
331 331
332 332 /*!
333 333 \fn void QAbstractAxis::labelsBrushChanged(const QBrush& brush)
334 334 The brush of the axis labels has changed to \a brush.
335 335 */
336 336
337 337 /*!
338 338 \fn void QAbstractAxis::labelsAngleChanged(int angle)
339 339 The angle of the axis labels has changed to \a angle.
340 340 */
341 341 /*!
342 342 \qmlsignal AbstractAxis::onLabelsAngleChanged(int angle)
343 343 The angle of the axis labels has changed to \a angle.
344 344 */
345 345
346 346 /*!
347 347 \fn void QAbstractAxis::gridVisibleChanged(bool visible)
348 348 Visibility of the grid lines of the axis has changed to \a visible.
349 349 */
350 350 /*!
351 351 \qmlsignal AbstractAxis::onGridVisibleChanged(bool visible)
352 352 Visibility of the grid lines of the axis has changed to \a visible.
353 353 */
354 354
355 355 /*!
356 356 \fn void QAbstractAxis::minorGridVisibleChanged(bool visible)
357 357 Visibility of the minor grid lines of the axis has changed to \a visible.
358 358 */
359 359 /*!
360 360 \qmlsignal AbstractAxis::onMinorGridVisibleChanged(bool visible)
361 361 Visibility of the minor grid lines of the axis has changed to \a visible.
362 362 */
363 363
364 364 /*!
365 365 \fn void QAbstractAxis::gridLinePenChanged(const QPen& pen)
366 366 The pen of the grid line has changed to \a pen.
367 367 */
368 368
369 369 /*!
370 370 \fn void QAbstractAxis::minorGridLinePenChanged(const QPen& pen)
371 371 The pen of the minor grid line has changed to \a pen.
372 372 */
373 373
374 374 /*!
375 375 \fn void QAbstractAxis::gridLineColorChanged(const QColor &color)
376 376 The color of the pen of the grid line has changed to \a color.
377 377 */
378 378
379 379 /*!
380 380 \fn void QAbstractAxis::minorGridLineColorChanged(const QColor &color)
381 381 The color of the pen of the minor grid line has changed to \a color.
382 382 */
383 383
384 384 /*!
385 385 \fn void QAbstractAxis::colorChanged(QColor color)
386 386 Emitted if the \a color of the axis is changed.
387 387 */
388 388 /*!
389 389 \qmlsignal AbstractAxis::onColorChanged(QColor color)
390 390 Emitted if the \a color of the axis is changed.
391 391 */
392 392
393 393 /*!
394 394 \fn void QAbstractAxis::labelsColorChanged(QColor color)
395 395 Emitted if the \a color of the axis labels is changed.
396 396 */
397 397 /*!
398 398 \qmlsignal AbstractAxis::onLabelsColorChanged(QColor color)
399 399 Emitted if the \a color of the axis labels is changed.
400 400 */
401 401
402 402 /*!
403 403 \fn void QAbstractAxis::titleVisibleChanged(bool visible)
404 404 Visibility of the title text of the axis has changed to \a visible.
405 405 */
406 406 /*!
407 407 \qmlsignal AbstractAxis::onTitleVisibleChanged(bool visible)
408 408 Visibility of the title text of the axis has changed to \a visible.
409 409 */
410 410
411 411 /*!
412 412 \fn void QAbstractAxis::titleTextChanged(const QString& text)
413 413 The text of the axis title has changed to \a text.
414 414 */
415 415 /*!
416 416 \qmlsignal AbstractAxis::onTitleTextChanged(String text)
417 417 The text of the axis title has changed to \a text.
418 418 */
419 419
420 420 /*!
421 421 \fn void QAbstractAxis::titleBrushChanged(const QBrush& brush)
422 422 The brush of the axis title has changed to \a brush.
423 423 */
424 424
425 425 /*!
426 426 \fn void QAbstractAxis::titleFontChanged(const QFont& font)
427 427 The font of the axis title has changed to \a font.
428 428 */
429 429 /*!
430 430 \qmlsignal AbstractAxis::onTitleFontChanged(Font font)
431 431 The font of the axis title has changed to \a font.
432 432 */
433 433
434 434 /*!
435 435 \fn void QAbstractAxis::shadesVisibleChanged(bool)
436 436 Emitted if the visibility of the axis shades is changed to \a visible.
437 437 */
438 438 /*!
439 439 \qmlsignal AbstractAxis::onShadesVisibleChanged(bool visible)
440 440 Emitted if the visibility of the axis shades is changed to \a visible.
441 441 */
442 442
443 443 /*!
444 444 \fn void QAbstractAxis::shadesColorChanged(QColor color)
445 445 Emitted if the \a color of the axis shades is changed.
446 446 */
447 447 /*!
448 448 \qmlsignal AbstractAxis::onShadesColorChanged(QColor color)
449 449 Emitted if the \a color of the axis shades is changed.
450 450 */
451 451
452 452 /*!
453 453 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
454 454 Emitted if the border \a color of the axis shades is changed.
455 455 */
456 456 /*!
457 457 \qmlsignal AbstractAxis::onBorderColorChanged(QColor color)
458 458 Emitted if the border \a color of the axis shades is changed.
459 459 */
460 460
461 461 /*!
462 462 \fn void QAbstractAxis::shadesBrushChanged(const QBrush& brush)
463 463 The brush of the axis shades has changed to \a brush.
464 464 */
465 465
466 466 /*!
467 467 \fn void QAbstractAxis::shadesPenChanged(const QPen& pen)
468 468 The pen of the axis shades has changed to \a pen.
469 469 */
470 470
471 471 /*!
472 472 \internal
473 473 Constructs new axis object which is a child of \a parent. Ownership is taken by
474 474 QChart when axis added.
475 475 */
476 476
477 477 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent)
478 478 : QObject(parent),
479 479 d_ptr(&d)
480 480 {
481 481 }
482 482
483 483 /*!
484 484 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
485 485 */
486 486
487 487 QAbstractAxis::~QAbstractAxis()
488 488 {
489 489 if (d_ptr->m_chart)
490 490 qFatal("Still binded axis detected !");
491 491 }
492 492
493 493 /*!
494 494 Sets \a pen used to draw axis line and ticks.
495 495 */
496 496 void QAbstractAxis::setLinePen(const QPen &pen)
497 497 {
498 498 if (d_ptr->m_axisPen != pen) {
499 499 d_ptr->m_axisPen = pen;
500 500 emit linePenChanged(pen);
501 501 }
502 502 }
503 503
504 504 /*!
505 505 Returns pen used to draw axis and ticks.
506 506 */
507 507 QPen QAbstractAxis::linePen() const
508 508 {
509 509 if (d_ptr->m_axisPen == QChartPrivate::defaultPen())
510 510 return QPen();
511 511 else
512 512 return d_ptr->m_axisPen;
513 513 }
514 514
515 515 void QAbstractAxis::setLinePenColor(QColor color)
516 516 {
517 QPen p = d_ptr->m_axisPen;
517 QPen p = linePen();
518 518 if (p.color() != color) {
519 519 p.setColor(color);
520 520 setLinePen(p);
521 521 emit colorChanged(color);
522 522 }
523 523 }
524 524
525 525 QColor QAbstractAxis::linePenColor() const
526 526 {
527 527 return linePen().color();
528 528 }
529 529
530 530 /*!
531 531 Sets if axis and ticks are \a visible.
532 532 */
533 533 void QAbstractAxis::setLineVisible(bool visible)
534 534 {
535 535 if (d_ptr->m_arrowVisible != visible) {
536 536 d_ptr->m_arrowVisible = visible;
537 537 emit lineVisibleChanged(visible);
538 538 }
539 539 }
540 540
541 541 bool QAbstractAxis::isLineVisible() const
542 542 {
543 543 return d_ptr->m_arrowVisible;
544 544 }
545 545
546 546 void QAbstractAxis::setGridLineVisible(bool visible)
547 547 {
548 548 if (d_ptr->m_gridLineVisible != visible) {
549 549 d_ptr->m_gridLineVisible = visible;
550 550 emit gridVisibleChanged(visible);
551 551 }
552 552 }
553 553
554 554 bool QAbstractAxis::isGridLineVisible() const
555 555 {
556 556 return d_ptr->m_gridLineVisible;
557 557 }
558 558
559 559 void QAbstractAxis::setMinorGridLineVisible(bool visible)
560 560 {
561 561 if (d_ptr->m_minorGridLineVisible != visible) {
562 562 d_ptr->m_minorGridLineVisible = visible;
563 563 emit minorGridVisibleChanged(visible);
564 564 }
565 565 }
566 566
567 567 bool QAbstractAxis::isMinorGridLineVisible() const
568 568 {
569 569 return d_ptr->m_minorGridLineVisible;
570 570 }
571 571
572 572 /*!
573 573 Sets \a pen used to draw grid line.
574 574 */
575 575 void QAbstractAxis::setGridLinePen(const QPen &pen)
576 576 {
577 577 if (d_ptr->m_gridLinePen != pen) {
578 578 d_ptr->m_gridLinePen = pen;
579 579 emit gridLinePenChanged(pen);
580 580 }
581 581 }
582 582
583 583 /*!
584 584 Returns pen used to draw grid.
585 585 */
586 586 QPen QAbstractAxis::gridLinePen() const
587 587 {
588 588 if (d_ptr->m_gridLinePen == QChartPrivate::defaultPen())
589 589 return QPen();
590 590 else
591 591 return d_ptr->m_gridLinePen;
592 592 }
593 593
594 594 void QAbstractAxis::setMinorGridLinePen(const QPen &pen)
595 595 {
596 596 if (d_ptr->m_minorGridLinePen != pen) {
597 597 d_ptr->m_minorGridLinePen = pen;
598 598 emit minorGridLinePenChanged(pen);
599 599 }
600 600 }
601 601
602 602 QPen QAbstractAxis::minorGridLinePen() const
603 603 {
604 604 if (d_ptr->m_minorGridLinePen == QChartPrivate::defaultPen())
605 605 return QPen();
606 606 else
607 607 return d_ptr->m_minorGridLinePen;
608 608 }
609 609
610 610 void QAbstractAxis::setGridLineColor(const QColor &color)
611 611 {
612 QPen pen = d_ptr->m_gridLinePen;
612 QPen pen = gridLinePen();
613 613 if (color != pen.color()) {
614 614 pen.setColor(color);
615 615 d_ptr->m_gridLinePen = pen;
616 616 emit gridLineColorChanged(color);
617 617 }
618 618 }
619 619
620 620 QColor QAbstractAxis::gridLineColor()
621 621 {
622 622 return d_ptr->m_gridLinePen.color();
623 623 }
624 624
625 625 void QAbstractAxis::setMinorGridLineColor(const QColor &color)
626 626 {
627 QPen pen = d_ptr->m_minorGridLinePen;
627 QPen pen = minorGridLinePen();
628 628 if (color != pen.color()) {
629 629 pen.setColor(color);
630 630 d_ptr->m_minorGridLinePen = pen;
631 631 emit minorGridLineColorChanged(color);
632 632 }
633 633 }
634 634
635 635 QColor QAbstractAxis::minorGridLineColor()
636 636 {
637 637 return d_ptr->m_minorGridLinePen.color();
638 638 }
639 639
640 640 void QAbstractAxis::setLabelsVisible(bool visible)
641 641 {
642 642 if (d_ptr->m_labelsVisible != visible) {
643 643 d_ptr->m_labelsVisible = visible;
644 644 emit labelsVisibleChanged(visible);
645 645 }
646 646 }
647 647
648 648 bool QAbstractAxis::labelsVisible() const
649 649 {
650 650 return d_ptr->m_labelsVisible;
651 651 }
652 652
653 653 /*!
654 654 Sets \a brush used to draw labels.
655 655 */
656 656 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
657 657 {
658 658 if (d_ptr->m_labelsBrush != brush) {
659 659 d_ptr->m_labelsBrush = brush;
660 660 emit labelsBrushChanged(brush);
661 661 }
662 662 }
663 663
664 664 /*!
665 665 Returns brush used to draw labels.
666 666 */
667 667 QBrush QAbstractAxis::labelsBrush() const
668 668 {
669 669 if (d_ptr->m_labelsBrush == QChartPrivate::defaultBrush())
670 670 return QBrush();
671 671 else
672 672 return d_ptr->m_labelsBrush;
673 673 }
674 674
675 675 /*!
676 676 Sets \a font used to draw labels.
677 677 */
678 678 void QAbstractAxis::setLabelsFont(const QFont &font)
679 679 {
680 680 if (d_ptr->m_labelsFont != font) {
681 681 d_ptr->m_labelsFont = font;
682 682 emit labelsFontChanged(font);
683 683 }
684 684 }
685 685
686 686 /*!
687 687 Returns font used to draw labels.
688 688 */
689 689 QFont QAbstractAxis::labelsFont() const
690 690 {
691 691 if (d_ptr->m_labelsFont == QChartPrivate::defaultFont())
692 692 return QFont();
693 693 else
694 694 return d_ptr->m_labelsFont;
695 695 }
696 696
697 697 void QAbstractAxis::setLabelsAngle(int angle)
698 698 {
699 699 if (d_ptr->m_labelsAngle != angle) {
700 700 d_ptr->m_labelsAngle = angle;
701 701 emit labelsAngleChanged(angle);
702 702 }
703 703 }
704 704
705 705 int QAbstractAxis::labelsAngle() const
706 706 {
707 707 return d_ptr->m_labelsAngle;
708 708 }
709 709 void QAbstractAxis::setLabelsColor(QColor color)
710 710 {
711 QBrush b = d_ptr->m_labelsBrush;
711 QBrush b = labelsBrush();
712 712 if (b.color() != color) {
713 713 b.setColor(color);
714 714 setLabelsBrush(b);
715 715 emit labelsColorChanged(color);
716 716 }
717 717 }
718 718
719 719 QColor QAbstractAxis::labelsColor() const
720 720 {
721 721 return labelsBrush().color();
722 722 }
723 723
724 724 void QAbstractAxis::setTitleVisible(bool visible)
725 725 {
726 726 if (d_ptr->m_titleVisible != visible) {
727 727 d_ptr->m_titleVisible = visible;
728 728 emit titleVisibleChanged(visible);
729 729 }
730 730 }
731 731
732 732 bool QAbstractAxis::isTitleVisible() const
733 733 {
734 734 return d_ptr->m_titleVisible;
735 735 }
736 736
737 737 /*!
738 738 Sets \a brush used to draw title.
739 739 */
740 740 void QAbstractAxis::setTitleBrush(const QBrush &brush)
741 741 {
742 742 if (d_ptr->m_titleBrush != brush) {
743 743 d_ptr->m_titleBrush = brush;
744 744 emit titleBrushChanged(brush);
745 745 }
746 746 }
747 747
748 748 /*!
749 749 Returns brush used to draw title.
750 750 */
751 751 QBrush QAbstractAxis::titleBrush() const
752 752 {
753 753 if (d_ptr->m_titleBrush == QChartPrivate::defaultBrush())
754 754 return QBrush();
755 755 else
756 756 return d_ptr->m_titleBrush;
757 757 }
758 758
759 759 /*!
760 760 Sets \a font used to draw title.
761 761 */
762 762 void QAbstractAxis::setTitleFont(const QFont &font)
763 763 {
764 764 if (d_ptr->m_titleFont != font) {
765 765 d_ptr->m_titleFont = font;
766 766 emit titleFontChanged(font);
767 767 }
768 768 }
769 769
770 770 /*!
771 771 Returns font used to draw title.
772 772 */
773 773 QFont QAbstractAxis::titleFont() const
774 774 {
775 775 if (d_ptr->m_titleFont == QChartPrivate::defaultFont())
776 776 return QFont();
777 777 else
778 778 return d_ptr->m_titleFont;
779 779 }
780 780
781 781 void QAbstractAxis::setTitleText(const QString &title)
782 782 {
783 783 if (d_ptr->m_title != title) {
784 784 d_ptr->m_title = title;
785 785 emit titleTextChanged(title);
786 786 }
787 787 }
788 788
789 789 QString QAbstractAxis::titleText() const
790 790 {
791 791 return d_ptr->m_title;
792 792 }
793 793
794 794
795 795 void QAbstractAxis::setShadesVisible(bool visible)
796 796 {
797 797 if (d_ptr->m_shadesVisible != visible) {
798 798 d_ptr->m_shadesVisible = visible;
799 799 emit shadesVisibleChanged(visible);
800 800 }
801 801 }
802 802
803 803 bool QAbstractAxis::shadesVisible() const
804 804 {
805 805 return d_ptr->m_shadesVisible;
806 806 }
807 807
808 808 /*!
809 809 Sets \a pen used to draw shades.
810 810 */
811 811 void QAbstractAxis::setShadesPen(const QPen &pen)
812 812 {
813 813 if (d_ptr->m_shadesPen != pen) {
814 814 d_ptr->m_shadesPen = pen;
815 815 emit shadesPenChanged(pen);
816 816 }
817 817 }
818 818
819 819 /*!
820 820 Returns pen used to draw shades.
821 821 */
822 822 QPen QAbstractAxis::shadesPen() const
823 823 {
824 824 if (d_ptr->m_shadesPen == QChartPrivate::defaultPen())
825 825 return QPen();
826 826 else
827 827 return d_ptr->m_shadesPen;
828 828 }
829 829
830 830 /*!
831 831 Sets \a brush used to draw shades.
832 832 */
833 833 void QAbstractAxis::setShadesBrush(const QBrush &brush)
834 834 {
835 835 if (d_ptr->m_shadesBrush != brush) {
836 836 d_ptr->m_shadesBrush = brush;
837 837 emit shadesBrushChanged(brush);
838 838 }
839 839 }
840 840
841 841 /*!
842 842 Returns brush used to draw shades.
843 843 */
844 844 QBrush QAbstractAxis::shadesBrush() const
845 845 {
846 846 if (d_ptr->m_shadesBrush == QChartPrivate::defaultBrush())
847 847 return QBrush(Qt::SolidPattern);
848 848 else
849 849 return d_ptr->m_shadesBrush;
850 850 }
851 851
852 852 void QAbstractAxis::setShadesColor(QColor color)
853 853 {
854 QBrush b = d_ptr->m_shadesBrush;
854 QBrush b = shadesBrush();
855 855 if (b.color() != color) {
856 856 b.setColor(color);
857 857 setShadesBrush(b);
858 858 emit shadesColorChanged(color);
859 859 }
860 860 }
861 861
862 862 QColor QAbstractAxis::shadesColor() const
863 863 {
864 864 return shadesBrush().color();
865 865 }
866 866
867 867 void QAbstractAxis::setShadesBorderColor(QColor color)
868 868 {
869 869 QPen p = d_ptr->m_shadesPen;
870 870 if (p.color() != color) {
871 871 p.setColor(color);
872 872 setShadesPen(p);
873 873 emit shadesColorChanged(color);
874 874 }
875 875 }
876 876
877 877 QColor QAbstractAxis::shadesBorderColor() const
878 878 {
879 879 return shadesPen().color();
880 880 }
881 881
882 882
883 883 bool QAbstractAxis::isVisible() const
884 884 {
885 885 return d_ptr->m_visible;
886 886 }
887 887
888 888 /*!
889 889 Sets axis, shades, labels and grid lines visibility to \a visible.
890 890 */
891 891 void QAbstractAxis::setVisible(bool visible)
892 892 {
893 893 if (d_ptr->m_visible != visible) {
894 894 d_ptr->m_visible = visible;
895 895 emit visibleChanged(visible);
896 896 }
897 897 }
898 898
899 899
900 900 /*!
901 901 Sets axis, shades, labels and grid lines to be visible.
902 902 */
903 903 void QAbstractAxis::show()
904 904 {
905 905 setVisible(true);
906 906 }
907 907
908 908 /*!
909 909 Sets axis, shades, labels and grid lines to not be visible.
910 910 */
911 911 void QAbstractAxis::hide()
912 912 {
913 913 setVisible(false);
914 914 }
915 915
916 916 /*!
917 917 Sets the minimum value shown on the axis.
918 918 Depending on the actual axis type the \a min parameter is converted to appropriate type.
919 919 If the conversion is impossible then the function call does nothing
920 920 */
921 921 void QAbstractAxis::setMin(const QVariant &min)
922 922 {
923 923 d_ptr->setMin(min);
924 924 }
925 925
926 926 /*!
927 927 Sets the maximum value shown on the axis.
928 928 Depending on the actual axis type the \a max parameter is converted to appropriate type.
929 929 If the conversion is impossible then the function call does nothing
930 930 */
931 931 void QAbstractAxis::setMax(const QVariant &max)
932 932 {
933 933 d_ptr->setMax(max);
934 934 }
935 935
936 936 /*!
937 937 Sets the range shown on the axis.
938 938 Depending on the actual axis type the \a min and \a max parameters are converted to appropriate types.
939 939 If the conversion is impossible then the function call does nothing.
940 940 */
941 941 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
942 942 {
943 943 d_ptr->setRange(min, max);
944 944 }
945 945
946 946
947 947 /*!
948 948 Returns the orientation in which the axis is being used (Vertical or Horizontal)
949 949 */
950 950 Qt::Orientation QAbstractAxis::orientation() const
951 951 {
952 952 return d_ptr->orientation();
953 953 }
954 954
955 955 Qt::Alignment QAbstractAxis::alignment() const
956 956 {
957 957 return d_ptr->alignment();
958 958 }
959 959
960 960 bool QAbstractAxis::isReverse() const
961 961 {
962 962 return d_ptr->m_reverse;
963 963 }
964 964
965 965 void QAbstractAxis::setReverse(bool reverse)
966 966 {
967 967 if (d_ptr->m_reverse != reverse && type() != QAbstractAxis::AxisTypeBarCategory) {
968 968 d_ptr->m_reverse = reverse;
969 969 emit reverseChanged(reverse);
970 970 }
971 971 }
972 972
973 973 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
974 974
975 975 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q)
976 976 : q_ptr(q),
977 977 m_chart(0),
978 978 m_alignment(0),
979 979 m_orientation(Qt::Orientation(0)),
980 980 m_visible(true),
981 981 m_arrowVisible(true),
982 982 m_axisPen(QChartPrivate::defaultPen()),
983 983 m_axisBrush(QChartPrivate::defaultBrush()),
984 984 m_gridLineVisible(true),
985 985 m_gridLinePen(QChartPrivate::defaultPen()),
986 986 m_minorGridLineVisible(true),
987 987 m_minorGridLinePen(QChartPrivate::defaultPen()),
988 988 m_labelsVisible(true),
989 989 m_labelsBrush(QChartPrivate::defaultBrush()),
990 990 m_labelsFont(QChartPrivate::defaultFont()),
991 991 m_labelsAngle(0),
992 992 m_titleVisible(true),
993 993 m_titleBrush(QChartPrivate::defaultBrush()),
994 994 m_titleFont(QChartPrivate::defaultFont()),
995 995 m_shadesVisible(false),
996 996 m_shadesPen(QChartPrivate::defaultPen()),
997 997 m_shadesBrush(QChartPrivate::defaultBrush()),
998 998 m_shadesOpacity(1.0),
999 999 m_dirty(false),
1000 1000 m_reverse(false)
1001 1001 {
1002 1002 }
1003 1003
1004 1004 QAbstractAxisPrivate::~QAbstractAxisPrivate()
1005 1005 {
1006 1006 }
1007 1007
1008 1008 void QAbstractAxisPrivate::setAlignment( Qt::Alignment alignment)
1009 1009 {
1010 1010 switch(alignment) {
1011 1011 case Qt::AlignTop:
1012 1012 case Qt::AlignBottom:
1013 1013 m_orientation = Qt::Horizontal;
1014 1014 break;
1015 1015 case Qt::AlignLeft:
1016 1016 case Qt::AlignRight:
1017 1017 m_orientation = Qt::Vertical;
1018 1018 break;
1019 1019 default:
1020 1020 qWarning()<<"No alignment specified !";
1021 1021 break;
1022 1022 };
1023 1023 m_alignment=alignment;
1024 1024 }
1025 1025
1026 1026 void QAbstractAxisPrivate::initializeTheme(ChartTheme* theme, bool forced)
1027 1027 {
1028 1028 if (forced || QChartPrivate::defaultPen() == m_axisPen)
1029 1029 q_ptr->setLinePen(theme->axisLinePen());
1030 1030
1031 1031 if (forced || QChartPrivate::defaultPen() == m_gridLinePen)
1032 1032 q_ptr->setGridLinePen(theme->gridLinePen());
1033 1033 if (forced || QChartPrivate::defaultPen() == m_minorGridLinePen)
1034 1034 q_ptr->setMinorGridLinePen(theme->minorGridLinePen());
1035 1035
1036 1036 if (forced || QChartPrivate::defaultBrush() == m_labelsBrush)
1037 1037 q_ptr->setLabelsBrush(theme->labelBrush());
1038 1038 if (forced || QChartPrivate::defaultFont() == m_labelsFont)
1039 1039 q_ptr->setLabelsFont(theme->labelFont());
1040 1040
1041 1041 if (forced || QChartPrivate::defaultBrush() == m_titleBrush)
1042 1042 q_ptr->setTitleBrush(theme->labelBrush());
1043 1043 if (forced || QChartPrivate::defaultFont() == m_titleFont) {
1044 1044 QFont font(m_labelsFont);
1045 1045 font.setBold(true);
1046 1046 q_ptr->setTitleFont(font);
1047 1047 }
1048 1048
1049 1049 if (forced || QChartPrivate::defaultBrush() == m_shadesBrush)
1050 1050 q_ptr->setShadesBrush(theme->backgroundShadesBrush());
1051 1051 if (forced || QChartPrivate::defaultPen() == m_shadesPen)
1052 1052 q_ptr->setShadesPen(theme->backgroundShadesPen());
1053 1053
1054 1054 bool axisX = m_orientation == Qt::Horizontal;
1055 1055 if (forced && (theme->backgroundShades() == ChartTheme::BackgroundShadesBoth
1056 1056 || (theme->backgroundShades() == ChartTheme::BackgroundShadesVertical && axisX)
1057 1057 || (theme->backgroundShades() == ChartTheme::BackgroundShadesHorizontal && !axisX))) {
1058 1058 q_ptr->setShadesVisible(true);
1059 1059 } else if (forced) {
1060 1060 q_ptr->setShadesVisible(false);
1061 1061 }
1062 1062 }
1063 1063
1064 1064 void QAbstractAxisPrivate::handleRangeChanged(qreal min, qreal max)
1065 1065 {
1066 1066 setRange(min,max);
1067 1067 }
1068 1068
1069 1069 void QAbstractAxisPrivate::initializeGraphics(QGraphicsItem* parent)
1070 1070 {
1071 1071 Q_UNUSED(parent);
1072 1072 }
1073 1073
1074 1074 void QAbstractAxisPrivate::initializeAnimations(QChart::AnimationOptions options, int duration,
1075 1075 QEasingCurve &curve)
1076 1076 {
1077 1077 ChartAxisElement *axis = m_item.data();
1078 1078 Q_ASSERT(axis);
1079 1079 if (axis->animation())
1080 1080 axis->animation()->stopAndDestroyLater();
1081 1081
1082 1082 if (options.testFlag(QChart::GridAxisAnimations))
1083 1083 axis->setAnimation(new AxisAnimation(axis, duration, curve));
1084 1084 else
1085 1085 axis->setAnimation(0);
1086 1086 }
1087 1087
1088 1088
1089 1089
1090 1090 #include "moc_qabstractaxis.cpp"
1091 1091 #include "moc_qabstractaxis_p.cpp"
1092 1092
1093 1093 QT_CHARTS_END_NAMESPACE
@@ -1,641 +1,650
1 1 /******************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd.
4 4 ** Contact: http://www.qt.io/licensing/
5 5 **
6 6 ** This file is part of the Qt Charts module.
7 7 **
8 8 ** $QT_BEGIN_LICENSE:COMM$
9 9 **
10 10 ** Commercial License Usage
11 11 ** Licensees holding valid commercial Qt licenses may use this file in
12 12 ** accordance with the commercial license agreement provided with the
13 13 ** Software or, alternatively, in accordance with the terms contained in
14 14 ** a written agreement between you and The Qt Company. For licensing terms
15 15 ** and conditions see http://www.qt.io/terms-conditions. For further
16 16 ** information use the contact form at http://www.qt.io/contact-us.
17 17 **
18 18 ** $QT_END_LICENSE$
19 19 **
20 20 ******************************************************************************/
21 21
22 22 #include <QtCharts/QLegend>
23 23 #include <private/qlegend_p.h>
24 24 #include <QtCharts/QAbstractSeries>
25 25 #include <private/qabstractseries_p.h>
26 26 #include <private/qchart_p.h>
27 27 #include <private/legendlayout_p.h>
28 28 #include <private/chartpresenter_p.h>
29 29 #include <private/abstractchartlayout_p.h>
30 30 #include <QtCharts/QLegendMarker>
31 31 #include <private/qlegendmarker_p.h>
32 32 #include <private/legendmarkeritem_p.h>
33 33 #include <private/chartdataset_p.h>
34 34 #include <QtGui/QPainter>
35 35 #include <QtGui/QPen>
36 36 #include <QtWidgets/QGraphicsItemGroup>
37 37
38 38 QT_CHARTS_BEGIN_NAMESPACE
39 39
40 40 /*!
41 41 \class QLegend
42 42 \inmodule Qt Charts
43 43 \inherits QGraphicsWidget
44 44 \brief Legend object.
45 45
46 46 QLegend is a graphical object for displaying the legend of the chart. Legend state is updated by QChart, when
47 47 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
48 48 handle the drawing manually.
49 49 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
50 50
51 51 \image examples_percentbarchart_legend.png
52 52
53 53 \sa QChart
54 54 */
55 55 /*!
56 56 \qmltype Legend
57 57 \instantiates QLegend
58 58 \inqmlmodule QtCharts
59 59
60 60 \brief Legend is part of Qt Chart QML API.
61 61
62 62 Legend is a graphical object, which displays legend of the chart. Legend state is updated by
63 63 ChartView, when series have been changed. Legend is used via ChartView class. For example:
64 64 \code
65 65 ChartView {
66 66 legend.visible: true
67 67 legend.alignment: Qt.AlignBottom
68 68 // Add a few series...
69 69 }
70 70 \endcode
71 71
72 72 \image examples_percentbarchart_legend.png
73 73
74 74 Please note that there is no QML API available for modifying legend markers, unlike in the Qt
75 75 API of Charts. The use case of modifying markers can be implemented for example by creating your
76 76 own custom legend. For an example on how to do this,
77 77 see \l {qmlcustomlegend}{Qml Custom Example} application.
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 \qmlproperty bool Legend::visible
106 106 \brief Whether the legend is visible or not.
107 107
108 108 By default, this property is \c true.
109 109 \sa QGraphicsObject::visible
110 110 */
111 111
112 112 /*!
113 113 \property QLegend::color
114 114 The color of the legend, i.e. the background (brush) color. Note that if you change the color
115 115 of the legend, the style of the legend brush is set to Qt::SolidPattern.
116 116 */
117 117 /*!
118 118 \qmlproperty color Legend::color
119 119 The color of the legend, i.e. the background (brush) color.
120 120 */
121 121
122 122 /*!
123 123 \property QLegend::borderColor
124 124 The border color of the legend, i.e. the line color.
125 125 */
126 126 /*!
127 127 \qmlproperty color Legend::borderColor
128 128 The border color of the legend, i.e. the line color.
129 129 */
130 130
131 131 /*!
132 132 \property QLegend::font
133 133 The font of markers used by legend.
134 134 */
135 135 /*!
136 136 \qmlproperty Font Legend::font
137 137 The font of markers used by legend.
138 138 */
139 139
140 140 /*!
141 141 \property QLegend::labelColor
142 142 The color of brush used to draw labels.
143 143 */
144 144 /*!
145 145 \qmlproperty color Legend::labelColor
146 146 The color of brush used to draw labels.
147 147 */
148 148
149 149 /*!
150 150 \property QLegend::reverseMarkers
151 151 Whether reverse order is used for the markers in legend or not. False by default.
152 152 */
153 153 /*!
154 154 \qmlproperty bool Legend::reverseMarkers
155 155 Whether reverse order is used for the markers in legend or not. False by default.
156 156 */
157 157
158 158 /*!
159 159 \fn void QLegend::backgroundVisibleChanged(bool)
160 160 The visibility of the legend background changed to \a visible.
161 161 */
162 162
163 163 /*!
164 164 \fn void QLegend::colorChanged(QColor)
165 165 The color of the legend background changed to \a color.
166 166 */
167 167
168 168 /*!
169 169 \fn void QLegend::borderColorChanged(QColor)
170 170 The border color of the legend background changed to \a color.
171 171 */
172 172
173 173 /*!
174 174 \fn void QLegend::fontChanged(QFont)
175 175 The font of markers of the legend changed to \a font.
176 176 */
177 177
178 178 /*!
179 179 \fn void QLegend::labelColorChanged(QColor color)
180 180 This signal is emitted when the color of brush used to draw labels has changed to \a color.
181 181 */
182 182
183 183 /*!
184 184 \fn void QLegend::reverseMarkersChanged(bool)
185 185 The use of reverse order for the markers in legend is changed to \a reverseMarkers.
186 186 */
187 187
188 188 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
189 189 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
190 190 {
191 191 setZValue(ChartPresenter::LegendZValue);
192 192 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
193 193 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
194 194 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
195 195 setLayout(d_ptr->m_layout);
196 196 }
197 197
198 198 /*!
199 199 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
200 200 */
201 201 QLegend::~QLegend()
202 202 {
203 203 }
204 204
205 205 /*!
206 206 \internal
207 207 */
208 208 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
209 209 {
210 210 Q_UNUSED(option)
211 211 Q_UNUSED(widget)
212 212
213 213 if (!d_ptr->m_backgroundVisible)
214 214 return;
215 215
216 216 painter->setOpacity(opacity());
217 217 painter->setPen(d_ptr->m_pen);
218 218 painter->setBrush(d_ptr->m_brush);
219 219 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
220 220 }
221 221
222 222
223 223 /*!
224 224 Sets the \a brush of legend. Brush affects the background of legend.
225 225 */
226 226 void QLegend::setBrush(const QBrush &brush)
227 227 {
228 228 if (d_ptr->m_brush != brush) {
229 229 d_ptr->m_brush = brush;
230 230 update();
231 231 emit colorChanged(brush.color());
232 232 }
233 233 }
234 234
235 235 /*!
236 236 Returns the brush used by legend.
237 237 */
238 238 QBrush QLegend::brush() const
239 239 {
240 return d_ptr->m_brush;
240 if (d_ptr->m_brush == QChartPrivate::defaultBrush())
241 return QBrush();
242 else
243 return d_ptr->m_brush;
241 244 }
242 245
243 246 void QLegend::setColor(QColor color)
244 247 {
245 QBrush b = d_ptr->m_brush;
248 QBrush b = brush();
246 249 if (b.style() != Qt::SolidPattern || b.color() != color) {
247 250 b.setStyle(Qt::SolidPattern);
248 251 b.setColor(color);
249 252 setBrush(b);
250 253 }
251 254 }
252 255
253 256 QColor QLegend::color()
254 257 {
255 258 return d_ptr->m_brush.color();
256 259 }
257 260
258 261 /*!
259 262 Sets the \a pen of legend. Pen affects the legend borders.
260 263 */
261 264 void QLegend::setPen(const QPen &pen)
262 265 {
263 266 if (d_ptr->m_pen != pen) {
264 267 d_ptr->m_pen = pen;
265 268 update();
266 269 emit borderColorChanged(pen.color());
267 270 }
268 271 }
269 272
270 273 /*!
271 274 Returns the pen used by legend.
272 275 */
273 276
274 277 QPen QLegend::pen() const
275 278 {
276 return d_ptr->m_pen;
279 if (d_ptr->m_pen == QChartPrivate::defaultPen())
280 return QPen();
281 else
282 return d_ptr->m_pen;
277 283 }
278 284
279 285 void QLegend::setFont(const QFont &font)
280 286 {
281 287 if (d_ptr->m_font != font) {
282 288 // Hide items to avoid flickering
283 289 d_ptr->items()->setVisible(false);
284 290 d_ptr->m_font = font;
285 291 foreach (QLegendMarker *marker, d_ptr->markers()) {
286 292 marker->setFont(d_ptr->m_font);
287 293 }
288 294 layout()->invalidate();
289 295 emit fontChanged(font);
290 296 }
291 297 }
292 298
293 299 QFont QLegend::font() const
294 300 {
295 301 return d_ptr->m_font;
296 302 }
297 303
298 304 void QLegend::setBorderColor(QColor color)
299 305 {
300 QPen p = d_ptr->m_pen;
306 QPen p = pen();
301 307 if (p.color() != color) {
302 308 p.setColor(color);
303 309 setPen(p);
304 310 }
305 311 }
306 312
307 313 QColor QLegend::borderColor()
308 314 {
309 315 return d_ptr->m_pen.color();
310 316 }
311 317
312 318 /*!
313 319 Set brush used to draw labels to \a brush.
314 320 */
315 321 void QLegend::setLabelBrush(const QBrush &brush)
316 322 {
317 323 if (d_ptr->m_labelBrush != brush) {
318 324 d_ptr->m_labelBrush = brush;
319 325 foreach (QLegendMarker *marker, d_ptr->markers()) {
320 326 marker->setLabelBrush(d_ptr->m_labelBrush);
321 327 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
322 328 // instead of mapping it from label brush color
323 329 marker->setPen(brush.color());
324 330 }
325 331 emit labelColorChanged(brush.color());
326 332 }
327 333 }
328 334
329 335 /*!
330 336 Brush used to draw labels.
331 337 */
332 338 QBrush QLegend::labelBrush() const
333 339 {
334 return d_ptr->m_labelBrush;
340 if (d_ptr->m_labelBrush == QChartPrivate::defaultBrush())
341 return QBrush();
342 else
343 return d_ptr->m_labelBrush;
335 344 }
336 345
337 346 void QLegend::setLabelColor(QColor color)
338 347 {
339 QBrush b = d_ptr->m_labelBrush;
348 QBrush b = labelBrush();
340 349 if (b.style() != Qt::SolidPattern || b.color() != color) {
341 350 b.setStyle(Qt::SolidPattern);
342 351 b.setColor(color);
343 352 setLabelBrush(b);
344 353 }
345 354 }
346 355
347 356 QColor QLegend::labelColor() const
348 357 {
349 358 return d_ptr->m_labelBrush.color();
350 359 }
351 360
352 361
353 362 void QLegend::setAlignment(Qt::Alignment alignment)
354 363 {
355 364 if (d_ptr->m_alignment != alignment) {
356 365 d_ptr->m_alignment = alignment;
357 366 layout()->invalidate();
358 367 }
359 368 }
360 369
361 370 Qt::Alignment QLegend::alignment() const
362 371 {
363 372 return d_ptr->m_alignment;
364 373 }
365 374
366 375 /*!
367 376 Detaches the legend from chart. Chart won't change layout of the legend.
368 377 */
369 378 void QLegend::detachFromChart()
370 379 {
371 380 d_ptr->m_attachedToChart = false;
372 381 // layout()->invalidate();
373 382 d_ptr->m_chart->layout()->invalidate();
374 383 setParent(0);
375 384
376 385 }
377 386
378 387 /*!
379 388 Attaches the legend to chart. Chart may change layout of the legend.
380 389 */
381 390 void QLegend::attachToChart()
382 391 {
383 392 d_ptr->m_attachedToChart = true;
384 393 // layout()->invalidate();
385 394 d_ptr->m_chart->layout()->invalidate();
386 395 setParent(d_ptr->m_chart);
387 396 }
388 397
389 398 /*!
390 399 Returns true, if legend is attached to chart.
391 400 */
392 401 bool QLegend::isAttachedToChart()
393 402 {
394 403 return d_ptr->m_attachedToChart;
395 404 }
396 405
397 406 /*!
398 407 Sets the visibility of legend background to \a visible
399 408 */
400 409 void QLegend::setBackgroundVisible(bool visible)
401 410 {
402 411 if (d_ptr->m_backgroundVisible != visible) {
403 412 d_ptr->m_backgroundVisible = visible;
404 413 update();
405 414 emit backgroundVisibleChanged(visible);
406 415 }
407 416 }
408 417
409 418 /*!
410 419 Returns the visibility of legend background
411 420 */
412 421 bool QLegend::isBackgroundVisible() const
413 422 {
414 423 return d_ptr->m_backgroundVisible;
415 424 }
416 425
417 426 /*!
418 427 Returns the list of markers in legend. The list can be filtered with \a series parameter.
419 428 If \a series is given, only markers related to that series are returned.
420 429 */
421 430 QList<QLegendMarker*> QLegend::markers(QAbstractSeries *series) const
422 431 {
423 432 return d_ptr->markers(series);
424 433 }
425 434
426 435 bool QLegend::reverseMarkers()
427 436 {
428 437 return d_ptr->m_reverseMarkers;
429 438 }
430 439
431 440 void QLegend::setReverseMarkers(bool reverseMarkers)
432 441 {
433 442 if (d_ptr->m_reverseMarkers != reverseMarkers) {
434 443 d_ptr->m_reverseMarkers = reverseMarkers;
435 444 layout()->invalidate();
436 445 emit reverseMarkersChanged(reverseMarkers);
437 446 }
438 447 }
439 448
440 449 /*!
441 450 \internal \a event see QGraphicsWidget for details
442 451 */
443 452 void QLegend::hideEvent(QHideEvent *event)
444 453 {
445 454 if (isAttachedToChart())
446 455 d_ptr->m_presenter->layout()->invalidate();
447 456 QGraphicsWidget::hideEvent(event);
448 457 }
449 458 /*!
450 459 \internal \a event see QGraphicsWidget for details
451 460 */
452 461 void QLegend::showEvent(QShowEvent *event)
453 462 {
454 463 if (isAttachedToChart())
455 464 layout()->invalidate();
456 465 QGraphicsWidget::showEvent(event);
457 466 //layout activation will show the items
458 467 }
459 468
460 469 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
461 470
462 471 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
463 472 : q_ptr(q),
464 473 m_presenter(presenter),
465 474 m_layout(new LegendLayout(q)),
466 475 m_chart(chart),
467 476 m_items(new QGraphicsItemGroup(q)),
468 477 m_alignment(Qt::AlignTop),
469 m_brush(QBrush()),
470 m_pen(QPen()),
471 m_labelBrush(QBrush()),
478 m_brush(QChartPrivate::defaultBrush()),
479 m_pen(QChartPrivate::defaultPen()),
480 m_labelBrush(QChartPrivate::defaultBrush()),
472 481 m_diameter(5),
473 482 m_attachedToChart(true),
474 483 m_backgroundVisible(false),
475 484 m_reverseMarkers(false)
476 485 {
477 486 m_items->setHandlesChildEvents(false);
478 487 }
479 488
480 489 QLegendPrivate::~QLegendPrivate()
481 490 {
482 491
483 492 }
484 493
485 494 void QLegendPrivate::setOffset(const QPointF &offset)
486 495 {
487 496 m_layout->setOffset(offset.x(), offset.y());
488 497 }
489 498
490 499 QPointF QLegendPrivate::offset() const
491 500 {
492 501 return m_layout->offset();
493 502 }
494 503
495 504 int QLegendPrivate::roundness(qreal size)
496 505 {
497 506 return 100 * m_diameter / int(size);
498 507 }
499 508
500 509 QList<QLegendMarker*> QLegendPrivate::markers(QAbstractSeries *series)
501 510 {
502 511 // Return all markers
503 512 if (!series) {
504 513 return m_markers;
505 514 }
506 515
507 516 // Create filtered list
508 517 QList<QLegendMarker *> markers;
509 518 foreach (QLegendMarker *marker, m_markers) {
510 519 if (marker->series() == series) {
511 520 markers.append(marker);
512 521 }
513 522 }
514 523 return markers;
515 524 }
516 525
517 526 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
518 527 {
519 528 if (m_series.contains(series)) {
520 529 return;
521 530 }
522 531
523 532 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
524 533 decorateMarkers(newMarkers);
525 534 addMarkers(newMarkers);
526 535
527 536 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
528 537 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
529 538
530 539 m_series.append(series);
531 540 m_items->setVisible(false);
532 541 m_layout->invalidate();
533 542 }
534 543
535 544 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
536 545 {
537 546 if (m_series.contains(series)) {
538 547 m_series.removeOne(series);
539 548 }
540 549
541 550 // Find out, which markers to remove
542 551 QList<QLegendMarker *> removed;
543 552 foreach (QLegendMarker *m, m_markers) {
544 553 if (m->series() == series) {
545 554 removed << m;
546 555 }
547 556 }
548 557 removeMarkers(removed);
549 558
550 559 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
551 560 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
552 561
553 562 m_layout->invalidate();
554 563 }
555 564
556 565 void QLegendPrivate::handleSeriesVisibleChanged()
557 566 {
558 567 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
559 568 Q_ASSERT(series);
560 569
561 570 foreach (QLegendMarker *marker, m_markers) {
562 571 if (marker->series() == series) {
563 572 marker->setVisible(series->isVisible());
564 573 }
565 574 }
566 575
567 576 if (m_chart->isVisible())
568 577 m_layout->invalidate();
569 578 }
570 579
571 580 void QLegendPrivate::handleCountChanged()
572 581 {
573 582 // Here we handle the changes in marker count.
574 583 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
575 584
576 585 QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender());
577 586 QList<QLegendMarker *> createdMarkers = series->createLegendMarkers(q_ptr);
578 587
579 588 // Find out removed markers and created markers
580 589 QList<QLegendMarker *> removedMarkers;
581 590 foreach (QLegendMarker *oldMarker, m_markers) {
582 591 // we have marker, which is related to sender.
583 592 if (oldMarker->series() == series->q_ptr) {
584 593 bool found = false;
585 594 foreach(QLegendMarker *newMarker, createdMarkers) {
586 595 // New marker considered existing if:
587 596 // - d_ptr->relatedObject() is same for both markers.
588 597 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
589 598 // Delete the new marker, since we already have existing marker, that might be connected on user side.
590 599 found = true;
591 600 createdMarkers.removeOne(newMarker);
592 601 delete newMarker;
593 602 }
594 603 }
595 604 if (!found) {
596 605 // No related object found for marker, add to removedMarkers list
597 606 removedMarkers << oldMarker;
598 607 }
599 608 }
600 609 }
601 610
602 611 removeMarkers(removedMarkers);
603 612 decorateMarkers(createdMarkers);
604 613 addMarkers(createdMarkers);
605 614
606 615 q_ptr->layout()->invalidate();
607 616 }
608 617
609 618 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
610 619 {
611 620 foreach (QLegendMarker *marker, markers) {
612 621 m_items->addToGroup(marker->d_ptr.data()->item());
613 622 m_markers << marker;
614 623 m_markerHash.insert(marker->d_ptr->item(), marker);
615 624 }
616 625 }
617 626
618 627 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
619 628 {
620 629 foreach (QLegendMarker *marker, markers) {
621 630 marker->d_ptr->item()->setVisible(false);
622 631 m_items->removeFromGroup(marker->d_ptr->item());
623 632 m_markers.removeOne(marker);
624 633 m_markerHash.remove(marker->d_ptr->item());
625 634 delete marker;
626 635 }
627 636 }
628 637
629 638 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
630 639 {
631 640 foreach (QLegendMarker *marker, markers) {
632 641 marker->setFont(m_font);
633 642 marker->setLabelBrush(m_labelBrush);
634 643 }
635 644 }
636 645
637 646
638 647 #include "moc_qlegend.cpp"
639 648 #include "moc_qlegend_p.cpp"
640 649
641 650 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now