diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-03-03 09:01:05 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-03-03 09:01:05 +0100 |
commit | 4d9746c828558c9f872e0aed52442863affb25d5 (patch) | |
tree | 507d7c2ba67f47d3cbbcf01a722236ff1b48426b /common/modelresult.cpp | |
parent | 9cea920b7dd51867a0be0fed2f461b6be73c103e (diff) | |
download | sink-4d9746c828558c9f872e0aed52442863affb25d5.tar.gz sink-4d9746c828558c9f872e0aed52442863affb25d5.zip |
Fromatted the whole codebase with clang-format.
clang-format -i */**{.cpp,.h}
Diffstat (limited to 'common/modelresult.cpp')
-rw-r--r-- | common/modelresult.cpp | 66 |
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 | ||
37 | template<class T, class Ptr> | 37 | template <class T, class Ptr> |
38 | ModelResult<T, Ptr>::ModelResult(const Sink::Query &query, const QList<QByteArray> &propertyColumns) | 38 | ModelResult<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 | ||
53 | template<class T, class Ptr> | 51 | template <class T, class Ptr> |
54 | qint64 ModelResult<T, Ptr>::parentId(const Ptr &value) | 52 | qint64 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 | ||
65 | template<class T, class Ptr> | 63 | template <class T, class Ptr> |
66 | int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const | 64 | int 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 | ||
71 | template<class T, class Ptr> | 69 | template <class T, class Ptr> |
72 | int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const | 70 | int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const |
73 | { | 71 | { |
74 | return mPropertyColumns.size(); | 72 | return mPropertyColumns.size(); |
75 | } | 73 | } |
76 | 74 | ||
77 | template<class T, class Ptr> | 75 | template <class T, class Ptr> |
78 | QVariant ModelResult<T, Ptr>::headerData(int section, Qt::Orientation orientation, int role) const | 76 | QVariant 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 | ||
88 | template<class T, class Ptr> | 86 | template <class T, class Ptr> |
89 | QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const | 87 | QVariant 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 | ||
114 | template<class T, class Ptr> | 112 | template <class T, class Ptr> |
115 | QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const | 113 | QModelIndex 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 | ||
127 | template<class T, class Ptr> | 125 | template <class T, class Ptr> |
128 | QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const | 126 | QModelIndex 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 | ||
138 | template<class T, class Ptr> | 136 | template <class T, class Ptr> |
139 | QModelIndex ModelResult<T, Ptr>::parent(const QModelIndex &index) const | 137 | QModelIndex 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 | ||
146 | template<class T, class Ptr> | 144 | template <class T, class Ptr> |
147 | bool ModelResult<T, Ptr>::hasChildren(const QModelIndex &parent) const | 145 | bool 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 | ||
155 | template<class T, class Ptr> | 153 | template <class T, class Ptr> |
156 | bool ModelResult<T, Ptr>::canFetchMore(const QModelIndex &parent) const | 154 | bool 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 | ||
162 | template<class T, class Ptr> | 160 | template <class T, class Ptr> |
163 | void ModelResult<T, Ptr>::fetchMore(const QModelIndex &parent) | 161 | void 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 | ||
169 | template<class T, class Ptr> | 167 | template <class T, class Ptr> |
170 | void ModelResult<T, Ptr>::add(const Ptr &value) | 168 | void 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 | ||
202 | template<class T, class Ptr> | 200 | template <class T, class Ptr> |
203 | void ModelResult<T, Ptr>::remove(const Ptr &value) | 201 | void 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 | ||
218 | template<class T, class Ptr> | 216 | template <class T, class Ptr> |
219 | void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent) | 217 | void 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 | ||
232 | template<class T, class Ptr> | 230 | template <class T, class Ptr> |
233 | void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent)> &fetcher) | 231 | void 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 | ||
239 | template<class T, class Ptr> | 237 | template <class T, class Ptr> |
240 | void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter) | 238 | void 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 | ||
261 | template<class T, class Ptr> | 253 | template <class T, class Ptr> |
262 | bool ModelResult<T, Ptr>::childrenFetched(const QModelIndex &index) const | 254 | bool 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 | ||
267 | template<class T, class Ptr> | 259 | template <class T, class Ptr> |
268 | void ModelResult<T, Ptr>::modify(const Ptr &value) | 260 | void 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 | } |