2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "imagescaling.h"
|
2020-11-09 15:44:26 +00:00
|
|
|
#include "downloaddialog.h"
|
|
|
|
|
|
|
|
#include <QNetworkReply>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-03 13:43:26 +00:00
|
|
|
#include <qmath.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-03 13:43:26 +00:00
|
|
|
#include <functional>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
Images::Images(QWidget *parent) : QWidget(parent), downloadDialog(new DownloadDialog())
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2020-11-09 15:44:26 +00:00
|
|
|
setWindowTitle(tr("Image downloading and scaling example"));
|
2011-04-27 10:05:43 +00:00
|
|
|
resize(800, 600);
|
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
addUrlsButton = new QPushButton(tr("Add URLs"));
|
|
|
|
//! [1]
|
|
|
|
connect(addUrlsButton, &QPushButton::clicked, this, &Images::process);
|
|
|
|
//! [1]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
cancelButton = new QPushButton(tr("Cancel"));
|
|
|
|
cancelButton->setEnabled(false);
|
2020-11-09 15:44:26 +00:00
|
|
|
//! [2]
|
|
|
|
connect(cancelButton, &QPushButton::clicked, this, &Images::cancel);
|
|
|
|
//! [2]
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QHBoxLayout *buttonLayout = new QHBoxLayout();
|
2020-11-09 15:44:26 +00:00
|
|
|
buttonLayout->addWidget(addUrlsButton);
|
2011-04-27 10:05:43 +00:00
|
|
|
buttonLayout->addWidget(cancelButton);
|
|
|
|
buttonLayout->addStretch();
|
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
statusBar = new QStatusBar();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
imagesLayout = new QGridLayout();
|
|
|
|
|
|
|
|
mainLayout = new QVBoxLayout();
|
|
|
|
mainLayout->addLayout(buttonLayout);
|
|
|
|
mainLayout->addLayout(imagesLayout);
|
|
|
|
mainLayout->addStretch();
|
2020-11-09 15:44:26 +00:00
|
|
|
mainLayout->addWidget(statusBar);
|
2011-04-27 10:05:43 +00:00
|
|
|
setLayout(mainLayout);
|
|
|
|
}
|
|
|
|
|
|
|
|
Images::~Images()
|
|
|
|
{
|
2020-11-09 15:44:26 +00:00
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
void Images::process()
|
|
|
|
{
|
|
|
|
// Clean previous state
|
|
|
|
replies.clear();
|
|
|
|
|
|
|
|
if (downloadDialog->exec() == QDialog::Accepted) {
|
|
|
|
|
|
|
|
const auto urls = downloadDialog->getUrls();
|
|
|
|
if (urls.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cancelButton->setEnabled(true);
|
|
|
|
|
|
|
|
initLayout(urls.size());
|
|
|
|
|
|
|
|
downloadFuture = download(urls);
|
|
|
|
statusBar->showMessage(tr("Downloading..."));
|
|
|
|
//! [3]
|
|
|
|
|
|
|
|
//! [4]
|
|
|
|
downloadFuture.then([this](auto) { cancelButton->setEnabled(false); })
|
|
|
|
.then(QtFuture::Launch::Async,
|
|
|
|
[this] {
|
2020-12-08 16:42:12 +00:00
|
|
|
QMetaObject::invokeMethod(this,
|
|
|
|
[this] { updateStatus(tr("Scaling...")); });
|
2020-11-09 15:44:26 +00:00
|
|
|
return scaled();
|
|
|
|
})
|
|
|
|
//! [4]
|
|
|
|
//! [5]
|
2020-12-08 16:42:12 +00:00
|
|
|
.then(this, [this](const QList<QImage> &scaled) {
|
|
|
|
showImages(scaled);
|
2020-11-09 15:44:26 +00:00
|
|
|
updateStatus(tr("Finished"));
|
|
|
|
})
|
|
|
|
//! [5]
|
|
|
|
//! [6]
|
|
|
|
.onCanceled([this] { updateStatus(tr("Download has been canceled.")); })
|
|
|
|
.onFailed([this](QNetworkReply::NetworkError error) {
|
2020-12-08 16:42:12 +00:00
|
|
|
updateStatus(tr("Download finished with error: %1").arg(error));
|
2020-11-09 15:44:26 +00:00
|
|
|
|
|
|
|
// Abort all pending requests
|
2020-12-08 16:42:12 +00:00
|
|
|
abortDownload();
|
2020-11-09 15:44:26 +00:00
|
|
|
})
|
|
|
|
.onFailed([this](const std::exception& ex) {
|
|
|
|
updateStatus(tr(ex.what()));
|
|
|
|
});
|
|
|
|
//! [6]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [7]
|
|
|
|
void Images::cancel()
|
|
|
|
{
|
|
|
|
statusBar->showMessage(tr("Canceling..."));
|
|
|
|
|
|
|
|
downloadFuture.cancel();
|
|
|
|
abortDownload();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2020-11-09 15:44:26 +00:00
|
|
|
//! [7]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
//! [8]
|
|
|
|
QFuture<QByteArray> Images::download(const QList<QUrl> &urls)
|
|
|
|
//! [8]
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2020-11-09 15:44:26 +00:00
|
|
|
//! [9]
|
|
|
|
QSharedPointer<QPromise<QByteArray>> promise(new QPromise<QByteArray>());
|
|
|
|
promise->start();
|
|
|
|
//! [9]
|
|
|
|
|
|
|
|
//! [10]
|
|
|
|
for (auto url : urls) {
|
|
|
|
QSharedPointer<QNetworkReply> reply(qnam.get(QNetworkRequest(url)));
|
|
|
|
replies.push_back(reply);
|
|
|
|
//! [10]
|
|
|
|
|
|
|
|
//! [11]
|
|
|
|
QtFuture::connect(reply.get(), &QNetworkReply::finished).then([=] {
|
|
|
|
if (promise->isCanceled()) {
|
|
|
|
if (!promise->future().isFinished())
|
|
|
|
promise->finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
if (!promise->future().isFinished())
|
|
|
|
throw reply->error();
|
|
|
|
}
|
|
|
|
//! [12]
|
|
|
|
promise->addResult(reply->readAll());
|
|
|
|
|
|
|
|
// Report finished on the last download
|
|
|
|
if (promise->future().resultCount() == urls.size()) {
|
|
|
|
promise->finish();
|
|
|
|
}
|
|
|
|
//! [12]
|
|
|
|
}).onFailed([=] (QNetworkReply::NetworkError error) {
|
|
|
|
promise->setException(std::make_exception_ptr(error));
|
|
|
|
promise->finish();
|
|
|
|
}).onFailed([=] {
|
|
|
|
const auto ex = std::make_exception_ptr(
|
|
|
|
std::runtime_error("Unknown error occurred while downloading."));
|
|
|
|
promise->setException(ex);
|
|
|
|
promise->finish();
|
|
|
|
});
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2020-11-09 15:44:26 +00:00
|
|
|
//! [11]
|
|
|
|
|
|
|
|
//! [13]
|
|
|
|
return promise->future();
|
|
|
|
}
|
|
|
|
//! [13]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
//! [14]
|
|
|
|
QList<QImage> Images::scaled() const
|
|
|
|
{
|
|
|
|
QList<QImage> scaled;
|
|
|
|
const auto data = downloadFuture.results();
|
|
|
|
for (auto imgData : data) {
|
|
|
|
QImage image;
|
|
|
|
image.loadFromData(imgData);
|
|
|
|
if (image.isNull())
|
|
|
|
throw std::runtime_error("Failed to load image.");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
scaled.push_back(image.scaled(100, 100, Qt::KeepAspectRatio));
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
return scaled;
|
|
|
|
}
|
|
|
|
//! [14]
|
2017-08-03 13:43:26 +00:00
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
void Images::showImages(const QList<QImage> &images)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < images.size(); ++i) {
|
|
|
|
labels[i]->setAlignment(Qt::AlignCenter);
|
|
|
|
labels[i]->setPixmap(QPixmap::fromImage(images[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Images::initLayout(qsizetype count)
|
|
|
|
{
|
|
|
|
// Clean old images
|
|
|
|
QLayoutItem *child;
|
|
|
|
while ((child = imagesLayout->takeAt(0)) != nullptr) {
|
|
|
|
child->widget()->setParent(nullptr);
|
|
|
|
delete child;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
labels.clear();
|
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
// Init the images layout for the new images
|
|
|
|
const auto dim = int(qSqrt(qreal(count))) + 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int i = 0; i < dim; ++i) {
|
|
|
|
for (int j = 0; j < dim; ++j) {
|
|
|
|
QLabel *imageLabel = new QLabel;
|
2020-11-09 15:44:26 +00:00
|
|
|
imageLabel->setFixedSize(100, 100);
|
|
|
|
imagesLayout->addWidget(imageLabel, i, j);
|
2011-04-27 10:05:43 +00:00
|
|
|
labels.append(imageLabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
void Images::updateStatus(const QString &msg)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2020-12-08 16:42:12 +00:00
|
|
|
statusBar->showMessage(msg);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:44:26 +00:00
|
|
|
void Images::abortDownload()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2020-11-09 15:44:26 +00:00
|
|
|
for (auto reply : replies)
|
|
|
|
reply->abort();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|