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

KNewStuff

  • knewstuff
  • knewstuff3
  • ui
imageloader.cpp
Go to the documentation of this file.
1/*
2 This file is part of KNewStuff2.
3 Copyright (c) 2006, 2007 Josef Spillner <spillner@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 "imageloader.h"
20
21#include <kio/job.h>
22#include <kio/scheduler.h>
23#include <kstandarddirs.h>
24#include <kapplication.h>
25#include <krandom.h>
26#include <kdebug.h>
27
28#include <QtCore/QFile>
29
30using namespace KNS3;
31
32ImageLoader::ImageLoader(const EntryInternal& entry, EntryInternal::PreviewType type, QObject* parent)
33 : QObject(parent)
34 , m_entry(entry)
35 , m_previewType(type)
36{
37}
38
39void ImageLoader::start()
40{
41 KUrl url(m_entry.previewUrl(m_previewType));
42 if (!url.isEmpty()) {
43 m_job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
44 connect(m_job, SIGNAL(result(KJob*)), SLOT(slotDownload(KJob*)));
45 connect(m_job, SIGNAL(data(KIO::Job*,QByteArray)), SLOT(slotData(KIO::Job*,QByteArray)));
46 KIO::Scheduler::setJobPriority(m_job, 1);
47 }
48}
49
50KJob* ImageLoader::job()
51{
52 return m_job;
53}
54
55void ImageLoader::slotData(KIO::Job *job, const QByteArray& buf)
56{
57 Q_UNUSED(job)
58 m_buffer.append(buf);
59}
60
61void ImageLoader::slotDownload(KJob *job)
62{
63 if (job->error()) {
64 m_buffer.clear();
65 return;
66 }
67 QImage image;
68 image.loadFromData(m_buffer);
69 m_buffer.clear();
70
71 if (m_previewType == EntryInternal::PreviewSmall1
72 || m_previewType == EntryInternal::PreviewSmall2
73 || m_previewType == EntryInternal::PreviewSmall3) {
74 if (image.width() > PreviewWidth || image.height() > PreviewHeight) {
75 // if the preview is really big, first scale fast to a smaller size, then smooth to desired size
76 if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) {
77 image = image.scaled(2 * PreviewWidth, 2 * PreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation);
78 }
79 image = image.scaled(PreviewWidth, PreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
80 } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) {
81 // upscale tiny previews to double size
82 image = image.scaled(2 * image.width(), 2 * image.height());
83 }
84 }
85
86 m_entry.setPreviewImage(image, m_previewType);
87 emit signalPreviewLoaded(m_entry, m_previewType);
88 deleteLater();
89}
90
91#include "imageloader.moc"
KIO::Job
KIO::Scheduler::setJobPriority
static void setJobPriority(SimpleJob *job, int priority)
KJob
KJob::error
int error() const
KNS3::EntryInternal
KNewStuff data entry container.
Definition: entryinternal.h:55
KNS3::EntryInternal::setPreviewImage
void setPreviewImage(const QImage &image, PreviewType type=PreviewSmall1)
Definition: entryinternal.cpp:279
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::PreviewSmall3
@ PreviewSmall3
Definition: entryinternal.h:73
KNS3::EntryInternal::PreviewSmall2
@ PreviewSmall2
Definition: entryinternal.h:72
KNS3::ImageLoader::job
KJob * job()
Get the job doing the image loading in the background (to have progress information available)
Definition: imageloader.cpp:50
KNS3::ImageLoader::ImageLoader
ImageLoader(const EntryInternal &entry, EntryInternal::PreviewType type, QObject *parent)
Definition: imageloader.cpp:32
KNS3::ImageLoader::start
void start()
Definition: imageloader.cpp:39
KNS3::ImageLoader::signalPreviewLoaded
void signalPreviewLoaded(const KNS3::EntryInternal &, KNS3::EntryInternal::PreviewType)
KUrl
QObject
imageloader.h
job.h
kapplication.h
kdebug.h
krandom.h
kstandarddirs.h
KIO::get
TransferJob * get(const KUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KIO::NoReload
NoReload
KIO::HideProgressInfo
HideProgressInfo
KNS3
Definition: atticaprovider.cpp:36
KNS3::PreviewHeight
static const int PreviewHeight
Definition: entryinternal.h:37
KNS3::PreviewWidth
static const int PreviewWidth
Definition: entryinternal.h:36
scheduler.h
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