1/*
2 Copyright (c) 2014 Daniel Vrátil <dvratil@redhat.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#include "tagmodel.h"
21#include "tagmodel_p.h"
22
23#include <akonadi/tagattribute.h>
24
25#include <KLocalizedString>
26#include <QIcon>
27
28using namespace Akonadi;
29
30TagModel::TagModel(Monitor *recorder, QObject *parent)
31 : QAbstractItemModel(parent)
32 , d_ptr(new TagModelPrivate(this))
33{
34 Q_D(TagModel);
35 d->init(recorder);
36}
37
38TagModel::TagModel(Monitor *recorder, TagModelPrivate *dd, QObject *parent)
39 : QAbstractItemModel(parent)
40 , d_ptr(dd)
41{
42 Q_D(TagModel);
43 d->init(recorder);
44}
45
46TagModel::~TagModel()
47{
48 delete d_ptr;
49}
50
51int TagModel::columnCount(const QModelIndex &parent) const
52{
53 if (parent.isValid() && parent.column() != 0) {
54 return 0;
55 }
56
57 return 1;
58}
59
60int TagModel::rowCount(const QModelIndex &parent) const
61{
62 Q_D(const TagModel);
63
64 Tag::Id parentTagId = 0;
65 if (parent.isValid()) {
66 parentTagId = d->mChildTags[parent.internalId()].at(parent.row()).id();
67 }
68
69 return d->mChildTags[parentTagId].count();
70}
71
72QVariant TagModel::headerData(int section, Qt::Orientation orientation, int role) const
73{
74 if (orientation == Qt::Vertical) {
75 return QVariant();
76 }
77
78 if (role == Qt::DisplayRole) {
79 switch (section) {
80 case 0:
81 return i18n("Tag");
82 }
83 }
84
85 return QAbstractItemModel::headerData(section, orientation, role);
86}
87
88QVariant TagModel::data(const QModelIndex &index, int role) const
89{
90 Q_D(const TagModel);
91
92 const Tag tag = d->tagForIndex(index);
93 if (!tag.isValid()) {
94 return QVariant();
95 }
96
97 switch (role) {
98 case Qt::DisplayRole: // fall-through
99 case NameRole:
100 return tag.name();
101 case IdRole:
102 return tag.id();
103 case GIDRole:
104 return tag.gid();
105 case ParentRole:
106 return QVariant::fromValue(tag.parent());
107 case TagRole:
108 return QVariant::fromValue(tag);
109 case Qt::DecorationRole: {
110 TagAttribute *attr = tag.attribute<TagAttribute>();
111 if (attr) {
112 return QIcon::fromTheme(attr->iconName());
113 } else {
114 return QVariant();
115 }
116 }
117 }
118
119 return QVariant();
120}
121
122QModelIndex TagModel::index(int row, int column, const QModelIndex &parent) const
123{
124 Q_D(const TagModel);
125
126 qint64 parentId = 0;
127 if (parent.isValid()) {
128 const Tag parentTag = d->tagForIndex(parent);
129 parentId = parentTag.id();
130 }
131
132 const Tag::List &children = d->mChildTags.value(parentId);
133 if (row >= children.count()) {
134 return QModelIndex();
135 }
136
137 return createIndex(row, column, (int) parentId);
138}
139
140QModelIndex TagModel::parent(const QModelIndex &child) const
141{
142 Q_D(const TagModel);
143
144 if (!child.isValid()) {
145 return QModelIndex();
146 }
147
148 const qint64 parentId = child.internalId();
149 return d->indexForTag(parentId);
150}
151
152Qt::ItemFlags TagModel::flags(const QModelIndex &index) const
153{
154 Q_UNUSED(index);
155
156 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
157}
158
159bool TagModel::insertColumns(int, int, const QModelIndex &)
160{
161 return false;
162}
163
164bool TagModel::insertRows(int, int, const QModelIndex &)
165{
166 return false;
167}
168
169bool TagModel::removeColumns(int, int, const QModelIndex &)
170{
171 return false;
172}
173
174bool TagModel::removeRows(int, int, const QModelIndex &)
175{
176 return false;
177}
178
179#include "moc_tagmodel.cpp"
180