Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qandroiditemmodelproxy.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtQuick/private/qandroiditemmodelproxy_p.h>
5#include <QtQuick/private/qandroidmodelindexproxy_p.h>
6#include <QtQuick/private/qandroidtypeconverter_p.h>
7
9
10using namespace QtJniTypes;
11
13{
14 Q_ASSERT(jInstance.isValid());
16 return jInstance.callMethod<jint>("columnCount", parentIndex);
17}
18
20{
21 Q_ASSERT(jInstance.isValid());
23 return jInstance.callMethod<jboolean>("canFetchMore", parentIndex);
24}
25
30
32{
33 Q_ASSERT(jInstance.isValid());
35 QJniObject jData = jInstance.callMethod<jobject>("data", jIndex, role);
37}
38
40{
41 Q_ASSERT(jInstance.isValid());
42 JQtModelIndex jIndex = jInstance.callMethod<JQtModelIndex>(
45}
46
48{
49 Q_ASSERT(jInstance.isValid());
50
53 jInstance.callMethod<JQtModelIndex>("parent", jIndex));
54}
56{
57 Q_ASSERT(jInstance.isValid());
58
60 return jInstance.callMethod<int>("rowCount", parentIndex);
61}
62
63QHash<int, QByteArray> QAndroidItemModelProxy::roleNames() const
64{
65 Q_ASSERT(jInstance.isValid());
66
67 QHash<int, QByteArray> roleNames;
68 HashMap hashMap = jInstance.callMethod<HashMap>("roleNames");
69 Set set = hashMap.callMethod<Set>("keySet");
70 QJniArray<jobject> keyArray = set.callMethod<QJniArray<jobject>>("toArray");
71
72 for (auto key : keyArray) {
73 const QJniObject roleName = hashMap.callMethod<jobject>("get", key);
74 const int intKey = QJniObject(key).callMethod<jint>("intValue");
75 const QByteArray roleByteArray = String(roleName).toString().toLatin1();
76 roleNames.insert(intKey, roleByteArray);
77 }
78 return roleNames;
79}
80
81QHash<int, QByteArray> QAndroidItemModelProxy::defaultRoleNames() const
82{
84}
85
87{
88 Q_ASSERT(jInstance.isValid());
90 jInstance.callMethod<void>("fetchMore", parentIndex);
91}
92
97
99{
100 Q_ASSERT(jInstance.isValid());
101 auto parentIndex = QAndroidModelIndexProxy::jInstance(parent);
102 return jInstance.callMethod<jboolean>("hasChildren", parentIndex);
103}
104
109
111{
112 Q_ASSERT(jInstance.isValid());
113 return QAndroidModelIndexProxy::qInstance(jInstance.callMethod<jobject>(
115}
116
121
123QAndroidItemModelProxy::nativeInstance(JQtAbstractItemModel itemModel)
124{
125 jlong nativeReference = itemModel.callMethod<jlong>("nativeReference");
126 return reinterpret_cast<QAbstractItemModel *>(nativeReference);
127}
128
131{
132 QAbstractItemModel *nativeProxy = nativeInstance(itemModel);
133 if (!nativeProxy) {
134 nativeProxy = new QAndroidItemModelProxy(itemModel);
135
136 itemModel.callMethod<void>("setNativeReference", reinterpret_cast<jlong>(nativeProxy));
137 connect(nativeProxy, &QAndroidItemModelProxy::destroyed, nativeProxy, [](QObject *obj) {
138 auto proxy = qobject_cast<QAndroidItemModelProxy *>(obj);
139 if (proxy)
140 proxy->jInstance.callMethod<void>("detachFromNative");
141 });
142 }
143 return nativeProxy;
144}
145
147{
148 return JQtAndroidItemModelProxy(reinterpret_cast<jlong>(itemModel));
149}
150
151int QAndroidItemModelProxy::jni_columnCount(JNIEnv *env, jobject object, JQtModelIndex parent)
152{
154 return invokeNativeMethod(env, object, &QAbstractItemModel::columnCount, nativeParent);
155}
156
157jobject QAndroidItemModelProxy::jni_data(JNIEnv *env, jobject object, JQtModelIndex index,
158 jint role)
159{
161 const QVariant data =
162 invokeNativeMethod(env, object, &QAbstractItemModel::data, nativeIndex, role);
164}
165
166jobject QAndroidItemModelProxy::jni_index(JNIEnv *env, jobject object, jint row, jint column,
167 JQtModelIndex parent)
168{
169 auto nativeParent = QAndroidModelIndexProxy::qInstance(parent);
170 const QModelIndex modelIndex =
171 invokeNativeMethod(env, object, &QAbstractItemModel::index, row, column, nativeParent);
172 return env->NewLocalRef(QAndroidModelIndexProxy::jInstance(modelIndex).object());
173}
174
175jobject QAndroidItemModelProxy::jni_parent(JNIEnv *env, jobject object, JQtModelIndex index)
176{
178 QModelIndex (QAbstractItemModel::*parentOverloadPtr)(const QModelIndex &) const =
180 const QModelIndex parent = invokeNativeMethod(env, object, parentOverloadPtr, nativeIndex);
181 return env->NewLocalRef(QAndroidModelIndexProxy::jInstance(parent).object());
182}
183
184jint QAndroidItemModelProxy::jni_rowCount(JNIEnv *env, jobject object, JQtModelIndex parent)
185{
188}
189
190jobject QAndroidItemModelProxy::jni_roleNames(JNIEnv *env, jobject object)
191{
194 HashMap jRoleNames{};
195 for (auto [role, roleName] : roleNames.asKeyValueRange()) {
196 const Integer jRole(role);
197 const QJniObject jRoleName = QJniObject::fromString(roleName);
198 jRoleNames.callMethod<jobject>("put", jRole.object(), jRoleName.object());
199 }
200 return env->NewLocalRef(jRoleNames.object());
201}
202
203jobject QAndroidItemModelProxy::jni_createIndex(JNIEnv *env, jobject object, jint row, jint column,
204 jlong id)
205{
206 QModelIndex (QAndroidItemModelProxy::*createIndexPtr)(int, int, quintptr) const =
208 const QModelIndex index = invokeNativeProxyMethod(env, object, createIndexPtr, row, column, id);
209 return env->NewLocalRef(QAndroidModelIndexProxy::jInstance(index).object());
210}
211
218
219void QAndroidItemModelProxy::jni_fetchMore(JNIEnv *env, jobject object, JQtModelIndex parent)
220{
224}
225
226jboolean QAndroidItemModelProxy::jni_hasChildren(JNIEnv *env, jobject object, JQtModelIndex parent)
227{
231}
232
233jboolean QAndroidItemModelProxy::jni_hasIndex(JNIEnv *env, jobject object, jint row, jint column,
234 JQtModelIndex parent)
235{
238}
239
240void QAndroidItemModelProxy::jni_beginInsertColumns(JNIEnv *env, jobject object,
241 JQtModelIndex parent, jint first, jint last)
242{
243
246}
247
248void QAndroidItemModelProxy::jni_beginInsertRows(JNIEnv *env, jobject object, JQtModelIndex parent,
249 jint first, jint last)
250{
253}
254
255jboolean QAndroidItemModelProxy::jni_beginMoveColumns(JNIEnv *env, jobject object,
256 JQtModelIndex sourceParent, jint sourceFirst,
257 jint sourceLast,
258 JQtModelIndex destinationParent,
259 jint destinationChild)
260{
263 QAndroidModelIndexProxy::qInstance(sourceParent), sourceFirst, sourceLast,
265}
266
267jboolean QAndroidItemModelProxy::jni_beginMoveRows(JNIEnv *env, jobject object,
268 JQtModelIndex sourceParent, jint sourceFirst,
269 jint sourceLast, JQtModelIndex destinationParent,
270 jint destinationChild)
271{
274 QAndroidModelIndexProxy::qInstance(sourceParent), sourceFirst, sourceLast,
276}
277
278void QAndroidItemModelProxy::jni_beginRemoveColumns(JNIEnv *env, jobject object,
279 JQtModelIndex parent, jint first, jint last)
280{
283}
284
285void QAndroidItemModelProxy::jni_beginRemoveRows(JNIEnv *env, jobject object, JQtModelIndex parent,
286 jint first, jint last)
287{
290}
291
296
301
306
311
316
321
326
331
332jobject QAndroidItemModelProxy::jni_sibling(JNIEnv *env, jobject object, jint row, jint column,
333 JQtModelIndex parent)
334{
338 return env->NewLocalRef(QAndroidModelIndexProxy::jInstance(index).object());
339}
340
342{
343 return env.registerNativeMethods(
344 Traits<JQtAbstractItemModel>::className(),
345 { Q_JNI_NATIVE_SCOPED_METHOD(jni_roleNames, QAndroidItemModelProxy),
346 Q_JNI_NATIVE_SCOPED_METHOD(jni_canFetchMore, QAndroidItemModelProxy),
347 Q_JNI_NATIVE_SCOPED_METHOD(jni_createIndex, QAndroidItemModelProxy),
348 Q_JNI_NATIVE_SCOPED_METHOD(jni_fetchMore, QAndroidItemModelProxy),
349 Q_JNI_NATIVE_SCOPED_METHOD(jni_hasChildren, QAndroidItemModelProxy),
350 Q_JNI_NATIVE_SCOPED_METHOD(jni_hasIndex, QAndroidItemModelProxy),
351 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginInsertColumns, QAndroidItemModelProxy),
352 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginInsertRows, QAndroidItemModelProxy),
353 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginMoveColumns, QAndroidItemModelProxy),
354 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginMoveRows, QAndroidItemModelProxy),
355 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginRemoveColumns, QAndroidItemModelProxy),
356 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginRemoveRows, QAndroidItemModelProxy),
357 Q_JNI_NATIVE_SCOPED_METHOD(jni_beginResetModel, QAndroidItemModelProxy),
358 Q_JNI_NATIVE_SCOPED_METHOD(jni_endInsertColumns, QAndroidItemModelProxy),
359 Q_JNI_NATIVE_SCOPED_METHOD(jni_endInsertRows, QAndroidItemModelProxy),
360 Q_JNI_NATIVE_SCOPED_METHOD(jni_endMoveColumns, QAndroidItemModelProxy),
361 Q_JNI_NATIVE_SCOPED_METHOD(jni_endMoveRows, QAndroidItemModelProxy),
362 Q_JNI_NATIVE_SCOPED_METHOD(jni_endRemoveColumns, QAndroidItemModelProxy),
363 Q_JNI_NATIVE_SCOPED_METHOD(jni_endRemoveRows, QAndroidItemModelProxy),
364 Q_JNI_NATIVE_SCOPED_METHOD(jni_endResetModel, QAndroidItemModelProxy),
365 Q_JNI_NATIVE_SCOPED_METHOD(jni_sibling, QAndroidItemModelProxy) });
366}
367
369{
370 return env.registerNativeMethods(
371 Traits<JQtAndroidItemModelProxy>::className(),
372 { Q_JNI_NATIVE_SCOPED_METHOD(jni_columnCount, QAndroidItemModelProxy),
373 Q_JNI_NATIVE_SCOPED_METHOD(jni_data, QAndroidItemModelProxy),
374 Q_JNI_NATIVE_SCOPED_METHOD(jni_index, QAndroidItemModelProxy),
375 Q_JNI_NATIVE_SCOPED_METHOD(jni_parent, QAndroidItemModelProxy),
376 Q_JNI_NATIVE_SCOPED_METHOD(jni_rowCount, QAndroidItemModelProxy) });
377}
378
Q_INVOKABLE int const QModelIndex & parent
Returns the parent of the model item with the given index.
void endResetModel()
Completes a model reset operation.
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow)
void endMoveColumns()
Ends a column move operation.
Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns {true} if the model returns a valid QModelIndex for row and column with parent,...
void endRemoveRows()
Ends a row removal operation.
void endMoveRows()
Ends a row move operation.
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
Begins a column removal operation.
Q_INVOKABLE int int const QModelIndex & destinationParent
virtual Q_INVOKABLE bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Returns {true} if parent has any children; otherwise returns {false}.
virtual Q_INVOKABLE int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
virtual Q_INVOKABLE void fetchMore(const QModelIndex &parent)
Fetches any available data for the items with the parent specified by the parent index.
Q_INVOKABLE int int const QModelIndex int destinationChild
virtual Q_INVOKABLE bool canFetchMore(const QModelIndex &parent) const
Returns {true} if there is more data available for parent; otherwise returns {false}.
virtual Q_INVOKABLE QModelIndex sibling(int row, int column, const QModelIndex &idx) const
Returns the sibling at row and column for the item at index, or an invalid QModelIndex if there is no...
bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn)
Begins a column move operation.
virtual QHash< int, QByteArray > roleNames() const
void beginInsertColumns(const QModelIndex &parent, int first, int last)
Begins a column insertion operation.
void endInsertRows()
Ends a row insertion operation.
void beginResetModel()
Begins a model reset operation.
void endRemoveColumns()
Ends a column removal operation.
virtual Q_INVOKABLE int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
QModelIndex createIndex(int row, int column, const void *data=nullptr) const
Creates a model index for the given row and column with the internal pointer ptr.
void endInsertColumns()
Ends a column insertion operation.
virtual Q_INVOKABLE QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
virtual Q_INVOKABLE QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index.
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
static jobject jni_roleNames(JNIEnv *env, jobject object)
static QJniObject createProxy(QAbstractItemModel *abstractClass)
static jboolean jni_beginMoveRows(JNIEnv *env, jobject object, JQtModelIndex sourceParent, jint sourceFirst, jint sourceLast, JQtModelIndex destinationParent, jint destinationChild)
static auto invokeNativeMethod(JNIEnv *, jobject jvmObject, Func func, Args &&...args)
static void jni_beginResetModel(JNIEnv *env, jobject object)
static void jni_beginInsertRows(JNIEnv *env, jobject object, JQtModelIndex parent, jint first, jint last)
static Q_REQUIRED_RESULT QAbstractItemModel * nativeInstance(QtJniTypes::JQtAbstractItemModel itemModel)
static void jni_endRemoveRows(JNIEnv *env, jobject object)
static void jni_endResetModel(JNIEnv *env, jobject object)
static bool registerProxyNatives(QJniEnvironment &env)
QAndroidItemModelProxy(QtJniTypes::JQtAbstractItemModel jInstance)
static void jni_endInsertRows(JNIEnv *env, jobject object)
bool canFetchMoreDefault(const QModelIndex &parent) const
static auto invokeNativeProxyMethod(JNIEnv *, jobject jvmObject, Func func, Args &&...args)
static void jni_endMoveColumns(JNIEnv *env, jobject object)
static void jni_beginRemoveRows(JNIEnv *env, jobject object, JQtModelIndex parent, jint first, jint last)
void fetchMoreDefault(const QModelIndex &parent)
static void jni_endMoveRows(JNIEnv *env, jobject object)
static jint jni_columnCount(JNIEnv *env, jobject object, JQtModelIndex parent)
static jobject jni_createIndex(JNIEnv *env, jobject object, jint row, jint column, jlong id)
void fetchMore(const QModelIndex &parent) override
Fetches any available data for the items with the parent specified by the parent index.
static void jni_beginInsertColumns(JNIEnv *env, jobject object, JQtModelIndex parent, jint first, jint last)
static void jni_endRemoveColumns(JNIEnv *env, jobject object)
QModelIndex sibling(int row, int column, const QModelIndex &parent) const override
Returns the sibling at row and column for the item at index, or an invalid QModelIndex if there is no...
static jboolean jni_canFetchMore(JNIEnv *env, jobject object, JQtModelIndex parent)
bool hasChildren(const QModelIndex &parent) const override
Returns {true} if parent has any children; otherwise returns {false}.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
static jobject jni_parent(JNIEnv *env, jobject object, JQtModelIndex index)
static void jni_endInsertColumns(JNIEnv *env, jobject object)
static bool registerAbstractNatives(QJniEnvironment &env)
static jobject jni_sibling(JNIEnv *env, jobject object, jint row, jint column, JQtModelIndex parent)
static void jni_beginRemoveColumns(JNIEnv *env, jobject object, JQtModelIndex parent, jint first, jint last)
QHash< int, QByteArray > defaultRoleNames() const
static jobject jni_data(JNIEnv *env, jobject object, JQtModelIndex index, jint role)
static jboolean jni_hasChildren(JNIEnv *env, jobject object, JQtModelIndex parent)
bool hasChildrenDefault(const QModelIndex &parent) const
QModelIndex siblingDefault(int row, int column, const QModelIndex &parent)
static Q_REQUIRED_RESULT QAbstractItemModel * createNativeProxy(QJniObject itemModel)
static jboolean jni_beginMoveColumns(JNIEnv *env, jobject object, JQtModelIndex sourceParent, jint sourceFirst, jint sourceLast, JQtModelIndex destinationParent, jint destinationChild)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
static void jni_fetchMore(JNIEnv *env, jobject object, JQtModelIndex parent)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
bool canFetchMore(const QModelIndex &parent) const override
Returns {true} if there is more data available for parent; otherwise returns {false}.
static jboolean jni_hasIndex(JNIEnv *env, jobject object, jint row, jint column, JQtModelIndex parent)
static auto invokeNativeImpl(JNIEnv *, jobject jvmObject, Func1 defaultFunc, Func2 func, Args &&...args)
static jobject jni_index(JNIEnv *env, jobject object, jint row, jint column, JQtModelIndex parent)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index.
static jint jni_rowCount(JNIEnv *env, jobject object, JQtModelIndex parent)
QHash< int, QByteArray > roleNames() const override
static JQtModelIndex jInstance(QModelIndex modelIndex)
static QModelIndex qInstance(JQtModelIndex jModelIndex)
\inmodule QtCore
Definition qbytearray.h:57
auto asKeyValueRange() &
Definition qhash.h:1229
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1304
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
\inmodule QtCore
Definition qvariant.h:65
static QVariant toQVariant(const QJniObject &object)
static Q_REQUIRED_RESULT jobject toJavaObject(const QVariant &var, JNIEnv *env)
Combined button and popup list for selecting options.
#define Q_REQUIRED_RESULT
GLuint64 key
GLuint index
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
GLenum GLenum GLsizei void GLsizei void * column
GLhandleARB obj
[2]
GLenum GLenum GLsizei void * row
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
size_t quintptr
Definition qtypes.h:167
QFuture< QSet< QChar > > set
[10]
QNetworkProxy proxy
[0]