##// END OF EJS Templates
fix to QTRD-1519:Setting color for a BarSet has no effect. Theme was overriding the color, which was defined on QML side
sauimone -
r1788:9c93fca09947
parent child
Show More
@@ -1,638 +1,645
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 "qbarset.h"
22 22 #include "qbarset_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QBarSet
28 28 \brief Building block for different bar charts
29 29
30 30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 31 First value of set is assumed to belong to first category, second to second category and so on.
32 32 If set has fewer values than there are categories, then the missing values are assumed to be
33 33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34 34
35 35 \mainclass
36 36
37 37 \sa QAbstractBarSeries, QBarSeries, QStackedBarSeries, QPercentBarSeries
38 38 */
39 39 /*!
40 40 \qmlclass BarSet QBarSet
41 41
42 42 BarSet represents one set of bars. Set of bars contains one data value for each category.
43 43 First value of set is assumed to belong to first category, second to second category and so on.
44 44 If set has fewer values than there are categories, then the missing values are assumed to be
45 45 at the end of set. For missing values in middle of a set, numerical value of zero is used.
46 46 \sa AbstractBarSeries, BarSeries, StackedBarSeries, PercentBarSeries
47 47 */
48 48
49 49 /*!
50 50 \property QBarSet::label
51 51 Defines the label of the barSet.
52 52 */
53 53 /*!
54 54 \qmlproperty string BarSet::label
55 55 Defines the label of the barSet.
56 56 */
57 57
58 58 /*!
59 59 \property QBarSet::pen
60 60 \brief Defines the pen used by the barSet.
61 61 */
62 62
63 63 /*!
64 64 \property QBarSet::brush
65 65 \brief Defines the brush used by the barSet.
66 66 */
67 67
68 68 /*!
69 69 \property QBarSet::labelBrush
70 70 \brief Defines the brush used by the barSet's label.
71 71 */
72 72
73 73 /*!
74 74 \property QBarSet::labelFont
75 75 \brief Defines the font used by the barSet's label.
76 76 */
77 77
78 78 /*!
79 79 \qmlproperty Font BarSet::labelFont
80 80 Defines the font used by the barSet's label.
81 81
82 82 See the \l {Font} {QML Font Element} for detailed documentation.
83 83 */
84 84
85 85 /*!
86 86 \property QBarSet::color
87 87 The fill (brush) color of the bar set.
88 88 */
89 89 /*!
90 90 \qmlproperty color BarSet::color
91 91 The fill (brush) color of the bar set.
92 92 */
93 93
94 94 /*!
95 95 \property QBarSet::borderColor
96 96 The line (pen) color of the bar set.
97 97 */
98 98 /*!
99 99 \qmlproperty color BarSet::borderColor
100 100 The line (pen) color of the bar set.
101 101 */
102 102
103 103 /*!
104 104 \property QBarSet::labelColor
105 105 The text (label) color of the bar set.
106 106 */
107 107 /*!
108 108 \qmlproperty color BarSet::labelColor
109 109 The text (label) color of the bar set.
110 110 */
111 111
112 112 /*!
113 113 \fn void QBarSet::clicked(int index)
114 114
115 115 The signal is emitted if the user clicks with a mouse on top of barset.
116 116 Clicked bar inside set is indexed by \a index
117 117 */
118 118
119 119 /*!
120 120 \fn void QBarSet::hovered(bool status)
121 121
122 122 The signal is emitted if mouse is hovered on top of barset.
123 123 Parameter \a status is true, if mouse entered on top of barset, false if mouse left from top of barset.
124 124 */
125 125
126 126
127 127 /*!
128 128 \fn void QBarSet::labelChanged()
129 129 This signal is emitted when the label of the barSet has changed.
130 130 \sa label
131 131 */
132 132 /*!
133 133 \qmlsignal BarSet::onLabelChanged()
134 134 This signal is emitted when the label of the barSet has changed.
135 135 */
136 136
137 137 /*!
138 138 \fn void QBarSet::penChanged()
139 139 This signal is emitted when the pen of the barSet has changed.
140 140 \sa pen
141 141 */
142 142
143 143 /*!
144 144 \fn void QBarSet::brushChanged()
145 145 This signal is emitted when the brush of the barSet has changed.
146 146 \sa brush
147 147 */
148 148
149 149 /*!
150 150 \fn void QBarSet::labelBrushChanged()
151 151 This signal is emitted when the brush of the barSet's label has changed.
152 152 \sa labelBrush
153 153 */
154 154
155 155 /*!
156 156 \fn void QBarSet::labelFontChanged()
157 157 This signal is emitted when the font of the barSet's label has changed.
158 158 \sa labelBrush
159 159 */
160 160
161 161 /*!
162 162 \fn void QBarSet::colorChanged(QColor)
163 163 This signal is emitted when the fill (brush) color of the set has changed to \a color.
164 164 */
165 165 /*!
166 166 \qmlsignal BarSet::onColorChanged(color color)
167 167 This signal is emitted when the fill (brush) color of the set has changed to \a color.
168 168 */
169 169
170 170 /*!
171 171 \fn void QBarSet::borderColorChanged(QColor)
172 172 This signal is emitted when the line (pen) color of the set has changed to \a color.
173 173 */
174 174 /*!
175 175 \qmlsignal BarSet::onBorderColorChanged(color color)
176 176 This signal is emitted when the line (pen) color of the set has changed to \a color.
177 177 */
178 178
179 179 /*!
180 180 \fn void QBarSet::labelColorChanged(QColor)
181 181 This signal is emitted when the text (label) color of the set has changed to \a color.
182 182 */
183 183 /*!
184 184 \qmlsignal BarSet::onLabelColorChanged(color color)
185 185 This signal is emitted when the text (label) color of the set has changed to \a color.
186 186 */
187 187
188 188 /*!
189 189 \fn void QBarSet::valuesAdded(int index, int count)
190 190 This signal is emitted when new values have been added to the set.
191 191 Parameter \a index indicates the position of the first inserted value.
192 192 Parameter \a count is the number of iserted values.
193 193 \sa append(), insert()
194 194 */
195 195 /*!
196 196 \qmlsignal BarSet::onValuesAdded(int index, int count)
197 197 This signal is emitted when new values have been added to the set.
198 198 Parameter \a index indicates the position of the first inserted value.
199 199 Parameter \a count is the number of iserted values.
200 200 */
201 201
202 202 /*!
203 203 \fn void QBarSet::valuesRemoved(int index, int count)
204 204 This signal is emitted values have been removed from the set.
205 205 Parameter \a index indicates the position of the first removed value.
206 206 Parameter \a count is the number of removed values.
207 207 \sa remove()
208 208 */
209 209 /*!
210 210 \qmlsignal BarSet::onValuesRemoved(int index, int count)
211 211 This signal is emitted values have been removed from the set.
212 212 Parameter \a index indicates the position of the first removed value.
213 213 Parameter \a count is the number of removed values.
214 214 */
215 215
216 216 /*!
217 217 \fn void QBarSet::valueChanged(int index)
218 218 This signal is emitted values the value in the set has been modified.
219 219 Parameter \a index indicates the position of the modified value.
220 220 \sa at()
221 221 */
222 222 /*!
223 223 \qmlsignal BarSet::onValueChanged(int index)
224 224 This signal is emitted values the value in the set has been modified.
225 225 Parameter \a index indicates the position of the modified value.
226 226 */
227 227
228 228 /*!
229 229 \qmlproperty int BarSet::count
230 230 The count of values on the barset
231 231 */
232 232
233 233 /*!
234 234 \qmlproperty QVariantList BarSet::values
235 235 The values of the barset. You can set either a list of reals or a list of points as values. If you set a list of
236 236 reals as values, the values are automatically completed to points by using the index of a value as it's
237 237 x-coordinate. For example:
238 238 \code
239 239 myBarSet1.values = [0, 5, 1, 5];
240 240 myBarSet2.values = [Qt.point(0, 1), Qt.point(1, 5), Qt.point(2.2, 4.3)];
241 241 \endcode
242 242 */
243 243
244 244 /*!
245 245 Constructs QBarSet with a label of \a label and with parent of \a parent
246 246 */
247 247 QBarSet::QBarSet(const QString label, QObject *parent)
248 248 : QObject(parent)
249 249 ,d_ptr(new QBarSetPrivate(label,this))
250 250 {
251 251 }
252 252
253 253 /*!
254 254 Destroys the barset
255 255 */
256 256 QBarSet::~QBarSet()
257 257 {
258 258 // NOTE: d_ptr destroyed by QObject
259 259 }
260 260
261 261 /*!
262 262 Sets new \a label for set.
263 263 */
264 264 void QBarSet::setLabel(const QString label)
265 265 {
266 266 d_ptr->m_label = label;
267 267 emit labelChanged();
268 268 }
269 269
270 270 /*!
271 271 Returns label of the set.
272 272 */
273 273 QString QBarSet::label() const
274 274 {
275 275 return d_ptr->m_label;
276 276 }
277 277
278 278 /*!
279 279 Appends new value \a value to the end of set.
280 280 */
281 281 void QBarSet::append(const qreal value)
282 282 {
283 283 // Convert to QPointF
284 284 int index = d_ptr->m_values.count();
285 285 d_ptr->append(QPointF(d_ptr->m_values.count(), value));
286 286 emit valuesAdded(index, 1);
287 287 }
288 288
289 289 /*!
290 290 Appends a list of reals to set. Works like append with single real value. The \a values in list
291 291 are appended to end of barset
292 292 \sa append()
293 293 */
294 294 void QBarSet::append(const QList<qreal> &values)
295 295 {
296 296 int index = d_ptr->m_values.count();
297 297 d_ptr->append(values);
298 298 emit valuesAdded(index, values.count());
299 299 }
300 300
301 301 /*!
302 302 Convinience operator. Same as append, with real \a value.
303 303 \sa append()
304 304 */
305 305 QBarSet& QBarSet::operator << (const qreal &value)
306 306 {
307 307 append(value);
308 308 return *this;
309 309 }
310 310
311 311 /*!
312 312 Inserts new \a value on the \a index position.
313 313 The value that is currently at this postion is moved to postion index + 1
314 314 \sa remove()
315 315 */
316 316 void QBarSet::insert(const int index, const qreal value)
317 317 {
318 318 d_ptr->insert(index, value);
319 319 emit valuesAdded(index,1);
320 320 }
321 321
322 322 /*!
323 323 Removes \a count number of values from the set starting at \a index.
324 324 \sa insert()
325 325 */
326 326 void QBarSet::remove(const int index, const int count)
327 327 {
328 328 int removedCount = d_ptr->remove(index,count);
329 329 if (removedCount > 0) {
330 330 emit valuesRemoved(index,removedCount);
331 331 }
332 332 return;
333 333 }
334 334
335 335 /*!
336 336 Sets a new value \a value to set, indexed by \a index
337 337 */
338 338 void QBarSet::replace(const int index, const qreal value)
339 339 {
340 340 if (index >= 0 && index < d_ptr->m_values.count()) {
341 341 d_ptr->replace(index,value);
342 342 emit valueChanged(index);
343 343 }
344 344 }
345 345
346 346
347 347 /*!
348 348 Returns value of set indexed by \a index.
349 349 If the index is out of bounds 0.0 is returned.
350 350 */
351 351 qreal QBarSet::at(const int index) const
352 352 {
353 353 if (index < 0 || index >= d_ptr->m_values.count()) {
354 354 return 0;
355 355 }
356 356
357 357 return d_ptr->m_values.at(index).y();
358 358 }
359 359
360 360 /*!
361 361 Returns value of set indexed by \a index.
362 362 If the index is out of bounds 0.0 is returned.
363 363 */
364 364 qreal QBarSet::operator [](const int index) const
365 365 {
366 366 return at(index);
367 367 }
368 368
369 369 /*!
370 370 Returns count of values in set.
371 371 */
372 372 int QBarSet::count() const
373 373 {
374 374 return d_ptr->m_values.count();
375 375 }
376 376
377 377 /*!
378 378 Returns sum of all values in barset.
379 379 */
380 380 qreal QBarSet::sum() const
381 381 {
382 382 qreal total(0);
383 383 for (int i=0; i < d_ptr->m_values.count(); i++) {
384 384 total += d_ptr->m_values.at(i).y();
385 385 }
386 386 return total;
387 387 }
388 388
389 389 /*!
390 390 Sets pen for set. Bars of this set are drawn using \a pen
391 391 */
392 392 void QBarSet::setPen(const QPen &pen)
393 393 {
394 394 if(d_ptr->m_pen!=pen){
395 395 d_ptr->m_pen = pen;
396 396 emit d_ptr->updatedBars();
397 397 emit penChanged();
398 398 }
399 399 }
400 400
401 401 /*!
402 402 Returns pen of the set.
403 403 */
404 404 QPen QBarSet::pen() const
405 405 {
406 406 return d_ptr->m_pen;
407 407 }
408 408
409 409 /*!
410 410 Sets brush for the set. Bars of this set are drawn using \a brush
411 411 */
412 412 void QBarSet::setBrush(const QBrush &brush)
413 413 {
414 414 if(d_ptr->m_brush!=brush){
415 415 d_ptr->m_brush = brush;
416 416 emit d_ptr->updatedBars();
417 417 emit brushChanged();
418 418 }
419 419 }
420 420
421 421 /*!
422 422 Returns brush of the set.
423 423 */
424 424 QBrush QBarSet::brush() const
425 425 {
426 426 return d_ptr->m_brush;
427 427 }
428 428
429 429 /*!
430 430 Sets \a brush of the values that are drawn on top of this barset
431 431 */
432 432 void QBarSet::setLabelBrush(const QBrush &brush)
433 433 {
434 434 if(d_ptr->m_labelBrush!=brush){
435 435 d_ptr->m_labelBrush = brush;
436 436 emit d_ptr->updatedBars();
437 437 emit labelBrushChanged();
438 438 }
439 439 }
440 440
441 441 /*!
442 442 Returns brush of the values that are drawn on top of this barset
443 443 */
444 444 QBrush QBarSet::labelBrush() const
445 445 {
446 446 return d_ptr->m_labelBrush;
447 447 }
448 448
449 449 /*!
450 450 Sets the \a font for values that are drawn on top of this barset
451 451 */
452 452 void QBarSet::setLabelFont(const QFont &font)
453 453 {
454 454 if(d_ptr->m_labelFont!=font) {
455 455 d_ptr->m_labelFont = font;
456 456 emit d_ptr->updatedBars();
457 457 emit labelFontChanged();
458 458 }
459 459
460 460 }
461 461
462 462 /*!
463 463 Returns the pen for values that are drawn on top of this barset
464 464 */
465 465 QFont QBarSet::labelFont() const
466 466 {
467 467 return d_ptr->m_labelFont;
468 468 }
469 469
470 470 /*!
471 471 Returns the color of the brush of barset.
472 472 */
473 473 QColor QBarSet::color()
474 474 {
475 475 return brush().color();
476 476 }
477 477
478 478 /*!
479 479 Sets the \a color of brush for this barset
480 480 */
481 481 void QBarSet::setColor(QColor color)
482 482 {
483 483 QBrush b = brush();
484 if (b.color() != color) {
484 if ((b.color() != color) || (b.style() == Qt::NoBrush)) {
485 485 b.setColor(color);
486 if (b.style() == Qt::NoBrush) {
487 // Set tyle to Qt::SolidPattern. (Default is Qt::NoBrush)
488 // This prevents theme to override color defined in QML side:
489 // BarSet { label: "Bob"; color:"red"; values: [1,2,3] }
490 // The color must be obeyed, since user wanted it.
491 b.setStyle(Qt::SolidPattern);
492 }
486 493 setBrush(b);
487 494 emit colorChanged(color);
488 495 }
489 496 }
490 497
491 498 /*!
492 499 Returns the color of pen of this barset
493 500 */
494 501 QColor QBarSet::borderColor()
495 502 {
496 503 return pen().color();
497 504 }
498 505
499 506 /*!
500 507 Sets the color of pen for this barset
501 508 */
502 509 void QBarSet::setBorderColor(QColor color)
503 510 {
504 511 QPen p = pen();
505 512 if (p.color() != color) {
506 513 p.setColor(color);
507 514 setPen(p);
508 515 emit borderColorChanged(color);
509 516 }
510 517 }
511 518
512 519 /*!
513 520 Returns the color of labels of this barset
514 521 */
515 522 QColor QBarSet::labelColor()
516 523 {
517 524 return labelBrush().color();
518 525 }
519 526
520 527 /*!
521 528 Sets the color of labels for this barset
522 529 */
523 530 void QBarSet::setLabelColor(QColor color)
524 531 {
525 532 QBrush b = labelBrush();
526 533 if (b == QBrush())
527 534 b.setStyle(Qt::SolidPattern);
528 535
529 536 if (b.color() != color) {
530 537 b.setColor(color);
531 538 setLabelBrush(b);
532 539 emit labelColorChanged(color);
533 540 }
534 541 }
535 542
536 543 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
537 544
538 545 QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent),
539 546 q_ptr(parent),
540 547 m_label(label)
541 548 {
542 549 }
543 550
544 551 QBarSetPrivate::~QBarSetPrivate()
545 552 {
546 553 }
547 554
548 555 void QBarSetPrivate::append(QPointF value)
549 556 {
550 557 m_values.append(value);
551 558 emit restructuredBars();
552 559 }
553 560
554 561 void QBarSetPrivate::append(QList<QPointF> values)
555 562 {
556 563 for (int i=0; i<values.count(); i++) {
557 564 m_values.append(values.at(i));
558 565 }
559 566 emit restructuredBars();
560 567 }
561 568
562 569 void QBarSetPrivate::append(QList<qreal> values)
563 570 {
564 571 int index = m_values.count();
565 572 for (int i=0; i<values.count(); i++) {
566 573 m_values.append(QPointF(index,values.at(i)));
567 574 index++;
568 575 }
569 576 emit restructuredBars();
570 577 }
571 578
572 579 void QBarSetPrivate::insert(const int index, const qreal value)
573 580 {
574 581 m_values.insert(index, QPointF(index, value));
575 582 emit restructuredBars();
576 583 }
577 584
578 585 void QBarSetPrivate::insert(const int index, const QPointF value)
579 586 {
580 587 m_values.insert(index, value);
581 588 emit restructuredBars();
582 589 }
583 590
584 591 int QBarSetPrivate::remove(const int index, const int count)
585 592 {
586 593 int removeCount = count;
587 594
588 595 if ((index <0) || (m_values.count() == 0)) {
589 596 // Invalid index or not values in list, remove nothing.
590 597 return 0;
591 598 } else if ((index + count) > m_values.count()) {
592 599 // Trying to remove more items than list has. Limit amount to be removed.
593 600 removeCount = m_values.count() - index;
594 601 }
595 602
596 603 int c = 0;
597 604 while (c < removeCount) {
598 605 m_values.removeAt(index);
599 606 c++;
600 607 }
601 608 emit restructuredBars();
602 609 return removeCount;
603 610 }
604 611
605 612 void QBarSetPrivate::replace(const int index, const qreal value)
606 613 {
607 614 m_values.replace(index,QPointF(index,value));
608 615 emit updatedBars();
609 616 }
610 617
611 618 void QBarSetPrivate::replace(const int index, const QPointF value)
612 619 {
613 620 m_values.replace(index,value);
614 621 emit updatedBars();
615 622 }
616 623
617 624 qreal QBarSetPrivate::pos(const int index)
618 625 {
619 626 if (index < 0 || index >= m_values.count()) {
620 627 return 0;
621 628 }
622 629
623 630 return m_values.at(index).x();
624 631 }
625 632
626 633 qreal QBarSetPrivate::value(const int index)
627 634 {
628 635 if (index < 0 || index >= m_values.count()) {
629 636 return 0;
630 637 }
631 638
632 639 return m_values.at(index).y();
633 640 }
634 641
635 642 #include "moc_qbarset.cpp"
636 643 #include "moc_qbarset_p.cpp"
637 644
638 645 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now