summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/listmodelresult.cpp21
-rw-r--r--common/listmodelresult.h104
-rw-r--r--examples/client/main.cpp84
3 files changed, 127 insertions, 82 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
diff --git a/examples/client/main.cpp b/examples/client/main.cpp
index e96ddb6..269f1aa 100644
--- a/examples/client/main.cpp
+++ b/examples/client/main.cpp
@@ -20,12 +20,10 @@
20#include <QApplication> 20#include <QApplication>
21#include <QCommandLineParser> 21#include <QCommandLineParser>
22#include <QCommandLineOption> 22#include <QCommandLineOption>
23#include <QAbstractListModel>
24 23
25#include "common/clientapi.h" 24#include "common/clientapi.h"
26#include "common/resultprovider.h"
27#include "common/resource.h" 25#include "common/resource.h"
28#include "common/synclistresult.h" 26#include "common/listmodelresult.h"
29#include "common/storage.h" 27#include "common/storage.h"
30#include "common/domain/event.h" 28#include "common/domain/event.h"
31#include "console.h" 29#include "console.h"
@@ -37,10 +35,6 @@
37#include <QPushButton> 35#include <QPushButton>
38#include <QItemSelectionModel> 36#include <QItemSelectionModel>
39 37
40enum Roles {
41 DomainObjectRole = Qt::UserRole + 1
42};
43
44template <typename T> 38template <typename T>
45class View : public QWidget 39class View : public QWidget
46{ 40{
@@ -85,80 +79,6 @@ public:
85 79
86}; 80};
87 81
88template<class T>
89class AkonadiListModel : public QAbstractListModel
90{
91public:
92 AkonadiListModel(const QSharedPointer<Akonadi2::ResultEmitter<T> > &emitter, const QByteArray &property)
93 :QAbstractListModel(),
94 mEmitter(emitter),
95 mProperty(property)
96 {
97 emitter->onAdded([this, property](const T &value) {
98 const auto keys = mEntities.keys();
99 int index = 0;
100 for (; index < keys.size(); index++) {
101 if (value->identifier() < keys.at(index)) {
102 break;
103 }
104 }
105 beginInsertRows(QModelIndex(), index, index);
106 mEntities.insert(value->identifier(), value);
107 endInsertRows();
108 });
109 emitter->onModified([this, property](const T &value) {
110 mEntities.remove(value->identifier());
111 mEntities.insert(value->identifier(), value);
112 //FIXME
113 // emit dataChanged();
114 });
115 emitter->onRemoved([this, property](const T &value) {
116 auto index = mEntities.keys().indexOf(value->identifier());
117 beginRemoveRows(QModelIndex(), index, index);
118 mEntities.remove(value->identifier());
119 endRemoveRows();
120 });
121 emitter->onInitialResultSetComplete([this]() {
122 });
123 emitter->onComplete([this]() {
124 // qDebug() << "COMPLETE";
125 mEmitter.clear();
126 });
127 emitter->onClear([this]() {
128 // qDebug() << "CLEAR";
129 beginResetModel();
130 mEntities.clear();
131 endResetModel();
132 });
133 }
134
135 int rowCount(const QModelIndex &parent = QModelIndex()) const
136 {
137 return mEntities.size();
138 }
139
140 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
141 {
142 if (index.row() >= mEntities.size()) {
143 qWarning() << "Out of bounds access";
144 return QVariant();
145 }
146 if (role == Qt::DisplayRole) {
147 auto entity = mEntities.value(mEntities.keys().at(index.row()));
148 return entity->getProperty(mProperty).toString() + entity->identifier();
149 }
150 if (role == DomainObjectRole) {
151 return QVariant::fromValue(mEntities.value(mEntities.keys().at(index.row())));
152 }
153 return QVariant();
154 }
155
156private:
157 QSharedPointer<Akonadi2::ResultEmitter<T> > mEmitter;
158 QMap<QByteArray, T> mEntities;
159 QByteArray mProperty;
160};
161
162int main(int argc, char *argv[]) 82int main(int argc, char *argv[])
163{ 83{
164 QApplication app(argc, argv); 84 QApplication app(argc, argv);
@@ -201,7 +121,7 @@ int main(int argc, char *argv[])
201 query.processAll = false; 121 query.processAll = false;
202 query.liveQuery = true; 122 query.liveQuery = true;
203 123
204 auto model = QSharedPointer<AkonadiListModel<Akonadi2::ApplicationDomain::Event::Ptr> >::create(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query), "summary"); 124 auto model = QSharedPointer<ListModelResult<Akonadi2::ApplicationDomain::Event::Ptr> >::create(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query), "summary");
205 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data()); 125 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data());
206 126
207 return app.exec(); 127 return app.exec();