summaryrefslogtreecommitdiffstats
path: root/src/graphs2d/barchart/qbarset.cpp
blob: 081e3cae741e8d4eba49aad516c855ff90808f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtGraphs/qbarset.h>
#include <private/qbarset_p.h>
#include <private/charthelpers_p.h>

QT_BEGIN_NAMESPACE

/*!
    \class QBarSet
    \inmodule QtGraphs
    \ingroup graphs_2D
    \brief The QBarSet class represents one set of bars in a bar graph.

    A bar set contains one data value for each category. The first value of a set is assumed to
    belong to the first category, the second one to the second category, and so on. If the set has
    fewer values than there are categories, the missing values are assumed to be located at the end
    of the set. For missing values in the middle of a set, the numerical value of zero is used.
    Labels for zero value sets are not shown.

    \sa QBarSeries
*/
/*!
    \qmltype BarSet
    \nativetype QBarSet
    \inqmlmodule QtGraphs
    \ingroup graphs_qml_2D
    \brief Represents one set of bars in a bar graph.

    A bar set contains one data value for each category. The first value of a set is assumed to
    belong to the first category, the second one to the second category, and so on. If the set has
    fewer values than there are categories, the missing values are assumed to be located at the end
    of the set. For missing values in the middle of a set, the numerical value of zero is used.
    Labels for zero value sets are not shown.

    \sa BarSeries
*/

/*!
    \property QBarSet::label
    \brief The label of the bar set.
*/
/*!
    \qmlproperty string BarSet::label
    The label of the bar set.
*/

/*!
    \property QBarSet::color
    \brief The fill color of the bar set.
*/
/*!
    \qmlproperty color BarSet::color
    The fill color of the bar set.
*/

/*!
    \property QBarSet::selectedColor
    \brief The fill color of the selected set.
*/
/*!
    \qmlproperty color BarSet::selectedColor
    The fill color of the selected set.
*/

/*!
    \property QBarSet::borderColor
    \brief The border color of the bar set.
*/
/*!
    \qmlproperty color BarSet::borderColor
    The border color of the bar set.
*/

/*!
    \property QBarSet::borderWidth
    \brief The width of the border line.
    By default, the width is -1, meaning the border width is defined by the theme.
*/
/*!
    \qmlproperty real BarSet::borderWidth
     By default, the width is -1, meaning the border width is defined by the theme.
*/

/*!
    \property QBarSet::count
    \brief The number of values in the bar set.
*/
/*!
    \qmlproperty int BarSet::count
    The number of values in the bar set.
*/

/*!
    \property QBarSet::labelColor
    \brief The text (label) color of the bar set.
*/
/*!
    \qmlproperty color BarSet::labelColor
    The text (label) color of the bar set.
*/

/*!
    \property QBarSet::selectedBars
    \brief The indexes of the bars which are currently selected.
*/
/*!
    \qmlproperty list BarSet::selectedBars
    The indexes of the bars which are currently selected.
*/

/*!
   \property QBarSet::values
   \brief The values of the bar set.

    You can set a list of either \l [QML]{real} or \l [QML]{point}
    types as values.

    If you set a list of real types as values, they directly define the bar set values.

    If you set a list of point types as values, the x-coordinate of the point specifies its
    zero-based index in the bar set. The size of the bar set is the highest x-coordinate value + 1.
    If a point is missing for any x-coordinate between zero and the highest value,
    it gets the value zero.
 */
/*!
    \qmlproperty list<variant> BarSet::values
    The values of the bar set. You can set a list of either \l [QML]{real} or \l [QML]{point}
    types as values.

    If you set a list of real types as values, they directly define the bar set values.

    If you set a list of point types as values, the x-coordinate of the point specifies its
    zero-based index in the bar set. The size of the bar set is the highest x-coordinate value + 1.
    If a point is missing for any x-coordinate between zero and the highest value,
    it gets the value zero.

    For example, the following bar sets have equal values:
    \code
        myBarSet1.values = [5, 0, 1, 5];
        myBarSet2.values = [Qt.point(0, 5), Qt.point(2, 1), Qt.point(3, 5)];
    \endcode
*/

/*!
    \fn void QBarSet::update()
    This signal is emitted when the barset is updated.
*/
/*!
    \qmlsignal BarSet::update()
    This signal is emitted when the barset is updated.
*/

/*!
    \qmlsignal BarSet::labelChanged()
    This signal is emitted when the label of the bar set changes.
    \sa label
*/

/*!
    \qmlsignal BarSet::colorChanged(color)
    This signal is emitted when the fill color of the bar set changes to \a color.
*/

/*!
    \qmlsignal BarSet::borderColorChanged(color)
    This signal is emitted when the border color of the bar set changes to \a color.
*/

/*!
    \qmlsignal BarSet::labelColorChanged(color)
    This signal is emitted when the text (label) color of the bar set changes to \a color.
*/

/*!
    \qmlsignal BarSet::valuesChanged()
    This signal is emitted when the values of the bar set change.
*/

/*!
    \qmlsignal BarSet::selectedColorChanged(color color)
    This signal is emitted when the selected bar color changes. The new color is
    \a color.
*/

/*!
    \qmlsignal BarSet::countChanged()
    This signal is emitted when the barset's value count changes.
*/

/*!
    \qmlsignal BarSet::borderWidthChanged(real width)
    This signal is emitted when the barset's border width changes.
    The new width is \a width.
*/

/*!
    \fn void QBarSet::valuesAdded(qsizetype index, qsizetype count)
    This signal is emitted when new values are added to the bar set.
    \a index indicates the position of the first inserted value, and \a count is the number
    of inserted values.
    \sa append(), insert()
*/
/*!
    \qmlsignal BarSet::valuesAdded(int index, int count)
    This signal is emitted when new values are added to the bar set.
    \a index indicates the position of the first inserted value, and \a count is the number
    of inserted values.
*/

/*!
    \fn void QBarSet::valuesRemoved(qsizetype index, qsizetype count)
    This signal is emitted when values are removed from the bar set.
    \a index indicates the position of the first removed value, and \a count is the number
    of removed values.
    \sa remove()
*/
/*!
    \qmlsignal BarSet::valuesRemoved(int index, int count)
    This signal is emitted when values are removed from the bar set.
    \a index indicates the position of the first removed value, and \a count is the number
    of removed values.
*/

/*!
    \fn void QBarSet::valueChanged(qsizetype index)
    This signal is emitted when the value at the position specified by \a index is modified.
    \sa at()
*/
/*!
    \qmlsignal BarSet::valueChanged(int index)
    This signal is emitted when the value at the position specified by \a index is modified.
*/

/*!
    \fn void QBarSet::updatedBars()
    This signal is emitted when the bars in this set are updated.
*/
/*!
    \qmlsignal BarSet::updatedBars()
    This signal is emitted when the bars in this set are updated.
*/

/*!
    \fn void QBarSet::valueAdded(qsizetype index, qsizetype count)
    This signal is emitted when new values are added to the bar set.
    \a index indicates the position of the first inserted value, and \a count
    is the number of inserted values.
*/
/*!
    \qmlsignal BarSet::valueAdded(int index, int count)
    This signal is emitted when new values are added to the bar set.
    \a index indicates the position of the first inserted value, and \a count
    is the number of inserted values.
*/

/*!
    \fn void QBarSet::valueRemoved(qsizetype index, qsizetype count)
    This signal is emitted when values are removed from the bar set.
    \a index indicates the position of the first removed value, and \a count
    is the number of removed values.
*/
/*!
    \qmlsignal BarSet::valueRemoved(int index, int count)
    This signal is emitted when values are removed from the bar set.
    \a index indicates the position of the first removed value, and \a count
    is the number of removed values.
*/

/*!
    \fn void QBarSet::selectedBarsChanged(const QList<qsizetype> &indexes)
    This signal is emitted when the selected bar changes. \a indexes is
    a list selected bar indexes.
*/
/*!
    \qmlsignal BarSet::selectedBarsChanged(list<int> indexes)
    This signal is emitted when the selected bar changes. \a indexes is
    a list selected bar indexes.
*/

QBarSet::QBarSet(QObject *parent)
    : QBarSet(QString(), parent)
{}

/*!
    Constructs a bar set with the label \a label and the parent \a parent.
*/
QBarSet::QBarSet(const QString &label, QObject *parent)
    : QObject(*(new QBarSetPrivate(label)), parent)
{}

/*!
    Removes the bar set.
*/
QBarSet::~QBarSet()
{
    // NOTE: d_ptr destroyed by QObject
}

/*!
    Sets \a label as the new label for the bar set.
*/
void QBarSet::setLabel(const QString &label)
{
    Q_D(QBarSet);
    if (d->m_label != label) {
        d->m_label = label;
        d->setLabelsDirty(true);
        emit update();
        emit labelChanged();
    }
}

/*!
    Returns the label of the bar set.
*/
QString QBarSet::label() const
{
    Q_D(const QBarSet);
    return d->m_label;
}

/*!
    \qmlmethod BarSet::append(real value)
    Appends the new value specified by \a value to the end of the bar set.
*/
/*!
    Appends the new value specified by \a value to the end of the bar set.
*/
void QBarSet::append(qreal value)
{
    Q_D(QBarSet);
    // Convert to QPointF
    qsizetype index = d->m_values.size();
    d->append(QPointF(d->m_values.size(), value));
    emit valuesAdded(index, 1);
    emit countChanged();
    emit update();
}

/*!
    \qmlmethod BarSet::append(list<real> values)
    Appends the list of real values specified by \a values to the end of the bar set.

    \sa append()
*/
/*!
    Appends the list of real values specified by \a values to the end of the bar set.

    \sa append()
*/
void QBarSet::append(const QList<qreal> &values)
{
    Q_D(QBarSet);
    qsizetype index = d->m_values.size();
    d->append(values);
    emit valuesAdded(index, values.size());
    emit countChanged();
    emit update();
}

/*!
    \qmlmethod BarSet::insert(int index, real value)
    Inserts \a value in the position specified by \a index.
    The values following the inserted value are moved up one position.

    \sa remove()
*/
/*!
    Inserts \a value in the position specified by \a index.
    The values following the inserted value are moved up one position.

    \sa remove()
*/
void QBarSet::insert(qsizetype index, qreal value)
{
    Q_D(QBarSet);
    d->insert(index, value);

    bool callSignal = false;
    if (!d->m_selectedBars.isEmpty()) {
        // if value was inserted we need to move already selected bars by 1
        QSet<qsizetype> selectedAfterInsert;
        for (const auto &value : std::as_const(d->m_selectedBars)) {
            if (value >= index) {
                selectedAfterInsert << value + 1;
                callSignal = true;
            } else {
                selectedAfterInsert << value;
            }
        }
        d->m_selectedBars = selectedAfterInsert;
        emit update();
    }

    emit valuesAdded(index, 1);
    emit countChanged();
    if (callSignal)
        emit selectedBarsChanged(selectedBars());
}

/*!
    \qmlmethod BarSet::remove(int index, int count)
    Removes the number of values specified by \a count from the bar set starting
    with the value specified by \a index.

    If you leave out \a count, only the value specified by \a index is removed.
*/
/*!
    Removes the number of values specified by \a count from the bar set starting with
    the value specified by \a index.
    \sa insert()
*/
void QBarSet::remove(qsizetype index, qsizetype count)
{
    Q_D(QBarSet);
    qsizetype removedCount = d->remove(index, count);
    if (removedCount > 0) {
        emit valuesRemoved(index, removedCount);
        emit countChanged();
        emit update();
    }
}

/*!
    \qmlmethod BarSet::replace(int index, real value)
    Adds the value specified by \a value to the bar set at the position
    specified by \a index.
*/
/*!
    Adds the value specified by \a value to the bar set at the position specified by \a index.
*/
void QBarSet::replace(qsizetype index, qreal value)
{
    Q_D(QBarSet);
    if (index >= 0 && index < d->m_values.size()) {
        d->replace(index, value);
        emit valueChanged(index);
        emit update();
    }
}

/*!
    \qmlmethod real BarSet::at(int index)
    Returns the value specified by \a index from the bar set.
    If the index is out of bounds, 0.0 is returned.
*/
/*!
    Returns the value specified by \a index from the bar set.
    If the index is out of bounds, 0.0 is returned.
*/
qreal QBarSet::at(qsizetype index) const
{
    Q_D(const QBarSet);
    if (index < 0 || index >= d->m_values.size())
        return 0;
    return d->m_values.at(index).y();
}

/*!
    \qmlmethod int BarSet::count()
    Returns the number of values in a bar set.
*/
/*!
    Returns the number of values in a bar set.
*/
qsizetype QBarSet::count() const
{
    Q_D(const QBarSet);
    return d->m_values.size();
}

/*!
    \qmlmethod real BarSet::sum()
    Returns the sum of all values in the bar set.
*/
/*!
    Returns the sum of all values in the bar set.
*/
qreal QBarSet::sum() const
{
    Q_D(const QBarSet);
    qreal total(0);
    for (int i = 0; i < d->m_values.size(); i++)
        total += d->m_values.at(i).y();
    return total;
}

/*!
    \qmlmethod BarSet::clear()
    Removes all values from the set.
*/
/*!
    Removes all values from the set.
*/
void QBarSet::clear()
{
    Q_D(QBarSet);
    remove(0, d->m_values.size());
}

/*!
    A convenience operator for appending the real value specified by \a value to the end of the
    bar set.

    \sa append()
*/
QBarSet &QBarSet::operator << (qreal value)
{
    append(value);
    return *this;
}

/*!
    Returns the value of the bar set specified by \a index.
    If the index is out of bounds, 0.0 is returned.
*/
qreal QBarSet::operator [](qsizetype index) const
{
    return at(index);
}

/*!
    Returns the fill color for the bar set.
*/
QColor QBarSet::color() const
{
    Q_D(const QBarSet);
    return d->m_color;
}

/*!
    Sets the fill color for the bar set to \a color.
*/
void QBarSet::setColor(QColor color)
{
    Q_D(QBarSet);
    if (d->m_color != color) {
        d->m_color = color;
        emit update();
        emit colorChanged(color);
    }
}

/*!
    Returns the line color for the bar set.
*/
QColor QBarSet::borderColor() const
{
    Q_D(const QBarSet);
    return d->m_borderColor;
}

/*!
    Sets the line color for the bar set to \a color.
*/
void QBarSet::setBorderColor(QColor color)
{
    Q_D(QBarSet);
    if (d->m_borderColor != color) {
        d->m_borderColor = color;
        emit update();
        emit borderColorChanged(color);
    }
}

/*!
    Returns the text color for the bar set.
*/
QColor QBarSet::labelColor() const
{
    Q_D(const QBarSet);
    return d->m_labelColor;
}

/*!
    Sets the text color for the bar set to \a color.
*/
void QBarSet::setLabelColor(QColor color)
{
    Q_D(QBarSet);
    if (d->m_labelColor != color) {
        d->m_labelColor = color;
        emit update();
        emit labelColorChanged(color);
    }
}

/*!
    Returns the color of the selected bars.

    This is the fill (brush) color of bars marked as selected. If not specified,
    value of QBarSet::color is used as default.
    \sa color
*/
QColor QBarSet::selectedColor() const
{
    Q_D(const QBarSet);
    return d->m_selectedColor;
}

/*!
    Sets the \a color of the selected bars.
    \sa selectedColor
*/
void QBarSet::setSelectedColor(QColor color)
{
    Q_D(QBarSet);
    if (d->m_selectedColor != color) {
        d->m_selectedColor = color;
        d->setLabelsDirty(true);
        emit update();
        emit updatedBars();
        emit selectedColorChanged(color);
    }
}


qreal QBarSet::borderWidth() const
{
    Q_D(const QBarSet);
    return d->m_borderWidth;
}

void QBarSet::setBorderWidth(qreal width)
{
    Q_D(QBarSet);
    width = qMax(0.0, width);
    if (!qFuzzyCompare(d->m_borderWidth, width)) {
        d->m_borderWidth = width;
        emit update();
        emit borderWidthChanged(width);
    }
}

QVariantList QBarSet::values() const
{
    QVariantList values;
    for (qsizetype i(0); i < count(); i++)
        values.append(QVariant(QBarSet::at(i)));
    return values;
}

void QBarSet::setValues(const QVariantList &values)
{
    bool valuesUpdated = false;
    // See if we can replace values instead of remove & add all.
    // This way e.g. selections remain.
    const bool doReplace = count() == values.size();

    if (!doReplace) {
        while (count())
            remove(count() - 1);
        valuesUpdated = true;
    }

    if (values.size() > 0 && values.at(0).canConvert<QPoint>()) {
        // Create list of values for appending if the first item is Qt.point
        int maxValue = 0;
        for (int i = 0; i < values.size(); i++) {
            if (values.at(i).canConvert<QPoint>() &&
                values.at(i).toPoint().x() > maxValue) {
                maxValue = values.at(i).toPoint().x();
            }
        }

        QList<qreal> indexValueList;
        indexValueList.resize(maxValue + 1);

        for (int i = 0; i < values.size(); i++) {
            if (values.at(i).canConvert<QPoint>())
                indexValueList.replace(values.at(i).toPoint().x(), values.at(i).toPointF().y());
        }

        for (int i = 0; i < indexValueList.size(); i++) {
            if (doReplace)
                QBarSet::replace(i, indexValueList.at(i));
            else
                QBarSet::append(indexValueList.at(i));
            valuesUpdated = true;
        }

    } else {
        for (int i(0); i < values.size(); i++) {
            if (values.at(i).canConvert<double>()) {
                if (doReplace)
                    QBarSet::replace(i, values[i].toDouble());
                else
                    QBarSet::append(values[i].toDouble());
                valuesUpdated = true;
            }
        }
    }
    emit update();
    if (valuesUpdated)
        emit valuesChanged();
}


/*!
    \qmlmethod bool BarSet::isBarSelected(int index)
    Returns \c true if the bar at the given \a index is among selected bars and \c false otherwise.
    \note Selected bars are drawn using the selected color if it was specified using BarSet::setSelectedColor.
    \sa selectedBars, setBarSelected(), selectedColor
 */
/*!
    Returns \c true if the bar at the given \a index is among selected bars and \c false otherwise.
    \note Selected bars are drawn using the selected color if it was specified using QBarSet::setSelectedColor.
    \sa selectedBars(), setBarSelected(), setSelectedColor()
 */
bool QBarSet::isBarSelected(qsizetype index) const
{
    Q_D(const QBarSet);
    return d->isBarSelected(index);
}

/*!
    \qmlmethod BarSet::selectBar(int index)
    Marks the bar at \a index as selected.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Marks the bar at \a index as selected.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::selectBar(qsizetype index)
{
    setBarSelected(index, true);
}

/*!
    \qmlmethod BarSet::deselectBar(int index)
    Deselects the bar at \a index.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Deselects the bar at \a index.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::deselectBar(qsizetype index)
{
    setBarSelected(index, false);
}

/*!
    \qmlmethod BarSet::setBarSelected(int index, bool selected)
    Marks the bar at \a index as either selected or deselected as specified by \a selected.
    \note Selected bars are drawn using the selected color if it was specified. Emits BarSet::selectedBarsChanged.
    \sa selectedColor
 */
/*!
    Marks the bar at \a index as either selected or deselected as specified by \a selected.
    \note Selected bars are drawn using the selected color if it was specified. Emits QBarSet::selectedBarsChanged.
    \sa setSelectedColor()
 */
void QBarSet::setBarSelected(qsizetype index, bool selected)
{
    Q_D(QBarSet);
    bool callSignal = false;
    d->setBarSelected(index, selected, callSignal);

    if (callSignal)
        emit selectedBarsChanged(selectedBars());
    emit update();
}

/*!
    \qmlmethod BarSet::selectAllBars()
    Marks all bars in the set as selected.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Marks all bars in the set as selected.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::selectAllBars()
{
    Q_D(QBarSet);
    bool callSignal = false;
    for (int i = 0; i < d->m_values.size(); ++i)
        d->setBarSelected(i, true, callSignal);

    if (callSignal)
        emit selectedBarsChanged(selectedBars());
    emit update();
}

/*!
    \qmlmethod BarSet::deselectAllBars()
    Deselects all bars in the set.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Deselects all bars in the set.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::deselectAllBars()
{
    Q_D(QBarSet);
    bool callSignal = false;
    for (int i = 0; i < d->m_values.size(); ++i)
        d->setBarSelected(i, false, callSignal);

    if (callSignal)
        emit selectedBarsChanged(selectedBars());
    emit update();
}

/*!
    \qmlmethod BarSet::selectBars(list<int> indexes)
    Marks multiple bars passed in an \a indexes list as selected.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Marks multiple bars passed in an \a indexes list as selected.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::selectBars(const QList<qsizetype> &indexes)
{
    Q_D(QBarSet);
    bool callSignal = false;
    for (const qsizetype &index : indexes)
        d->setBarSelected(index, true, callSignal);

    if (callSignal)
        emit selectedBarsChanged(selectedBars());
    emit update();
}

/*!
    \qmlmethod BarSet::deselectBars(list<int> indexes)
    Marks multiple bars passed in an \a indexes list as deselected.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Marks multiple bars passed in an \a indexes list as deselected.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::deselectBars(const QList<qsizetype> &indexes)
{
    Q_D(QBarSet);
    bool callSignal = false;
    for (const qsizetype &index : indexes)
        d->setBarSelected(index, false, callSignal);

    if (callSignal)
        emit selectedBarsChanged(selectedBars());
    emit update();
}

/*!
    \qmlmethod BarSet::toggleSelection(list<int> indexes)
    Changes the selection state of bars at the given \a indexes to the opposite one.
    \note Emits BarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
/*!
    Changes the selection state of bars at the given \a indexes to the opposite one.
    \note Emits QBarSet::selectedBarsChanged.
    \sa setBarSelected()
 */
void QBarSet::toggleSelection(const QList<qsizetype> &indexes)
{
    Q_D(QBarSet);
    bool callSignal = false;
    for (const qsizetype &index : indexes)
        d->setBarSelected(index, !isBarSelected(index), callSignal);

    if (callSignal)
        emit selectedBarsChanged(selectedBars());
    emit update();
}

/*!
  Returns a list of bars marked as selected.
  \sa setBarSelected()
 */
QList<qsizetype> QBarSet::selectedBars() const
{
    Q_D(const QBarSet);
    return QList<qsizetype>(d->m_selectedBars.begin(), d->m_selectedBars.end());
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QBarSetPrivate::QBarSetPrivate(const QString &label)
    : m_label(label)
    , m_visualsDirty(true)
{}

QBarSetPrivate::~QBarSetPrivate() {}

void QBarSetPrivate::append(QPointF value)
{
    if (isValidValue(value)) {
        Q_Q(QBarSet);
        m_values.append(value);
        emit q->valueAdded(m_values.size() - 1, 1);
    }
}

void QBarSetPrivate::append(const QList<QPointF> &values)
{
    qsizetype originalIndex = m_values.size();
    for (const auto &value : values) {
        if (isValidValue(value))
            m_values.append(value);
    }
    Q_Q(QBarSet);
    emit q->valueAdded(originalIndex, values.size());
}

void QBarSetPrivate::append(const QList<qreal> &values)
{
    qsizetype originalIndex = m_values.size();
    qsizetype index = originalIndex;
    for (const auto value : values) {
        if (isValidValue(value)) {
            m_values.append(QPointF(index, value));
            index++;
        }
    }
    Q_Q(QBarSet);
    emit q->valueAdded(originalIndex, values.size());
}

void QBarSetPrivate::insert(qsizetype index, qreal value)
{
    m_values.insert(index, QPointF(index, value));
    Q_Q(QBarSet);
    emit q->valueAdded(index, 1);
}

void QBarSetPrivate::insert(qsizetype index, QPointF value)
{
    m_values.insert(index, value);
    Q_Q(QBarSet);
    emit q->valueAdded(index, 1);
}

qsizetype QBarSetPrivate::remove(qsizetype index, qsizetype count)
{
    qsizetype removeCount = count;

    if ((index < 0) || (m_values.size() == 0))
        return 0; // Invalid index or not values in list, remove nothing.
    else if ((index + count) > m_values.size())
        removeCount = m_values.size() - index; // Trying to remove more items than list has. Limit amount to be removed.

    int c = 0;
    while (c < removeCount) {
        m_values.removeAt(index);
        c++;
    }

    bool callSignal = false;
    if (!m_selectedBars.empty()) {
        QSet<qsizetype> selectedAfterRemoving;

        for (const qsizetype &selectedBarIndex : std::as_const(m_selectedBars)) {
            if (selectedBarIndex < index) {
                selectedAfterRemoving << selectedBarIndex;
            } else if (selectedBarIndex >= index + removeCount) {
                selectedAfterRemoving << selectedBarIndex - removeCount;
                callSignal = true;
            } else {
                callSignal = true;
            }
        }

        m_selectedBars = selectedAfterRemoving;
    }
    Q_Q(QBarSet);
    emit q->valueRemoved(index, removeCount);
    if (callSignal)
        emit q->selectedBarsChanged(q->selectedBars());

    return removeCount;
}

void QBarSetPrivate::replace(qsizetype index, qreal value)
{
    if (index < 0 || index >= m_values.size())
        return;

    m_values.replace(index, QPointF(index, value));
}

qreal QBarSetPrivate::pos(qsizetype index) const
{
    if (index < 0 || index >= m_values.size())
        return 0;
    return m_values.at(index).x();
}

qreal QBarSetPrivate::value(qsizetype index) const
{
    if (index < 0 || index >= m_values.size())
        return 0;
    return m_values.at(index).y();
}

void QBarSetPrivate::setBarSelected(qsizetype index, bool selected, bool &callSignal)
{
    if (index < 0 || index > m_values.size() - 1)
        return;

    if (selected) {
        if (!isBarSelected(index)) {
            m_selectedBars << index;
            callSignal = true;
        }
    } else {
        if (isBarSelected(index)) {
            m_selectedBars.remove(index);
            callSignal = true;
        }
    }

    if (callSignal)
        setVisualsDirty(true);
}

bool QBarSetPrivate::isBarSelected(qsizetype index) const
{
    return m_selectedBars.contains(index);
}

QT_END_NAMESPACE

#include "moc_qbarset.cpp"