From 3f7d21e434d427a470f91ebaca885787b670bad1 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Wed, 24 Jun 2020 18:00:48 +0300 Subject: [PATCH] Clean up some duplicated code --- src/declarative/dynamicwallpaperglobals.h | 20 +++++++++ .../dynamicwallpaperimageprovider.cpp | 26 +----------- .../dynamicwallpaperpreviewjob.cpp | 42 ++++--------------- 3 files changed, 30 insertions(+), 58 deletions(-) create mode 100644 src/declarative/dynamicwallpaperglobals.h diff --git a/src/declarative/dynamicwallpaperglobals.h b/src/declarative/dynamicwallpaperglobals.h new file mode 100644 index 0000000..0215377 --- /dev/null +++ b/src/declarative/dynamicwallpaperglobals.h @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2020 Vlad Zahorodnii + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +class DynamicWallpaperImageAsyncResult +{ +public: + DynamicWallpaperImageAsyncResult() {} + explicit DynamicWallpaperImageAsyncResult(const QImage &image) : image(image) {} + explicit DynamicWallpaperImageAsyncResult(const QString &text) : errorString(text) {} + + QImage image; + QString errorString; +}; diff --git a/src/declarative/dynamicwallpaperimageprovider.cpp b/src/declarative/dynamicwallpaperimageprovider.cpp index 85183e8..ebfadb0 100644 --- a/src/declarative/dynamicwallpaperimageprovider.cpp +++ b/src/declarative/dynamicwallpaperimageprovider.cpp @@ -5,6 +5,7 @@ */ #include "dynamicwallpaperimageprovider.h" +#include "dynamicwallpaperglobals.h" #include "dynamicwallpaperimagehandle.h" #include @@ -12,31 +13,6 @@ #include #include -class DynamicWallpaperImageAsyncResult -{ -public: - DynamicWallpaperImageAsyncResult(); - explicit DynamicWallpaperImageAsyncResult(const QImage &image); - explicit DynamicWallpaperImageAsyncResult(const QString &text); - - QImage image; - QString errorString; -}; - -DynamicWallpaperImageAsyncResult::DynamicWallpaperImageAsyncResult() -{ -} - -DynamicWallpaperImageAsyncResult::DynamicWallpaperImageAsyncResult(const QImage &image) - : image(image) -{ -} - -DynamicWallpaperImageAsyncResult::DynamicWallpaperImageAsyncResult(const QString &text) - : errorString(text) -{ -} - static DynamicWallpaperImageAsyncResult load(const QString &fileName, int index, const QSize &requestedSize) { const KDynamicWallpaperReader reader(fileName); diff --git a/src/declarative/dynamicwallpaperpreviewjob.cpp b/src/declarative/dynamicwallpaperpreviewjob.cpp index 9e9ed75..541b9f9 100644 --- a/src/declarative/dynamicwallpaperpreviewjob.cpp +++ b/src/declarative/dynamicwallpaperpreviewjob.cpp @@ -5,6 +5,7 @@ */ #include "dynamicwallpaperpreviewjob.h" +#include "dynamicwallpaperglobals.h" #include "dynamicwallpaperpreviewcache.h" #include @@ -31,35 +32,10 @@ * be destroyed automatically. */ -class DynamicWallpaperPreviewAsyncResponse -{ -public: - DynamicWallpaperPreviewAsyncResponse(); - explicit DynamicWallpaperPreviewAsyncResponse(const QImage &image); - explicit DynamicWallpaperPreviewAsyncResponse(const QString &text); - - QImage image; - QString errorString; -}; - -DynamicWallpaperPreviewAsyncResponse::DynamicWallpaperPreviewAsyncResponse() -{ -} - -DynamicWallpaperPreviewAsyncResponse::DynamicWallpaperPreviewAsyncResponse(const QImage &image) - : image(image) -{ -} - -DynamicWallpaperPreviewAsyncResponse::DynamicWallpaperPreviewAsyncResponse(const QString &text) - : errorString(text) -{ -} - class DynamicWallpaperPreviewJobPrivate { public: - QFutureWatcher *watcher; + QFutureWatcher *watcher; }; static QRgb blend(QRgb a, QRgb b, qreal blendFactor) @@ -134,7 +110,7 @@ static bool score_compare(const KDynamicWallpaperMetaData &a, const KDynamicWall * * Note that this function runs off the main thread. */ -static DynamicWallpaperPreviewAsyncResponse makePreview(const QString &fileName, const QSize &size) +static DynamicWallpaperImageAsyncResult makePreview(const QString &fileName, const QSize &size) { QImage preview = DynamicWallpaperPreviewCache::load(fileName); @@ -142,9 +118,9 @@ static DynamicWallpaperPreviewAsyncResponse makePreview(const QString &fileName, // The cache has no preview for the specified wallpaper yet, so generate one... KDynamicWallpaperReader reader(fileName); if (reader.error() != KDynamicWallpaperReader::NoError) - return DynamicWallpaperPreviewAsyncResponse(reader.errorString()); + return DynamicWallpaperImageAsyncResult(reader.errorString()); if (reader.imageCount() < 2) - return DynamicWallpaperPreviewAsyncResponse(i18n("Not enough images")); + return DynamicWallpaperImageAsyncResult(i18n("Not enough images")); QVector metadata; for (int i = 0; i < reader.imageCount(); ++i) @@ -161,7 +137,7 @@ static DynamicWallpaperPreviewAsyncResponse makePreview(const QString &fileName, DynamicWallpaperPreviewCache::store(preview, fileName); } - return DynamicWallpaperPreviewAsyncResponse(preview.scaled(size, Qt::KeepAspectRatio)); + return DynamicWallpaperImageAsyncResult(preview.scaled(size, Qt::KeepAspectRatio)); } /*! @@ -170,10 +146,10 @@ static DynamicWallpaperPreviewAsyncResponse makePreview(const QString &fileName, DynamicWallpaperPreviewJob::DynamicWallpaperPreviewJob(const QString &fileName, const QSize &requestedSize) : d(new DynamicWallpaperPreviewJobPrivate) { - d->watcher = new QFutureWatcher(this); + d->watcher = new QFutureWatcher(this); d->watcher->setFuture(QtConcurrent::run(makePreview, fileName, requestedSize)); - connect(d->watcher, &QFutureWatcher::finished, + connect(d->watcher, &QFutureWatcher::finished, this, &DynamicWallpaperPreviewJob::handleFinished); } @@ -205,7 +181,7 @@ DynamicWallpaperPreviewJob::~DynamicWallpaperPreviewJob() void DynamicWallpaperPreviewJob::handleFinished() { - const DynamicWallpaperPreviewAsyncResponse response = d->watcher->result(); + const DynamicWallpaperImageAsyncResult response = d->watcher->result(); if (response.errorString.isNull()) emit finished(response.image); else