summaryrefslogtreecommitdiffstats
path: root/common/modelresult.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/modelresult.cpp')
-rw-r--r--common/modelresult.cpp31
1 files changed, 28 insertions, 3 deletions
diff --git a/common/modelresult.cpp b/common/modelresult.cpp
index 4a85610..6feca43 100644
--- a/common/modelresult.cpp
+++ b/common/modelresult.cpp
@@ -20,9 +20,11 @@
20#include "modelresult.h" 20#include "modelresult.h"
21 21
22#include <QDebug> 22#include <QDebug>
23#include <QThread>
23 24
24#include "domain/folder.h" 25#include "domain/folder.h"
25#include "log.h" 26#include "log.h"
27#include "threadboundary.h"
26 28
27#undef DEBUG_AREA 29#undef DEBUG_AREA
28#define DEBUG_AREA "client.modelresult" 30#define DEBUG_AREA "client.modelresult"
@@ -63,12 +65,14 @@ qint64 ModelResult<T, Ptr>::parentId(const Ptr &value)
63template <class T, class Ptr> 65template <class T, class Ptr>
64int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const 66int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const
65{ 67{
68 Q_ASSERT(QThread::currentThread() == this->thread());
66 return mTree[getIdentifier(parent)].size(); 69 return mTree[getIdentifier(parent)].size();
67} 70}
68 71
69template <class T, class Ptr> 72template <class T, class Ptr>
70int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const 73int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const
71{ 74{
75 Q_ASSERT(QThread::currentThread() == this->thread());
72 return mPropertyColumns.size(); 76 return mPropertyColumns.size();
73} 77}
74 78
@@ -86,6 +90,7 @@ QVariant ModelResult<T, Ptr>::headerData(int section, Qt::Orientation orientatio
86template <class T, class Ptr> 90template <class T, class Ptr>
87QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const 91QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const
88{ 92{
93 Q_ASSERT(QThread::currentThread() == this->thread());
89 if (role == DomainObjectRole && index.isValid()) { 94 if (role == DomainObjectRole && index.isValid()) {
90 Q_ASSERT(mEntities.contains(index.internalId())); 95 Q_ASSERT(mEntities.contains(index.internalId()));
91 return QVariant::fromValue(mEntities.value(index.internalId())); 96 return QVariant::fromValue(mEntities.value(index.internalId()));
@@ -112,6 +117,7 @@ QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const
112template <class T, class Ptr> 117template <class T, class Ptr>
113QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const 118QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const
114{ 119{
120 Q_ASSERT(QThread::currentThread() == this->thread());
115 const auto id = getIdentifier(parent); 121 const auto id = getIdentifier(parent);
116 const auto list = mTree.value(id); 122 const auto list = mTree.value(id);
117 if (list.size() > row) { 123 if (list.size() > row) {
@@ -119,12 +125,14 @@ QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &p
119 return createIndex(row, column, childId); 125 return createIndex(row, column, childId);
120 } 126 }
121 Warning() << "Index not available " << row << column << parent; 127 Warning() << "Index not available " << row << column << parent;
128 Q_ASSERT(false);
122 return QModelIndex(); 129 return QModelIndex();
123} 130}
124 131
125template <class T, class Ptr> 132template <class T, class Ptr>
126QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const 133QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const
127{ 134{
135 Q_ASSERT(QThread::currentThread() == this->thread());
128 if (id == 0) { 136 if (id == 0) {
129 return QModelIndex(); 137 return QModelIndex();
130 } 138 }
@@ -185,6 +193,7 @@ void ModelResult<T, Ptr>::add(const Ptr &value)
185 } 193 }
186 if (mEntities.contains(childId)) { 194 if (mEntities.contains(childId)) {
187 Warning() << "Entity already in model " << value->identifier(); 195 Warning() << "Entity already in model " << value->identifier();
196 Q_ASSERT(false);
188 return; 197 return;
189 } 198 }
190 // qDebug() << "Inserting rows " << index << parent; 199 // qDebug() << "Inserting rows " << index << parent;
@@ -216,6 +225,7 @@ void ModelResult<T, Ptr>::remove(const Ptr &value)
216template <class T, class Ptr> 225template <class T, class Ptr>
217void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent) 226void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent)
218{ 227{
228 Q_ASSERT(QThread::currentThread() == this->thread());
219 const auto id = getIdentifier(parent); 229 const auto id = getIdentifier(parent);
220 mEntityChildrenFetchComplete.remove(id); 230 mEntityChildrenFetchComplete.remove(id);
221 mEntityChildrenFetched.insert(id); 231 mEntityChildrenFetched.insert(id);
@@ -237,11 +247,26 @@ void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent)
237template <class T, class Ptr> 247template <class T, class Ptr>
238void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter) 248void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter)
239{ 249{
250 static async::ThreadBoundary threadBoundary;
240 setFetcher([this](const Ptr &parent) { mEmitter->fetch(parent); }); 251 setFetcher([this](const Ptr &parent) { mEmitter->fetch(parent); });
241 emitter->onAdded([this](const Ptr &value) { this->add(value); }); 252
242 emitter->onModified([this](const Ptr &value) { this->modify(value); }); 253 emitter->onAdded([this](const Ptr &value) {
243 emitter->onRemoved([this](const Ptr &value) { this->remove(value); }); 254 threadBoundary.callInMainThread([this, value]() {
255 add(value);
256 });
257 });
258 emitter->onModified([this](const Ptr &value) {
259 threadBoundary.callInMainThread([this, value]() {
260 modify(value);
261 });
262 });
263 emitter->onRemoved([this](const Ptr &value) {
264 threadBoundary.callInMainThread([this, value]() {
265 remove(value);
266 });
267 });
244 emitter->onInitialResultSetComplete([this](const Ptr &parent) { 268 emitter->onInitialResultSetComplete([this](const Ptr &parent) {
269 Trace() << "Initial result set complete";
245 const qint64 parentId = parent ? qHash(*parent) : 0; 270 const qint64 parentId = parent ? qHash(*parent) : 0;
246 const auto parentIndex = createIndexFromId(parentId); 271 const auto parentIndex = createIndexFromId(parentId);
247 mEntityChildrenFetchComplete.insert(parentId); 272 mEntityChildrenFetchComplete.insert(parentId);