1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Ford Motor Company |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtRemoteObjects module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QREMOTEOBJECTS_ABSTRACT_ITEM_MODEL_TYPES_H |
41 | #define QREMOTEOBJECTS_ABSTRACT_ITEM_MODEL_TYPES_H |
42 | |
43 | #include <QtCore/qdatastream.h> |
44 | #include <QtCore/qlist.h> |
45 | #include <QtCore/qvector.h> |
46 | #include <QtCore/qpair.h> |
47 | #include <QtCore/qvariant.h> |
48 | #include <QtCore/qabstractitemmodel.h> |
49 | #include <QtCore/qitemselectionmodel.h> |
50 | #include <QtCore/qsize.h> |
51 | #include <QtCore/qdebug.h> |
52 | #include <QtCore/qnamespace.h> |
53 | #include <QtRemoteObjects/qtremoteobjectglobal.h> |
54 | |
55 | QT_BEGIN_NAMESPACE |
56 | |
57 | struct ModelIndex |
58 | { |
59 | ModelIndex() : row(-1), column(-1) {} |
60 | ModelIndex(int row_, int column_) |
61 | : row(row_) |
62 | , column(column_) |
63 | {} |
64 | |
65 | inline bool operator==(const ModelIndex &other) const { return row == other.row && column == other.column; } |
66 | inline bool operator!=(const ModelIndex &other) const { return !(*this == other); } |
67 | int row; |
68 | int column; |
69 | }; |
70 | |
71 | typedef QList<ModelIndex> IndexList; |
72 | |
73 | struct IndexValuePair |
74 | { |
75 | explicit IndexValuePair(const IndexList index_ = IndexList(), const QVariantList &data_ = QVariantList(), |
76 | bool hasChildren_ = false, const Qt::ItemFlags &flags_ = Qt::ItemFlags(), const QSize &size_ = {}) |
77 | : index(index_) |
78 | , data(data_) |
79 | , flags(flags_) |
80 | , hasChildren(hasChildren_) |
81 | , size(size_) |
82 | {} |
83 | |
84 | inline bool operator==(const IndexValuePair &other) const { return index == other.index && data == other.data && hasChildren == other.hasChildren && flags == other.flags; } |
85 | inline bool operator!=(const IndexValuePair &other) const { return !(*this == other); } |
86 | |
87 | IndexList index; |
88 | QVariantList data; |
89 | Qt::ItemFlags flags; |
90 | bool hasChildren; |
91 | QVector<IndexValuePair> children; |
92 | QSize size; |
93 | }; |
94 | |
95 | struct DataEntries |
96 | { |
97 | inline bool operator==(const DataEntries &other) const { return data == other.data; } |
98 | inline bool operator!=(const DataEntries &other) const { return !(*this == other); } |
99 | |
100 | QVector<IndexValuePair> data; |
101 | }; |
102 | |
103 | struct MetaAndDataEntries : DataEntries |
104 | { |
105 | QVector<int> roles; |
106 | QSize size; |
107 | }; |
108 | |
109 | inline QDebug operator<<(QDebug stream, const ModelIndex &index) |
110 | { |
111 | return stream.nospace() << "ModelIndex[row=" << index.row << ", column=" << index.column << "]" ; |
112 | } |
113 | |
114 | inline QDataStream& operator<<(QDataStream &stream, const ModelIndex &index) |
115 | { |
116 | return stream << index.row << index.column; |
117 | } |
118 | |
119 | inline QDataStream& operator>>(QDataStream &stream, ModelIndex &index) |
120 | { |
121 | return stream >> index.row >> index.column; |
122 | } |
123 | |
124 | inline QDataStream& operator<<(QDataStream &stream, Qt::Orientation orient) |
125 | { |
126 | return stream << static_cast<int>(orient); |
127 | } |
128 | |
129 | inline QDataStream& operator>>(QDataStream &stream, Qt::Orientation &orient) |
130 | { |
131 | int val; |
132 | QDataStream &ret = stream >> val; |
133 | orient = static_cast<Qt::Orientation>(val); |
134 | return ret; |
135 | } |
136 | |
137 | inline QDataStream& operator<<(QDataStream &stream, QItemSelectionModel::SelectionFlags command) |
138 | { |
139 | return stream << static_cast<int>(command); |
140 | } |
141 | |
142 | inline QDataStream& operator>>(QDataStream &stream, QItemSelectionModel::SelectionFlags &command) |
143 | { |
144 | int val; |
145 | QDataStream &ret = stream >> val; |
146 | command = static_cast<QItemSelectionModel::SelectionFlags>(val); |
147 | return ret; |
148 | } |
149 | |
150 | inline QDebug operator<<(QDebug stream, const IndexValuePair &pair) |
151 | { |
152 | return stream.nospace() << "IndexValuePair[index=" << pair.index << ", data=" << pair.data << ", hasChildren=" << pair.hasChildren << ", flags=" << pair.flags << "]" ; |
153 | } |
154 | |
155 | inline QDebug operator<<(QDebug stream, const DataEntries &entries) |
156 | { |
157 | return stream.nospace() << "DataEntries[" << entries.data << "]" ; |
158 | } |
159 | |
160 | inline QDataStream& operator<<(QDataStream &stream, const DataEntries &entries) |
161 | { |
162 | return stream << entries.data; |
163 | } |
164 | |
165 | inline QDataStream& operator>>(QDataStream &stream, DataEntries &entries) |
166 | { |
167 | return stream >> entries.data; |
168 | } |
169 | |
170 | inline QDataStream& operator<<(QDataStream &stream, const MetaAndDataEntries &entries) |
171 | { |
172 | return stream << entries.data << entries.roles << entries.size; |
173 | } |
174 | |
175 | inline QDataStream& operator>>(QDataStream &stream, MetaAndDataEntries &entries) |
176 | { |
177 | return stream >> entries.data >> entries.roles >> entries.size; |
178 | } |
179 | |
180 | inline QDataStream& operator<<(QDataStream &stream, const IndexValuePair &pair) |
181 | { |
182 | return stream << pair.index << pair.data << pair.hasChildren << static_cast<int>(pair.flags) << pair.children << pair.size; |
183 | } |
184 | |
185 | inline QDataStream& operator>>(QDataStream &stream, IndexValuePair &pair) |
186 | { |
187 | int flags; |
188 | QDataStream &ret = stream >> pair.index >> pair.data >> pair.hasChildren >> flags >> pair.children >> pair.size; |
189 | pair.flags = static_cast<Qt::ItemFlags>(flags); |
190 | return ret; |
191 | } |
192 | |
193 | inline QString modelIndexToString(const IndexList &list) |
194 | { |
195 | QString s; |
196 | QDebug(&s) << list; |
197 | return s; |
198 | } |
199 | |
200 | inline QString modelIndexToString(const ModelIndex &index) |
201 | { |
202 | QString s; |
203 | QDebug(&s) << index; |
204 | return s; |
205 | } |
206 | |
207 | inline QModelIndex toQModelIndex(const IndexList &list, const QAbstractItemModel *model, bool *ok = nullptr, bool ensureItem = false) |
208 | { |
209 | if (ok) |
210 | *ok = true; |
211 | QModelIndex result; |
212 | for (int i = 0; i < list.count(); ++i) { |
213 | const ModelIndex &index = list[i]; |
214 | if (ensureItem) |
215 | const_cast<QAbstractItemModel *>(model)->setData(index: result, value: index.row, role: Qt::UserRole - 1); |
216 | |
217 | result = model->index(row: index.row, column: index.column, parent: result); |
218 | if (!result.isValid()) { |
219 | if (ok) { |
220 | *ok = false; |
221 | } else { |
222 | qFatal(msg: "Internal error: invalid index=%s in indexList=%s" , |
223 | qPrintable(modelIndexToString(list[i])), qPrintable(modelIndexToString(list))); |
224 | } |
225 | return QModelIndex(); |
226 | } |
227 | } |
228 | return result; |
229 | } |
230 | |
231 | inline IndexList toModelIndexList(const QModelIndex &index, const QAbstractItemModel *model) |
232 | { |
233 | IndexList list; |
234 | if (index.isValid()) { |
235 | list << ModelIndex(index.row(), index.column()); |
236 | for (QModelIndex curIndex = model->parent(child: index); curIndex.isValid(); curIndex = model->parent(child: curIndex)) |
237 | list.prepend(t: ModelIndex(curIndex.row(), curIndex.column())); |
238 | } |
239 | return list; |
240 | } |
241 | |
242 | QT_END_NAMESPACE |
243 | |
244 | Q_DECLARE_METATYPE(ModelIndex) |
245 | Q_DECLARE_METATYPE(IndexList) |
246 | Q_DECLARE_METATYPE(DataEntries) |
247 | Q_DECLARE_METATYPE(MetaAndDataEntries) |
248 | Q_DECLARE_METATYPE(IndexValuePair) |
249 | Q_DECLARE_METATYPE(Qt::Orientation) |
250 | Q_DECLARE_METATYPE(QItemSelectionModel::SelectionFlags) |
251 | |
252 | #endif // QREMOTEOBJECTS_ABSTRACT_ITEM_MODEL_TYPES_H |
253 | |