Skip to content

Commit

Permalink
Clean up some duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
zzag committed Jun 24, 2020
1 parent f648cb8 commit 3f7d21e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 58 deletions.
20 changes: 20 additions & 0 deletions src/declarative/dynamicwallpaperglobals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#pragma once

#include <QImage>

class DynamicWallpaperImageAsyncResult
{
public:
DynamicWallpaperImageAsyncResult() {}
explicit DynamicWallpaperImageAsyncResult(const QImage &image) : image(image) {}
explicit DynamicWallpaperImageAsyncResult(const QString &text) : errorString(text) {}

QImage image;
QString errorString;
};
26 changes: 1 addition & 25 deletions src/declarative/dynamicwallpaperimageprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,14 @@
*/

#include "dynamicwallpaperimageprovider.h"
#include "dynamicwallpaperglobals.h"
#include "dynamicwallpaperimagehandle.h"

#include <KDynamicWallpaperReader>

#include <QtConcurrent>
#include <QFutureWatcher>

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);
Expand Down
42 changes: 9 additions & 33 deletions src/declarative/dynamicwallpaperpreviewjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "dynamicwallpaperpreviewjob.h"
#include "dynamicwallpaperglobals.h"
#include "dynamicwallpaperpreviewcache.h"

#include <KDynamicWallpaperMetaData>
Expand All @@ -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<DynamicWallpaperPreviewAsyncResponse> *watcher;
QFutureWatcher<DynamicWallpaperImageAsyncResult> *watcher;
};

static QRgb blend(QRgb a, QRgb b, qreal blendFactor)
Expand Down Expand Up @@ -134,17 +110,17 @@ 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);

if (preview.isNull()) {
// 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<KDynamicWallpaperMetaData> metadata;
for (int i = 0; i < reader.imageCount(); ++i)
Expand All @@ -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));
}

/*!
Expand All @@ -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<DynamicWallpaperPreviewAsyncResponse>(this);
d->watcher = new QFutureWatcher<DynamicWallpaperImageAsyncResult>(this);
d->watcher->setFuture(QtConcurrent::run(makePreview, fileName, requestedSize));

connect(d->watcher, &QFutureWatcher<DynamicWallpaperPreviewAsyncResponse>::finished,
connect(d->watcher, &QFutureWatcher<DynamicWallpaperImageAsyncResult>::finished,
this, &DynamicWallpaperPreviewJob::handleFinished);
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f7d21e

Please sign in to comment.