summaryrefslogtreecommitdiffstats
path: root/src/graphs2d/xychart/qxymodelmapper.cpp
blob: 2b33d2224d83f736b04cc3b247a74c563d73363c (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/QAbstractItemModel>
#include <QtGraphs/QXYModelMapper>
#include <QtGraphs/QXYSeries>
#include "qxymodelmapper_p.h"
#include "qxyseries_p.h"

QT_BEGIN_NAMESPACE

/*!
    \class QXYModelMapper
    \inmodule QtGraphs
    \ingroup graphs_2D

    \brief The QXYModelMapper class is a model mapper for line,
    spline, and scatter series.

    Model mappers enable using a data model derived from the QAbstractItemModel
    class as a data source for a graph. A model mapper is used to
    create a connection between a line, spline, or scatter series.
    A \e TableModel is a natural choice
    for the model.

    Both model and series properties can be used to manipulate the data. The
    model mapper keeps the series and the data model in sync.

    \sa QXYSeries
*/
/*!
    \qmltype XYModelMapper
    \nativetype QXYModelMapper
    \inqmlmodule QtGraphs
    \ingroup graphs_qml_2D

    \brief A model mapper for XYSeries.

    Model mappers enable using a data model derived from the QAbstractItemModel
    class as a data source for a graph. A model mapper is used to
    create a connection between a line, spline, or scatter series.
    A \e TableModel is a natural choice
    for the model.

    Both model and series properties can be used to manipulate the data. The
    model mapper keeps the series and the data model in sync.

   \sa XYSeries
*/

/*!
    \property QXYModelMapper::series
    \brief The series that is used by the mapper.

    All the data in the series is discarded when it is set to the mapper.
    When a new series is specified, the old series is disconnected (but it
    preserves its data).
*/
/*!
    \qmlproperty XYSeries XYModelMapper::series
    The series that is used by the mapper. All the data in the series is
    discarded when it is set to the mapper. When a new series is specified, the
    old series is disconnected (but it preserves its data).
*/

/*!
    \property QXYModelMapper::model
    \brief The model that is used by the mapper.
*/
/*!
    \qmlproperty SomeModel XYModelMapper::model
    The data model that is used by the mapper. You need to implement the model
    and expose it to QML.

    \note The model has to support adding and removing rows or columns and
    modifying the data in the cells.
*/

/*!
    \property QXYModelMapper::xSection
    \brief The section of the model that contains the x-coordinates of data
    points.

    The default value is -1 (invalid mapping).

    \sa QXYModelMapper::orientation
*/
/*!
    \qmlproperty qsizetype XYModelMapper::xSection
    the section of the model that contains the x-coordinates of data points.
    The default value is -1 (invalid mapping).

    \sa orientation
*/

/*!
    \property QXYModelMapper::ySection
    \brief the section of the model that contains the y-coordinates of data
    points.

    The default value is -1 (invalid mapping).

    \sa QXYModelMapper::orientation
*/
/*!
    \qmlproperty qsizetype XYModelMapper::ySection
    the section of the model that contains the y-coordinates of data points.
    The default value is -1 (invalid mapping).

    \sa orientation
*/

/*!
    \property QXYModelMapper::first
    \brief The row of the model that contains the data for the first point
    of the series.

    The minimum and default value is 0.

    \sa QXYModelMapper::orientation
*/
/*!
    \qmlproperty qsizetype XYModelMapper::first
    The row of the model that contains the data for the first point of the series.
    The default value is 0.

    \sa orientation
*/

/*!
    \property QXYModelMapper::count
    \brief The number of rows of the model that are mapped as the data for series.

    The minimum and default value is -1 (the number is limited by the number of
    rows in the model).

    \sa QXYModelMapper::orientation
*/
/*!
    \qmlproperty qsizetype XYModelMapper::count
    The number of rows of the model that are mapped as the data for series. The default value is
    -1 (the number is limited by the number of rows in the model).

    \sa orientation
*/

/*!
    \property QXYModelMapper::orientation
    \brief Tells the modelmapper how to map data from a model. If
    \c{Qt::Vertical} is used, each of the model's columns defines a bar set, and the
    model's rows define the categories. When the orientation is set to
    \c{Qt::Horizontal}, each of the model's rows defines a bar set, and the model's
    columns define categories.

    The default value is \c{Qt::Vertical}
*/
/*!
    \qmlproperty orientation XYModelMapper::orientation
    Tells the modelmapper how to map data from a model. If
    \c{Qt.Vertical} is used, the model has \e X and \e Y columns, and the
    model's rows define the data points. When the orientation is set to
    \c{Qt.Horizontal}, the model has \e X and \e Y rows, and the model's
    columns define the data points.
*/

/*!
    \qmlsignal QXYModelMapper::seriesChanged()

    This signal is emitted when the series that the mapper is connected to changes.
*/

/*!
    \qmlsignal XYModelMapper::modelChanged()

    This signal is emitted when the model that the mapper is connected to changes.
*/

/*!
    \qmlsignal XYModelMapper::xSectionChanged()

    This signal is emitted when the section that contains the x-coordinates of
    data points changes.
*/

/*!
    \qmlsignal XYModelMapper::ySectionChanged()

    This signal is emitted when the section that contains the y-coordinates of
    data points changes.
*/

/*!
    \qmlsignal XYModelMapper::firstChanged()
    This signal is emitted when the first row changes.
*/

/*!
    \qmlsignal XYModelMapper::countChanged()
    This signal is emitted when the number of rows changes.
*/

/*!
    \qmlsignal BarModelMapper::orientationChanged()
    This signal is emitted when the orientation changes.
*/

QXYModelMapper::~QXYModelMapper() {}

QXYModelMapper::QXYModelMapper(QObject *parent)
    : QObject{*(new QXYModelMapperPrivate), parent}
{}

QXYModelMapper::QXYModelMapper(QXYModelMapperPrivate &dd, QObject *parent)
    : QObject(dd, parent)
{}

QAbstractItemModel *QXYModelMapper::model() const
{
    Q_D(const QXYModelMapper);
    return d->m_model;
}

void QXYModelMapper::setModel(QAbstractItemModel *model)
{
    if (model == 0)
        return;

    Q_D(QXYModelMapper);
    if (d->m_model) {
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::dataChanged,
                                   d,
                                   &QXYModelMapperPrivate::onModelUpdated);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::rowsInserted,
                                   d,
                                   &QXYModelMapperPrivate::onModelRowsAdded);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::rowsRemoved,
                                   d,
                                   &QXYModelMapperPrivate::onModelRowsRemoved);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::columnsInserted,
                                   d,
                                   &QXYModelMapperPrivate::onModelColumnsAdded);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::columnsRemoved,
                                   d,
                                   &QXYModelMapperPrivate::onModelColumnsRemoved);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::modelReset,
                                   d,
                                   &QXYModelMapperPrivate::initializeXYFromModel);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::layoutChanged,
                                   d,
                                   &QXYModelMapperPrivate::initializeXYFromModel);
        QObjectPrivate::disconnect(d->m_model,
                                   &QAbstractItemModel::destroyed,
                                   d,
                                   &QXYModelMapperPrivate::handleModelDestroyed);
    }

    d->m_model = model;
    d->initializeXYFromModel();
    //    connect signals from the model
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::dataChanged,
                            d,
                            &QXYModelMapperPrivate::onModelUpdated);
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::rowsInserted,
                            d,
                            &QXYModelMapperPrivate::onModelRowsAdded);
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::rowsRemoved,
                            d,
                            &QXYModelMapperPrivate::onModelRowsRemoved);
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::columnsInserted,
                            d,
                            &QXYModelMapperPrivate::onModelColumnsAdded);
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::columnsRemoved,
                            d,
                            &QXYModelMapperPrivate::onModelColumnsRemoved);

    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::modelReset,
                            d,
                            &QXYModelMapperPrivate::initializeXYFromModel);
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::layoutChanged,
                            d,
                            &QXYModelMapperPrivate::initializeXYFromModel);
    QObjectPrivate::connect(d->m_model,
                            &QAbstractItemModel::destroyed,
                            d,
                            &QXYModelMapperPrivate::handleModelDestroyed);
    Q_EMIT modelChanged();
}

QXYSeries *QXYModelMapper::series() const
{
    Q_D(const QXYModelMapper);
    return d->m_series;
}

void QXYModelMapper::setSeries(QXYSeries *series)
{
    Q_D(QXYModelMapper);
    if (d->m_series) {
        QObjectPrivate::disconnect(d->m_series,
                                   &QXYSeries::pointAdded,
                                   d,
                                   &QXYModelMapperPrivate::onPointAdded);
        QObjectPrivate::disconnect(d->m_series,
                                   &QXYSeries::pointRemoved,
                                   d,
                                   &QXYModelMapperPrivate::onPointRemoved);
        QObjectPrivate::disconnect(d->m_series,
                                   &QXYSeries::pointReplaced,
                                   d,
                                   &QXYModelMapperPrivate::onPointReplaced);
        QObjectPrivate::disconnect(d->m_series,
                                   &QXYSeries::destroyed,
                                   d,
                                   &QXYModelMapperPrivate::handleSeriesDestroyed);
        QObjectPrivate::disconnect(d->m_series,
                                   &QXYSeries::pointsRemoved,
                                   d,
                                   &QXYModelMapperPrivate::onPointsRemoved);
    }

    if (series == 0)
        return;

    d->m_series = series;
    d->initializeXYFromModel();
    // connect the signals from the series
    QObjectPrivate::connect(d->m_series,
                            &QXYSeries::pointAdded,
                            d,
                            &QXYModelMapperPrivate::onPointAdded);
    QObjectPrivate::connect(d->m_series,
                            &QXYSeries::pointRemoved,
                            d,
                            &QXYModelMapperPrivate::onPointRemoved);
    QObjectPrivate::connect(d->m_series,
                            &QXYSeries::pointReplaced,
                            d,
                            &QXYModelMapperPrivate::onPointReplaced);
    QObjectPrivate::connect(d->m_series,
                            &QXYSeries::destroyed,
                            d,
                            &QXYModelMapperPrivate::handleSeriesDestroyed);
    QObjectPrivate::connect(d->m_series,
                            &QXYSeries::pointsRemoved,
                            d,
                            &QXYModelMapperPrivate::onPointsRemoved);
    Q_EMIT seriesChanged();
}

qsizetype QXYModelMapper::first() const
{
    Q_D(const QXYModelMapper);
    return d->m_first;
}

void QXYModelMapper::setFirst(qsizetype first)
{
    Q_D(QXYModelMapper);
    d->m_first = qMax(first, 0);
    d->initializeXYFromModel();
    Q_EMIT firstChanged();
}

qsizetype QXYModelMapper::count() const
{
    Q_D(const QXYModelMapper);
    return d->m_count;
}

void QXYModelMapper::setCount(qsizetype count)
{
    Q_D(QXYModelMapper);
    d->m_count = qMax(count, -1);
    d->initializeXYFromModel();
    Q_EMIT countChanged();
}

Qt::Orientation QXYModelMapper::orientation() const
{
    Q_D(const QXYModelMapper);
    return d->m_orientation;
}

void QXYModelMapper::setOrientation(Qt::Orientation orientation)
{
    Q_D(QXYModelMapper);
    d->m_orientation = orientation;
    d->initializeXYFromModel();
    Q_EMIT orientationChanged();
}

qsizetype QXYModelMapper::xSection() const
{
    Q_D(const QXYModelMapper);
    return d->m_xSection;
}

void QXYModelMapper::setXSection(qsizetype xSection)
{
    Q_D(QXYModelMapper);
    d->m_xSection = qMax(-1, xSection);
    d->initializeXYFromModel();
    Q_EMIT xSectionChanged();
}

qsizetype QXYModelMapper::ySection() const
{
    Q_D(const QXYModelMapper);
    return d->m_ySection;
}

void QXYModelMapper::setYSection(qsizetype ySection)
{
    Q_D(QXYModelMapper);
    d->m_ySection = qMax(-1, ySection);
    d->initializeXYFromModel();
    Q_EMIT ySectionChanged();
}

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

QXYModelMapperPrivate::QXYModelMapperPrivate() {}

QXYModelMapperPrivate::~QXYModelMapperPrivate() {}

void QXYModelMapperPrivate::blockModelSignals(bool block)
{
    m_modelSignalsBlock = block;
}

void QXYModelMapperPrivate::blockSeriesSignals(bool block)
{
    m_seriesSignalsBlock = block;
}

QModelIndex QXYModelMapperPrivate::xModelIndex(qsizetype xIndex)
{
    if (m_count != -1 && xIndex >= m_count)
        return QModelIndex(); // invalid
    if (m_orientation == Qt::Vertical)
        return m_model->index(int(xIndex) + m_first, m_xSection);
    else
        return m_model->index(m_xSection, int(xIndex) + m_first);
}

QModelIndex QXYModelMapperPrivate::yModelIndex(qsizetype yIndex)
{
    if (m_count != -1 && yIndex >= m_count)
        return QModelIndex(); // invalid
    if (m_orientation == Qt::Vertical)
        return m_model->index(int(yIndex) + m_first, m_ySection);
    else
        return m_model->index(m_ySection, int(yIndex) + m_first);
}

qreal QXYModelMapperPrivate::valueFromModel(QModelIndex index)
{
    QVariant value = m_model->data(index, Qt::DisplayRole);
    switch (value.metaType().id()) {
    case QMetaType::QDateTime:
        return value.toDateTime().toMSecsSinceEpoch();
    case QMetaType::QDate:
        return value.toDate().startOfDay().toMSecsSinceEpoch();
    default:
        return value.toReal();
    }
}

void QXYModelMapperPrivate::setValueToModel(QModelIndex index, qreal value)
{
    QVariant oldValue = m_model->data(index, Qt::DisplayRole);
    switch (oldValue.metaType().id()) {
    case QMetaType::QDateTime:
        m_model->setData(index, QDateTime::fromMSecsSinceEpoch(value));
        break;
    case QMetaType::QDate:
        m_model->setData(index, QDateTime::fromMSecsSinceEpoch(value).date());
        break;
    default:
        m_model->setData(index, value);
    }
}

void QXYModelMapperPrivate::onPointAdded(qsizetype pointIndex)
{
    if (m_seriesSignalsBlock)
        return;

    if (m_count != -1)
        m_count += 1;

    blockModelSignals();
    if (m_orientation == Qt::Vertical)
        m_model->insertRows(int(pointIndex) + m_first, 1);
    else
        m_model->insertColumns(int(pointIndex) + m_first, 1);

    setValueToModel(xModelIndex(pointIndex), m_series->points().at(pointIndex).x());
    setValueToModel(yModelIndex(pointIndex), m_series->points().at(pointIndex).y());
    blockModelSignals(false);
}

void QXYModelMapperPrivate::onPointRemoved(qsizetype pointIndex)
{
    if (m_seriesSignalsBlock)
        return;

    if (m_count != -1)
        m_count -= 1;

    blockModelSignals();
    if (m_orientation == Qt::Vertical)
        m_model->removeRow(int(pointIndex) + m_first);
    else
        m_model->removeColumn(int(pointIndex) + m_first);
    blockModelSignals(false);
}

void QXYModelMapperPrivate::onPointsRemoved(qsizetype pointIndex, qsizetype count)
{
    if (m_seriesSignalsBlock)
        return;

    m_count -= count;

    if (m_count < -1)
        m_count = -1;

    blockModelSignals();
    if (m_orientation == Qt::Vertical)
        m_model->removeRows(int(pointIndex) + m_first, int(count));
    else
        m_model->removeColumns(int(pointIndex) + m_first, int(count));
    blockModelSignals(false);
}

void QXYModelMapperPrivate::onPointReplaced(qsizetype pointIndex)
{
    if (m_seriesSignalsBlock)
        return;

    blockModelSignals();
    setValueToModel(xModelIndex(pointIndex), m_series->points().at(pointIndex).x());
    setValueToModel(yModelIndex(pointIndex), m_series->points().at(pointIndex).y());
    blockModelSignals(false);
}

void QXYModelMapperPrivate::handleSeriesDestroyed()
{
    m_series = 0;
}

void QXYModelMapperPrivate::onModelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
{
    if (m_model == 0 || m_series == 0)
        return;

    if (m_modelSignalsBlock)
        return;

    blockSeriesSignals();
    QModelIndex index;
    QPointF newPoint;
    int indexColumn = 0;
    int indexRow = 0;
    for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
        for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
            index = topLeft.sibling(row, column);
            indexColumn = index.column();
            indexRow = index.row();
            if (m_orientation == Qt::Vertical
                && (indexColumn == m_xSection || indexColumn == m_ySection)) {
                if (indexRow >= m_first && (m_count == -1 || indexRow < m_first + m_count)) {
                    QModelIndex xIndex = xModelIndex(indexRow - m_first);
                    QModelIndex yIndex = yModelIndex(indexRow - m_first);
                    if (xIndex.isValid() && yIndex.isValid()) {
                        newPoint.setX(valueFromModel(xIndex));
                        newPoint.setY(valueFromModel(yIndex));
                        m_series->replace(indexRow - m_first, newPoint);
                    }
                }
            } else if (m_orientation == Qt::Horizontal
                       && (indexRow == m_xSection || indexRow == m_ySection)) {
                if (indexColumn >= m_first && (m_count == -1 || indexColumn < m_first + m_count)) {
                    QModelIndex xIndex = xModelIndex(indexColumn - m_first);
                    QModelIndex yIndex = yModelIndex(indexColumn - m_first);
                    if (xIndex.isValid() && yIndex.isValid()) {
                        newPoint.setX(valueFromModel(xIndex));
                        newPoint.setY(valueFromModel(yIndex));
                        m_series->replace(indexColumn - m_first, newPoint);
                    }
                }
            }
        }
    }
    blockSeriesSignals(false);
}

void QXYModelMapperPrivate::onModelRowsAdded(QModelIndex parent, qsizetype start, qsizetype end)
{
    Q_UNUSED(parent);
    if (m_modelSignalsBlock)
        return;

    blockSeriesSignals();
    if (m_orientation == Qt::Vertical) {
        insertData(start, end);
    } else if (start <= m_xSection || start <= m_ySection) {
        // if the changes affect the map - reinitialize the xy
        initializeXYFromModel();
    }
    blockSeriesSignals(false);
}

void QXYModelMapperPrivate::onModelRowsRemoved(QModelIndex parent, qsizetype start, qsizetype end)
{
    Q_UNUSED(parent);
    if (m_modelSignalsBlock)
        return;

    blockSeriesSignals();
    if (m_orientation == Qt::Vertical) {
        removeData(start, end);
    } else if (start <= m_xSection || start <= m_ySection) {
        // if the changes affect the map - reinitialize the xy
        initializeXYFromModel();
    }
    blockSeriesSignals(false);
}

void QXYModelMapperPrivate::onModelColumnsAdded(QModelIndex parent, qsizetype start, qsizetype end)
{
    Q_UNUSED(parent);
    if (m_modelSignalsBlock)
        return;

    blockSeriesSignals();
    if (m_orientation == Qt::Horizontal) {
        insertData(start, end);
    } else if (start <= m_xSection || start <= m_ySection) {
        // if the changes affect the map - reinitialize the xy
        initializeXYFromModel();
    }
    blockSeriesSignals(false);
}

void QXYModelMapperPrivate::onModelColumnsRemoved(QModelIndex parent, qsizetype start, qsizetype end)
{
    Q_UNUSED(parent);
    if (m_modelSignalsBlock)
        return;

    blockSeriesSignals();
    if (m_orientation == Qt::Horizontal) {
        removeData(start, end);
    } else if (start <= m_xSection || start <= m_ySection) {
        // if the changes affect the map - reinitialize the xy
        initializeXYFromModel();
    }
    blockSeriesSignals(false);
}

void QXYModelMapperPrivate::handleModelDestroyed()
{
    m_model = 0;
}

void QXYModelMapperPrivate::insertData(int start, int end)
{
    if (m_model == 0 || m_series == 0)
        return;

    if (m_count != -1 && start >= m_first + m_count) {
        return;
    } else {
        int addedCount = end - start + 1;
        if (m_count != -1 && addedCount > m_count)
            addedCount = m_count;
        int first = qMax(start, m_first);
        int last = qMin(first + addedCount - 1,
                        m_orientation == Qt::Vertical ? m_model->rowCount() - 1
                                                      : m_model->columnCount() - 1);
        for (int i = first; i <= last; i++) {
            QPointF point;
            QModelIndex xIndex = xModelIndex(i - m_first);
            QModelIndex yIndex = yModelIndex(i - m_first);
            if (xIndex.isValid() && yIndex.isValid()) {
                point.setX(valueFromModel(xIndex));
                point.setY(valueFromModel(yIndex));
                m_series->insert(i - m_first, point);
            }
        }

        // remove excess of points (above m_count)
        if (m_count != -1 && m_series->points().size() > m_count) {
            for (qsizetype i = m_series->points().size() - 1; i >= m_count; i--)
                m_series->remove(m_series->points().at(i));
        }
    }
}

void QXYModelMapperPrivate::removeData(int start, int end)
{
    if (m_model == 0 || m_series == 0)
        return;

    int removedCount = end - start + 1;
    if (m_count != -1 && start >= m_first + m_count) {
        return;
    } else {
        int toRemove = qMin(int(m_series->count()),
                            removedCount); // first find how many items can actually be removed
        int first = qMax(start, m_first);  // get the index of the first item that will be removed.
        int last = qMin(first + toRemove - 1,
                        int(m_series->count()) + m_first
                            - 1); // get the index of the last item that will be removed.
        for (int i = last; i >= first; i--)
            m_series->remove(m_series->points().at(i - m_first));

        if (m_count != -1) {
            qsizetype itemsAvailable; // check how many are available to be added
            if (m_orientation == Qt::Vertical)
                itemsAvailable = m_model->rowCount() - m_first - m_series->count();
            else
                itemsAvailable = m_model->columnCount() - m_first - m_series->count();
            int toBeAdded = qMin(
                int(itemsAvailable),
                m_count
                    - int(m_series->count())); // add not more items than there is space left to be filled.
            qsizetype currentSize = m_series->count();
            if (toBeAdded > 0) {
                for (qsizetype i = m_series->count(); i < currentSize + toBeAdded; i++) {
                    QPointF point;
                    QModelIndex xIndex = xModelIndex(i);
                    QModelIndex yIndex = yModelIndex(i);
                    if (xIndex.isValid() && yIndex.isValid()) {
                        point.setX(valueFromModel(xIndex));
                        point.setY(valueFromModel(yIndex));
                        m_series->insert(i, point);
                    }
                }
            }
        }
    }
}

void QXYModelMapperPrivate::initializeXYFromModel()
{
    if (m_model == 0 || m_series == 0)
        return;

    blockSeriesSignals();
    // clear current content
    m_series->clear();

    // create the initial points set
    int pointPos = 0;
    QModelIndex xIndex = xModelIndex(pointPos);
    QModelIndex yIndex = yModelIndex(pointPos);
    if (xIndex.isValid() && yIndex.isValid()) {
        QList<QPointF> temp;

        while (xIndex.isValid() && yIndex.isValid()) {
            QPointF point;
            point.setX(valueFromModel(xIndex));
            point.setY(valueFromModel(yIndex));
            temp.append(point);
            pointPos++;
            xIndex = xModelIndex(pointPos);
            yIndex = yModelIndex(pointPos);
            // Don't warn about invalid index after the first, those are valid and used to
            // determine when we should end looping.
        }
        QXYSeriesPrivate::get(m_series)->append(temp);
    } else {
        // Invalid index right off the bat means series will be left empty, so output a warning,
        // unless model is also empty
        int count = m_orientation == Qt::Vertical ? m_model->rowCount() : m_model->columnCount();
        if (count > 0) {
            if (!xIndex.isValid()) {
                qWarning("%ls Invalid X coordinate index in model mapper.",
                         qUtf16Printable(QString::fromUtf8(__func__)));
            } else if (!yIndex.isValid()) {
                qWarning("%ls Invalid Y coordinate index in model mapper.",
                         qUtf16Printable(QString::fromUtf8(__func__)));
            }
        }
    }

    blockSeriesSignals(false);
}
QT_END_NAMESPACE