1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLCHANGESET_P_H
41#define QQMLCHANGESET_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/qdebug.h>
55#include <QtCore/qvector.h>
56#include <QtQmlModels/private/qtqmlmodelsglobal_p.h>
57
58QT_BEGIN_NAMESPACE
59
60class Q_QMLMODELS_PRIVATE_EXPORT QQmlChangeSet
61{
62public:
63 struct MoveKey
64 {
65 MoveKey() {}
66 MoveKey(int moveId, int offset) : moveId(moveId), offset(offset) {}
67 int moveId = -1;
68 int offset = 0;
69 };
70
71 // The storrage for Change (below). This struct is trivial, which it has to be in order to store
72 // it in a QV4::Heap::Base object. The Change struct doesn't add any storage fields, so it is
73 // safe to cast ChangeData to/from Change.
74 struct ChangeData
75 {
76 int index;
77 int count;
78 int moveId;
79 int offset;
80 };
81
82 struct Change: ChangeData
83 {
84 Change() {
85 index = 0;
86 count = 0;
87 moveId = -1;
88 offset = 0;
89 }
90 Change(int index, int count, int moveId = -1, int offset = 0) {
91 this->index = index;
92 this->count = count;
93 this->moveId = moveId;
94 this->offset = offset;
95 }
96
97 bool isMove() const { return moveId >= 0; }
98
99 MoveKey moveKey(int index) const {
100 return MoveKey(moveId, index - Change::index + offset); }
101
102 int start() const { return index; }
103 int end() const { return index + count; }
104 };
105
106 QQmlChangeSet();
107 QQmlChangeSet(const QQmlChangeSet &changeSet);
108 ~QQmlChangeSet();
109
110 QQmlChangeSet &operator =(const QQmlChangeSet &changeSet);
111
112 const QVector<Change> &removes() const { return m_removes; }
113 const QVector<Change> &inserts() const { return m_inserts; }
114 const QVector<Change> &changes() const { return m_changes; }
115
116 void insert(int index, int count);
117 void remove(int index, int count);
118 void move(int from, int to, int count, int moveId);
119 void change(int index, int count);
120
121 void insert(const QVector<Change> &inserts);
122 void remove(const QVector<Change> &removes, QVector<Change> *inserts = nullptr);
123 void move(const QVector<Change> &removes, const QVector<Change> &inserts);
124 void change(const QVector<Change> &changes);
125 void apply(const QQmlChangeSet &changeSet);
126
127 bool isEmpty() const { return m_removes.empty() && m_inserts.empty() && m_changes.isEmpty(); }
128
129 void clear()
130 {
131 m_removes.clear();
132 m_inserts.clear();
133 m_changes.clear();
134 m_difference = 0;
135 }
136
137 int difference() const { return m_difference; }
138
139private:
140 void remove(QVector<Change> *removes, QVector<Change> *inserts);
141 void change(QVector<Change> *changes);
142
143 QVector<Change> m_removes;
144 QVector<Change> m_inserts;
145 QVector<Change> m_changes;
146 int m_difference;
147};
148
149Q_DECLARE_TYPEINFO(QQmlChangeSet::Change, Q_PRIMITIVE_TYPE);
150Q_DECLARE_TYPEINFO(QQmlChangeSet::MoveKey, Q_PRIMITIVE_TYPE);
151
152inline uint qHash(const QQmlChangeSet::MoveKey &key) { return qHash(key: qMakePair(x: key.moveId, y: key.offset)); }
153inline bool operator ==(const QQmlChangeSet::MoveKey &l, const QQmlChangeSet::MoveKey &r) {
154 return l.moveId == r.moveId && l.offset == r.offset; }
155
156Q_QMLMODELS_PRIVATE_EXPORT QDebug operator <<(QDebug debug, const QQmlChangeSet::Change &change);
157Q_QMLMODELS_PRIVATE_EXPORT QDebug operator <<(QDebug debug, const QQmlChangeSet &change);
158
159QT_END_NAMESPACE
160
161#endif
162

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