summaryrefslogtreecommitdiffstats
path: root/common/modelresult.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-12-28 12:05:34 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-02 13:31:14 +0100
commit625190d311adfcf3f0436cfece82249a92489348 (patch)
tree1010950e14b57560ae90abe20a5657750ad27137 /common/modelresult.cpp
parent11b790ba6f06141db802273628ce2d191982677e (diff)
downloadsink-625190d311adfcf3f0436cfece82249a92489348.tar.gz
sink-625190d311adfcf3f0436cfece82249a92489348.zip
No parent query
Diffstat (limited to 'common/modelresult.cpp')
-rw-r--r--common/modelresult.cpp59
1 files changed, 31 insertions, 28 deletions
diff --git a/common/modelresult.cpp b/common/modelresult.cpp
index 4295a44..e11ec9c 100644
--- a/common/modelresult.cpp
+++ b/common/modelresult.cpp
@@ -253,14 +253,14 @@ bool ModelResult<T, Ptr>::hasChildren(const QModelIndex &parent) const
253 if (mQuery.parentProperty().isEmpty() && parent.isValid()) { 253 if (mQuery.parentProperty().isEmpty() && parent.isValid()) {
254 return false; 254 return false;
255 } 255 }
256 //Figure out whether we have children 256 // //Figure out whether we have children
257 const auto id = parent.internalId(); 257 // const auto id = parent.internalId();
258 if (!mEntityChildrenFetched.contains(id)) { 258 // if (!mEntityChildrenFetched.contains(id)) {
259 //Since we don't retrieve that information as part of the entity, 259 // //Since we don't retrieve that information as part of the entity,
260 //we have to query for the children to see if we have some 260 // //we have to query for the children to see if we have some
261 auto p = const_cast<ModelResult<T, Ptr>*>(this); 261 // auto p = const_cast<ModelResult<T, Ptr>*>(this);
262 p->fetchMore(parent); 262 // p->fetchMore(parent);
263 } 263 // }
264 return QAbstractItemModel::hasChildren(parent); 264 return QAbstractItemModel::hasChildren(parent);
265} 265}
266 266
@@ -278,7 +278,9 @@ template <class T, class Ptr>
278void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent) 278void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent)
279{ 279{
280 SinkTraceCtx(mLogCtx) << "Fetching more: " << parent; 280 SinkTraceCtx(mLogCtx) << "Fetching more: " << parent;
281 fetchEntities(parent); 281 if (!parent.isValid()) {
282 fetchEntities();
283 }
282} 284}
283 285
284template <class T, class Ptr> 286template <class T, class Ptr>
@@ -287,10 +289,10 @@ void ModelResult<T, Ptr>::add(const Ptr &value)
287 const auto childId = qHash(*value); 289 const auto childId = qHash(*value);
288 const auto id = parentId(value); 290 const auto id = parentId(value);
289 // Ignore updates we get before the initial fetch is done 291 // Ignore updates we get before the initial fetch is done
290 if (!mEntityChildrenFetched.contains(id)) { 292 // if (!mEntityChildrenFetched.contains(id)) {
291 SinkTraceCtx(mLogCtx) << "Too early" << id; 293 // SinkTraceCtx(mLogCtx) << "Too early" << id;
292 return; 294 // return;
293 } 295 // }
294 if (mEntities.contains(childId)) { 296 if (mEntities.contains(childId)) {
295 SinkWarningCtx(mLogCtx) << "Entity already in model: " << value->identifier(); 297 SinkWarningCtx(mLogCtx) << "Entity already in model: " << value->identifier();
296 return; 298 return;
@@ -335,27 +337,27 @@ void ModelResult<T, Ptr>::remove(const Ptr &value)
335} 337}
336 338
337template <class T, class Ptr> 339template <class T, class Ptr>
338void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent) 340void ModelResult<T, Ptr>::fetchEntities()
339{ 341{
340 Q_ASSERT(QThread::currentThread() == this->thread()); 342 Q_ASSERT(QThread::currentThread() == this->thread());
341 const auto id = getIdentifier(parent); 343 const auto id = getIdentifier({});
342 //There is already a fetch in progress, don't fetch again. 344 //There is already a fetch in progress, don't fetch again.
343 if (mEntityChildrenFetched.contains(id) && !mEntityChildrenFetchComplete.contains(id)) { 345 if (mEntityChildrenFetched.contains(id) && !mEntityChildrenFetchComplete.contains(id)) {
344 SinkTraceCtx(mLogCtx) << "A fetch is already in progress: " << parent; 346 SinkTraceCtx(mLogCtx) << "A fetch is already in progress.";
345 return; 347 return;
346 } 348 }
347 mEntityChildrenFetchComplete.remove(id); 349 mEntityChildrenFetchComplete.remove(id);
348 mEntityChildrenFetched.insert(id); 350 mEntityChildrenFetched.insert(id);
349 SinkTraceCtx(mLogCtx) << "Loading child entities of parent " << id; 351 SinkTraceCtx(mLogCtx) << "Loading child entities of parent " << id;
350 if (loadEntities) { 352 if (loadEntities) {
351 loadEntities(parent.data(DomainObjectRole).template value<Ptr>()); 353 loadEntities();
352 } else { 354 } else {
353 SinkWarningCtx(mLogCtx) << "No way to fetch entities"; 355 SinkWarningCtx(mLogCtx) << "No way to fetch entities";
354 } 356 }
355} 357}
356 358
357template <class T, class Ptr> 359template <class T, class Ptr>
358void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent)> &fetcher) 360void ModelResult<T, Ptr>::setFetcher(const std::function<void()> &fetcher)
359{ 361{
360 SinkTraceCtx(mLogCtx) << "Setting fetcher"; 362 SinkTraceCtx(mLogCtx) << "Setting fetcher";
361 loadEntities = fetcher; 363 loadEntities = fetcher;
@@ -364,7 +366,7 @@ void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent)
364template <class T, class Ptr> 366template <class T, class Ptr>
365void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter) 367void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter)
366{ 368{
367 setFetcher([this](const Ptr &parent) { mEmitter->fetch(parent); }); 369 setFetcher([this]() { mEmitter->fetch(); });
368 370
369 QPointer<QObject> guard(this); 371 QPointer<QObject> guard(this);
370 emitter->onAdded([this, guard](const Ptr &value) { 372 emitter->onAdded([this, guard](const Ptr &value) {
@@ -389,18 +391,19 @@ void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Pt
389 remove(value); 391 remove(value);
390 }); 392 });
391 }); 393 });
392 emitter->onInitialResultSetComplete([this, guard](const Ptr &parent, bool fetchedAll) { 394 emitter->onInitialResultSetComplete([this, guard](bool fetchedAll) {
393 SinkTraceCtx(mLogCtx) << "Initial result set complete. Fetched all: " << fetchedAll; 395 SinkTraceCtx(mLogCtx) << "Initial result set complete. Fetched all: " << fetchedAll;
394 Q_ASSERT(guard); 396 Q_ASSERT(guard);
395 Q_ASSERT(QThread::currentThread() == this->thread()); 397 Q_ASSERT(QThread::currentThread() == this->thread());
396 398
397 const qint64 parentId = parent ? qHash(*parent) : 0; 399 // const qint64 parentId = parent ? qHash(*parent) : 0;
398 const auto parentIndex = createIndexFromId(parentId); 400 // const auto parentIndex = createIndexFromId(parentId);
399 mEntityChildrenFetchComplete.insert(parentId); 401 mEntityChildrenFetchComplete.insert(0);
400 if (fetchedAll) { 402 if (fetchedAll) {
401 mEntityAllChildrenFetched.insert(parentId); 403 mEntityAllChildrenFetched.insert(0);
402 } 404 }
403 emit dataChanged(parentIndex, parentIndex, QVector<int>() << ChildrenFetchedRole); 405 // emit dataChanged(parentIndex, parentIndex, QVector<int>() << ChildrenFetchedRole);
406 emit dataChanged({}, {}, QVector<int>() << ChildrenFetchedRole);
404 }); 407 });
405 mEmitter = emitter; 408 mEmitter = emitter;
406} 409}
@@ -423,9 +426,9 @@ void ModelResult<T, Ptr>::modify(const Ptr &value)
423 } 426 }
424 auto id = parentId(value); 427 auto id = parentId(value);
425 // Ignore updates we get before the initial fetch is done 428 // Ignore updates we get before the initial fetch is done
426 if (!mEntityChildrenFetched.contains(id)) { 429 // if (!mEntityChildrenFetched.contains(id)) {
427 return; 430 // return;
428 } 431 // }
429 auto parent = createIndexFromId(id); 432 auto parent = createIndexFromId(id);
430 SinkTraceCtx(mLogCtx) << "Modified entity:" << value->identifier() << ", id: " << childId; 433 SinkTraceCtx(mLogCtx) << "Modified entity:" << value->identifier() << ", id: " << childId;
431 auto i = mTree[id].indexOf(childId); 434 auto i = mTree[id].indexOf(childId);