File indexing completed on 2024-06-23 05:06:45
0001 /* 0002 SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "tagmodel.h" 0008 #include "tagattribute.h" 0009 #include "tagmodel_p.h" 0010 0011 #include <KLocalizedString> 0012 #include <QIcon> 0013 0014 using namespace Akonadi; 0015 0016 TagModel::TagModel(Monitor *recorder, QObject *parent) 0017 : QAbstractItemModel(parent) 0018 , d_ptr(new TagModelPrivate(this)) 0019 { 0020 Q_D(TagModel); 0021 d->init(recorder); 0022 } 0023 0024 TagModel::TagModel(Monitor *recorder, TagModelPrivate *dd, QObject *parent) 0025 : QAbstractItemModel(parent) 0026 , d_ptr(dd) 0027 { 0028 Q_D(TagModel); 0029 d->init(recorder); 0030 } 0031 0032 TagModel::~TagModel() = default; 0033 0034 int TagModel::columnCount(const QModelIndex &parent) const 0035 { 0036 if (parent.isValid() && parent.column() != 0) { 0037 return 0; 0038 } 0039 0040 return 1; 0041 } 0042 0043 int TagModel::rowCount(const QModelIndex &parent) const 0044 { 0045 Q_D(const TagModel); 0046 0047 Tag::Id parentTagId = -1; 0048 if (parent.isValid()) { 0049 parentTagId = d->mChildTags[parent.internalId()].at(parent.row()).id(); 0050 } 0051 0052 return d->mChildTags[parentTagId].count(); 0053 } 0054 0055 QVariant TagModel::headerData(int section, Qt::Orientation orientation, int role) const 0056 { 0057 if (orientation == Qt::Vertical) { 0058 return QVariant(); 0059 } 0060 0061 if (role == Qt::DisplayRole) { 0062 switch (section) { 0063 case 0: 0064 return i18n("Tag"); 0065 } 0066 } 0067 0068 return QAbstractItemModel::headerData(section, orientation, role); 0069 } 0070 0071 QVariant TagModel::data(const QModelIndex &index, int role) const 0072 { 0073 Q_D(const TagModel); 0074 0075 if (!index.isValid()) { 0076 return QVariant(); 0077 } 0078 const Tag tag = d->tagForIndex(index); 0079 if (!tag.isValid()) { 0080 return QVariant(); 0081 } 0082 0083 switch (role) { 0084 case Qt::DisplayRole: // fall-through 0085 case NameRole: 0086 return tag.name(); 0087 case IdRole: 0088 return tag.id(); 0089 case GIDRole: 0090 return tag.gid(); 0091 case ParentRole: 0092 return QVariant::fromValue(tag.parent()); 0093 case TagRole: 0094 return QVariant::fromValue(tag); 0095 case Qt::DecorationRole: { 0096 if (const auto attr = tag.attribute<TagAttribute>()) { 0097 return QIcon::fromTheme(attr->iconName()); 0098 } else { 0099 return QVariant(); 0100 } 0101 } 0102 } 0103 0104 return QVariant(); 0105 } 0106 0107 QModelIndex TagModel::index(int row, int column, const QModelIndex &parent) const 0108 { 0109 Q_D(const TagModel); 0110 0111 qint64 parentId = -1; 0112 if (parent.isValid()) { 0113 const Tag parentTag = d->tagForIndex(parent); 0114 parentId = parentTag.id(); 0115 } 0116 0117 const Tag::List &children = d->mChildTags.value(parentId); 0118 if (row >= children.count()) { 0119 return QModelIndex(); 0120 } 0121 0122 return createIndex(row, column, static_cast<int>(parentId)); 0123 } 0124 0125 QModelIndex TagModel::parent(const QModelIndex &child) const 0126 { 0127 Q_D(const TagModel); 0128 0129 if (!child.isValid()) { 0130 return QModelIndex(); 0131 } 0132 0133 const qint64 parentId = child.internalId(); 0134 return d->indexForTag(parentId); 0135 } 0136 0137 Qt::ItemFlags TagModel::flags(const QModelIndex &index) const 0138 { 0139 Q_UNUSED(index) 0140 0141 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; 0142 } 0143 0144 bool TagModel::insertColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/) 0145 { 0146 return false; 0147 } 0148 0149 bool TagModel::insertRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/) 0150 { 0151 return false; 0152 } 0153 0154 bool TagModel::removeColumns(int /*column*/, int /*count*/, const QModelIndex & /*parent*/) 0155 { 0156 return false; 0157 } 0158 0159 bool TagModel::removeRows(int /*row*/, int /*count*/, const QModelIndex & /*parent*/) 0160 { 0161 return false; 0162 } 0163 0164 #include "moc_tagmodel.cpp"