86c24e8cb0810ce112b8d4cf953339280857c50c
[profile/ivi/qtdeclarative.git] / src / quick / util / qdeclarativechangeset_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVECHANGESET_P_H
43 #define QDECLARATIVECHANGESET_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qdebug.h>
57 #include <QtCore/qvector.h>
58
59 QT_BEGIN_NAMESPACE
60
61 class Q_AUTOTEST_EXPORT QDeclarativeChangeSet
62 {
63 public:
64     struct MoveKey
65     {
66         MoveKey() : moveId(-1), offset(0) {}
67         MoveKey(int moveId, int offset) : moveId(moveId), offset(offset) {}
68         int moveId;
69         int offset;
70     };
71
72     struct Change
73     {
74         Change() : index(0), count(0), moveId(-1) {}
75         Change(int index, int count) : index(index), count(count), moveId(-1) {}
76         Change(int index, int count, int moveId) : index(index), count(count), moveId(moveId) {}
77
78         int index;
79         int count;
80         int moveId;
81
82         bool isMove() const { return moveId >= 0; }
83
84         MoveKey moveKey(int index) const { return MoveKey(moveId, index - Change::index); }
85
86         int start() const { return index; }
87         int end() const { return index + count; }
88     };
89
90
91     struct Insert : public Change
92     {
93         Insert() {}
94         Insert(int index, int count) : Change(index, count) {}
95         Insert(int index, int count, int moveId) : Change(index, count, moveId) {}
96     };
97
98     struct Remove : public Change
99     {
100         Remove() {}
101         Remove(int index, int count) : Change(index, count) {}
102         Remove(int index, int count, int moveId) : Change(index, count, moveId) {}
103     };
104
105     QDeclarativeChangeSet();
106     QDeclarativeChangeSet(const QDeclarativeChangeSet &changeSet);
107     ~QDeclarativeChangeSet();
108
109     QDeclarativeChangeSet &operator =(const QDeclarativeChangeSet &changeSet);
110
111     const QVector<Remove> &removes() const { return m_removes; }
112     const QVector<Insert> &inserts() const { return m_inserts; }
113     const QVector<Change> &changes() const {return  m_changes; }
114
115     void insert(int index, int count);
116     void remove(int index, int count);
117     void move(int from, int to, int count);
118     void change(int index, int count);
119
120     void apply(const QDeclarativeChangeSet &changeSet);
121     void apply(const QVector<Remove> &removals);
122     void apply(const QVector<Insert> &insertions);
123     void apply(const QVector<Change> &changes);
124     void apply(
125             const QVector<Remove> &removals,
126             const QVector<Insert> &insertions,
127             const QVector<Change> &changes = QVector<Change>());
128
129     bool isEmpty() const { return m_removes.empty() && m_inserts.empty() && m_changes.empty(); }
130
131     void clear()
132     {
133         m_removes.clear();
134         m_inserts.clear();
135         m_changes.clear();
136         m_moveCounter = 0;
137         m_difference = 0;
138     }
139
140     int moveCounter() const { return m_moveCounter; }
141     int difference() const { return m_difference; }
142
143 private:
144     void applyRemovals(QVector<Remove> &removals, QVector<Insert> &insertions);
145     void applyInsertions(QVector<Insert> &insertions);
146     void applyChanges(QVector<Change> &changes);
147
148     QVector<Remove> m_removes;
149     QVector<Insert> m_inserts;
150     QVector<Change> m_changes;
151     int m_moveCounter;
152     int m_difference;
153 };
154
155 inline uint qHash(const QDeclarativeChangeSet::MoveKey &key) { return qHash(qMakePair(key.moveId, key.offset)); }
156 inline bool operator ==(const QDeclarativeChangeSet::MoveKey &l, const QDeclarativeChangeSet::MoveKey &r) {
157     return l.moveId == r.moveId && l.offset == r.offset; }
158
159 Q_AUTOTEST_EXPORT QDebug operator <<(QDebug debug, const QDeclarativeChangeSet &change);
160 Q_AUTOTEST_EXPORT QDebug operator <<(QDebug debug, const QDeclarativeChangeSet::Remove &remove);
161 Q_AUTOTEST_EXPORT QDebug operator <<(QDebug debug, const QDeclarativeChangeSet::Insert &insert);
162 Q_AUTOTEST_EXPORT QDebug operator <<(QDebug debug, const QDeclarativeChangeSet::Change &change);
163
164 QT_END_NAMESPACE
165
166 #endif