KNewStuff
itemsmodel.cpp
Go to the documentation of this file.
00001 /* 00002 knewstuff3/ui/itemsmodel.cpp. 00003 Copyright (C) 2008 Jeremy Whiting <jpwhiting@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #include "itemsmodel.h" 00020 00021 #include "kdebug.h" 00022 #include "klocalizedstring.h" 00023 00024 #include "core/entryinternal.h" 00025 #include "core/engine.h" 00026 #include "imageloader.h" 00027 00028 namespace KNS3 00029 { 00030 ItemsModel::ItemsModel(Engine* engine, QObject* parent) 00031 : QAbstractListModel(parent) 00032 , m_engine(engine) 00033 , m_hasPreviewImages(false) 00034 { 00035 } 00036 00037 ItemsModel::~ItemsModel() 00038 { 00039 } 00040 00041 int ItemsModel::rowCount(const QModelIndex & /*parent*/) const 00042 { 00043 return m_entries.count(); 00044 } 00045 00046 QVariant ItemsModel::data(const QModelIndex & index, int role) const 00047 { 00048 if (role != Qt::UserRole) { 00049 return QVariant(); 00050 } 00051 EntryInternal entry = m_entries[index.row()]; 00052 return QVariant::fromValue(entry); 00053 } 00054 00055 void ItemsModel::slotEntriesLoaded(EntryInternal::List entries) 00056 { 00057 foreach(const KNS3::EntryInternal &entry, entries) { 00058 addEntry(entry); 00059 } 00060 } 00061 00062 void ItemsModel::addEntry(const EntryInternal& entry) 00063 { 00064 QString preview = entry.previewUrl(EntryInternal::PreviewSmall1); 00065 if (!m_hasPreviewImages && !preview.isEmpty()) { 00066 m_hasPreviewImages = true; 00067 if (rowCount() > 0) { 00068 emit dataChanged(index(0,0), index(rowCount()-1,0)); 00069 } 00070 } 00071 00072 //kDebug(551) << "adding entry " << entry.name() << " to the model"; 00073 beginInsertRows(QModelIndex(), m_entries.count(), m_entries.count()); 00074 m_entries.append(entry); 00075 endInsertRows(); 00076 00077 if (!preview.isEmpty() && entry.previewImage(EntryInternal::PreviewSmall1).isNull()) { 00078 m_engine->loadPreview(entry, EntryInternal::PreviewSmall1); 00079 } 00080 } 00081 00082 void ItemsModel::removeEntry(const EntryInternal& entry) 00083 { 00084 kDebug(551) << "removing entry " << entry.name() << " from the model"; 00085 int index = m_entries.indexOf(entry); 00086 if (index > -1) { 00087 beginRemoveRows(QModelIndex(), index, index); 00088 m_entries.removeAt(index); 00089 endRemoveRows(); 00090 } 00091 } 00092 00093 void ItemsModel::slotEntryChanged(const EntryInternal& entry) 00094 { 00095 int i = m_entries.indexOf(entry); 00096 QModelIndex entryIndex = index(i, 0); 00097 emit dataChanged(entryIndex, entryIndex); 00098 } 00099 00100 void ItemsModel::clearEntries() 00101 { 00102 m_entries.clear(); 00103 reset(); 00104 } 00105 00106 void ItemsModel::slotEntryPreviewLoaded(const EntryInternal& entry, EntryInternal::PreviewType type) 00107 { 00108 // we only care about the first small preview in the list 00109 if (type != EntryInternal::PreviewSmall1) { 00110 return; 00111 } 00112 slotEntryChanged(entry); 00113 } 00114 00115 /* 00116 void ItemsModel::slotEntryPreviewLoaded(const QString &url, const QImage & pix) 00117 { 00118 if (pix.isNull()) { 00119 return; 00120 } 00121 00122 QImage image = pix; 00123 m_largePreviewImages.insert(url, image); 00124 if (image.width() > PreviewWidth || image.height() > PreviewHeight) { 00125 // if the preview is really big, first scale fast to a smaller size, then smooth to desired size 00126 if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) { 00127 image = image.scaled(2 * PreviewWidth, 2 * PreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation); 00128 } 00129 m_previewImages.insert(url, image.scaled(PreviewWidth, PreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation)); 00130 } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) { 00131 // upscale tiny previews to double size 00132 m_previewImages.insert(url, image.scaled(2 * image.width(), 2 * image.height())); 00133 } else { 00134 m_previewImages.insert(url, image); 00135 } 00136 00137 QModelIndex thisIndex = m_imageIndexes[url]; 00138 00139 emit dataChanged(thisIndex, thisIndex); 00140 }*/ 00141 00142 bool ItemsModel::hasPreviewImages() const 00143 { 00144 return m_hasPreviewImages; 00145 } 00146 00147 } // end KNS namespace 00148 00149 #include "itemsmodel.moc"
KDE 4.6 API Reference