summaryrefslogtreecommitdiffstats
path: root/common/modelresult.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-03 11:36:00 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-03 12:35:54 +0100
commit46bf82eaec0a30281d0b8deaf1ffbd06030eb997 (patch)
treecf081690acfd5a0911b1ce0475bc17ef75a45dea /common/modelresult.cpp
parentf63f772eb59e246948fcdf178abee72dc3efc8f0 (diff)
downloadsink-46bf82eaec0a30281d0b8deaf1ffbd06030eb997.tar.gz
sink-46bf82eaec0a30281d0b8deaf1ffbd06030eb997.zip
Since we only support incremental fetching for flat lists a boolean is
enough.
Diffstat (limited to 'common/modelresult.cpp')
-rw-r--r--common/modelresult.cpp65
1 files changed, 23 insertions, 42 deletions
diff --git a/common/modelresult.cpp b/common/modelresult.cpp
index 5f8fc8a..9685e3c 100644
--- a/common/modelresult.cpp
+++ b/common/modelresult.cpp
@@ -259,8 +259,8 @@ bool ModelResult<T, Ptr>::hasChildren(const QModelIndex &parent) const
259template <class T, class Ptr> 259template <class T, class Ptr>
260bool ModelResult<T, Ptr>::canFetchMore(const QModelIndex &parent) const 260bool ModelResult<T, Ptr>::canFetchMore(const QModelIndex &parent) const
261{ 261{
262 const auto id = parent.internalId(); 262 //We fetch trees immediately so can never fetch more
263 if (mEntityAllChildrenFetched.contains(id)) { 263 if (parent.isValid() || mFetchedAll) {
264 return false; 264 return false;
265 } 265 }
266 return true; 266 return true;
@@ -270,8 +270,23 @@ template <class T, class Ptr>
270void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent) 270void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent)
271{ 271{
272 SinkTraceCtx(mLogCtx) << "Fetching more: " << parent; 272 SinkTraceCtx(mLogCtx) << "Fetching more: " << parent;
273 if (!parent.isValid()) { 273 Q_ASSERT(QThread::currentThread() == this->thread());
274 fetchEntities(); 274 //We only suppor fetchMore for flat lists
275 if (parent.isValid()) {
276 return;
277 }
278 //There is already a fetch in progress, don't fetch again.
279 if (mFetchInProgress) {
280 SinkTraceCtx(mLogCtx) << "A fetch is already in progress.";
281 return;
282 }
283 mFetchInProgress = true;
284 mFetchComplete = false;
285 SinkTraceCtx(mLogCtx) << "Fetching more.";
286 if (loadEntities) {
287 loadEntities();
288 } else {
289 SinkWarningCtx(mLogCtx) << "No way to fetch entities";
275 } 290 }
276} 291}
277 292
@@ -280,11 +295,6 @@ void ModelResult<T, Ptr>::add(const Ptr &value)
280{ 295{
281 const auto childId = qHash(*value); 296 const auto childId = qHash(*value);
282 const auto pId = parentId(value); 297 const auto pId = parentId(value);
283 // Ignore updates we get before the initial fetch is done
284 // if (!mEntityChildrenFetched.contains(id)) {
285 // SinkTraceCtx(mLogCtx) << "Too early" << id;
286 // return;
287 // }
288 if (mEntities.contains(childId)) { 298 if (mEntities.contains(childId)) {
289 SinkWarningCtx(mLogCtx) << "Entity already in model: " << value->identifier(); 299 SinkWarningCtx(mLogCtx) << "Entity already in model: " << value->identifier();
290 return; 300 return;
@@ -333,26 +343,6 @@ void ModelResult<T, Ptr>::remove(const Ptr &value)
333} 343}
334 344
335template <class T, class Ptr> 345template <class T, class Ptr>
336void ModelResult<T, Ptr>::fetchEntities()
337{
338 Q_ASSERT(QThread::currentThread() == this->thread());
339 const auto id = getIdentifier({});
340 //There is already a fetch in progress, don't fetch again.
341 if (mEntityChildrenFetched.contains(id) && !mEntityChildrenFetchComplete.contains(id)) {
342 SinkTraceCtx(mLogCtx) << "A fetch is already in progress.";
343 return;
344 }
345 mEntityChildrenFetchComplete.remove(id);
346 mEntityChildrenFetched.insert(id);
347 SinkTraceCtx(mLogCtx) << "Loading child entities of parent " << id;
348 if (loadEntities) {
349 loadEntities();
350 } else {
351 SinkWarningCtx(mLogCtx) << "No way to fetch entities";
352 }
353}
354
355template <class T, class Ptr>
356void ModelResult<T, Ptr>::setFetcher(const std::function<void()> &fetcher) 346void ModelResult<T, Ptr>::setFetcher(const std::function<void()> &fetcher)
357{ 347{
358 SinkTraceCtx(mLogCtx) << "Setting fetcher"; 348 SinkTraceCtx(mLogCtx) << "Setting fetcher";
@@ -391,14 +381,9 @@ void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Pt
391 SinkTraceCtx(mLogCtx) << "Initial result set complete. Fetched all: " << fetchedAll; 381 SinkTraceCtx(mLogCtx) << "Initial result set complete. Fetched all: " << fetchedAll;
392 Q_ASSERT(guard); 382 Q_ASSERT(guard);
393 Q_ASSERT(QThread::currentThread() == this->thread()); 383 Q_ASSERT(QThread::currentThread() == this->thread());
394 384 mFetchInProgress = false;
395 // const qint64 parentId = parent ? qHash(*parent) : 0; 385 mFetchedAll = fetchedAll;
396 // const auto parentIndex = createIndexFromId(parentId); 386 mFetchComplete = true;
397 mEntityChildrenFetchComplete.insert(0);
398 if (fetchedAll) {
399 mEntityAllChildrenFetched.insert(0);
400 }
401 // emit dataChanged(parentIndex, parentIndex, QVector<int>() << ChildrenFetchedRole);
402 emit dataChanged({}, {}, QVector<int>() << ChildrenFetchedRole); 387 emit dataChanged({}, {}, QVector<int>() << ChildrenFetchedRole);
403 }); 388 });
404 mEmitter = emitter; 389 mEmitter = emitter;
@@ -407,7 +392,7 @@ void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Pt
407template <class T, class Ptr> 392template <class T, class Ptr>
408bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const 393bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const
409{ 394{
410 return mEntityChildrenFetchComplete.contains(getIdentifier(index)); 395 return mFetchComplete;
411} 396}
412 397
413template <class T, class Ptr> 398template <class T, class Ptr>
@@ -421,10 +406,6 @@ void ModelResult<T, Ptr>::modify(const Ptr &value)
421 return; 406 return;
422 } 407 }
423 auto id = parentId(value); 408 auto id = parentId(value);
424 // Ignore updates we get before the initial fetch is done
425 // if (!mEntityChildrenFetched.contains(id)) {
426 // return;
427 // }
428 auto parent = createIndexFromId(id); 409 auto parent = createIndexFromId(id);
429 SinkTraceCtx(mLogCtx) << "Modified entity:" << value->identifier() << ", id: " << childId; 410 SinkTraceCtx(mLogCtx) << "Modified entity:" << value->identifier() << ", id: " << childId;
430 auto i = mTree[id].indexOf(childId); 411 auto i = mTree[id].indexOf(childId);