diff options
Diffstat (limited to 'examples/client/main.cpp')
-rw-r--r-- | examples/client/main.cpp | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/examples/client/main.cpp b/examples/client/main.cpp index ead9dd6..e96ddb6 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "common/resource.h" | 27 | #include "common/resource.h" |
28 | #include "common/synclistresult.h" | 28 | #include "common/synclistresult.h" |
29 | #include "common/storage.h" | 29 | #include "common/storage.h" |
30 | #include "common/domain/event.h" | ||
30 | #include "console.h" | 31 | #include "console.h" |
31 | 32 | ||
32 | #include <QWidget> | 33 | #include <QWidget> |
@@ -34,7 +35,13 @@ | |||
34 | #include <QVBoxLayout> | 35 | #include <QVBoxLayout> |
35 | #include <QLabel> | 36 | #include <QLabel> |
36 | #include <QPushButton> | 37 | #include <QPushButton> |
38 | #include <QItemSelectionModel> | ||
37 | 39 | ||
40 | enum Roles { | ||
41 | DomainObjectRole = Qt::UserRole + 1 | ||
42 | }; | ||
43 | |||
44 | template <typename T> | ||
38 | class View : public QWidget | 45 | class View : public QWidget |
39 | { | 46 | { |
40 | public: | 47 | public: |
@@ -60,6 +67,15 @@ public: | |||
60 | Akonadi2::Store::synchronize("org.kde.dummy.instance1"); | 67 | Akonadi2::Store::synchronize("org.kde.dummy.instance1"); |
61 | }); | 68 | }); |
62 | 69 | ||
70 | auto removeButton = new QPushButton(this); | ||
71 | removeButton->setText("Remove"); | ||
72 | QObject::connect(removeButton, &QPushButton::pressed, [listView]() { | ||
73 | for (auto index :listView->selectionModel()->selectedIndexes()) { | ||
74 | auto object = index.data(DomainObjectRole).value<typename T::Ptr>(); | ||
75 | Akonadi2::Store::remove(*object, "org.kde.dummy.instance1").exec(); | ||
76 | } | ||
77 | }); | ||
78 | |||
63 | topLayout->addWidget(titleLabel); | 79 | topLayout->addWidget(titleLabel); |
64 | topLayout->addWidget(syncButton); | 80 | topLayout->addWidget(syncButton); |
65 | topLayout->addWidget(listView, 10); | 81 | topLayout->addWidget(listView, 10); |
@@ -75,15 +91,33 @@ class AkonadiListModel : public QAbstractListModel | |||
75 | public: | 91 | public: |
76 | AkonadiListModel(const QSharedPointer<Akonadi2::ResultEmitter<T> > &emitter, const QByteArray &property) | 92 | AkonadiListModel(const QSharedPointer<Akonadi2::ResultEmitter<T> > &emitter, const QByteArray &property) |
77 | :QAbstractListModel(), | 93 | :QAbstractListModel(), |
78 | mEmitter(emitter) | 94 | mEmitter(emitter), |
95 | mProperty(property) | ||
79 | { | 96 | { |
80 | emitter->onAdded([this, property](const T &value) { | 97 | emitter->onAdded([this, property](const T &value) { |
81 | // qDebug() << "VALUE ADDED"; | 98 | const auto keys = mEntities.keys(); |
82 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr type(value); | 99 | int index = 0; |
83 | beginInsertRows(QModelIndex(), mStringList.size(), mStringList.size()); | 100 | for (; index < keys.size(); index++) { |
84 | mStringList << type->getProperty(property).toString(); | 101 | if (value->identifier() < keys.at(index)) { |
102 | break; | ||
103 | } | ||
104 | } | ||
105 | beginInsertRows(QModelIndex(), index, index); | ||
106 | mEntities.insert(value->identifier(), value); | ||
85 | endInsertRows(); | 107 | endInsertRows(); |
86 | }); | 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 | }); | ||
87 | emitter->onInitialResultSetComplete([this]() { | 121 | emitter->onInitialResultSetComplete([this]() { |
88 | }); | 122 | }); |
89 | emitter->onComplete([this]() { | 123 | emitter->onComplete([this]() { |
@@ -93,31 +127,36 @@ public: | |||
93 | emitter->onClear([this]() { | 127 | emitter->onClear([this]() { |
94 | // qDebug() << "CLEAR"; | 128 | // qDebug() << "CLEAR"; |
95 | beginResetModel(); | 129 | beginResetModel(); |
96 | mStringList.clear(); | 130 | mEntities.clear(); |
97 | endResetModel(); | 131 | endResetModel(); |
98 | }); | 132 | }); |
99 | } | 133 | } |
100 | 134 | ||
101 | int rowCount(const QModelIndex &parent = QModelIndex()) const | 135 | int rowCount(const QModelIndex &parent = QModelIndex()) const |
102 | { | 136 | { |
103 | return mStringList.size(); | 137 | return mEntities.size(); |
104 | } | 138 | } |
105 | 139 | ||
106 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const | 140 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const |
107 | { | 141 | { |
108 | if (index.row() >= mStringList.size()) { | 142 | if (index.row() >= mEntities.size()) { |
109 | qWarning() << "Out of bounds access"; | 143 | qWarning() << "Out of bounds access"; |
110 | return QVariant(); | 144 | return QVariant(); |
111 | } | 145 | } |
112 | if (role == Qt::DisplayRole) { | 146 | if (role == Qt::DisplayRole) { |
113 | return QVariant::fromValue(mStringList.at(index.row())); | 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()))); | ||
114 | } | 152 | } |
115 | return QVariant(); | 153 | return QVariant(); |
116 | } | 154 | } |
117 | 155 | ||
118 | private: | 156 | private: |
119 | QSharedPointer<Akonadi2::ResultEmitter<T> > mEmitter; | 157 | QSharedPointer<Akonadi2::ResultEmitter<T> > mEmitter; |
120 | QStringList mStringList; | 158 | QMap<QByteArray, T> mEntities; |
159 | QByteArray mProperty; | ||
121 | }; | 160 | }; |
122 | 161 | ||
123 | int main(int argc, char *argv[]) | 162 | int main(int argc, char *argv[]) |
@@ -163,7 +202,7 @@ int main(int argc, char *argv[]) | |||
163 | query.liveQuery = true; | 202 | query.liveQuery = true; |
164 | 203 | ||
165 | auto model = QSharedPointer<AkonadiListModel<Akonadi2::ApplicationDomain::Event::Ptr> >::create(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query), "summary"); | 204 | auto model = QSharedPointer<AkonadiListModel<Akonadi2::ApplicationDomain::Event::Ptr> >::create(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query), "summary"); |
166 | auto view = QSharedPointer<View>::create(model.data()); | 205 | auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data()); |
167 | 206 | ||
168 | return app.exec(); | 207 | return app.exec(); |
169 | } | 208 | } |