KNewStuff
imageloader.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of KNewStuff2. 00003 Copyright (c) 2006, 2007 Josef Spillner <spillner@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 "imageloader.h" 00020 00021 #include <kio/job.h> 00022 #include <kio/scheduler.h> 00023 #include <kstandarddirs.h> 00024 #include <kapplication.h> 00025 #include <krandom.h> 00026 #include <kdebug.h> 00027 00028 #include <QtCore/QFile> 00029 00030 using namespace KNS3; 00031 00032 ImageLoader::ImageLoader(const EntryInternal& entry, EntryInternal::PreviewType type, QObject* parent) 00033 : QObject(parent) 00034 , m_entry(entry) 00035 , m_previewType(type) 00036 { 00037 } 00038 00039 void ImageLoader::start() 00040 { 00041 KUrl url(m_entry.previewUrl(m_previewType)); 00042 if (!url.isEmpty()) { 00043 m_job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); 00044 connect(m_job, SIGNAL(result(KJob*)), SLOT(slotDownload(KJob*))); 00045 connect(m_job, SIGNAL(data(KIO::Job*, const QByteArray&)), SLOT(slotData(KIO::Job*, const QByteArray&))); 00046 KIO::Scheduler::setJobPriority(m_job, 1); 00047 } 00048 } 00049 00050 KJob* ImageLoader::job() 00051 { 00052 return m_job; 00053 } 00054 00055 void ImageLoader::slotData(KIO::Job *job, const QByteArray& buf) 00056 { 00057 Q_UNUSED(job) 00058 m_buffer.append(buf); 00059 } 00060 00061 void ImageLoader::slotDownload(KJob *job) 00062 { 00063 if (job->error()) { 00064 m_buffer.clear(); 00065 return; 00066 } 00067 QImage image; 00068 image.loadFromData(m_buffer); 00069 m_buffer.clear(); 00070 00071 if (m_previewType == EntryInternal::PreviewSmall1 00072 || m_previewType == EntryInternal::PreviewSmall2 00073 || m_previewType == EntryInternal::PreviewSmall3) { 00074 if (image.width() > PreviewWidth || image.height() > PreviewHeight) { 00075 // if the preview is really big, first scale fast to a smaller size, then smooth to desired size 00076 if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) { 00077 image = image.scaled(2 * PreviewWidth, 2 * PreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation); 00078 } 00079 image = image.scaled(PreviewWidth, PreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation); 00080 } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) { 00081 // upscale tiny previews to double size 00082 image = image.scaled(2 * image.width(), 2 * image.height()); 00083 } 00084 } 00085 00086 m_entry.setPreviewImage(image, m_previewType); 00087 emit signalPreviewLoaded(m_entry, m_previewType); 00088 deleteLater(); 00089 } 00090 00091 #include "imageloader.moc"
KDE 4.6 API Reference