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_ADAPTER_P_H |
41 | #define QREMOTEOBJECTS_ABSTRACT_ITEM_ADAPTER_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include "qremoteobjectabstractitemmodeltypes.h" |
55 | #include "qremoteobjectsource.h" |
56 | |
57 | #include <QtCore/qsize.h> |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | class QAbstractItemModel; |
62 | class QItemSelectionModel; |
63 | |
64 | class QAbstractItemModelSourceAdapter : public QObject |
65 | { |
66 | Q_OBJECT |
67 | public: |
68 | Q_INVOKABLE explicit QAbstractItemModelSourceAdapter(QAbstractItemModel *object, QItemSelectionModel *sel, const QVector<int> &roles = QVector<int>()); |
69 | Q_PROPERTY(QVector<int> availableRoles READ availableRoles WRITE setAvailableRoles NOTIFY availableRolesChanged) |
70 | Q_PROPERTY(QIntHash roleNames READ roleNames) |
71 | static void registerTypes(); |
72 | QItemSelectionModel* selectionModel() const; |
73 | |
74 | public Q_SLOTS: |
75 | QVector<int> availableRoles() const { return m_availableRoles; } |
76 | void setAvailableRoles(QVector<int> availableRoles) |
77 | { |
78 | if (availableRoles != m_availableRoles) |
79 | { |
80 | m_availableRoles = availableRoles; |
81 | Q_EMIT availableRolesChanged(); |
82 | } |
83 | } |
84 | |
85 | QIntHash roleNames() const {return m_model->roleNames();} |
86 | |
87 | QSize replicaSizeRequest(IndexList parentList); |
88 | DataEntries replicaRowRequest(IndexList start, IndexList end, QVector<int> roles); |
89 | QVariantList (QVector<Qt::Orientation> orientations, QVector<int> sections, QVector<int> roles); |
90 | void replicaSetCurrentIndex(IndexList index, QItemSelectionModel::SelectionFlags command); |
91 | void replicaSetData(const IndexList &index, const QVariant &value, int role); |
92 | MetaAndDataEntries replicaCacheRequest(size_t size, const QVector<int> &roles); |
93 | |
94 | void sourceDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles = QVector<int> ()) const; |
95 | void sourceRowsInserted(const QModelIndex & parent, int start, int end); |
96 | void sourceColumnsInserted(const QModelIndex & parent, int start, int end); |
97 | void sourceRowsRemoved(const QModelIndex & parent, int start, int end); |
98 | void sourceRowsMoved(const QModelIndex & sourceParent, int sourceRow, int count, const QModelIndex & destinationParent, int destinationChild) const; |
99 | void sourceCurrentChanged(const QModelIndex & current, const QModelIndex & previous); |
100 | void sourceLayoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint); |
101 | Q_SIGNALS: |
102 | void availableRolesChanged(); |
103 | void dataChanged(IndexList topLeft, IndexList bottomRight, QVector<int> roles) const; |
104 | void rowsInserted(IndexList parent, int start, int end) const; |
105 | void rowsRemoved(IndexList parent, int start, int end) const; |
106 | void rowsMoved(IndexList sourceParent, int sourceRow, int count, IndexList destinationParent, int destinationChild) const; |
107 | void currentChanged(IndexList current, IndexList previous); |
108 | void columnsInserted(IndexList parent, int start, int end) const; |
109 | void layoutChanged(IndexList parents, QAbstractItemModel::LayoutChangeHint hint); |
110 | |
111 | private: |
112 | QAbstractItemModelSourceAdapter(); |
113 | QVector<IndexValuePair> fetchTree(const QModelIndex &parent, size_t &size, const QVector<int> &roles); |
114 | |
115 | QAbstractItemModel *m_model; |
116 | QItemSelectionModel *m_selectionModel; |
117 | QVector<int> m_availableRoles; |
118 | }; |
119 | |
120 | template <class ObjectType, class AdapterType> |
121 | struct QAbstractItemAdapterSourceAPI : public SourceApiMap |
122 | { |
123 | QAbstractItemAdapterSourceAPI(const QString &name) |
124 | : SourceApiMap() |
125 | , m_signalArgTypes {} |
126 | , m_methodArgTypes {} |
127 | , m_name(name) |
128 | { |
129 | m_properties[0] = 2; |
130 | m_properties[1] = QtPrivate::qtro_property_index<AdapterType>(&AdapterType::availableRoles, static_cast<QVector<int> (QObject::*)()>(0),"availableRoles" ); |
131 | m_properties[2] = QtPrivate::qtro_property_index<AdapterType>(&AdapterType::roleNames, static_cast<QIntHash (QObject::*)()>(0),"roleNames" ); |
132 | m_signals[0] = 10; |
133 | m_signals[1] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::availableRolesChanged, static_cast<void (QObject::*)()>(0),m_signalArgCount+0,&m_signalArgTypes[0]); |
134 | m_signals[2] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::dataChanged, static_cast<void (QObject::*)(IndexList,IndexList,QVector<int>)>(0),m_signalArgCount+1,&m_signalArgTypes[1]); |
135 | m_signals[3] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::rowsInserted, static_cast<void (QObject::*)(IndexList,int,int)>(0),m_signalArgCount+2,&m_signalArgTypes[2]); |
136 | m_signals[4] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::rowsRemoved, static_cast<void (QObject::*)(IndexList,int,int)>(0),m_signalArgCount+3,&m_signalArgTypes[3]); |
137 | m_signals[5] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::rowsMoved, static_cast<void (QObject::*)(IndexList,int,int,IndexList,int)>(0),m_signalArgCount+4,&m_signalArgTypes[4]); |
138 | m_signals[6] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::currentChanged, static_cast<void (QObject::*)(IndexList,IndexList)>(0),m_signalArgCount+5,&m_signalArgTypes[5]); |
139 | m_signals[7] = QtPrivate::qtro_signal_index<ObjectType>(&ObjectType::modelReset, static_cast<void (QObject::*)()>(0),m_signalArgCount+6,&m_signalArgTypes[6]); |
140 | m_signals[8] = QtPrivate::qtro_signal_index<ObjectType>(&ObjectType::headerDataChanged, static_cast<void (QObject::*)(Qt::Orientation,int,int)>(0),m_signalArgCount+7,&m_signalArgTypes[7]); |
141 | m_signals[9] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::columnsInserted, static_cast<void (QObject::*)(IndexList,int,int)>(0),m_signalArgCount+8,&m_signalArgTypes[8]); |
142 | m_signals[10] = QtPrivate::qtro_signal_index<AdapterType>(&AdapterType::layoutChanged, static_cast<void (QObject::*)(IndexList,QAbstractItemModel::LayoutChangeHint)>(nullptr),m_signalArgCount+9,&m_signalArgTypes[9]); |
143 | m_methods[0] = 6; |
144 | m_methods[1] = QtPrivate::qtro_method_index<AdapterType>(&AdapterType::replicaSizeRequest, static_cast<void (QObject::*)(IndexList)>(0),"replicaSizeRequest(IndexList)" ,m_methodArgCount+0,&m_methodArgTypes[0]); |
145 | m_methods[2] = QtPrivate::qtro_method_index<AdapterType>(&AdapterType::replicaRowRequest, static_cast<void (QObject::*)(IndexList,IndexList,QVector<int>)>(0),"replicaRowRequest(IndexList,IndexList,QVector<int>)" ,m_methodArgCount+1,&m_methodArgTypes[1]); |
146 | m_methods[3] = QtPrivate::qtro_method_index<AdapterType>(&AdapterType::replicaHeaderRequest, static_cast<void (QObject::*)(QVector<Qt::Orientation>,QVector<int>,QVector<int>)>(0),"replicaHeaderRequest(QVector<Qt::Orientation>,QVector<int>,QVector<int>)" ,m_methodArgCount+2,&m_methodArgTypes[2]); |
147 | m_methods[4] = QtPrivate::qtro_method_index<AdapterType>(&AdapterType::replicaSetCurrentIndex, static_cast<void (QObject::*)(IndexList,QItemSelectionModel::SelectionFlags)>(0),"replicaSetCurrentIndex(IndexList,QItemSelectionModel::SelectionFlags)" ,m_methodArgCount+3,&m_methodArgTypes[3]); |
148 | m_methods[5] = QtPrivate::qtro_method_index<AdapterType>(&AdapterType::replicaSetData, static_cast<void (QObject::*)(IndexList,QVariant,int)>(0),"replicaSetData(IndexList,QVariant,int)" ,m_methodArgCount+4,&m_methodArgTypes[4]); |
149 | m_methods[6] = QtPrivate::qtro_method_index<AdapterType>(&AdapterType::replicaCacheRequest, static_cast<void (QObject::*)(size_t,QVector<int>)>(0),"replicaCacheRequest(size_t,QVector<int>)" ,m_methodArgCount+5,&m_methodArgTypes[5]); |
150 | } |
151 | |
152 | QString name() const override { return m_name; } |
153 | QString typeName() const override { return QStringLiteral("QAbstractItemModelAdapter" ); } |
154 | int enumCount() const override { return 0; } |
155 | int propertyCount() const override { return m_properties[0]; } |
156 | int signalCount() const override { return m_signals[0]; } |
157 | int methodCount() const override { return m_methods[0]; } |
158 | int sourceEnumIndex(int /*index*/) const override |
159 | { |
160 | return -1; |
161 | } |
162 | int sourcePropertyIndex(int index) const override |
163 | { |
164 | if (index < 0 || index >= m_properties[0]) |
165 | return -1; |
166 | return m_properties[index+1]; |
167 | } |
168 | int sourceSignalIndex(int index) const override |
169 | { |
170 | if (index < 0 || index >= m_signals[0]) |
171 | return -1; |
172 | return m_signals[index+1]; |
173 | } |
174 | int sourceMethodIndex(int index) const override |
175 | { |
176 | if (index < 0 || index >= m_methods[0]) |
177 | return -1; |
178 | return m_methods[index+1]; |
179 | } |
180 | int signalParameterCount(int index) const override { return m_signalArgCount[index]; } |
181 | int signalParameterType(int sigIndex, int paramIndex) const override { return m_signalArgTypes[sigIndex][paramIndex]; } |
182 | int methodParameterCount(int index) const override { return m_methodArgCount[index]; } |
183 | int methodParameterType(int methodIndex, int paramIndex) const override { return m_methodArgTypes[methodIndex][paramIndex]; } |
184 | QList<QByteArray> signalParameterNames(int index) const override |
185 | { |
186 | QList<QByteArray> res; |
187 | int count = signalParameterCount(index); |
188 | while (count--) |
189 | res << QByteArray{}; |
190 | return res; |
191 | } |
192 | int propertyIndexFromSignal(int index) const override |
193 | { |
194 | switch (index) { |
195 | case 0: return m_properties[1]; |
196 | } |
197 | return -1; |
198 | } |
199 | int propertyRawIndexFromSignal(int index) const override |
200 | { |
201 | switch (index) { |
202 | case 0: return 0; |
203 | } |
204 | return -1; |
205 | } |
206 | const QByteArray signalSignature(int index) const override |
207 | { |
208 | switch (index) { |
209 | case 0: return QByteArrayLiteral("availableRolesChanged()" ); |
210 | case 1: return QByteArrayLiteral("dataChanged(IndexList,IndexList,QVector<int>)" ); |
211 | case 2: return QByteArrayLiteral("rowsInserted(IndexList,int,int)" ); |
212 | case 3: return QByteArrayLiteral("rowsRemoved(IndexList,int,int)" ); |
213 | case 4: return QByteArrayLiteral("rowsMoved(IndexList,int,int,IndexList,int)" ); |
214 | case 5: return QByteArrayLiteral("currentChanged(IndexList,IndexList)" ); |
215 | case 6: return QByteArrayLiteral("resetModel()" ); |
216 | case 7: return QByteArrayLiteral("headerDataChanged(Qt::Orientation,int,int)" ); |
217 | case 8: return QByteArrayLiteral("columnsInserted(IndexList,int,int)" ); |
218 | case 9: return QByteArrayLiteral("layoutChanged(IndexList,QAbstractItemModel::LayoutChangeHint)" ); |
219 | } |
220 | return QByteArrayLiteral("" ); |
221 | } |
222 | const QByteArray methodSignature(int index) const override |
223 | { |
224 | switch (index) { |
225 | case 0: return QByteArrayLiteral("replicaSizeRequest(IndexList)" ); |
226 | case 1: return QByteArrayLiteral("replicaRowRequest(IndexList,IndexList,QVector<int>)" ); |
227 | case 2: return QByteArrayLiteral("replicaHeaderRequest(QVector<Qt::Orientation>,QVector<int>,QVector<int>)" ); |
228 | case 3: return QByteArrayLiteral("replicaSetCurrentIndex(IndexList,QItemSelectionModel::SelectionFlags)" ); |
229 | case 4: return QByteArrayLiteral("replicaSetData(IndexList,QVariant,int)" ); |
230 | case 5: return QByteArrayLiteral("replicaCacheRequest(size_t,QVector<int>)" ); |
231 | } |
232 | return QByteArrayLiteral("" ); |
233 | } |
234 | QMetaMethod::MethodType methodType(int) const override |
235 | { |
236 | return QMetaMethod::Slot; |
237 | } |
238 | const QByteArray typeName(int index) const override |
239 | { |
240 | switch (index) { |
241 | case 0: return QByteArrayLiteral("QSize" ); |
242 | case 1: return QByteArrayLiteral("DataEntries" ); |
243 | case 2: return QByteArrayLiteral("QVariantList" ); |
244 | case 3: return QByteArrayLiteral("" ); |
245 | case 5: return QByteArrayLiteral("MetaAndDataEntries" ); |
246 | } |
247 | return QByteArrayLiteral("" ); |
248 | } |
249 | |
250 | QList<QByteArray> methodParameterNames(int index) const override |
251 | { |
252 | QList<QByteArray> res; |
253 | int count = methodParameterCount(index); |
254 | while (count--) |
255 | res << QByteArray{}; |
256 | return res; |
257 | } |
258 | |
259 | QByteArray objectSignature() const override { return QByteArray{}; } |
260 | bool isAdapterSignal(int index) const override |
261 | { |
262 | switch (index) { |
263 | case 0: |
264 | case 1: |
265 | case 2: |
266 | case 3: |
267 | case 4: |
268 | case 5: |
269 | case 8: |
270 | case 9: |
271 | return true; |
272 | } |
273 | return false; |
274 | } |
275 | bool isAdapterMethod(int index) const override |
276 | { |
277 | switch (index) { |
278 | case 0: |
279 | case 1: |
280 | case 2: |
281 | case 3: |
282 | case 4: |
283 | case 5: |
284 | return true; |
285 | } |
286 | return false; |
287 | } |
288 | bool isAdapterProperty(int index) const override |
289 | { |
290 | switch (index) { |
291 | case 0: |
292 | case 1: |
293 | return true; |
294 | } |
295 | return false; |
296 | } |
297 | |
298 | int m_properties[3]; |
299 | int m_signals[11]; |
300 | int m_methods[7]; |
301 | int m_signalArgCount[10]; |
302 | const int* m_signalArgTypes[10]; |
303 | int m_methodArgCount[6]; |
304 | const int* m_methodArgTypes[6]; |
305 | QString m_name; |
306 | }; |
307 | |
308 | QT_END_NAMESPACE |
309 | |
310 | #endif //QREMOTEOBJECTS_ABSTRACT_ITEM_ADAPTER_P_H |
311 | |