KNewStuff

imageloader.cpp
1 /*
2  This file is part of KNewStuff2.
3  Copyright (c) 2006, 2007 Josef Spillner <[email protected]>
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_p.h"
20 
21 
22 using namespace KNSCore;
23 
24 ImageLoader::ImageLoader(const EntryInternal &entry, EntryInternal::PreviewType type, QObject *parent)
25  : QObject(parent)
26  , m_entry(entry)
27  , m_previewType(type)
28 {
29 }
30 
31 void ImageLoader::start()
32 {
33  QUrl url(m_entry.previewUrl(m_previewType));
34  if (!url.isEmpty()) {
35  m_job = HTTPJob::get(url, NoReload, JobFlag::HideProgressInfo, this);
36  connect(m_job, &KJob::result, this, &ImageLoader::slotDownload);
37  connect(m_job, &HTTPJob::data, this, &ImageLoader::slotData);
38  } else {
39  emit signalError(m_entry, m_previewType, QStringLiteral("Empty url"));
40  deleteLater();
41  }
42 }
43 
44 KJob *ImageLoader::job()
45 {
46  return m_job;
47 }
48 
49 void ImageLoader::slotData(KJob *job, const QByteArray &buf)
50 {
51  Q_UNUSED(job)
52  m_buffer.append(buf);
53 }
54 
55 void ImageLoader::slotDownload(KJob *job)
56 {
57  if (job->error()) {
58  m_buffer.clear();
59  emit signalError(m_entry, m_previewType, job->errorText());
60  deleteLater();
61  return;
62  }
63  QImage image;
64  image.loadFromData(std::move(m_buffer));
65 
66  if (m_previewType == EntryInternal::PreviewSmall1
67  || m_previewType == EntryInternal::PreviewSmall2
68  || m_previewType == EntryInternal::PreviewSmall3) {
69  if (image.width() > PreviewWidth || image.height() > PreviewHeight) {
70  // if the preview is really big, first scale fast to a smaller size, then smooth to desired size
71  if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) {
72  image = image.scaled(2 * PreviewWidth, 2 * PreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation);
73  }
74  image = image.scaled(PreviewWidth, PreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
75  } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) {
76  // upscale tiny previews to double size
77  image = image.scaled(2 * image.width(), 2 * image.height());
78  }
79  }
80 
81  m_entry.setPreviewImage(image, m_previewType);
82  emit signalPreviewLoaded(m_entry, m_previewType);
83  deleteLater();
84 }
85 
bool loadFromData(const uchar *data, int len, const char *format)
Contains the core functionality for handling interaction with NewStuff providers. ...
int width() const const
KNewStuff data entry container.
Definition: entryinternal.h:60
KeepAspectRatio
int height() const const
void result(KJob *job)
FastTransformation
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.