Android: Add OnDataChangedListerner callback interface

Adds a new interface to QtAbstractItemModel Java class that
can be used as a callback to listen to the changes of an
editable model. The model can be an implementation of
QtAbstractItemModel with an underlaying C++
QAndroidAbstractModelProxy instance, or it can be an instance
of QtAbstractItemModel that proxies and refers to
a QAbstractItemModel instance.

This also adds setOnDataChangedListener() to QtAbstractItemModel
so that it can call the interface callback function by connecting
dataChanged() signal of the native instance to a lambda that
invokes the handleDataChanged() private method of the JVM instance.

Task-number: QTBUG-129387
Task-number: QTBUG-130251
Task-number: QTBUG-130252
Change-Id: I23da9e980b5402f4dcf7446560155271ccd8809e
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Soheil Armin 2024-10-17 10:41:36 +03:00
parent ab027eb73a
commit a065c6bfe0
2 changed files with 44 additions and 0 deletions

View File

@ -263,6 +263,27 @@ public abstract class QtAbstractItemModel
public void dataChanged(QtModelIndex topLeft, QtModelIndex bottomRight, int[] roles) {
jni_dataChanged(topLeft, bottomRight, roles);
}
/**
* Interface for a callback to be invoked when the data in an existing item changes.
*/
public interface OnDataChangedListener {
/**
* Called when the data in an existing item changes.
*
* @param topLeft The top-left index of changed items
* @param bottomRight The bottom-right index of changed items
* @param roles Changed roles; Empty array indicates all roles
*/
void onDataChanged(QtModelIndex topLeft, QtModelIndex bottomRight, int[] roles);
}
/**
* Register a callback to be invoked when the data in an existing item changes.
*
* @param listener The data change listener
*/
public void setOnDataChangedListener(OnDataChangedListener listener) {
m_OnDataChangedListener = listener;
}
/**
* Begins a column insertion operation.
@ -503,6 +524,15 @@ public abstract class QtAbstractItemModel
*/
protected final void endResetModel() { jni_endResetModel(); }
private void handleDataChanged(QtModelIndex topLeft, QtModelIndex bottomRight, int[] roles)
{
if (m_OnDataChangedListener != null) {
QtNative.runAction(() -> {
if (m_OnDataChangedListener != null)
m_OnDataChangedListener.onDataChanged(topLeft, bottomRight, roles);
});
}
}
private native void jni_beginInsertColumns(QtModelIndex parent, int first, int last);
private native void jni_beginInsertRows(QtModelIndex parent, int first, int last);
private native boolean jni_beginMoveColumns(QtModelIndex sourceParent, int sourceFirst,
@ -529,6 +559,7 @@ public abstract class QtAbstractItemModel
int[] roles);
private long m_nativeReference = 0;
private OnDataChangedListener m_OnDataChangedListener;
private QtAbstractItemModel(long nativeReference) { m_nativeReference = nativeReference; }
private void detachFromNative() { m_nativeReference = 0; }

View File

@ -160,6 +160,19 @@ QAndroidItemModelProxy::createNativeProxy(QJniObject itemModel)
if (proxy)
proxy->jInstance.callMethod<void>("detachFromNative");
});
connect(nativeProxy, &QAndroidItemModelProxy::dataChanged, nativeProxy,
[nativeProxy](const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QList<int> &roles) {
auto proxy = qobject_cast<QAndroidItemModelProxy *>(nativeProxy);
if (proxy) {
QJniObject jInstance = proxy->jInstance;
jInstance.callMethod<void>("handleDataChanged",
QAndroidModelIndexProxy::jInstance(topLeft),
QAndroidModelIndexProxy::jInstance(bottomRight),
QJniArray<jint>(roles));
}
});
}
return nativeProxy;
}