1 | /* |
2 | knewstuff3/ui/itemsmodel.cpp. |
3 | SPDX-FileCopyrightText: 2008 Jeremy Whiting <jpwhiting@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.1-or-later |
6 | */ |
7 | |
8 | #include "itemsmodel.h" |
9 | |
10 | #include <KLocalizedString> |
11 | #include <knewstuffcore_debug.h> |
12 | |
13 | #include "enginebase.h" |
14 | #include "imageloader_p.h" |
15 | |
16 | namespace KNSCore |
17 | { |
18 | class ItemsModelPrivate |
19 | { |
20 | public: |
21 | ItemsModelPrivate(EngineBase *e) |
22 | : engine(e) |
23 | { |
24 | } |
25 | EngineBase *const engine; |
26 | // the list of entries |
27 | QList<Entry> entries; |
28 | bool hasPreviewImages = false; |
29 | }; |
30 | ItemsModel::ItemsModel(EngineBase *engine, QObject *parent) |
31 | : QAbstractListModel(parent) |
32 | , d(new ItemsModelPrivate(engine)) |
33 | { |
34 | } |
35 | |
36 | ItemsModel::~ItemsModel() = default; |
37 | |
38 | int ItemsModel::rowCount(const QModelIndex & /*parent*/) const |
39 | { |
40 | return d->entries.count(); |
41 | } |
42 | |
43 | QVariant ItemsModel::data(const QModelIndex &index, int role) const |
44 | { |
45 | if (role != Qt::UserRole) { |
46 | return QVariant(); |
47 | } |
48 | Entry entry = d->entries[index.row()]; |
49 | return QVariant::fromValue(value: entry); |
50 | } |
51 | |
52 | int ItemsModel::row(const Entry &entry) const |
53 | { |
54 | return d->entries.indexOf(t: entry); |
55 | } |
56 | |
57 | void ItemsModel::slotEntriesLoaded(const KNSCore::Entry::List &entries) |
58 | { |
59 | for (const KNSCore::Entry &entry : entries) { |
60 | addEntry(entry); |
61 | } |
62 | } |
63 | |
64 | void ItemsModel::addEntry(const Entry &entry) |
65 | { |
66 | // This might be expensive, but it avoids duplicates, which is not awesome for the user |
67 | if (!d->entries.contains(t: entry)) { |
68 | QString preview = entry.previewUrl(type: Entry::PreviewSmall1); |
69 | if (!d->hasPreviewImages && !preview.isEmpty()) { |
70 | d->hasPreviewImages = true; |
71 | if (rowCount() > 0) { |
72 | Q_EMIT dataChanged(topLeft: index(row: 0, column: 0), bottomRight: index(row: rowCount() - 1, column: 0)); |
73 | } |
74 | } |
75 | |
76 | qCDebug(KNEWSTUFFCORE) << "adding entry " << entry.name() << " to the model" ; |
77 | beginInsertRows(parent: QModelIndex(), first: d->entries.count(), last: d->entries.count()); |
78 | d->entries.append(t: entry); |
79 | endInsertRows(); |
80 | |
81 | if (!preview.isEmpty() && entry.previewImage(type: Entry::PreviewSmall1).isNull()) { |
82 | Q_EMIT loadPreview(entry, type: Entry::PreviewSmall1); |
83 | } |
84 | } |
85 | } |
86 | |
87 | void ItemsModel::removeEntry(const Entry &entry) |
88 | { |
89 | qCDebug(KNEWSTUFFCORE) << "removing entry " << entry.name() << " from the model" ; |
90 | int index = d->entries.indexOf(t: entry); |
91 | if (index > -1) { |
92 | beginRemoveRows(parent: QModelIndex(), first: index, last: index); |
93 | d->entries.removeAt(i: index); |
94 | endRemoveRows(); |
95 | } |
96 | } |
97 | |
98 | void ItemsModel::slotEntryChanged(const Entry &entry) |
99 | { |
100 | int i = d->entries.indexOf(t: entry); |
101 | if (i == -1) { |
102 | return; |
103 | } |
104 | QModelIndex entryIndex = index(row: i, column: 0); |
105 | Q_EMIT dataChanged(topLeft: entryIndex, bottomRight: entryIndex); |
106 | } |
107 | |
108 | void ItemsModel::clearEntries() |
109 | { |
110 | beginResetModel(); |
111 | d->entries.clear(); |
112 | endResetModel(); |
113 | } |
114 | |
115 | void ItemsModel::slotEntryPreviewLoaded(const Entry &entry, Entry::PreviewType type) |
116 | { |
117 | // we only care about the first small preview in the list |
118 | if (type == Entry::PreviewSmall1) { |
119 | slotEntryChanged(entry); |
120 | } |
121 | } |
122 | |
123 | bool ItemsModel::hasPreviewImages() const |
124 | { |
125 | return d->hasPreviewImages; |
126 | } |
127 | |
128 | } // end KNS namespace |
129 | |
130 | #include "moc_itemsmodel.cpp" |
131 | |