KNewStuff

imageloader.cpp
1 /*
2  This file is part of KNewStuff2.
3  SPDX-FileCopyrightText: 2006, 2007 Josef Spillner <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "imageloader_p.h"
9 
10 
11 using namespace KNSCore;
12 
13 ImageLoader::ImageLoader(const EntryInternal &entry, EntryInternal::PreviewType type, QObject *parent)
14  : QObject(parent)
15  , m_entry(entry)
16  , m_previewType(type)
17 {
18 }
19 
20 void ImageLoader::start()
21 {
22  QUrl url(m_entry.previewUrl(m_previewType));
23  if (!url.isEmpty()) {
24  m_job = HTTPJob::get(url, NoReload, JobFlag::HideProgressInfo, this);
25  connect(m_job, &KJob::result, this, &ImageLoader::slotDownload);
26  connect(m_job, &HTTPJob::data, this, &ImageLoader::slotData);
27  } else {
28  Q_EMIT signalError(m_entry, m_previewType, QStringLiteral("Empty url"));
29  deleteLater();
30  }
31 }
32 
33 KJob *ImageLoader::job()
34 {
35  return m_job;
36 }
37 
38 void ImageLoader::slotData(KJob *job, const QByteArray &buf)
39 {
40  Q_UNUSED(job)
41  m_buffer.append(buf);
42 }
43 
44 void ImageLoader::slotDownload(KJob *job)
45 {
46  if (job->error()) {
47  m_buffer.clear();
48  Q_EMIT signalError(m_entry, m_previewType, job->errorText());
49  deleteLater();
50  return;
51  }
52  QImage image;
53  image.loadFromData(std::move(m_buffer));
54 
55  if (m_previewType == EntryInternal::PreviewSmall1
56  || m_previewType == EntryInternal::PreviewSmall2
57  || m_previewType == EntryInternal::PreviewSmall3) {
58  if (image.width() > PreviewWidth || image.height() > PreviewHeight) {
59  // if the preview is really big, first scale fast to a smaller size, then smooth to desired size
60  if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) {
61  image = image.scaled(2 * PreviewWidth, 2 * PreviewHeight, Qt::KeepAspectRatio, Qt::FastTransformation);
62  }
63  image = image.scaled(PreviewWidth, PreviewHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
64  } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) {
65  // upscale tiny previews to double size
66  image = image.scaled(2 * image.width(), 2 * image.height());
67  }
68  }
69 
70  m_entry.setPreviewImage(image, m_previewType);
71  Q_EMIT signalPreviewLoaded(m_entry, m_previewType);
72  deleteLater();
73 }
74 
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:49
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-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.