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
qandroidmodelindexproxy.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
8#include <QtCore/qjniarray.h>
9
11
12using namespace QtJniTypes;
13
15{
16 if (!jModelIndex.isValid())
17 return QModelIndex();
18
19 const QJniArray<jlong> jPrivateArray = jModelIndex.getField<jlong[]>("m_privateData");
20 const auto privateData = jPrivateArray.toContainer();
21 Q_ASSERT(privateData.size() == 4);
22
23 const jlong modelReference = privateData[3];
24 if (!modelReference)
25 return QModelIndex();
26
27 const jint row = privateData[0];
28 const jint column = privateData[1];
29 QAbstractItemModel *model = reinterpret_cast<QAbstractItemModel *>(modelReference);
30 QAndroidItemModelProxy *proxyModel = qobject_cast<QAndroidItemModelProxy *>(model);
31
32 // If the native model instance is a proxy we have access to the protected function
33 // createIndex(). Else, if the native instance is not a results Java->Qt proxy, we
34 // use index() to get the QModelIndex.
35 if (proxyModel) {
36 const jint internalId = privateData[2];
37 return proxyModel->createIndex(row, column, internalId);
38 } else {
39 const JQtModelIndex parent = jModelIndex.getField<JQtModelIndex>("m_parent");
40 if (parent.isValid())
42 }
43 return QModelIndex();
44}
45
47{
48 if (!modelIndex.isValid())
49 return JQtModelIndex();
50 bool isModelProxy = qobject_cast<const QAndroidItemModelProxy *>(modelIndex.model());
51 if (isModelProxy)
52 return JQtModelIndex(modelIndex.row(), modelIndex.column(), modelIndex.internalId(),
53 reinterpret_cast<jlong>(modelIndex.model()));
54 else
55 return JQtModelIndex(modelIndex.row(), modelIndex.column(),
57 reinterpret_cast<jlong>(modelIndex.model()));
58}
59
60jobject QAndroidModelIndexProxy::data(JNIEnv *env, jobject object, int role)
61{
62 Q_ASSERT(env);
63 Q_ASSERT(object);
64
65 QModelIndex modelIndex = qInstance(object);
66 if (!modelIndex.isValid())
67 return nullptr;
68
69 return QAndroidTypeConverter::toJavaObject(modelIndex.model()->data(modelIndex, role), env);
70}
71
72jlong QAndroidModelIndexProxy::internalId(JNIEnv *env, jobject object)
73{
74 Q_ASSERT(env);
75 Q_ASSERT(object);
76 return qInstance(object).internalId();
77};
78
79jboolean QAndroidModelIndexProxy::isValid(JNIEnv *env, jobject object)
80{
81 Q_ASSERT(env);
82 Q_ASSERT(object);
83 return qInstance(object).isValid();
84}
85
86JQtModelIndex QAndroidModelIndexProxy::parent(JNIEnv *env, jobject object)
87{
88 Q_ASSERT(env);
89 Q_ASSERT(object);
90 return jInstance(qInstance(object).parent());
91};
92
94{
95 return env.registerNativeMethods(
96 Traits<JQtModelIndex>::className(),
97 { Q_JNI_NATIVE_SCOPED_METHOD(data, QAndroidModelIndexProxy),
98 Q_JNI_NATIVE_SCOPED_METHOD(internalId, QAndroidModelIndexProxy),
99 Q_JNI_NATIVE_SCOPED_METHOD(isValid, QAndroidModelIndexProxy),
100 Q_JNI_NATIVE_SCOPED_METHOD(parent, QAndroidModelIndexProxy) });
101}
102
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.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
static jboolean isValid(JNIEnv *env, jobject object)
static JQtModelIndex jInstance(QModelIndex modelIndex)
static JQtModelIndex parent(JNIEnv *env, jobject object)
static QModelIndex qInstance(JQtModelIndex jModelIndex)
static jlong internalId(JNIEnv *env, jobject object)
static bool registerNatives(QJniEnvironment &env)
static jobject data(JNIEnv *env, jobject object, int role)
\inmodule QtCore
\inmodule QtCore
constexpr int row() const noexcept
Returns the row this model index refers to.
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
constexpr const QAbstractItemModel * model() const noexcept
Returns a pointer to the model containing the item that this index refers to.
constexpr int column() const noexcept
Returns the column this model index refers to.
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
constexpr quintptr internalId() const noexcept
Returns a {quintptr} used by the model to associate the index with the internal data structure.
static Q_REQUIRED_RESULT jobject toJavaObject(const QVariant &var, JNIEnv *env)
Combined button and popup list for selecting options.
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QSqlQueryModel * model
[16]