groupeditproxymodel.cpp 3.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Copyright 2008  Rolf Eike Beer <kde@opensource.sf-tec.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
19
20
21
22
23
24
25
26
27
28
#include "groupeditproxymodel.h"
#include "kgpgitemnode.h"
#include "kgpgitemmodel.h"
#include "core/kgpgkey.h"

#include <KDebug>

using namespace KgpgCore;

GroupEditProxyModel::GroupEditProxyModel(QObject *parent, const bool &invert, QList<KGpgNode *> *ids)
Rolf Eike Beer's avatar
Rolf Eike Beer committed
29
	: QSortFilterProxyModel(parent), m_model(NULL), m_invert(invert), m_ids(ids)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{
}

void
GroupEditProxyModel::setKeyModel(KGpgItemModel *md)
{
	m_model = md;
	setSourceModel(md);
}

bool
GroupEditProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
	QModelIndex idx = m_model->index(source_row, 0, source_parent);
	KGpgNode *l = m_model->nodeForIndex(idx);

	if (l->getType() & ~ITYPE_GPAIR)
		return false;

	if ((l->getTrust() != TRUST_FULL) && (l->getTrust() != TRUST_ULTIMATE))
		return false;

	for (int i = 0; i < m_ids->count(); i++)
		if (m_ids->at(i)->getId() == l->getId())
			return !m_invert;

	return m_invert;
}

KGpgNode *
GroupEditProxyModel::nodeForIndex(const QModelIndex &index) const
{
	return m_model->nodeForIndex(mapToSource(index));
}

int
GroupEditProxyModel::columnCount(const QModelIndex &) const
{
	return 3;
}

Rolf Eike Beer's avatar
Rolf Eike Beer committed
71
72
73
74
75
76
77
78
79
80
81
82
int
GroupEditProxyModel::rowCount(const QModelIndex &parent) const
{
	if (parent.column() > 0)
		return 0;
	if (parent.isValid())
		return 0;
	if (m_model == NULL)
		return 0;
	return QSortFilterProxyModel::rowCount(parent);
}

83
84
85
QVariant
GroupEditProxyModel::data(const QModelIndex &index, int role) const
{
86
	if (!index.isValid() || (index.column() >= 3) || (role != Qt::DisplayRole))
87
88
89
90
91
92
93
94
95
96
97
98
		return QVariant();

	KGpgNode *nd = m_model->nodeForIndex(mapToSource(index));
	switch (index.column()) {
		case 0:	return nd->getName();
		case 1:	return nd->getEmail();
		case 2:	return nd->getId().right(8);
	}

	return QVariant();
}

99
bool
Rolf Eike Beer's avatar
Rolf Eike Beer committed
100
GroupEditProxyModel::hasChildren(const QModelIndex &parent) const
101
{
Rolf Eike Beer's avatar
Rolf Eike Beer committed
102
103
104
105
106
	if (m_model == NULL)
		return false;
	if (parent.column() > 0)
		return false;
	return !parent.isValid();
107
108
}

109
110
QVariant
GroupEditProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
111
{
112
113
	if (role != Qt::DisplayRole)
		return QVariant();
114

115
116
117
	if (orientation != Qt::Horizontal)
		return QVariant();

118
119
120
	if (m_model == NULL)
		return QVariant();

121
	switch (section) {
122
123
124
125
	case 0:	return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
	case 1:	return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
	case 2:	return m_model->headerData(KEYCOLUMN_ID, orientation, role);
	default:	return QVariant();
126
	}
127
}