KNewStuff
itemsmodel.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of KNewStuff2. 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 "qasyncimage_p.h" 00023 00024 namespace KNS 00025 { 00026 ItemsModel::ItemsModel(QObject * parent, bool hasWebService) 00027 : QAbstractListModel(parent), m_hasPreviewImages(false), m_hasWebService(hasWebService) 00028 { 00029 } 00030 00031 ItemsModel::~ItemsModel() 00032 { 00033 } 00034 00035 int ItemsModel::rowCount(const QModelIndex & /*parent*/) const 00036 { 00037 return m_entries.count(); 00038 } 00039 00040 QVariant ItemsModel::data(const QModelIndex & index, int role) const 00041 { 00042 Entry * entry = m_entries[index.row()]; 00043 switch (role) { 00044 case Qt::DisplayRole: 00045 case kNameRole: 00046 return entry->name().representation(); 00047 break; 00048 case kCategory: 00049 return entry->category(); 00050 break; 00051 case kAuthorName: 00052 return entry->author().name(); 00053 break; 00054 case kAuthorEmail: 00055 return entry->author().email(); 00056 break; 00057 case kAuthorJabber: 00058 return entry->author().jabber(); 00059 break; 00060 case kAuthorHomepage: 00061 return entry->author().homepage(); 00062 break; 00063 case kLicense: 00064 return entry->license(); 00065 break; 00066 //case Qt::ToolTipRole: 00067 case kSummary: 00068 return entry->summary().representation(); 00069 break; 00070 case kVersion: 00071 return entry->version(); 00072 break; 00073 case kRelease: 00074 return entry->release(); 00075 break; 00076 case kReleaseDate: 00077 return entry->releaseDate(); 00078 break; 00079 case kPayload: 00080 return entry->payload().representation(); 00081 break; 00082 case kPreview: 00083 return entry->preview().representation(); 00084 break; 00085 case kPreviewPixmap: 00086 if (m_previewImages.contains(entry->preview().representation())) { 00087 return m_previewImages[entry->preview().representation()]; 00088 } 00089 break; 00090 case kLargePreviewPixmap: 00091 if (m_largePreviewImages.contains(entry->preview().representation())) { 00092 return m_largePreviewImages[entry->preview().representation()]; 00093 } 00094 break; 00095 case kRating: 00096 return entry->rating(); 00097 break; 00098 case kDownloads: 00099 return entry->downloads(); 00100 break; 00101 case kStatus: 00102 return entry->status(); 00103 break; 00104 } 00105 return QVariant(); 00106 } 00107 00108 KNS::Entry* ItemsModel::entryForIndex(const QModelIndex & index) const 00109 { 00110 if (index.row() < 0) 00111 return 0; 00112 else 00113 return m_entries[index.row()]; 00114 } 00115 00116 void ItemsModel::addEntry(Entry * entry) 00117 { 00118 QString preview = entry->preview().representation(); 00119 if (!preview.isEmpty()) { 00120 m_hasPreviewImages = true; 00121 } 00122 00123 //kDebug(551) << "adding entry " << entry->name().representation() << " to the model"; 00124 beginInsertRows(QModelIndex(), m_entries.count(), m_entries.count()); 00125 m_entries.append(entry); 00126 endInsertRows(); 00127 00128 if (!preview.isEmpty()) { 00129 m_imageIndexes.insert(preview, index(m_entries.count() - 1, 0)); 00130 QAsyncImage *pix = new QAsyncImage(preview, this); 00131 connect(pix, SIGNAL(signalLoaded(const QString &, const QImage&)), 00132 this, SLOT(slotEntryPreviewLoaded(const QString &, const QImage&))); 00133 } 00134 } 00135 00136 void ItemsModel::removeEntry(Entry * entry) 00137 { 00138 kDebug(551) << "removing entry " << entry->name().representation() << " from the model"; 00139 int index = m_entries.indexOf(entry); 00140 if (index > -1) { 00141 beginRemoveRows(QModelIndex(), index, index); 00142 m_entries.removeAt(index); 00143 endRemoveRows(); 00144 } 00145 } 00146 00147 void ItemsModel::slotEntryChanged(Entry * entry) 00148 { 00149 int i = m_entries.indexOf(entry); 00150 QModelIndex entryIndex = index(i, 0); 00151 emit dataChanged(entryIndex, entryIndex); 00152 } 00153 00154 void ItemsModel::slotEntryPreviewLoaded(const QString &url, const QImage & pix) 00155 { 00156 if( pix.isNull()) 00157 return; 00158 QImage image = pix; 00159 m_largePreviewImages.insert(url, image); 00160 if (image.width() > kPreviewWidth || image.height() > kPreviewHeight) { 00161 // if the preview is really big, first scale fast to a smaller size, then smooth to desired size 00162 if (image.width() > 4 * kPreviewWidth || image.height() > 4 * kPreviewHeight) { 00163 image = image.scaled(2 * kPreviewWidth, 2 * kPreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation); 00164 } 00165 m_previewImages.insert(url, image.scaled(kPreviewWidth, kPreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation)); 00166 } else if (image.width() <= kPreviewWidth / 2 && image.height() <= kPreviewHeight / 2) { 00167 // upscale tiny previews to double size 00168 m_previewImages.insert(url, image.scaled(2 * image.width(), 2 * image.height())); 00169 } else { 00170 m_previewImages.insert(url, image); 00171 } 00172 00173 QModelIndex thisIndex = m_imageIndexes[url]; 00174 00175 emit dataChanged(thisIndex, thisIndex); 00176 } 00177 00178 bool ItemsModel::hasPreviewImages() const 00179 { 00180 return m_hasPreviewImages; 00181 } 00182 00183 bool ItemsModel::hasWebService() const 00184 { 00185 return m_hasWebService; 00186 } 00187 00188 } // end KNS namespace
KDE 4.6 API Reference