summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/modelresult.cpp24
-rw-r--r--common/modelresult.h1
-rw-r--r--tests/clientapitest.cpp8
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<T, Ptr>::createIndexFromId(const qint64 &id) const
236 } 236 }
237 auto grandParentId = mParents.value(id, 0); 237 auto grandParentId = mParents.value(id, 0);
238 auto row = mTree.value(grandParentId).indexOf(id); 238 auto row = mTree.value(grandParentId).indexOf(id);
239 Q_ASSERT(row >= 0);
239 return createIndex(row, 0, id); 240 return createIndex(row, 0, id);
240} 241}
241 242
@@ -291,6 +292,19 @@ void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent)
291} 292}
292 293
293template <class T, class Ptr> 294template <class T, class Ptr>
295bool ModelResult<T, Ptr>::allParentsAvailable(qint64 id) const
296{
297 auto p = id;
298 while (p) {
299 if (!mEntities.contains(p)) {
300 return false;
301 }
302 p = mParents.value(p, 0);
303 }
304 return true;
305}
306
307template <class T, class Ptr>
294void ModelResult<T, Ptr>::add(const Ptr &value) 308void ModelResult<T, Ptr>::add(const Ptr &value)
295{ 309{
296 const auto childId = qHash(*value); 310 const auto childId = qHash(*value);
@@ -306,16 +320,16 @@ void ModelResult<T, Ptr>::add(const Ptr &value)
306 break; 320 break;
307 } 321 }
308 } 322 }
309 bool parentIsAvailable = mEntities.contains(pId) || (pId == 0); 323 bool parentIsVisible = allParentsAvailable(pId);
310 SinkTraceCtx(mLogCtx) << "Added entity " << childId << "id: " << value->identifier() << "parent: " << pId;
311 // SinkTraceCtx(mLogCtx) << "Inserting rows " << index << parent; 324 // SinkTraceCtx(mLogCtx) << "Inserting rows " << index << parent;
312 if (parentIsAvailable) { 325 if (parentIsVisible) {
313 beginInsertRows(createIndexFromId(pId), idx, idx); 326 auto parent = createIndexFromId(pId);
327 beginInsertRows(parent, idx, idx);
314 } 328 }
315 mEntities.insert(childId, value); 329 mEntities.insert(childId, value);
316 mTree[pId].insert(idx, childId); 330 mTree[pId].insert(idx, childId);
317 mParents.insert(childId, pId); 331 mParents.insert(childId, pId);
318 if (parentIsAvailable) { 332 if (parentIsVisible) {
319 endInsertRows(); 333 endInsertRows();
320 } 334 }
321 // SinkTraceCtx(mLogCtx) << "Inserted rows " << mTree[id].size(); 335 // 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:
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 bool allParentsAvailable(qint64 id) const;
78 79
79 Sink::Log::Context mLogCtx; 80 Sink::Log::Context mLogCtx;
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> 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>
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:
227 facade->runAsync = true; 227 facade->runAsync = true;
228 auto folder = QSharedPointer<Sink::ApplicationDomain::Folder>::create("resource", "id", 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); 228 auto folder = QSharedPointer<Sink::ApplicationDomain::Folder>::create("resource", "id", 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
229 auto subfolder = QSharedPointer<Sink::ApplicationDomain::Folder>::create("resource", "subId", 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); 229 auto subfolder = QSharedPointer<Sink::ApplicationDomain::Folder>::create("resource", "subId", 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
230 subfolder->setParent("id"); 230 subfolder->setParent(folder->identifier());
231 facade->results << subfolder << folder; 231
232 auto subsubfolder = QSharedPointer<Sink::ApplicationDomain::Folder>::create("resource", "subsubId", 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
233 subsubfolder->setParent(subfolder->identifier());
234
235 facade->results << subsubfolder << subfolder << folder;
232 236
233 // Test 237 // Test
234 Sink::Query query; 238 Sink::Query query;