summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/listmodelresult.cpp21
-rw-r--r--common/listmodelresult.h125
2 files changed, 0 insertions, 146 deletions
diff --git a/common/listmodelresult.cpp b/common/listmodelresult.cpp
deleted file mode 100644
index 6ef1c5f..0000000
--- a/common/listmodelresult.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
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
deleted file mode 100644
index 71a0d09..0000000
--- a/common/listmodelresult.h
+++ /dev/null
@@ -1,125 +0,0 @@
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#include <QDebug>
24
25#include "resultprovider.h"
26
27enum Roles {
28 DomainObjectRole = Qt::UserRole + 1
29};
30
31template<class T>
32class ListModelResult : public QAbstractListModel
33{
34public:
35
36 ListModelResult(const QList<QByteArray> &propertyColumns)
37 :QAbstractListModel(),
38 mPropertyColumns(propertyColumns)
39 {
40 }
41
42 ListModelResult(const QSharedPointer<Sink::ResultEmitter<T> > &emitter, const QList<QByteArray> &propertyColumns)
43 :QAbstractListModel(),
44 mPropertyColumns(propertyColumns)
45 {
46 setEmitter(emitter);
47 }
48
49 void setEmitter(const QSharedPointer<Sink::ResultEmitter<T> > &emitter)
50 {
51 beginResetModel();
52 mEntities.clear();
53 mEmitter = emitter;
54 emitter->onAdded([this](const T &value) {
55 const auto keys = mEntities.keys();
56 int index = 0;
57 for (; index < keys.size(); index++) {
58 if (value->identifier() < keys.at(index)) {
59 break;
60 }
61 }
62 beginInsertRows(QModelIndex(), index, index);
63 mEntities.insert(value->identifier(), value);
64 endInsertRows();
65 });
66 emitter->onModified([this](const T &value) {
67 auto i = mEntities.keys().indexOf(value->identifier());
68 mEntities.remove(value->identifier());
69 mEntities.insert(value->identifier(), value);
70 auto idx = index(i, 0, QModelIndex());
71 emit dataChanged(idx, idx);
72 });
73 emitter->onRemoved([this](const T &value) {
74 auto index = mEntities.keys().indexOf(value->identifier());
75 beginRemoveRows(QModelIndex(), index, index);
76 mEntities.remove(value->identifier());
77 endRemoveRows();
78 });
79 emitter->onInitialResultSetComplete([this]() {
80 });
81 emitter->onComplete([this]() {
82 mEmitter.clear();
83 });
84 emitter->onClear([this]() {
85 beginResetModel();
86 mEntities.clear();
87 endResetModel();
88 });
89 endResetModel();
90 }
91
92 int rowCount(const QModelIndex &parent = QModelIndex()) const
93 {
94 return mEntities.size();
95 }
96
97 int columnCount(const QModelIndex &parent = QModelIndex()) const
98 {
99 return mPropertyColumns.size();
100 }
101
102 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
103 {
104 if (index.row() >= mEntities.size()) {
105 qWarning() << "Out of bounds access";
106 return QVariant();
107 }
108 if (role == Qt::DisplayRole) {
109 if (index.column() < mPropertyColumns.size()) {
110 auto entity = mEntities.value(mEntities.keys().at(index.row()));
111 return entity->getProperty(mPropertyColumns.at(index.column())).toString();
112 }
113 }
114 if (role == DomainObjectRole) {
115 return QVariant::fromValue(mEntities.value(mEntities.keys().at(index.row())));
116 }
117 return QVariant();
118 }
119
120private:
121 QSharedPointer<Sink::ResultEmitter<T> > mEmitter;
122 QMap<QByteArray, T> mEntities;
123 QList<QByteArray> mPropertyColumns;
124};
125