| 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 <QtQmlIntegration/qqmlintegration.h> |
| 19 | #include <QtCore/qdebug.h> |
| 20 | #include <QtCore/qvector.h> |
| 21 | #include <QtQmlModels/private/qtqmlmodelsglobal_p.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class Q_QMLMODELS_EXPORT QQmlChangeSet |
| 26 | { |
| 27 | Q_GADGET |
| 28 | QML_ANONYMOUS |
| 29 | public: |
| 30 | struct MoveKey |
| 31 | { |
| 32 | MoveKey() {} |
| 33 | MoveKey(int moveId, int offset) : moveId(moveId), offset(offset) {} |
| 34 | int moveId = -1; |
| 35 | int offset = 0; |
| 36 | }; |
| 37 | |
| 38 | // The storrage for Change (below). This struct is trivial, which it has to be in order to store |
| 39 | // it in a QV4::Heap::Base object. The Change struct doesn't add any storage fields, so it is |
| 40 | // safe to cast ChangeData to/from Change. |
| 41 | struct ChangeData |
| 42 | { |
| 43 | int index; |
| 44 | int count; |
| 45 | int moveId; |
| 46 | int offset; |
| 47 | }; |
| 48 | |
| 49 | struct Change: ChangeData |
| 50 | { |
| 51 | Change() { |
| 52 | index = 0; |
| 53 | count = 0; |
| 54 | moveId = -1; |
| 55 | offset = 0; |
| 56 | } |
| 57 | Change(int index, int count, int moveId = -1, int offset = 0) { |
| 58 | this->index = index; |
| 59 | this->count = count; |
| 60 | this->moveId = moveId; |
| 61 | this->offset = offset; |
| 62 | } |
| 63 | |
| 64 | bool isMove() const { return moveId >= 0; } |
| 65 | |
| 66 | MoveKey moveKey(int index) const { |
| 67 | return MoveKey(moveId, index - Change::index + offset); } |
| 68 | |
| 69 | int start() const { return index; } |
| 70 | int end() const { return index + count; } |
| 71 | }; |
| 72 | |
| 73 | QQmlChangeSet(); |
| 74 | QQmlChangeSet(const QQmlChangeSet &changeSet); |
| 75 | ~QQmlChangeSet(); |
| 76 | |
| 77 | QQmlChangeSet &operator =(const QQmlChangeSet &changeSet); |
| 78 | |
| 79 | const QVector<Change> &removes() const { return m_removes; } |
| 80 | const QVector<Change> &inserts() const { return m_inserts; } |
| 81 | const QVector<Change> &changes() const { return m_changes; } |
| 82 | |
| 83 | void insert(int index, int count); |
| 84 | void remove(int index, int count); |
| 85 | void move(int from, int to, int count, int moveId); |
| 86 | void change(int index, int count); |
| 87 | |
| 88 | void insert(const QVector<Change> &inserts); |
| 89 | void remove(const QVector<Change> &removes, QVector<Change> *inserts = nullptr); |
| 90 | void move(const QVector<Change> &removes, const QVector<Change> &inserts); |
| 91 | void change(const QVector<Change> &changes); |
| 92 | void apply(const QQmlChangeSet &changeSet); |
| 93 | |
| 94 | bool isEmpty() const { return m_removes.empty() && m_inserts.empty() && m_changes.isEmpty(); } |
| 95 | |
| 96 | void clear() |
| 97 | { |
| 98 | m_removes.clear(); |
| 99 | m_inserts.clear(); |
| 100 | m_changes.clear(); |
| 101 | m_difference = 0; |
| 102 | } |
| 103 | |
| 104 | int difference() const { return m_difference; } |
| 105 | |
| 106 | private: |
| 107 | void remove(QVector<Change> *removes, QVector<Change> *inserts); |
| 108 | void change(QVector<Change> *changes); |
| 109 | |
| 110 | QVector<Change> m_removes; |
| 111 | QVector<Change> m_inserts; |
| 112 | QVector<Change> m_changes; |
| 113 | int m_difference; |
| 114 | }; |
| 115 | |
| 116 | Q_DECLARE_TYPEINFO(QQmlChangeSet::Change, Q_PRIMITIVE_TYPE); |
| 117 | Q_DECLARE_TYPEINFO(QQmlChangeSet::MoveKey, Q_PRIMITIVE_TYPE); |
| 118 | |
| 119 | inline size_t qHash(const QQmlChangeSet::MoveKey &key) { return qHash(key: qMakePair(value1: key.moveId, value2: key.offset)); } |
| 120 | inline bool operator ==(const QQmlChangeSet::MoveKey &l, const QQmlChangeSet::MoveKey &r) { |
| 121 | return l.moveId == r.moveId && l.offset == r.offset; } |
| 122 | |
| 123 | Q_QMLMODELS_EXPORT QDebug operator <<(QDebug debug, const QQmlChangeSet::Change &change); |
| 124 | Q_QMLMODELS_EXPORT QDebug operator <<(QDebug debug, const QQmlChangeSet &change); |
| 125 | |
| 126 | QT_END_NAMESPACE |
| 127 | |
| 128 | #endif |
| 129 | |