KNewStuff

itemsmodel.cpp
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
16namespace KNSCore
17{
18class ItemsModelPrivate
19{
20public:
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};
30ItemsModel::ItemsModel(EngineBase *engine, QObject *parent)
31 : QAbstractListModel(parent)
32 , d(new ItemsModelPrivate(engine))
33{
34}
35
36ItemsModel::~ItemsModel() = default;
37
38int ItemsModel::rowCount(const QModelIndex & /*parent*/) const
39{
40 return d->entries.count();
41}
42
43QVariant 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(entry);
50}
51
52int ItemsModel::row(const Entry &entry) const
53{
54 return d->entries.indexOf(entry);
55}
56
57void ItemsModel::slotEntriesLoaded(const KNSCore::Entry::List &entries)
58{
59 for (const KNSCore::Entry &entry : entries) {
60 addEntry(entry);
61 }
62}
63
64void 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(entry)) {
68 QString preview = entry.previewUrl(Entry::PreviewSmall1);
69 if (!d->hasPreviewImages && !preview.isEmpty()) {
70 d->hasPreviewImages = true;
71 if (rowCount() > 0) {
72 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0));
73 }
74 }
75
76 qCDebug(KNEWSTUFFCORE) << "adding entry " << entry.name() << " to the model";
77 beginInsertRows(QModelIndex(), d->entries.count(), d->entries.count());
78 d->entries.append(entry);
80
81 if (!preview.isEmpty() && entry.previewImage(Entry::PreviewSmall1).isNull()) {
82 Q_EMIT loadPreview(entry, Entry::PreviewSmall1);
83 }
84 }
85}
86
87void ItemsModel::removeEntry(const Entry &entry)
88{
89 qCDebug(KNEWSTUFFCORE) << "removing entry " << entry.name() << " from the model";
90 int index = d->entries.indexOf(entry);
91 if (index > -1) {
93 d->entries.removeAt(index);
95 }
96}
97
98void ItemsModel::slotEntryChanged(const Entry &entry)
99{
100 int i = d->entries.indexOf(entry);
101 if (i == -1) {
102 return;
103 }
104 QModelIndex entryIndex = index(i, 0);
105 Q_EMIT dataChanged(entryIndex, entryIndex);
106}
107
108void ItemsModel::clearEntries()
109{
111 d->entries.clear();
113}
114
115void 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
123bool ItemsModel::hasPreviewImages() const
124{
125 return d->hasPreviewImages;
126}
127
128} // end KNS namespace
129
130#include "moc_itemsmodel.cpp"
KNewStuff data entry container.
Definition entry.h:48
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int row() const const
Q_EMITQ_EMIT
bool isEmpty() const const
UserRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:53:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.