summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-21 09:57:33 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-21 09:57:33 +0200
commit98a057260f51b8af2cf3f933119e08590cc0639b (patch)
tree56a27c8aeafac9a16f97fff86d7b4cca24380ee2 /common
parent830de76043696c591009d763bffcd38d5039b0d2 (diff)
downloadsink-98a057260f51b8af2cf3f933119e08590cc0639b.tar.gz
sink-98a057260f51b8af2cf3f933119e08590cc0639b.zip
Moved the ListModelResult to a separate file
Diffstat (limited to 'common')
-rw-r--r--common/listmodelresult.cpp21
-rw-r--r--common/listmodelresult.h104
2 files changed, 125 insertions, 0 deletions
diff --git a/common/listmodelresult.cpp b/common/listmodelresult.cpp
new file mode 100644
index 0000000..6ef1c5f
--- /dev/null
+++ b/common/listmodelresult.cpp
@@ -0,0 +1,21 @@
1/*
2 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "listmodelresult.h"
diff --git a/common/listmodelresult.h b/common/listmodelresult.h
new file mode 100644
index 0000000..a893fee
--- /dev/null
+++ b/common/listmodelresult.h
@@ -0,0 +1,104 @@
1/*
2 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22#include <QAbstractListModel>
23
24#include "common/resultprovider.h"
25
26enum Roles {
27 DomainObjectRole = Qt::UserRole + 1
28};
29
30template<class T>
31class ListModelResult : public QAbstractListModel
32{
33public:
34
35 ListModelResult(const QSharedPointer<Akonadi2::ResultEmitter<T> > &emitter, const QByteArray &property)
36 :QAbstractListModel(),
37 mEmitter(emitter),
38 mProperty(property)
39 {
40 emitter->onAdded([this, property](const T &value) {
41 const auto keys = mEntities.keys();
42 int index = 0;
43 for (; index < keys.size(); index++) {
44 if (value->identifier() < keys.at(index)) {
45 break;
46 }
47 }
48 beginInsertRows(QModelIndex(), index, index);
49 mEntities.insert(value->identifier(), value);
50 endInsertRows();
51 });
52 emitter->onModified([this, property](const T &value) {
53 mEntities.remove(value->identifier());
54 mEntities.insert(value->identifier(), value);
55 //FIXME
56 // emit dataChanged();
57 });
58 emitter->onRemoved([this, property](const T &value) {
59 auto index = mEntities.keys().indexOf(value->identifier());
60 beginRemoveRows(QModelIndex(), index, index);
61 mEntities.remove(value->identifier());
62 endRemoveRows();
63 });
64 emitter->onInitialResultSetComplete([this]() {
65 });
66 emitter->onComplete([this]() {
67 // qDebug() << "COMPLETE";
68 mEmitter.clear();
69 });
70 emitter->onClear([this]() {
71 // qDebug() << "CLEAR";
72 beginResetModel();
73 mEntities.clear();
74 endResetModel();
75 });
76 }
77
78 int rowCount(const QModelIndex &parent = QModelIndex()) const
79 {
80 return mEntities.size();
81 }
82
83 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
84 {
85 if (index.row() >= mEntities.size()) {
86 qWarning() << "Out of bounds access";
87 return QVariant();
88 }
89 if (role == Qt::DisplayRole) {
90 auto entity = mEntities.value(mEntities.keys().at(index.row()));
91 return entity->getProperty(mProperty).toString() + entity->identifier();
92 }
93 if (role == DomainObjectRole) {
94 return QVariant::fromValue(mEntities.value(mEntities.keys().at(index.row())));
95 }
96 return QVariant();
97 }
98
99private:
100 QSharedPointer<Akonadi2::ResultEmitter<T> > mEmitter;
101 QMap<QByteArray, T> mEntities;
102 QByteArray mProperty;
103};
104