File indexing completed on 2024-11-24 04:34:34
0001 /*************************************************************************** 0002 * SPDX-License-Identifier: GPL-2.0-or-later 0003 * * 0004 * SPDX-FileCopyrightText: 2004-2019 Thomas Fischer <fischer@unix-ag.uni-kl.de> 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, * 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0014 * GNU General Public License for more details. * 0015 * * 0016 * You should have received a copy of the GNU General Public License * 0017 * along with this program; if not, see <https://www.gnu.org/licenses/>. * 0018 ***************************************************************************/ 0019 0020 #include "tagmodel.h" 0021 0022 #include <QHash> 0023 #include <QIcon> 0024 0025 #include "zotero/tags.h" 0026 0027 using namespace Zotero; 0028 0029 class Zotero::TagModel::Private 0030 { 0031 public: 0032 Private(Tags *t, Zotero::TagModel *parent) 0033 : tags(t) { 0034 Q_UNUSED(parent) 0035 } 0036 0037 Tags *tags; 0038 0039 QHash<QString, QModelIndex> tagToModelIndex; 0040 }; 0041 0042 TagModel::TagModel(Tags *tags, QObject *parent) 0043 : QAbstractItemModel(parent), d(new Zotero::TagModel::Private(tags, this)) 0044 { 0045 beginResetModel(); 0046 connect(tags, &Tags::finishedLoading, this, [this]() { 0047 endResetModel(); 0048 }); 0049 } 0050 0051 QVariant TagModel::data(const QModelIndex &index, int role) const 0052 { 0053 if (!d->tags->initialized()) 0054 return QVariant(); 0055 0056 if (index == QModelIndex()) 0057 return QVariant(); 0058 0059 const QMap<QString, int> data = d->tags->tags(); 0060 if (index.row() < 0 || index.row() >= data.count()) 0061 return QVariant(); 0062 0063 const QList<QString> dataKeys = data.keys(); 0064 if (role == Qt::DisplayRole) { 0065 if (index.column() == 0) 0066 return dataKeys[index.row()]; 0067 else if (index.column() == 1) 0068 return data.value(dataKeys[index.row()]); 0069 } else if (role == Qt::DecorationRole) { 0070 if (index.column() == 0) 0071 return QIcon::fromTheme(QStringLiteral("mail-tagged")); 0072 } else if (role == TagRole) 0073 return dataKeys[index.row()]; 0074 else if (role == TagCountRole) 0075 return data.value(dataKeys[index.row()]); 0076 0077 return QVariant(); 0078 } 0079 0080 QModelIndex TagModel::index(int row, int column, const QModelIndex &parent) const 0081 { 0082 if (!d->tags->initialized() || parent != QModelIndex()) 0083 return QModelIndex(); 0084 0085 const QMap<QString, int> data = d->tags->tags(); 0086 if (row < 0 || column < 0 || row >= data.count() || column >= 2) 0087 return QModelIndex(); 0088 0089 const QList<QString> dataKeys = data.keys(); 0090 return createIndex(row, column, qHash(dataKeys[row])); 0091 } 0092 0093 QModelIndex TagModel::parent(const QModelIndex &) const 0094 { 0095 /// This is a flat list 0096 return QModelIndex(); 0097 } 0098 0099 int TagModel::rowCount(const QModelIndex &parent) const 0100 { 0101 if (!d->tags->initialized() || parent != QModelIndex()) 0102 return 0; 0103 0104 const QMap<QString, int> data = d->tags->tags(); 0105 return data.count(); 0106 } 0107 0108 int TagModel::columnCount(const QModelIndex &) const 0109 { 0110 /// Double column design; 0111 return 2; 0112 } 0113 0114 bool TagModel::hasChildren(const QModelIndex &) const 0115 { 0116 return false; 0117 }