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 + 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'common') 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 -- cgit v1.2.3