summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Holland <dominik.holland@pelagicore.com>2018-03-01 11:12:21 +0100
committerDominik Holland <dominik.holland@pelagicore.com>2018-03-09 15:16:28 +0000
commite2914fa9702ad553494ee05a58dde967bdefe004 (patch)
tree9c44d6573e53a44369c5a7036c86e1febe276113
parentca12be0bcbbf460efdf9ccf7ba5c9eb3fab98564 (diff)
downloadqtivi-e2914fa9702ad553494ee05a58dde967bdefe004.tar.gz
Add a reload method to QIviSearchAndBrowseModel
The reload() method will reset the model and fetch the data again from the backend. This also fixes some problems, which resulted in duplicated entries during the initial fetch. Change-Id: I9741f23165a2da7a1587a2893f891a386d2cbabe Reviewed-by: Robert Griebl <robert.griebl@pelagicore.com>
-rw-r--r--src/ivicore/qivisearchandbrowsemodel.cpp23
-rw-r--r--src/ivicore/qivisearchandbrowsemodel.h1
-rw-r--r--tests/auto/core/qivisearchandbrowsemodel/tst_qivisearchandbrowsemodel.cpp30
3 files changed, 52 insertions, 2 deletions
diff --git a/src/ivicore/qivisearchandbrowsemodel.cpp b/src/ivicore/qivisearchandbrowsemodel.cpp
index 2c18227..abf2061 100644
--- a/src/ivicore/qivisearchandbrowsemodel.cpp
+++ b/src/ivicore/qivisearchandbrowsemodel.cpp
@@ -216,11 +216,14 @@ void QIviSearchAndBrowseModelPrivate::onFetchMoreThresholdReached()
void QIviSearchAndBrowseModelPrivate::resetModel()
{
Q_Q(QIviSearchAndBrowseModel);
+
q->beginResetModel();
m_itemList.clear();
m_availableChunks.clear();
- q->endResetModel();
m_fetchedDataCount = 0;
+ //Setting this to true to let fetchMore do one first fetchcall.
+ m_moreAvailable = true;
+ q->endResetModel();
if (searchBackend())
setAvailableContenTypes(searchBackend()->availableContentTypes().toList());
@@ -228,7 +231,6 @@ void QIviSearchAndBrowseModelPrivate::resetModel()
checkType();
parseQuery();
- m_moreAvailable = false;
q->fetchMore(QModelIndex());
}
@@ -1342,6 +1344,20 @@ void QIviSearchAndBrowseModel::indexOf(const QVariant &variant, const QJSValue &
}
/*!
+ \qmlmethod SearchAndBrowseModel::reload()
+
+ Resets the model and starts fetching the content again.
+*/
+/*!
+ Resets the model and starts fetching the content again.
+*/
+void QIviSearchAndBrowseModel::reload()
+{
+ Q_D(QIviSearchAndBrowseModel);
+ d->resetModel();
+}
+
+/*!
\reimp
*/
bool QIviSearchAndBrowseModel::canFetchMore(const QModelIndex &parent) const
@@ -1365,6 +1381,9 @@ void QIviSearchAndBrowseModel::fetchMore(const QModelIndex &parent)
if (!d->searchBackend() || d->m_contentType.isEmpty())
return;
+ if (!d->m_moreAvailable)
+ return;
+
d->m_moreAvailable = false;
d->fetchData(-1);
}
diff --git a/src/ivicore/qivisearchandbrowsemodel.h b/src/ivicore/qivisearchandbrowsemodel.h
index fc85aac..cf201e8 100644
--- a/src/ivicore/qivisearchandbrowsemodel.h
+++ b/src/ivicore/qivisearchandbrowsemodel.h
@@ -146,6 +146,7 @@ public:
Q_INVOKABLE void move(int cur_index, int new_index);
//TODO add a C++ version for this, similar to QTimer::singleShot()
Q_INVOKABLE void indexOf(const QVariant &variant, const QJSValue &functor);
+ Q_INVOKABLE void reload();
template <typename T> T at(int i) const {
return data(index(i,0), ItemRole).value<T>();
diff --git a/tests/auto/core/qivisearchandbrowsemodel/tst_qivisearchandbrowsemodel.cpp b/tests/auto/core/qivisearchandbrowsemodel/tst_qivisearchandbrowsemodel.cpp
index d7a2e88..5b90a25 100644
--- a/tests/auto/core/qivisearchandbrowsemodel/tst_qivisearchandbrowsemodel.cpp
+++ b/tests/auto/core/qivisearchandbrowsemodel/tst_qivisearchandbrowsemodel.cpp
@@ -357,6 +357,7 @@ private Q_SLOTS:
void testFetchMore_data();
void testFetchMore();
void testDataChangedMode();
+ void testReload();
void testDataChangedMode_jump();
void testNavigation_data();
void testNavigation();
@@ -607,6 +608,35 @@ void tst_QIviSearchAndBrowseModel::testDataChangedMode()
QCOMPARE(fetchDataSpy.at(0).at(2).toInt(), testIndex + 1);
}
+void tst_QIviSearchAndBrowseModel::testReload()
+{
+ TestServiceObject *service = new TestServiceObject();
+ manager->registerService(service, service->interfaces());
+ service->testBackend()->setCapabilities(QIviSearchAndBrowseModel::SupportsGetSize);
+ service->testBackend()->initializeSimpleData();
+
+ QIviSearchAndBrowseModel model;
+ model.setServiceObject(service);
+
+ QVERIFY(model.serviceObject());
+
+ QVERIFY(model.availableContentTypes().contains("simple"));
+ QSignalSpy countChangedSpy(&model, SIGNAL(countChanged()));
+ model.setContentType("simple");
+ countChangedSpy.wait(100);
+ QVERIFY(countChangedSpy.count());
+
+ QCOMPARE(model.rowCount(), model.chunkSize());
+ countChangedSpy.clear();
+
+ QSignalSpy resetSpy(&model, SIGNAL(modelReset()));
+ model.reload();
+ countChangedSpy.wait(100);
+ QCOMPARE(resetSpy.count(), 1);
+ QCOMPARE(countChangedSpy.count(), 2);
+ QCOMPARE(model.rowCount(), model.chunkSize());
+}
+
void tst_QIviSearchAndBrowseModel::testDataChangedMode_jump()
{
TestServiceObject *service = new TestServiceObject();