| 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 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QABSTRACTITEMMODEL_P_H |
| 6 | #define QABSTRACTITEMMODEL_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists for the convenience |
| 13 | // of QAbstractItemModel*. This header file may change from version |
| 14 | // to version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | // |
| 19 | |
| 20 | #include "QtCore/qabstractitemmodel.h" |
| 21 | #include "QtCore/private/qobject_p.h" |
| 22 | #include "QtCore/qstack.h" |
| 23 | #include "QtCore/qset.h" |
| 24 | #include "QtCore/qhash.h" |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | QT_REQUIRE_CONFIG(itemmodel); |
| 29 | |
| 30 | class QPersistentModelIndexData |
| 31 | { |
| 32 | public: |
| 33 | QPersistentModelIndexData() {} |
| 34 | QPersistentModelIndexData(const QModelIndex &idx) : index(idx) {} |
| 35 | QModelIndex index; |
| 36 | QAtomicInt ref; |
| 37 | static QPersistentModelIndexData *create(const QModelIndex &index); |
| 38 | static void destroy(QPersistentModelIndexData *data); |
| 39 | }; |
| 40 | |
| 41 | namespace QtPrivate { |
| 42 | // This class is just a wrapper so we can use the fixed qHash() function for QModelIndex. |
| 43 | struct QModelIndexWrapper // ### Qt 7: Remove again, use QModelIndex directly. |
| 44 | { |
| 45 | QModelIndex index; |
| 46 | Q_IMPLICIT QModelIndexWrapper(const QModelIndex &i) : index(i) { } |
| 47 | Q_IMPLICIT inline operator QModelIndex() const { return index; } |
| 48 | friend bool operator==(const QModelIndexWrapper &l, const QModelIndexWrapper &r) { return l.index == r.index; } |
| 49 | friend bool operator!=(const QModelIndexWrapper &l, const QModelIndexWrapper &r) { return !(operator==(l,r)); } |
| 50 | friend bool operator==(const QModelIndexWrapper &l, const QModelIndex &r) { return l.index == r; } |
| 51 | friend bool operator!=(const QModelIndexWrapper &l, const QModelIndex &r) { return !(operator==(lhs: l.index,rhs: r)); } |
| 52 | friend bool operator==(const QModelIndex &l, const QModelIndexWrapper &r) { return l == r.index; } |
| 53 | friend bool operator!=(const QModelIndex &l, const QModelIndexWrapper &r) { return !(operator==(lhs: l,rhs: r.index)); } |
| 54 | friend inline size_t qHash(const QtPrivate::QModelIndexWrapper &index, size_t seed = 0) noexcept |
| 55 | { |
| 56 | return qHashMulti(seed, args: index.index.row(), args: index.index.column(), args: index.index.internalId()); |
| 57 | } |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate |
| 62 | { |
| 63 | Q_DECLARE_PUBLIC(QAbstractItemModel) |
| 64 | |
| 65 | public: |
| 66 | QAbstractItemModelPrivate(); |
| 67 | ~QAbstractItemModelPrivate(); |
| 68 | |
| 69 | static const QAbstractItemModelPrivate *get(const QAbstractItemModel *model) { return model->d_func(); } |
| 70 | |
| 71 | void removePersistentIndexData(QPersistentModelIndexData *data); |
| 72 | void movePersistentIndexes(const QList<QPersistentModelIndexData *> &indexes, int change, const QModelIndex &parent, |
| 73 | Qt::Orientation orientation); |
| 74 | void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last); |
| 75 | void rowsInserted(const QModelIndex &parent, int first, int last); |
| 76 | void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last); |
| 77 | void rowsRemoved(const QModelIndex &parent, int first, int last); |
| 78 | void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last); |
| 79 | void columnsInserted(const QModelIndex &parent, int first, int last); |
| 80 | void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last); |
| 81 | void columnsRemoved(const QModelIndex &parent, int first, int last); |
| 82 | static QAbstractItemModel *staticEmptyModel(); |
| 83 | static bool variantLessThan(const QVariant &v1, const QVariant &v2); |
| 84 | |
| 85 | void itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation); |
| 86 | void itemsMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation); |
| 87 | bool allowMove(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation); |
| 88 | |
| 89 | // ugly hack for QTreeModel, see QTBUG-94546 |
| 90 | virtual void executePendingOperations() const; |
| 91 | |
| 92 | inline QModelIndex createIndex(int row, int column, void *data = nullptr) const { |
| 93 | return q_func()->createIndex(arow: row, acolumn: column, adata: data); |
| 94 | } |
| 95 | |
| 96 | inline QModelIndex createIndex(int row, int column, int id) const { |
| 97 | return q_func()->createIndex(arow: row, acolumn: column, aid: id); |
| 98 | } |
| 99 | |
| 100 | inline bool indexValid(const QModelIndex &index) const { |
| 101 | return (index.row() >= 0) && (index.column() >= 0) && (index.model() == q_func()); |
| 102 | } |
| 103 | |
| 104 | void invalidatePersistentIndexes(); |
| 105 | void invalidatePersistentIndex(const QModelIndex &index); |
| 106 | |
| 107 | struct Change { |
| 108 | constexpr Change() : parent(), first(-1), last(-1), needsAdjust(false) {} |
| 109 | constexpr Change(const QModelIndex &p, int f, int l) : parent(p), first(f), last(l), needsAdjust(false) {} |
| 110 | |
| 111 | QModelIndex parent; |
| 112 | int first, last; |
| 113 | |
| 114 | |
| 115 | // In cases such as this: |
| 116 | // - A |
| 117 | // - B |
| 118 | // - C |
| 119 | // - - D |
| 120 | // - - E |
| 121 | // - - F |
| 122 | // |
| 123 | // If B is moved to above E, C is the source parent in the signal and its row is 2. When the move is |
| 124 | // completed however, C is at row 1 and there is no row 2 at the same level in the model at all. |
| 125 | // The QModelIndex is adjusted to correct that in those cases before reporting it though the |
| 126 | // rowsMoved signal. |
| 127 | bool needsAdjust; |
| 128 | |
| 129 | constexpr bool isValid() const { return first >= 0 && last >= 0; } |
| 130 | }; |
| 131 | QStack<Change> changes; |
| 132 | |
| 133 | struct Persistent { |
| 134 | Persistent() {} |
| 135 | QMultiHash<QtPrivate::QModelIndexWrapper, QPersistentModelIndexData *> indexes; |
| 136 | QStack<QList<QPersistentModelIndexData *>> moved; |
| 137 | QStack<QList<QPersistentModelIndexData *>> invalidated; |
| 138 | void insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data); |
| 139 | } persistent; |
| 140 | |
| 141 | bool resetting = false; |
| 142 | |
| 143 | static const QHash<int,QByteArray> &defaultRoleNames(); |
| 144 | static bool isVariantLessThan(const QVariant &left, const QVariant &right, |
| 145 | Qt::CaseSensitivity cs = Qt::CaseSensitive, bool isLocaleAware = false); |
| 146 | }; |
| 147 | Q_DECLARE_TYPEINFO(QAbstractItemModelPrivate::Change, Q_RELOCATABLE_TYPE); |
| 148 | |
| 149 | namespace QtPrivate { |
| 150 | |
| 151 | /*! |
| 152 | \internal |
| 153 | This is a workaround for QTBUG-75172. |
| 154 | |
| 155 | Some predefined model roles are supposed to use certain enum/flag |
| 156 | types (e.g. fetching Qt::TextAlignmentRole is supposed to return a |
| 157 | variant containing a Qt::Alignment object). |
| 158 | |
| 159 | For historical reasons, a plain `int` was used sometimes. This is |
| 160 | surprising to end-users and also sloppy on Qt's part; users were |
| 161 | forced to use `int` rather than the correct datatype. |
| 162 | |
| 163 | This function tries both the "right" type and plain `int`, for a |
| 164 | given QVariant. This fixes the problem (using the correct datatype) |
| 165 | but also keeps compatibility with existing code using `int`. |
| 166 | |
| 167 | ### Qt 7: get rid of this. Always use the correct datatype. |
| 168 | */ |
| 169 | template <typename T> |
| 170 | T legacyEnumValueFromModelData(const QVariant &data) |
| 171 | { |
| 172 | static_assert(std::is_enum_v<T>); |
| 173 | if (data.userType() == qMetaTypeId<T>()) { |
| 174 | return data.value<T>(); |
| 175 | } else if (std::is_same_v<std::underlying_type_t<T>, int> || |
| 176 | std::is_same_v<std::underlying_type_t<T>, uint>) { |
| 177 | return T(data.toInt()); |
| 178 | } |
| 179 | |
| 180 | return T(); |
| 181 | } |
| 182 | |
| 183 | template <typename T> |
| 184 | T legacyFlagValueFromModelData(const QVariant &data) |
| 185 | { |
| 186 | if (data.userType() == qMetaTypeId<T>()) { |
| 187 | return data.value<T>(); |
| 188 | } else if (std::is_same_v<std::underlying_type_t<typename T::enum_type>, int> || |
| 189 | std::is_same_v<std::underlying_type_t<typename T::enum_type>, uint>) { |
| 190 | return T::fromInt(data.toInt()); |
| 191 | } |
| 192 | |
| 193 | return T(); |
| 194 | } |
| 195 | |
| 196 | } // namespace QtPrivate |
| 197 | |
| 198 | |
| 199 | QT_END_NAMESPACE |
| 200 | |
| 201 | #endif // QABSTRACTITEMMODEL_P_H |
| 202 | |