summaryrefslogtreecommitdiffstats
path: root/common/modelresult.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/modelresult.h')
-rw-r--r--common/modelresult.h78
1 files changed, 78 insertions, 0 deletions
diff --git a/common/modelresult.h b/common/modelresult.h
new file mode 100644
index 0000000..700064b
--- /dev/null
+++ b/common/modelresult.h
@@ -0,0 +1,78 @@
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
23#include <QAbstractItemModel>
24#include <QModelIndex>
25#include <QDebug>
26#include <QSharedPointer>
27#include <functional>
28#include "query.h"
29#include "resultprovider.h"
30
31template<class T, class Ptr>
32class ModelResult : public QAbstractItemModel
33{
34public:
35 enum Roles {
36 DomainObjectRole = Qt::UserRole + 1,
37 ChildrenFetchedRole
38 };
39
40 ModelResult(const Akonadi2::Query &query, const QList<QByteArray> &propertyColumns);
41
42 void setEmitter(const typename Akonadi2::ResultEmitter<Ptr>::Ptr &);
43
44 int rowCount(const QModelIndex &parent = QModelIndex()) const;
45 int columnCount(const QModelIndex &parent = QModelIndex()) const;
46 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
47 QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
48 QModelIndex parent(const QModelIndex &index) const;
49 bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
50
51 bool canFetchMore(const QModelIndex &parent) const;
52 void fetchMore(const QModelIndex &parent);
53
54 void add(const Ptr &value);
55 void modify(const Ptr &value);
56 void remove(const Ptr &value);
57
58 void setFetcher(const std::function<void(const Ptr &parent)> &fetcher);
59
60 bool childrenFetched(const QModelIndex &) const;
61
62private:
63 qint64 parentId(const Ptr &value);
64 QModelIndex createIndexFromId(const qint64 &id) const;
65 void fetchEntities(const QModelIndex &parent);
66
67 //TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap<T, T> and QList<T>
68 QMap<qint64 /* entity id */, Ptr> mEntities;
69 QMap<qint64 /* parent entity id */, QList<qint64> /* child entity id*/> mTree;
70 QMap<qint64 /* child entity id */, qint64 /* parent entity id*/> mParents;
71 QSet<qint64 /* entity id */> mEntityChildrenFetched;
72 QSet<qint64 /* entity id */> mEntityChildrenFetchComplete;
73 QList<QByteArray> mPropertyColumns;
74 Akonadi2::Query mQuery;
75 std::function<void(const Ptr &)> loadEntities;
76 typename Akonadi2::ResultEmitter<Ptr>::Ptr mEmitter;
77};
78