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 "tagmodel_p.h"
9 #include "tagattribute.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()
33 {
34  delete d_ptr;
35 }
36 
37 int TagModel::columnCount(const QModelIndex &parent) const
38 {
39  if (parent.isValid() && parent.column() != 0) {
40  return 0;
41  }
42 
43  return 1;
44 }
45 
46 int TagModel::rowCount(const QModelIndex &parent) const
47 {
48  Q_D(const TagModel);
49 
50  Tag::Id parentTagId = -1;
51  if (parent.isValid()) {
52  parentTagId = d->mChildTags[parent.internalId()].at(parent.row()).id();
53  }
54 
55  return d->mChildTags[parentTagId].count();
56 }
57 
58 QVariant TagModel::headerData(int section, Qt::Orientation orientation, int role) const
59 {
60  if (orientation == Qt::Vertical) {
61  return QVariant();
62  }
63 
64  if (role == Qt::DisplayRole) {
65  switch (section) {
66  case 0:
67  return i18n("Tag");
68  }
69  }
70 
71  return QAbstractItemModel::headerData(section, orientation, role);
72 }
73 
74 QVariant TagModel::data(const QModelIndex &index, int role) const
75 {
76  Q_D(const TagModel);
77 
78  if (!index.isValid()) {
79  return QVariant();
80  }
81  const Tag tag = d->tagForIndex(index);
82  if (!tag.isValid()) {
83  return QVariant();
84  }
85 
86  switch (role) {
87  case Qt::DisplayRole: // fall-through
88  case NameRole:
89  return tag.name();
90  case IdRole:
91  return tag.id();
92  case GIDRole:
93  return tag.gid();
94  case ParentRole:
95  return QVariant::fromValue(tag.parent());
96  case TagRole:
97  return QVariant::fromValue(tag);
98  case Qt::DecorationRole: {
99  if (const TagAttribute *attr = tag.attribute<TagAttribute>()) {
100  return QIcon::fromTheme(attr->iconName());
101  } else {
102  return QVariant();
103  }
104  }
105  }
106 
107  return QVariant();
108 }
109 
110 QModelIndex TagModel::index(int row, int column, const QModelIndex &parent) const
111 {
112  Q_D(const TagModel);
113 
114  qint64 parentId = -1;
115  if (parent.isValid()) {
116  const Tag parentTag = d->tagForIndex(parent);
117  parentId = parentTag.id();
118  }
119 
120  const Tag::List &children = d->mChildTags.value(parentId);
121  if (row >= children.count()) {
122  return QModelIndex();
123  }
124 
125  return createIndex(row, column, static_cast<int>(parentId));
126 }
127 
128 QModelIndex TagModel::parent(const QModelIndex &child) const
129 {
130  Q_D(const TagModel);
131 
132  if (!child.isValid()) {
133  return QModelIndex();
134  }
135 
136  const qint64 parentId = child.internalId();
137  return d->indexForTag(parentId);
138 }
139 
140 Qt::ItemFlags TagModel::flags(const QModelIndex &index) const
141 {
142  Q_UNUSED(index);
143 
145 }
146 
147 bool TagModel::insertColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/)
148 {
149  return false;
150 }
151 
152 bool TagModel::insertRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/)
153 {
154  return false;
155 }
156 
157 bool TagModel::removeColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/)
158 {
159  return false;
160 }
161 
162 bool TagModel::removeRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/)
163 {
164  return false;
165 }
166 
167 #include "moc_tagmodel.cpp"
Qt::Orientation orientation() const const
const QObjectList & children() const const
Id id() const
Returns the unique identifier of the tag.
Definition: tag.cpp:139
quintptr internalId() const const
bool isValid() const const
const Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Definition: tag.cpp:123
DisplayRole
int row() const const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
Attribute that stores the properties that are used to display a tag.
Definition: tagattribute.h:24
QVariant fromValue(const T &value)
Monitors an item or collection for changes.
Definition: monitor.h:71
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
int count(const T &value) const const
An Akonadi Tag.
Definition: tag.h:26
int column() const const
QIcon fromTheme(const QString &name)
Orientation
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 23:15:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.