From fb09018d49e182012422d721fb0fbf95130b82c5 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 3 Jan 2018 17:58:47 +0100 Subject: We need all parents available, not only one --- common/modelresult.cpp | 24 +++++++++++++++++++----- common/modelresult.h | 1 + tests/clientapitest.cpp | 8 ++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/common/modelresult.cpp b/common/modelresult.cpp index 9685e3c..495eca0 100644 --- a/common/modelresult.cpp +++ b/common/modelresult.cpp @@ -236,6 +236,7 @@ QModelIndex ModelResult::createIndexFromId(const qint64 &id) const } auto grandParentId = mParents.value(id, 0); auto row = mTree.value(grandParentId).indexOf(id); + Q_ASSERT(row >= 0); return createIndex(row, 0, id); } @@ -290,6 +291,19 @@ void ModelResult::fetchMore(const QModelIndex &parent) } } +template +bool ModelResult::allParentsAvailable(qint64 id) const +{ + auto p = id; + while (p) { + if (!mEntities.contains(p)) { + return false; + } + p = mParents.value(p, 0); + } + return true; +} + template void ModelResult::add(const Ptr &value) { @@ -306,16 +320,16 @@ void ModelResult::add(const Ptr &value) break; } } - bool parentIsAvailable = mEntities.contains(pId) || (pId == 0); - SinkTraceCtx(mLogCtx) << "Added entity " << childId << "id: " << value->identifier() << "parent: " << pId; + bool parentIsVisible = allParentsAvailable(pId); // SinkTraceCtx(mLogCtx) << "Inserting rows " << index << parent; - if (parentIsAvailable) { - beginInsertRows(createIndexFromId(pId), idx, idx); + if (parentIsVisible) { + auto parent = createIndexFromId(pId); + beginInsertRows(parent, idx, idx); } mEntities.insert(childId, value); mTree[pId].insert(idx, childId); mParents.insert(childId, pId); - if (parentIsAvailable) { + if (parentIsVisible) { endInsertRows(); } // SinkTraceCtx(mLogCtx) << "Inserted rows " << mTree[id].size(); diff --git a/common/modelresult.h b/common/modelresult.h index 55a95b9..321f9f1 100644 --- a/common/modelresult.h +++ b/common/modelresult.h @@ -75,6 +75,7 @@ private: qint64 parentId(const Ptr &value); QModelIndex createIndexFromId(const qint64 &id) const; + bool allParentsAvailable(qint64 id) const; Sink::Log::Context mLogCtx; // TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap and QList diff --git a/tests/clientapitest.cpp b/tests/clientapitest.cpp index 6e8403f..3582a3c 100644 --- a/tests/clientapitest.cpp +++ b/tests/clientapitest.cpp @@ -227,8 +227,12 @@ private slots: facade->runAsync = true; auto folder = QSharedPointer::create("resource", "id", 0, QSharedPointer::create()); auto subfolder = QSharedPointer::create("resource", "subId", 0, QSharedPointer::create()); - subfolder->setParent("id"); - facade->results << subfolder << folder; + subfolder->setParent(folder->identifier()); + + auto subsubfolder = QSharedPointer::create("resource", "subsubId", 0, QSharedPointer::create()); + subsubfolder->setParent(subfolder->identifier()); + + facade->results << subsubfolder << subfolder << folder; // Test Sink::Query query; -- cgit v1.2.3