• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KNewStuff

  • knewstuff
  • knewstuff3
  • ui
knewstuff3/ui/itemsmodel.cpp
Go to the documentation of this file.
1/*
2 knewstuff3/ui/itemsmodel.cpp.
3 Copyright (C) 2008 Jeremy Whiting <jpwhiting@kde.org>
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 "kdebug.h"
22#include "klocalizedstring.h"
23
24#include "core/entryinternal.h"
25#include "core/engine.h"
26#include "imageloader.h"
27
28namespace KNS3
29{
30ItemsModel::ItemsModel(Engine* engine, QObject* parent)
31 : QAbstractListModel(parent)
32 , m_engine(engine)
33 , m_hasPreviewImages(false)
34{
35}
36
37ItemsModel::~ItemsModel()
38{
39}
40
41int ItemsModel::rowCount(const QModelIndex & /*parent*/) const
42{
43 return m_entries.count();
44}
45
46QVariant 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
55void ItemsModel::slotEntriesLoaded(EntryInternal::List entries)
56{
57 foreach(const KNS3::EntryInternal &entry, entries) {
58 addEntry(entry);
59 }
60}
61
62void ItemsModel::addEntry(const EntryInternal& entry)
63{
64 QString preview = entry.previewUrl(EntryInternal::PreviewSmall1);
65 if (!m_hasPreviewImages && !preview.isEmpty()) {
66 m_hasPreviewImages = true;
67 if (rowCount() > 0) {
68 emit dataChanged(index(0,0), index(rowCount()-1,0));
69 }
70 }
71
72 //kDebug(551) << "adding entry " << entry.name() << " to the model";
73 beginInsertRows(QModelIndex(), m_entries.count(), m_entries.count());
74 m_entries.append(entry);
75 endInsertRows();
76
77 if (!preview.isEmpty() && entry.previewImage(EntryInternal::PreviewSmall1).isNull()) {
78 m_engine->loadPreview(entry, EntryInternal::PreviewSmall1);
79 }
80}
81
82void ItemsModel::removeEntry(const EntryInternal& entry)
83{
84 kDebug(551) << "removing entry " << entry.name() << " from the model";
85 int index = m_entries.indexOf(entry);
86 if (index > -1) {
87 beginRemoveRows(QModelIndex(), index, index);
88 m_entries.removeAt(index);
89 endRemoveRows();
90 }
91}
92
93void ItemsModel::slotEntryChanged(const EntryInternal& entry)
94{
95 int i = m_entries.indexOf(entry);
96 QModelIndex entryIndex = index(i, 0);
97 emit dataChanged(entryIndex, entryIndex);
98}
99
100void ItemsModel::clearEntries()
101{
102 m_entries.clear();
103 reset();
104}
105
106void ItemsModel::slotEntryPreviewLoaded(const EntryInternal& entry, EntryInternal::PreviewType type)
107{
108 // we only care about the first small preview in the list
109 if (type != EntryInternal::PreviewSmall1) {
110 return;
111 }
112 slotEntryChanged(entry);
113}
114
115/*
116void ItemsModel::slotEntryPreviewLoaded(const QString &url, const QImage & pix)
117{
118 if (pix.isNull()) {
119 return;
120 }
121
122 QImage image = pix;
123 m_largePreviewImages.insert(url, image);
124 if (image.width() > PreviewWidth || image.height() > PreviewHeight) {
125 // if the preview is really big, first scale fast to a smaller size, then smooth to desired size
126 if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) {
127 image = image.scaled(2 * PreviewWidth, 2 * PreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation);
128 }
129 m_previewImages.insert(url, image.scaled(PreviewWidth, PreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
130 } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) {
131 // upscale tiny previews to double size
132 m_previewImages.insert(url, image.scaled(2 * image.width(), 2 * image.height()));
133 } else {
134 m_previewImages.insert(url, image);
135 }
136
137 QModelIndex thisIndex = m_imageIndexes[url];
138
139 emit dataChanged(thisIndex, thisIndex);
140}*/
141
142bool ItemsModel::hasPreviewImages() const
143{
144 return m_hasPreviewImages;
145}
146
147} // end KNS namespace
148
149#include "itemsmodel.moc"
KNS3::Engine
KNewStuff engine.
Definition: knewstuff3/core/engine.h:53
KNS3::Engine::loadPreview
void loadPreview(const KNS3::EntryInternal &entry, EntryInternal::PreviewType type)
Definition: knewstuff3/core/engine.cpp:469
KNS3::EntryInternal
KNewStuff data entry container.
Definition: entryinternal.h:55
KNS3::EntryInternal::name
QString name() const
Retrieve the name of the data object.
Definition: entryinternal.cpp:124
KNS3::EntryInternal::previewUrl
QString previewUrl(PreviewType type=PreviewSmall1) const
Retrieve the file name of an image containing a preview of the object.
Definition: entryinternal.cpp:264
KNS3::EntryInternal::PreviewType
PreviewType
Definition: entryinternal.h:70
KNS3::EntryInternal::PreviewSmall1
@ PreviewSmall1
Definition: entryinternal.h:71
KNS3::EntryInternal::previewImage
QImage previewImage(PreviewType type=PreviewSmall1) const
This will not be loaded automatically, instead use Engine to load the actual images.
Definition: entryinternal.cpp:274
KNS3::ItemsModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: knewstuff3/ui/itemsmodel.cpp:41
KNS3::ItemsModel::slotEntryChanged
void slotEntryChanged(const KNS3::EntryInternal &entry)
Definition: knewstuff3/ui/itemsmodel.cpp:93
KNS3::ItemsModel::clearEntries
void clearEntries()
Definition: knewstuff3/ui/itemsmodel.cpp:100
KNS3::ItemsModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: knewstuff3/ui/itemsmodel.cpp:46
KNS3::ItemsModel::hasPreviewImages
bool hasPreviewImages() const
Definition: knewstuff3/ui/itemsmodel.cpp:142
KNS3::ItemsModel::removeEntry
void removeEntry(const EntryInternal &entry)
Definition: knewstuff3/ui/itemsmodel.cpp:82
KNS3::ItemsModel::addEntry
void addEntry(const EntryInternal &entry)
Definition: knewstuff3/ui/itemsmodel.cpp:62
KNS3::ItemsModel::slotEntriesLoaded
void slotEntriesLoaded(KNS3::EntryInternal::List entries)
Definition: knewstuff3/ui/itemsmodel.cpp:55
KNS3::ItemsModel::slotEntryPreviewLoaded
void slotEntryPreviewLoaded(const KNS3::EntryInternal &entry, KNS3::EntryInternal::PreviewType type)
Definition: knewstuff3/ui/itemsmodel.cpp:106
KNS3::ItemsModel::~ItemsModel
~ItemsModel()
Definition: knewstuff3/ui/itemsmodel.cpp:37
KNS3::ItemsModel::ItemsModel
ItemsModel(Engine *engine, QObject *parent=0)
Definition: knewstuff3/ui/itemsmodel.cpp:30
QAbstractListModel
QList< EntryInternal >
QObject
entryinternal.h
kDebug
#define kDebug
imageloader.h
kdebug.h
klocalizedstring.h
engine.h
itemsmodel.h
KNS3
Definition: atticaprovider.cpp:36
reset
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

Skip menu "KNewStuff"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal