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

source code of qtremoteobjects/src/remoteobjects/qremoteobjectabstractitemmodeltypes_p.h