summaryrefslogtreecommitdiffstats
path: root/common/modelresult.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/modelresult.cpp')
-rw-r--r--common/modelresult.cpp66
1 files changed, 29 insertions, 37 deletions
diff --git a/common/modelresult.cpp b/common/modelresult.cpp
index ceefa76..1c06a7d 100644
--- a/common/modelresult.cpp
+++ b/common/modelresult.cpp
@@ -34,11 +34,9 @@ static uint qHash(const Sink::ApplicationDomain::ApplicationDomainType &type)
34 return qHash(type.resourceInstanceIdentifier() + type.identifier()); 34 return qHash(type.resourceInstanceIdentifier() + type.identifier());
35} 35}
36 36
37template<class T, class Ptr> 37template <class T, class Ptr>
38ModelResult<T, Ptr>::ModelResult(const Sink::Query &query, const QList<QByteArray> &propertyColumns) 38ModelResult<T, Ptr>::ModelResult(const Sink::Query &query, const QList<QByteArray> &propertyColumns)
39 :QAbstractItemModel(), 39 : QAbstractItemModel(), mPropertyColumns(propertyColumns), mQuery(query)
40 mPropertyColumns(propertyColumns),
41 mQuery(query)
42{ 40{
43} 41}
44 42
@@ -50,7 +48,7 @@ static qint64 getIdentifier(const QModelIndex &idx)
50 return idx.internalId(); 48 return idx.internalId();
51} 49}
52 50
53template<class T, class Ptr> 51template <class T, class Ptr>
54qint64 ModelResult<T, Ptr>::parentId(const Ptr &value) 52qint64 ModelResult<T, Ptr>::parentId(const Ptr &value)
55{ 53{
56 if (!mQuery.parentProperty.isEmpty()) { 54 if (!mQuery.parentProperty.isEmpty()) {
@@ -62,19 +60,19 @@ qint64 ModelResult<T, Ptr>::parentId(const Ptr &value)
62 return 0; 60 return 0;
63} 61}
64 62
65template<class T, class Ptr> 63template <class T, class Ptr>
66int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const 64int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const
67{ 65{
68 return mTree[getIdentifier(parent)].size(); 66 return mTree[getIdentifier(parent)].size();
69} 67}
70 68
71template<class T, class Ptr> 69template <class T, class Ptr>
72int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const 70int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const
73{ 71{
74 return mPropertyColumns.size(); 72 return mPropertyColumns.size();
75} 73}
76 74
77template<class T, class Ptr> 75template <class T, class Ptr>
78QVariant ModelResult<T, Ptr>::headerData(int section, Qt::Orientation orientation, int role) const 76QVariant ModelResult<T, Ptr>::headerData(int section, Qt::Orientation orientation, int role) const
79{ 77{
80 if (role == Qt::DisplayRole) { 78 if (role == Qt::DisplayRole) {
@@ -85,7 +83,7 @@ QVariant ModelResult<T, Ptr>::headerData(int section, Qt::Orientation orientatio
85 return QVariant(); 83 return QVariant();
86} 84}
87 85
88template<class T, class Ptr> 86template <class T, class Ptr>
89QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const 87QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const
90{ 88{
91 if (role == DomainObjectRole && index.isValid()) { 89 if (role == DomainObjectRole && index.isValid()) {
@@ -94,7 +92,7 @@ QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const
94 } 92 }
95 if (role == DomainObjectBaseRole && index.isValid()) { 93 if (role == DomainObjectBaseRole && index.isValid()) {
96 Q_ASSERT(mEntities.contains(index.internalId())); 94 Q_ASSERT(mEntities.contains(index.internalId()));
97 return QVariant::fromValue(mEntities.value(index.internalId()). template staticCast<Sink::ApplicationDomain::ApplicationDomainType>()); 95 return QVariant::fromValue(mEntities.value(index.internalId()).template staticCast<Sink::ApplicationDomain::ApplicationDomainType>());
98 } 96 }
99 if (role == ChildrenFetchedRole) { 97 if (role == ChildrenFetchedRole) {
100 return childrenFetched(index); 98 return childrenFetched(index);
@@ -111,7 +109,7 @@ QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const
111 return QVariant(); 109 return QVariant();
112} 110}
113 111
114template<class T, class Ptr> 112template <class T, class Ptr>
115QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const 113QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const
116{ 114{
117 const auto id = getIdentifier(parent); 115 const auto id = getIdentifier(parent);
@@ -124,7 +122,7 @@ QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &p
124 return QModelIndex(); 122 return QModelIndex();
125} 123}
126 124
127template<class T, class Ptr> 125template <class T, class Ptr>
128QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const 126QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const
129{ 127{
130 if (id == 0) { 128 if (id == 0) {
@@ -135,7 +133,7 @@ QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const
135 return createIndex(row, 0, id); 133 return createIndex(row, 0, id);
136} 134}
137 135
138template<class T, class Ptr> 136template <class T, class Ptr>
139QModelIndex ModelResult<T, Ptr>::parent(const QModelIndex &index) const 137QModelIndex ModelResult<T, Ptr>::parent(const QModelIndex &index) const
140{ 138{
141 auto id = getIdentifier(index); 139 auto id = getIdentifier(index);
@@ -143,7 +141,7 @@ QModelIndex ModelResult<T, Ptr>::parent(const QModelIndex &index) const
143 return createIndexFromId(parentId); 141 return createIndexFromId(parentId);
144} 142}
145 143
146template<class T, class Ptr> 144template <class T, class Ptr>
147bool ModelResult<T, Ptr>::hasChildren(const QModelIndex &parent) const 145bool ModelResult<T, Ptr>::hasChildren(const QModelIndex &parent) const
148{ 146{
149 if (mQuery.parentProperty.isEmpty() && parent.isValid()) { 147 if (mQuery.parentProperty.isEmpty() && parent.isValid()) {
@@ -152,26 +150,26 @@ bool ModelResult<T, Ptr>::hasChildren(const QModelIndex &parent) const
152 return QAbstractItemModel::hasChildren(parent); 150 return QAbstractItemModel::hasChildren(parent);
153} 151}
154 152
155template<class T, class Ptr> 153template <class T, class Ptr>
156bool ModelResult<T, Ptr>::canFetchMore(const QModelIndex &parent) const 154bool ModelResult<T, Ptr>::canFetchMore(const QModelIndex &parent) const
157{ 155{
158 const auto id = parent.internalId(); 156 const auto id = parent.internalId();
159 return !mEntityChildrenFetched.contains(id) || mEntityChildrenFetchComplete.contains(id); 157 return !mEntityChildrenFetched.contains(id) || mEntityChildrenFetchComplete.contains(id);
160} 158}
161 159
162template<class T, class Ptr> 160template <class T, class Ptr>
163void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent) 161void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent)
164{ 162{
165 Trace() << "Fetching more: " << parent; 163 Trace() << "Fetching more: " << parent;
166 fetchEntities(parent); 164 fetchEntities(parent);
167} 165}
168 166
169template<class T, class Ptr> 167template <class T, class Ptr>
170void ModelResult<T, Ptr>::add(const Ptr &value) 168void ModelResult<T, Ptr>::add(const Ptr &value)
171{ 169{
172 const auto childId = qHash(*value); 170 const auto childId = qHash(*value);
173 const auto id = parentId(value); 171 const auto id = parentId(value);
174 //Ignore updates we get before the initial fetch is done 172 // Ignore updates we get before the initial fetch is done
175 if (!mEntityChildrenFetched.contains(id)) { 173 if (!mEntityChildrenFetched.contains(id)) {
176 Trace() << "Too early" << id; 174 Trace() << "Too early" << id;
177 return; 175 return;
@@ -199,7 +197,7 @@ void ModelResult<T, Ptr>::add(const Ptr &value)
199} 197}
200 198
201 199
202template<class T, class Ptr> 200template <class T, class Ptr>
203void ModelResult<T, Ptr>::remove(const Ptr &value) 201void ModelResult<T, Ptr>::remove(const Ptr &value)
204{ 202{
205 auto childId = qHash(*value); 203 auto childId = qHash(*value);
@@ -211,11 +209,11 @@ void ModelResult<T, Ptr>::remove(const Ptr &value)
211 mEntities.remove(childId); 209 mEntities.remove(childId);
212 mTree[id].removeAll(childId); 210 mTree[id].removeAll(childId);
213 mParents.remove(childId); 211 mParents.remove(childId);
214 //TODO remove children 212 // TODO remove children
215 endRemoveRows(); 213 endRemoveRows();
216} 214}
217 215
218template<class T, class Ptr> 216template <class T, class Ptr>
219void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent) 217void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent)
220{ 218{
221 const auto id = getIdentifier(parent); 219 const auto id = getIdentifier(parent);
@@ -229,26 +227,20 @@ void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent)
229 } 227 }
230} 228}
231 229
232template<class T, class Ptr> 230template <class T, class Ptr>
233void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent)> &fetcher) 231void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent)> &fetcher)
234{ 232{
235 Trace() << "Setting fetcher"; 233 Trace() << "Setting fetcher";
236 loadEntities = fetcher; 234 loadEntities = fetcher;
237} 235}
238 236
239template<class T, class Ptr> 237template <class T, class Ptr>
240void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter) 238void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter)
241{ 239{
242 setFetcher([this](const Ptr &parent) {mEmitter->fetch(parent);}); 240 setFetcher([this](const Ptr &parent) { mEmitter->fetch(parent); });
243 emitter->onAdded([this](const Ptr &value) { 241 emitter->onAdded([this](const Ptr &value) { this->add(value); });
244 this->add(value); 242 emitter->onModified([this](const Ptr &value) { this->modify(value); });
245 }); 243 emitter->onRemoved([this](const Ptr &value) { this->remove(value); });
246 emitter->onModified([this](const Ptr &value) {
247 this->modify(value);
248 });
249 emitter->onRemoved([this](const Ptr &value) {
250 this->remove(value);
251 });
252 emitter->onInitialResultSetComplete([this](const Ptr &parent) { 244 emitter->onInitialResultSetComplete([this](const Ptr &parent) {
253 const qint64 parentId = parent ? qHash(*parent) : 0; 245 const qint64 parentId = parent ? qHash(*parent) : 0;
254 const auto parentIndex = createIndexFromId(parentId); 246 const auto parentIndex = createIndexFromId(parentId);
@@ -258,18 +250,18 @@ void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Pt
258 mEmitter = emitter; 250 mEmitter = emitter;
259} 251}
260 252
261template<class T, class Ptr> 253template <class T, class Ptr>
262bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const 254bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const
263{ 255{
264 return mEntityChildrenFetchComplete.contains(getIdentifier(index)); 256 return mEntityChildrenFetchComplete.contains(getIdentifier(index));
265} 257}
266 258
267template<class T, class Ptr> 259template <class T, class Ptr>
268void ModelResult<T, Ptr>::modify(const Ptr &value) 260void ModelResult<T, Ptr>::modify(const Ptr &value)
269{ 261{
270 auto childId = qHash(*value); 262 auto childId = qHash(*value);
271 auto id = parentId(value); 263 auto id = parentId(value);
272 //Ignore updates we get before the initial fetch is done 264 // Ignore updates we get before the initial fetch is done
273 if (!mEntityChildrenFetched.contains(id)) { 265 if (!mEntityChildrenFetched.contains(id)) {
274 return; 266 return;
275 } 267 }
@@ -278,7 +270,7 @@ void ModelResult<T, Ptr>::modify(const Ptr &value)
278 auto i = mTree[id].indexOf(childId); 270 auto i = mTree[id].indexOf(childId);
279 mEntities.remove(childId); 271 mEntities.remove(childId);
280 mEntities.insert(childId, value); 272 mEntities.insert(childId, value);
281 //TODO check for change of parents 273 // TODO check for change of parents
282 auto idx = index(i, 0, parent); 274 auto idx = index(i, 0, parent);
283 emit dataChanged(idx, idx); 275 emit dataChanged(idx, idx);
284} 276}