File indexing completed on 2024-04-21 03:56:23
0001 /* 0002 knewstuff3/ui/itemsmodel.cpp. 0003 SPDX-FileCopyrightText: 2008 Jeremy Whiting <jpwhiting@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "itemsmodel.h" 0009 0010 #include <KLocalizedString> 0011 #include <knewstuffcore_debug.h> 0012 0013 #include "enginebase.h" 0014 #include "imageloader_p.h" 0015 0016 namespace KNSCore 0017 { 0018 class ItemsModelPrivate 0019 { 0020 public: 0021 ItemsModelPrivate(EngineBase *e) 0022 : engine(e) 0023 { 0024 } 0025 EngineBase *const engine; 0026 // the list of entries 0027 QList<Entry> entries; 0028 bool hasPreviewImages = false; 0029 }; 0030 ItemsModel::ItemsModel(EngineBase *engine, QObject *parent) 0031 : QAbstractListModel(parent) 0032 , d(new ItemsModelPrivate(engine)) 0033 { 0034 } 0035 0036 ItemsModel::~ItemsModel() = default; 0037 0038 int ItemsModel::rowCount(const QModelIndex & /*parent*/) const 0039 { 0040 return d->entries.count(); 0041 } 0042 0043 QVariant ItemsModel::data(const QModelIndex &index, int role) const 0044 { 0045 if (role != Qt::UserRole) { 0046 return QVariant(); 0047 } 0048 Entry entry = d->entries[index.row()]; 0049 return QVariant::fromValue(entry); 0050 } 0051 0052 int ItemsModel::row(const Entry &entry) const 0053 { 0054 return d->entries.indexOf(entry); 0055 } 0056 0057 void ItemsModel::slotEntriesLoaded(const KNSCore::Entry::List &entries) 0058 { 0059 for (const KNSCore::Entry &entry : entries) { 0060 addEntry(entry); 0061 } 0062 } 0063 0064 void ItemsModel::addEntry(const Entry &entry) 0065 { 0066 // This might be expensive, but it avoids duplicates, which is not awesome for the user 0067 if (!d->entries.contains(entry)) { 0068 QString preview = entry.previewUrl(Entry::PreviewSmall1); 0069 if (!d->hasPreviewImages && !preview.isEmpty()) { 0070 d->hasPreviewImages = true; 0071 if (rowCount() > 0) { 0072 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0)); 0073 } 0074 } 0075 0076 qCDebug(KNEWSTUFFCORE) << "adding entry " << entry.name() << " to the model"; 0077 beginInsertRows(QModelIndex(), d->entries.count(), d->entries.count()); 0078 d->entries.append(entry); 0079 endInsertRows(); 0080 0081 if (!preview.isEmpty() && entry.previewImage(Entry::PreviewSmall1).isNull()) { 0082 Q_EMIT loadPreview(entry, Entry::PreviewSmall1); 0083 } 0084 } 0085 } 0086 0087 void ItemsModel::removeEntry(const Entry &entry) 0088 { 0089 qCDebug(KNEWSTUFFCORE) << "removing entry " << entry.name() << " from the model"; 0090 int index = d->entries.indexOf(entry); 0091 if (index > -1) { 0092 beginRemoveRows(QModelIndex(), index, index); 0093 d->entries.removeAt(index); 0094 endRemoveRows(); 0095 } 0096 } 0097 0098 void ItemsModel::slotEntryChanged(const Entry &entry) 0099 { 0100 int i = d->entries.indexOf(entry); 0101 Q_ASSERT(i != -1); 0102 QModelIndex entryIndex = index(i, 0); 0103 Q_EMIT dataChanged(entryIndex, entryIndex); 0104 } 0105 0106 void ItemsModel::clearEntries() 0107 { 0108 beginResetModel(); 0109 d->entries.clear(); 0110 endResetModel(); 0111 } 0112 0113 void ItemsModel::slotEntryPreviewLoaded(const Entry &entry, Entry::PreviewType type) 0114 { 0115 // we only care about the first small preview in the list 0116 if (type == Entry::PreviewSmall1) { 0117 slotEntryChanged(entry); 0118 } 0119 } 0120 0121 bool ItemsModel::hasPreviewImages() const 0122 { 0123 return d->hasPreviewImages; 0124 } 0125 0126 } // end KNS namespace 0127 0128 #include "moc_itemsmodel.cpp"