Akonadi

tagmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Daniel Vr├ítil <[email protected]>
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 
14 using namespace Akonadi;
15 
16 TagModel::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 
24 TagModel::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 
32 TagModel::~TagModel() = default;
33 
34 int TagModel::columnCount(const QModelIndex &parent) const
35 {
36  if (parent.isValid() && parent.column() != 0) {
37  return 0;
38  }
39 
40  return 1;
41 }
42 
43 int 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 
55 QVariant 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 
71 QVariant 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 
107 QModelIndex 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 
125 QModelIndex 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 
137 Qt::ItemFlags TagModel::flags(const QModelIndex &index) const
138 {
139  Q_UNUSED(index)
140 
142 }
143 
144 bool TagModel::insertColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/)
145 {
146  return false;
147 }
148 
149 bool TagModel::insertRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/)
150 {
151  return false;
152 }
153 
154 bool TagModel::removeColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/)
155 {
156  return false;
157 }
158 
159 bool TagModel::removeRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/)
160 {
161  return false;
162 }
163 
164 #include "moc_tagmodel.cpp"
quintptr internalId() const const
DisplayRole
QVariant fromValue(const T &value)
An Akonadi Tag.
Definition: tag.h:25
int column() const const
QIcon fromTheme(const QString &name)
Monitors an item or collection for changes.
Definition: monitor.h:71
T value(int i) const const
typedef ItemFlags
QString i18n(const char *text, const TYPE &arg...)
const Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Definition: tag.cpp:123
Orientation
bool isValid() const const
int row() const const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
Id id() const
Returns the unique identifier of the tag.
Definition: tag.cpp:139
int count(const T &value) const const
Attribute that stores the properties that are used to display a tag.
Definition: tagattribute.h:26
Q_D(Todo)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.