KNewStuff

itemsmodel.cpp
1 /*
2  knewstuff3/ui/itemsmodel.cpp.
3  SPDX-FileCopyrightText: 2008 Jeremy Whiting <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "itemsmodel.h"
9 
10 #include <knewstuffcore_debug.h>
11 #include <KLocalizedString>
12 
13 #include "engine.h"
14 #include "imageloader_p.h"
15 
16 namespace KNSCore
17 {
18 ItemsModel::ItemsModel(Engine *engine, QObject *parent)
19  : QAbstractListModel(parent)
20  , m_engine(engine)
21 {
22 }
23 
24 ItemsModel::~ItemsModel()
25 {
26 }
27 
28 int ItemsModel::rowCount(const QModelIndex & /*parent*/) const
29 {
30  return m_entries.count();
31 }
32 
33 QVariant ItemsModel::data(const QModelIndex &index, int role) const
34 {
35  if (role != Qt::UserRole) {
36  return QVariant();
37  }
38  EntryInternal entry = m_entries[index.row()];
39  return QVariant::fromValue(entry);
40 }
41 
42 int ItemsModel::row(const EntryInternal &entry) const
43 {
44  return m_entries.indexOf(entry);
45 }
46 
47 void ItemsModel::slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
48 {
49  for (const KNSCore::EntryInternal &entry : entries) {
50  addEntry(entry);
51  }
52 }
53 
54 void ItemsModel::addEntry(const EntryInternal &entry)
55 {
56  // This might be expensive, but it avoids duplicates, which is not awesome for the user
57  if (!m_entries.contains(entry)) {
58  QString preview = entry.previewUrl(EntryInternal::PreviewSmall1);
59  if (!m_hasPreviewImages && !preview.isEmpty()) {
60  m_hasPreviewImages = true;
61  if (rowCount() > 0) {
62  Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0));
63  }
64  }
65 
66  qCDebug(KNEWSTUFFCORE) << "adding entry " << entry.name() << " to the model";
67  beginInsertRows(QModelIndex(), m_entries.count(), m_entries.count());
68  m_entries.append(entry);
69  endInsertRows();
70 
71  if (!preview.isEmpty() && entry.previewImage(EntryInternal::PreviewSmall1).isNull()) {
72  m_engine->loadPreview(entry, EntryInternal::PreviewSmall1);
73  }
74  }
75 }
76 
77 void ItemsModel::removeEntry(const EntryInternal &entry)
78 {
79  qCDebug(KNEWSTUFFCORE) << "removing entry " << entry.name() << " from the model";
80  int index = m_entries.indexOf(entry);
81  if (index > -1) {
82  beginRemoveRows(QModelIndex(), index, index);
83  m_entries.removeAt(index);
84  endRemoveRows();
85  }
86 }
87 
88 void ItemsModel::slotEntryChanged(const EntryInternal &entry)
89 {
90  int i = m_entries.indexOf(entry);
91  QModelIndex entryIndex = index(i, 0);
92  Q_EMIT dataChanged(entryIndex, entryIndex);
93 }
94 
95 void ItemsModel::clearEntries()
96 {
97  beginResetModel();
98  m_entries.clear();
99  endResetModel();
100 }
101 
102 void ItemsModel::slotEntryPreviewLoaded(const EntryInternal &entry, EntryInternal::PreviewType type)
103 {
104  // we only care about the first small preview in the list
105  if (type != EntryInternal::PreviewSmall1) {
106  return;
107  }
108  slotEntryChanged(entry);
109 }
110 
111 bool ItemsModel::hasPreviewImages() const
112 {
113  return m_hasPreviewImages;
114 }
115 
116 } // end KNS namespace
117 
Contains the core functionality for handling interaction with NewStuff providers. ...
Encapsulates a KNSCore::Engine for use in Qt Quick.
Definition: quickengine.h:23
UserRole
bool isEmpty() const const
int row() const const
QVariant fromValue(const T &value)
KNewStuff data entry container.
Definition: entryinternal.h:49
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.