diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-29 17:08:48 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-29 17:10:15 +0100 |
commit | 0467b39e1ca034ec7298017e3055e352c755a386 (patch) | |
tree | dbd91cd868cf6889ba9c483b7f6de8567f282a2a /framework/mail/retriever.cpp | |
parent | 44f957669c4d7603c8279c41987ee5a74c818bca (diff) | |
download | kube-0467b39e1ca034ec7298017e3055e352c755a386.tar.gz kube-0467b39e1ca034ec7298017e3055e352c755a386.zip |
A helper class to access a value from a model in qml
Diffstat (limited to 'framework/mail/retriever.cpp')
-rw-r--r-- | framework/mail/retriever.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/framework/mail/retriever.cpp b/framework/mail/retriever.cpp new file mode 100644 index 00000000..b8e29523 --- /dev/null +++ b/framework/mail/retriever.cpp | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsystems.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #include "retriever.h" | ||
22 | |||
23 | Retriever::Retriever(QObject *parent) : QObject(parent) | ||
24 | { | ||
25 | } | ||
26 | |||
27 | QAbstractItemModel* Retriever::model() const | ||
28 | { | ||
29 | return mModel; | ||
30 | } | ||
31 | |||
32 | void Retriever::setModel(QAbstractItemModel* model) | ||
33 | { | ||
34 | mValue = QVariant(); | ||
35 | mModel = model; | ||
36 | connect(model, &QAbstractItemModel::rowsInserted, this, &Retriever::onRowsInserted); | ||
37 | connect(model, &QAbstractItemModel::modelReset, this, &Retriever::onModelReset); | ||
38 | if (model->rowCount(QModelIndex())) { | ||
39 | mValue = model->index(0, 0, QModelIndex()).data(mModel->roleNames().key(mPropertyName.toLatin1())); | ||
40 | emit valueChanged(); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | void Retriever::onRowsInserted(const QModelIndex &parent, int first, int last) | ||
45 | { | ||
46 | if (!mValue.isValid()) { | ||
47 | mValue = mModel->index(0, 0, QModelIndex()).data(mModel->roleNames().key(mPropertyName.toLatin1())); | ||
48 | emit valueChanged(); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | void Retriever::onModelReset() | ||
53 | { | ||
54 | mValue = QVariant(); | ||
55 | } | ||