Akonadi

tagmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "tagmodel.h"
8#include "tagattribute.h"
9#include "tagmodel_p.h"
10
11#include <KLocalizedString>
12#include <QIcon>
13
14using namespace Akonadi;
15
16TagModel::TagModel(Monitor *recorder, QObject *parent)
17 : QAbstractItemModel(parent)
18 , d_ptr(new TagModelPrivate(this))
19{
20 Q_D(TagModel);
21 d->init(recorder);
22}
23
24TagModel::TagModel(Monitor *recorder, TagModelPrivate *dd, QObject *parent)
25 : QAbstractItemModel(parent)
26 , d_ptr(dd)
27{
28 Q_D(TagModel);
29 d->init(recorder);
30}
31
32TagModel::~TagModel() = default;
33
34int TagModel::columnCount(const QModelIndex &parent) const
35{
36 if (parent.isValid() && parent.column() != 0) {
37 return 0;
38 }
39
40 return 1;
41}
42
43int TagModel::rowCount(const QModelIndex &parent) const
44{
45 Q_D(const TagModel);
46
47 Tag::Id parentTagId = -1;
48 if (parent.isValid()) {
49 parentTagId = d->mChildTags[parent.internalId()].at(parent.row()).id();
50 }
51
52 return d->mChildTags[parentTagId].count();
53}
54
55QVariant TagModel::headerData(int section, Qt::Orientation orientation, int role) const
56{
57 if (orientation == Qt::Vertical) {
58 return QVariant();
59 }
60
61 if (role == Qt::DisplayRole) {
62 switch (section) {
63 case 0:
64 return i18n("Tag");
65 }
66 }
67
68 return QAbstractItemModel::headerData(section, orientation, role);
69}
70
71QVariant TagModel::data(const QModelIndex &index, int role) const
72{
73 Q_D(const TagModel);
74
75 if (!index.isValid()) {
76 return QVariant();
77 }
78 const Tag tag = d->tagForIndex(index);
79 if (!tag.isValid()) {
80 return QVariant();
81 }
82
83 switch (role) {
84 case Qt::DisplayRole: // fall-through
85 case NameRole:
86 return tag.name();
87 case IdRole:
88 return tag.id();
89 case GIDRole:
90 return tag.gid();
91 case ParentRole:
92 return QVariant::fromValue(tag.parent());
93 case TagRole:
94 return QVariant::fromValue(tag);
95 case Qt::DecorationRole: {
96 if (const auto attr = tag.attribute<TagAttribute>()) {
97 return QIcon::fromTheme(attr->iconName());
98 } else {
99 return QVariant();
100 }
101 }
102 }
103
104 return QVariant();
105}
106
107QModelIndex TagModel::index(int row, int column, const QModelIndex &parent) const
108{
109 Q_D(const TagModel);
110
111 qint64 parentId = -1;
112 if (parent.isValid()) {
113 const Tag parentTag = d->tagForIndex(parent);
114 parentId = parentTag.id();
115 }
116
117 const Tag::List &children = d->mChildTags.value(parentId);
118 if (row >= children.count()) {
119 return QModelIndex();
120 }
121
122 return createIndex(row, column, static_cast<int>(parentId));
123}
124
125QModelIndex TagModel::parent(const QModelIndex &child) const
126{
127 Q_D(const TagModel);
128
129 if (!child.isValid()) {
130 return QModelIndex();
131 }
132
133 const qint64 parentId = child.internalId();
134 return d->indexForTag(parentId);
135}
136
137Qt::ItemFlags TagModel::flags(const QModelIndex &index) const
138{
139 Q_UNUSED(index)
140
142}
143
144bool TagModel::insertColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/)
145{
146 return false;
147}
148
149bool TagModel::insertRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/)
150{
151 return false;
152}
153
154bool TagModel::removeColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/)
155{
156 return false;
157}
158
159bool TagModel::removeRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/)
160{
161 return false;
162}
163
164#include "moc_tagmodel.cpp"
Monitors an item or collection for changes.
Definition monitor.h:72
Attribute that stores the properties that are used to display a tag.
An Akonadi Tag.
Definition tag.h:26
Id id() const
Returns the unique identifier of the tag.
Definition tag.cpp:139
const Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Definition tag.cpp:123
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
QModelIndex createIndex(int row, int column, const void *ptr) const const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
QIcon fromTheme(const QString &name)
T value(qsizetype i) const const
quintptr internalId() const const
bool isValid() const const
const QObjectList & children() const const
QObject * parent() const const
T qobject_cast(QObject *object)
DisplayRole
typedef ItemFlags
Orientation
QVariant fromValue(T &&value)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:07:19 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.