| 1 | // Copyright (C) 2017 Ford Motor Company |
| 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 QREMOTEOBJECTS_ABSTRACT_ITEM_MODEL_TYPES_P_H |
| 5 | #define QREMOTEOBJECTS_ABSTRACT_ITEM_MODEL_TYPES_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/qabstractitemmodel.h> |
| 19 | #include <QtCore/qdatastream.h> |
| 20 | #include <QtCore/qdebug.h> |
| 21 | #include <QtCore/qitemselectionmodel.h> |
| 22 | #include <QtCore/qlist.h> |
| 23 | #include <QtCore/qnamespace.h> |
| 24 | #include <QtCore/qpair.h> |
| 25 | #include <QtCore/qsize.h> |
| 26 | #include <QtCore/qvariant.h> |
| 27 | #include <QtRemoteObjects/qtremoteobjectglobal.h> |
| 28 | #include <QtCore/private/qglobal_p.h> |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | namespace QtPrivate { |
| 33 | |
| 34 | struct ModelIndex |
| 35 | { |
| 36 | ModelIndex() : row(-1), column(-1) {} |
| 37 | ModelIndex(int row_, int column_) |
| 38 | : row(row_) |
| 39 | , column(column_) |
| 40 | {} |
| 41 | |
| 42 | inline bool operator==(const ModelIndex &other) const { return row == other.row && column == other.column; } |
| 43 | inline bool operator!=(const ModelIndex &other) const { return !(*this == other); } |
| 44 | int row; |
| 45 | int column; |
| 46 | }; |
| 47 | |
| 48 | typedef QList<ModelIndex> IndexList; |
| 49 | |
| 50 | struct IndexValuePair |
| 51 | { |
| 52 | explicit IndexValuePair(const IndexList index_ = IndexList(), const QVariantList &data_ = QVariantList(), |
| 53 | bool hasChildren_ = false, const Qt::ItemFlags &flags_ = Qt::ItemFlags(), const QSize &size_ = {}) |
| 54 | : index(index_) |
| 55 | , data(data_) |
| 56 | , flags(flags_) |
| 57 | , hasChildren(hasChildren_) |
| 58 | , size(size_) |
| 59 | {} |
| 60 | |
| 61 | inline bool operator==(const IndexValuePair &other) const { return index == other.index && data == other.data && hasChildren == other.hasChildren && flags == other.flags; } |
| 62 | inline bool operator!=(const IndexValuePair &other) const { return !(*this == other); } |
| 63 | |
| 64 | IndexList index; |
| 65 | QVariantList data; |
| 66 | Qt::ItemFlags flags; |
| 67 | bool hasChildren; |
| 68 | QList<IndexValuePair> children; |
| 69 | QSize size; |
| 70 | }; |
| 71 | |
| 72 | struct DataEntries |
| 73 | { |
| 74 | inline bool operator==(const DataEntries &other) const { return data == other.data; } |
| 75 | inline bool operator!=(const DataEntries &other) const { return !(*this == other); } |
| 76 | |
| 77 | QList<IndexValuePair> data; |
| 78 | }; |
| 79 | |
| 80 | struct MetaAndDataEntries : DataEntries |
| 81 | { |
| 82 | QList<int> roles; |
| 83 | QSize size; |
| 84 | }; |
| 85 | |
| 86 | inline QDebug operator<<(QDebug stream, const ModelIndex &index) |
| 87 | { |
| 88 | return stream.nospace() << "ModelIndex[row=" << index.row << ", column=" << index.column << "]" ; |
| 89 | } |
| 90 | |
| 91 | inline QDataStream& operator<<(QDataStream &stream, const ModelIndex &index) |
| 92 | { |
| 93 | return stream << index.row << index.column; |
| 94 | } |
| 95 | |
| 96 | inline QDataStream& operator>>(QDataStream &stream, ModelIndex &index) |
| 97 | { |
| 98 | return stream >> index.row >> index.column; |
| 99 | } |
| 100 | |
| 101 | inline QDataStream& operator<<(QDataStream &stream, Qt::Orientation orient) |
| 102 | { |
| 103 | return stream << static_cast<int>(orient); |
| 104 | } |
| 105 | |
| 106 | inline QDataStream& operator>>(QDataStream &stream, Qt::Orientation &orient) |
| 107 | { |
| 108 | int val; |
| 109 | QDataStream &ret = stream >> val; |
| 110 | orient = static_cast<Qt::Orientation>(val); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | inline QDataStream& operator<<(QDataStream &stream, QItemSelectionModel::SelectionFlags command) |
| 115 | { |
| 116 | return stream << static_cast<int>(command); |
| 117 | } |
| 118 | |
| 119 | inline QDataStream& operator>>(QDataStream &stream, QItemSelectionModel::SelectionFlags &command) |
| 120 | { |
| 121 | int val; |
| 122 | QDataStream &ret = stream >> val; |
| 123 | command = static_cast<QItemSelectionModel::SelectionFlags>(val); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | inline QDebug operator<<(QDebug stream, const IndexValuePair &pair) |
| 128 | { |
| 129 | return stream.nospace() << "IndexValuePair[index=" << pair.index << ", data=" << pair.data << ", hasChildren=" << pair.hasChildren << ", flags=" << pair.flags << "]" ; |
| 130 | } |
| 131 | |
| 132 | inline QDataStream& operator<<(QDataStream &stream, const IndexValuePair &pair) |
| 133 | { |
| 134 | return stream << pair.index << pair.data << pair.hasChildren << static_cast<int>(pair.flags) << pair.children << pair.size; |
| 135 | } |
| 136 | |
| 137 | inline QDataStream& operator>>(QDataStream &stream, IndexValuePair &pair) |
| 138 | { |
| 139 | int flags; |
| 140 | QDataStream &ret = stream >> pair.index >> pair.data >> pair.hasChildren >> flags >> pair.children >> pair.size; |
| 141 | pair.flags = static_cast<Qt::ItemFlags>(flags); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | inline QDebug operator<<(QDebug stream, const DataEntries &entries) |
| 146 | { |
| 147 | return stream.nospace() << "DataEntries[" << entries.data << "]" ; |
| 148 | } |
| 149 | |
| 150 | inline QDataStream& operator<<(QDataStream &stream, const DataEntries &entries) |
| 151 | { |
| 152 | return stream << entries.data; |
| 153 | } |
| 154 | |
| 155 | inline QDataStream& operator>>(QDataStream &stream, DataEntries &entries) |
| 156 | { |
| 157 | return stream >> entries.data; |
| 158 | } |
| 159 | |
| 160 | inline QDataStream& operator<<(QDataStream &stream, const MetaAndDataEntries &entries) |
| 161 | { |
| 162 | return stream << entries.data << entries.roles << entries.size; |
| 163 | } |
| 164 | |
| 165 | inline QDataStream& operator>>(QDataStream &stream, MetaAndDataEntries &entries) |
| 166 | { |
| 167 | return stream >> entries.data >> entries.roles >> entries.size; |
| 168 | } |
| 169 | |
| 170 | inline QString modelIndexToString(const IndexList &list) |
| 171 | { |
| 172 | QString s; |
| 173 | QDebug(&s) << list; |
| 174 | return s; |
| 175 | } |
| 176 | |
| 177 | inline QString modelIndexToString(const ModelIndex &index) |
| 178 | { |
| 179 | QString s; |
| 180 | QDebug(&s) << index; |
| 181 | return s; |
| 182 | } |
| 183 | |
| 184 | inline QModelIndex toQModelIndex(const IndexList &list, const QAbstractItemModel *model, bool *ok = nullptr, bool ensureItem = false) |
| 185 | { |
| 186 | if (ok) |
| 187 | *ok = true; |
| 188 | QModelIndex result; |
| 189 | for (int i = 0; i < list.size(); ++i) { |
| 190 | const ModelIndex &index = list[i]; |
| 191 | if (ensureItem) |
| 192 | const_cast<QAbstractItemModel *>(model)->setData(index: result, value: index.row, role: Qt::UserRole - 1); |
| 193 | |
| 194 | result = model->index(row: index.row, column: index.column, parent: result); |
| 195 | if (!result.isValid()) { |
| 196 | if (ok) { |
| 197 | *ok = false; |
| 198 | } else { |
| 199 | qFatal(msg: "Internal error: invalid index=%s in indexList=%s" , |
| 200 | qPrintable(modelIndexToString(list[i])), qPrintable(modelIndexToString(list))); |
| 201 | } |
| 202 | return QModelIndex(); |
| 203 | } |
| 204 | } |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | inline IndexList toModelIndexList(const QModelIndex &index, const QAbstractItemModel *model) |
| 209 | { |
| 210 | IndexList list; |
| 211 | if (index.isValid()) { |
| 212 | list << ModelIndex(index.row(), index.column()); |
| 213 | for (QModelIndex curIndex = model->parent(child: index); curIndex.isValid(); curIndex = model->parent(child: curIndex)) |
| 214 | list.prepend(t: ModelIndex(curIndex.row(), curIndex.column())); |
| 215 | } |
| 216 | return list; |
| 217 | } |
| 218 | |
| 219 | } // namespace QtPrivate |
| 220 | |
| 221 | QT_END_NAMESPACE |
| 222 | |
| 223 | QT_DECL_METATYPE_EXTERN_TAGGED(QtPrivate::ModelIndex, QtPrivate__ModelIndex, /* not exported */) |
| 224 | QT_DECL_METATYPE_EXTERN_TAGGED(QtPrivate::IndexList, QtPrivate__IndexList, /* not exported */) |
| 225 | QT_DECL_METATYPE_EXTERN_TAGGED(QtPrivate::DataEntries, QtPrivate__DataEntries, /* not exported */) |
| 226 | QT_DECL_METATYPE_EXTERN_TAGGED(QtPrivate::MetaAndDataEntries, QtPrivate__MetaAndDataEntries, |
| 227 | /* not exported */) |
| 228 | QT_DECL_METATYPE_EXTERN_TAGGED(QtPrivate::IndexValuePair, QtPrivate__IndexValuePair, |
| 229 | /* not exported */) |
| 230 | QT_DECL_METATYPE_EXTERN_TAGGED(Qt::Orientation, Qt__Orientation, /* not exported */) |
| 231 | QT_DECL_METATYPE_EXTERN_TAGGED(QItemSelectionModel::SelectionFlags, |
| 232 | QItemSelectionModel__SelectionFlags, |
| 233 | /* not exported */) |
| 234 | |
| 235 | #endif // QREMOTEOBJECTS_ABSTRACT_ITEM_MODEL_TYPES_P_H |
| 236 | |