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

KNewStuff

  • sources
  • kde-4.14
  • kdelibs
  • 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 
30 using namespace KNS3;
31 
32 ImageLoader::ImageLoader(const EntryInternal& entry, EntryInternal::PreviewType type, QObject* parent)
33  : QObject(parent)
34  , m_entry(entry)
35  , m_previewType(type)
36 {
37 }
38 
39 void 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 
50 KJob* ImageLoader::job()
51 {
52  return m_job;
53 }
54 
55 void ImageLoader::slotData(KIO::Job *job, const QByteArray& buf)
56 {
57  Q_UNUSED(job)
58  m_buffer.append(buf);
59 }
60 
61 void 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"
KNS3::ImageLoader::signalPreviewLoaded
void signalPreviewLoaded(const KNS3::EntryInternal &, KNS3::EntryInternal::PreviewType)
QImage::loadFromData
bool loadFromData(const uchar *data, int len, const char *format)
krandom.h
KNS3::EntryInternal::PreviewSmall1
Definition: entryinternal.h:71
QByteArray::clear
void clear()
kdebug.h
KNS3::EntryInternal
KNewStuff data entry container.
Definition: entryinternal.h:54
kapplication.h
QByteArray
KIO::Scheduler::setJobPriority
static void setJobPriority(SimpleJob *job, int priority)
KIO::HideProgressInfo
imageloader.h
KNS3::PreviewHeight
static const int PreviewHeight
Definition: entryinternal.h:37
KIO::get
TransferJob * get(const KUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KUrl
scheduler.h
QObject
QImage::width
int width() const
KNS3::EntryInternal::setPreviewImage
void setPreviewImage(const QImage &image, PreviewType type=PreviewSmall1)
Definition: entryinternal.cpp:279
KNS3::PreviewWidth
static const int PreviewWidth
Definition: entryinternal.h:36
KNS3::EntryInternal::PreviewSmall2
Definition: entryinternal.h:72
QObject::deleteLater
void deleteLater()
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::PreviewSmall3
Definition: entryinternal.h:73
KNS3::EntryInternal::PreviewType
PreviewType
Definition: entryinternal.h:70
QImage
job.h
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
Convenience class for images with remote sources.
Definition: imageloader.h:51
KNS3::ImageLoader::ImageLoader
ImageLoader(const EntryInternal &entry, EntryInternal::PreviewType type, QObject *parent)
Definition: imageloader.cpp:32
kstandarddirs.h
KIO::Job
KNS3::ImageLoader::start
void start()
Definition: imageloader.cpp:39
KIO::NoReload
QImage::height
int height() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KJob
QImage::scaled
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:25:43 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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