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

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