Verified Commit cca481b7 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

wallpapers/image: Add custom image provider for KPackage wallpapers

The url format is image://package/get?dir=<DIR>

DIR is package path.
parent 31535402
......@@ -5,14 +5,24 @@
*/
#include "imageplugin.h"
#include "imagebackend.h"
#include <QQmlContext>
#include "imagebackend.h"
#include "provider/packageimageprovider.h"
#include "sortingmode.h"
const auto pluginName = QByteArrayLiteral("org.kde.plasma.wallpapers.image");
void ImagePlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
Q_ASSERT(uri == pluginName);
engine->addImageProvider(QStringLiteral("package"), new PackageImageProvider);
}
void ImagePlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("org.kde.plasma.wallpapers.image"));
Q_ASSERT(uri == pluginName);
qmlRegisterType<ImageBackend>(uri, 2, 0, "ImageBackend");
qmlRegisterAnonymousType<QAbstractItemModel>("QAbstractItemModel", 1);
......
......@@ -15,5 +15,6 @@ class ImagePlugin : public QQmlExtensionPlugin
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void initializeEngine(QQmlEngine *engine, const char *uri) override;
void registerTypes(const char *uri) override;
};
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "packageimageprovider.h"
#include <QImageReader>
#include <QUrlQuery>
#include <KPackage/PackageLoader>
#include "finder/packagefinder.h"
class AsyncPackageImageResponseRunnable : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit AsyncPackageImageResponseRunnable(const QString &path, const QSize &requestedSize);
/**
* Read the image and resize it if the requested size is valid.
*/
void run() override;
Q_SIGNALS:
void done(const QImage &image);
private:
QString m_path;
QSize m_requestedSize;
};
class AsyncPackageImageResponse : public QQuickImageResponse
{
Q_OBJECT
public:
explicit AsyncPackageImageResponse(const QString &path, const QSize &requestedSize, QThreadPool *pool);
QQuickTextureFactory *textureFactory() const override;
protected Q_SLOTS:
void slotHandleDone(const QImage &image);
protected:
QImage m_image;
};
AsyncPackageImageResponseRunnable::AsyncPackageImageResponseRunnable(const QString &path, const QSize &requestedSize)
: m_path(path)
, m_requestedSize(requestedSize)
{
}
void AsyncPackageImageResponseRunnable::run()
{
const QUrlQuery urlQuery(QUrl(QStringLiteral("image://package/%1").arg(m_path)));
const QString dir = urlQuery.queryItemValue(QStringLiteral("dir"));
if (dir.isEmpty()) {
Q_EMIT done(QImage());
return;
}
KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images"));
package.setPath(dir);
if (!package.isValid()) {
Q_EMIT done(QImage());
return;
}
PackageFinder::findPreferredImageInPackage(package, m_requestedSize);
QImage image(package.filePath("preferred"));
if (!image.isNull() && m_requestedSize.isValid()) {
image = image.scaled(m_requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
Q_EMIT done(image);
}
AsyncPackageImageResponse::AsyncPackageImageResponse(const QString &path, const QSize &requestedSize, QThreadPool *pool)
{
auto runnable = new AsyncPackageImageResponseRunnable(path, requestedSize);
connect(runnable, &AsyncPackageImageResponseRunnable::done, this, &AsyncPackageImageResponse::slotHandleDone);
pool->start(runnable);
}
void AsyncPackageImageResponse::slotHandleDone(const QImage &image)
{
m_image = image;
Q_EMIT finished();
}
QQuickTextureFactory *AsyncPackageImageResponse::textureFactory() const
{
return QQuickTextureFactory::textureFactoryForImage(m_image);
}
PackageImageProvider::PackageImageProvider()
{
}
QQuickImageResponse *PackageImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
{
AsyncPackageImageResponse *response = new AsyncPackageImageResponse(id, requestedSize, &m_pool);
return response;
}
#include "packageimageprovider.moc"
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef PACKAGEIMAGEPROVIDER_H
#define PACKAGEIMAGEPROVIDER_H
#include <QQuickAsyncImageProvider>
#include <QThreadPool>
/**
* Custom image provider for KPackage
*/
class PackageImageProvider : public QQuickAsyncImageProvider
{
public:
explicit PackageImageProvider();
QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
private:
QThreadPool m_pool;
};
#endif // PACKAGEIMAGEPROVIDER_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment