KNewStuff

itemsmodel.cpp
1 /*
2  knewstuff3/ui/itemsmodel.cpp.
3  Copyright (C) 2008 Jeremy Whiting <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "itemsmodel.h"
20 
21 #include <knewstuffcore_debug.h>
22 #include <KLocalizedString>
23 
24 #include "entryinternal.h"
25 #include "engine.h"
26 #include "imageloader_p.h"
27 
28 namespace KNSCore
29 {
30 ItemsModel::ItemsModel(Engine *engine, QObject *parent)
31  : QAbstractListModel(parent)
32  , m_engine(engine)
33  , m_hasPreviewImages(false)
34 {
35 }
36 
37 ItemsModel::~ItemsModel()
38 {
39 }
40 
41 int ItemsModel::rowCount(const QModelIndex & /*parent*/) const
42 {
43  return m_entries.count();
44 }
45 
46 QVariant ItemsModel::data(const QModelIndex &index, int role) const
47 {
48  if (role != Qt::UserRole) {
49  return QVariant();
50  }
51  EntryInternal entry = m_entries[index.row()];
52  return QVariant::fromValue(entry);
53 }
54 
55 int ItemsModel::row(const EntryInternal &entry) const
56 {
57  return m_entries.indexOf(entry);
58 }
59 
60 void ItemsModel::slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
61 {
62  for (const KNSCore::EntryInternal &entry : entries) {
63  addEntry(entry);
64  }
65 }
66 
67 void ItemsModel::addEntry(const EntryInternal &entry)
68 {
69  // This might be expensive, but it avoids duplicates, which is not awesome for the user
70  if (!m_entries.contains(entry)) {
71  QString preview = entry.previewUrl(EntryInternal::PreviewSmall1);
72  if (!m_hasPreviewImages && !preview.isEmpty()) {
73  m_hasPreviewImages = true;
74  if (rowCount() > 0) {
75  emit dataChanged(index(0, 0), index(rowCount() - 1, 0));
76  }
77  }
78 
79  qCDebug(KNEWSTUFFCORE) << "adding entry " << entry.name() << " to the model";
80  beginInsertRows(QModelIndex(), m_entries.count(), m_entries.count());
81  m_entries.append(entry);
82  endInsertRows();
83 
84  if (!preview.isEmpty() && entry.previewImage(EntryInternal::PreviewSmall1).isNull()) {
85  m_engine->loadPreview(entry, EntryInternal::PreviewSmall1);
86  }
87  }
88 }
89 
90 void ItemsModel::removeEntry(const EntryInternal &entry)
91 {
92  qCDebug(KNEWSTUFFCORE) << "removing entry " << entry.name() << " from the model";
93  int index = m_entries.indexOf(entry);
94  if (index > -1) {
95  beginRemoveRows(QModelIndex(), index, index);
96  m_entries.removeAt(index);
97  endRemoveRows();
98  }
99 }
100 
101 void ItemsModel::slotEntryChanged(const EntryInternal &entry)
102 {
103  int i = m_entries.indexOf(entry);
104  QModelIndex entryIndex = index(i, 0);
105  emit dataChanged(entryIndex, entryIndex);
106 }
107 
108 void ItemsModel::clearEntries()
109 {
110  beginResetModel();
111  m_entries.clear();
112  endResetModel();
113 }
114 
115 void ItemsModel::slotEntryPreviewLoaded(const EntryInternal &entry, EntryInternal::PreviewType type)
116 {
117  // we only care about the first small preview in the list
118  if (type != EntryInternal::PreviewSmall1) {
119  return;
120  }
121  slotEntryChanged(entry);
122 }
123 
124 bool ItemsModel::hasPreviewImages() const
125 {
126  return m_hasPreviewImages;
127 }
128 
129 } // end KNS namespace
130 
Contains the core functionality for handling interaction with NewStuff providers. ...
Encapsulates a KNSCore::Engine for use in Qt Quick.
Definition: quickengine.h:38
UserRole
bool isEmpty() const const
int row() const const
QVariant fromValue(const T &value)
KNewStuff data entry container.
Definition: entryinternal.h:60
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.