1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLCHANGESET_P_H
5#define QQMLCHANGESET_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qdebug.h>
19#include <QtCore/qvector.h>
20#include <QtQmlModels/private/qtqmlmodelsglobal_p.h>
21
22QT_BEGIN_NAMESPACE
23
24class Q_QMLMODELS_PRIVATE_EXPORT QQmlChangeSet
25{
26public:
27 struct MoveKey
28 {
29 MoveKey() {}
30 MoveKey(int moveId, int offset) : moveId(moveId), offset(offset) {}
31 int moveId = -1;
32 int offset = 0;
33 };
34
35 // The storrage for Change (below). This struct is trivial, which it has to be in order to store
36 // it in a QV4::Heap::Base object. The Change struct doesn't add any storage fields, so it is
37 // safe to cast ChangeData to/from Change.
38 struct ChangeData
39 {
40 int index;
41 int count;
42 int moveId;
43 int offset;
44 };
45
46 struct Change: ChangeData
47 {
48 Change() {
49 index = 0;
50 count = 0;
51 moveId = -1;
52 offset = 0;
53 }
54 Change(int index, int count, int moveId = -1, int offset = 0) {
55 this->index = index;
56 this->count = count;
57 this->moveId = moveId;
58 this->offset = offset;
59 }
60
61 bool isMove() const { return moveId >= 0; }
62
63 MoveKey moveKey(int index) const {
64 return MoveKey(moveId, index - Change::index + offset); }
65
66 int start() const { return index; }
67 int end() const { return index + count; }
68 };
69
70 QQmlChangeSet();
71 QQmlChangeSet(const QQmlChangeSet &changeSet);
72 ~QQmlChangeSet();
73
74 QQmlChangeSet &operator =(const QQmlChangeSet &changeSet);
75
76 const QVector<Change> &removes() const { return m_removes; }
77 const QVector<Change> &inserts() const { return m_inserts; }
78 const QVector<Change> &changes() const { return m_changes; }
79
80 void insert(int index, int count);
81 void remove(int index, int count);
82 void move(int from, int to, int count, int moveId);
83 void change(int index, int count);
84
85 void insert(const QVector<Change> &inserts);
86 void remove(const QVector<Change> &removes, QVector<Change> *inserts = nullptr);
87 void move(const QVector<Change> &removes, const QVector<Change> &inserts);
88 void change(const QVector<Change> &changes);
89 void apply(const QQmlChangeSet &changeSet);
90
91 bool isEmpty() const { return m_removes.empty() && m_inserts.empty() && m_changes.isEmpty(); }
92
93 void clear()
94 {
95 m_removes.clear();
96 m_inserts.clear();
97 m_changes.clear();
98 m_difference = 0;
99 }
100
101 int difference() const { return m_difference; }
102
103private:
104 void remove(QVector<Change> *removes, QVector<Change> *inserts);
105 void change(QVector<Change> *changes);
106
107 QVector<Change> m_removes;
108 QVector<Change> m_inserts;
109 QVector<Change> m_changes;
110 int m_difference;
111};
112
113Q_DECLARE_TYPEINFO(QQmlChangeSet::Change, Q_PRIMITIVE_TYPE);
114Q_DECLARE_TYPEINFO(QQmlChangeSet::MoveKey, Q_PRIMITIVE_TYPE);
115
116inline size_t qHash(const QQmlChangeSet::MoveKey &key) { return qHash(key: qMakePair(value1: key.moveId, value2: key.offset)); }
117inline bool operator ==(const QQmlChangeSet::MoveKey &l, const QQmlChangeSet::MoveKey &r) {
118 return l.moveId == r.moveId && l.offset == r.offset; }
119
120Q_QMLMODELS_PRIVATE_EXPORT QDebug operator <<(QDebug debug, const QQmlChangeSet::Change &change);
121Q_QMLMODELS_PRIVATE_EXPORT QDebug operator <<(QDebug debug, const QQmlChangeSet &change);
122
123QT_END_NAMESPACE
124
125#endif
126

source code of qtdeclarative/src/qmlmodels/qqmlchangeset_p.h