KNewStuff

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

KDE's Doxygen guidelines are available online.