diff options
-rw-r--r-- | common/modelresult.cpp | 65 | ||||
-rw-r--r-- | common/modelresult.h | 7 |
2 files changed, 26 insertions, 46 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 | |||
259 | template <class T, class Ptr> | 259 | template <class T, class Ptr> |
260 | bool ModelResult<T, Ptr>::canFetchMore(const QModelIndex &parent) const | 260 | bool 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> | |||
270 | void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent) | 270 | void 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 | ||
335 | template <class T, class Ptr> | 345 | template <class T, class Ptr> |
336 | void 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 | |||
355 | template <class T, class Ptr> | ||
356 | void ModelResult<T, Ptr>::setFetcher(const std::function<void()> &fetcher) | 346 | void 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 | |||
407 | template <class T, class Ptr> | 392 | template <class T, class Ptr> |
408 | bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const | 393 | bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const |
409 | { | 394 | { |
410 | return mEntityChildrenFetchComplete.contains(getIdentifier(index)); | 395 | return mFetchComplete; |
411 | } | 396 | } |
412 | 397 | ||
413 | template <class T, class Ptr> | 398 | template <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); |
diff --git a/common/modelresult.h b/common/modelresult.h index a4def81..55a95b9 100644 --- a/common/modelresult.h +++ b/common/modelresult.h | |||
@@ -75,17 +75,16 @@ private: | |||
75 | 75 | ||
76 | qint64 parentId(const Ptr &value); | 76 | qint64 parentId(const Ptr &value); |
77 | QModelIndex createIndexFromId(const qint64 &id) const; | 77 | QModelIndex createIndexFromId(const qint64 &id) const; |
78 | void fetchEntities(); | ||
79 | 78 | ||
80 | Sink::Log::Context mLogCtx; | 79 | Sink::Log::Context mLogCtx; |
81 | // TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap<T, T> and QList<T> | 80 | // TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap<T, T> and QList<T> |
82 | QMap<qint64 /* entity id */, Ptr> mEntities; | 81 | QMap<qint64 /* entity id */, Ptr> mEntities; |
83 | QMap<qint64 /* parent entity id */, QList<qint64> /* child entity id*/> mTree; | 82 | QMap<qint64 /* parent entity id */, QList<qint64> /* child entity id*/> mTree; |
84 | QMap<qint64 /* child entity id */, qint64 /* parent entity id*/> mParents; | 83 | QMap<qint64 /* child entity id */, qint64 /* parent entity id*/> mParents; |
85 | QSet<qint64 /* entity id */> mEntityChildrenFetched; | ||
86 | QSet<qint64 /* entity id */> mEntityChildrenFetchComplete; | ||
87 | QSet<qint64 /* entity id */> mEntityAllChildrenFetched; | ||
88 | QMap<qint64 /* entity id */, int /* Status */> mEntityStatus; | 84 | QMap<qint64 /* entity id */, int /* Status */> mEntityStatus; |
85 | bool mFetchInProgress{false}; | ||
86 | bool mFetchedAll{false}; | ||
87 | bool mFetchComplete{false}; | ||
89 | QList<QByteArray> mPropertyColumns; | 88 | QList<QByteArray> mPropertyColumns; |
90 | Sink::Query mQuery; | 89 | Sink::Query mQuery; |
91 | std::function<void()> loadEntities; | 90 | std::function<void()> loadEntities; |