introduce wallpaper image rendering into libplasma so it can be shared amongst all wallpaper plugins; most of the plugins are duplicating this code which can't be good. :)

next up is to port the Image wallpaper plugin to this and make sure it all still works and then look at bringing the wallpaper listing model/view somewhere all the plugins can share it for their configuration needs

svn path=/trunk/KDE/kdelibs/; revision=948064
This commit is contained in:
Aaron J. Seigo 2009-04-02 06:31:37 +00:00
parent e309bf2da3
commit 1c62ca5b79
5 changed files with 262 additions and 59 deletions

View File

@ -84,6 +84,7 @@ set(plasma_LIB_SRCS
private/style.cpp
private/toolbox.cpp
private/tooltip.cpp
private/wallpaperrenderthread.cpp
private/windowpreview.cpp
querymatch.cpp
runnercontext.cpp

View File

@ -1,28 +1,43 @@
/*
Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
* Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
* Copyright (c) 2009 Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include "renderthread.h"
#include "plasma/private/wallpaperrenderthread_p.h"
#include <QPainter>
#include <QFile>
#include <KDebug>
#include <KSvgRenderer>
#include <QSvgRenderer>
RenderThread::RenderThread()
: m_current_token(-1)
, m_size(0, 0)
#include <kdebug.h>
namespace Plasma
{
WallpaperRenderThread::WallpaperRenderThread(QObject *parent)
: QThread(parent),
m_currentToken(-1)
{
m_abort = false;
m_restart = false;
}
RenderThread::~RenderThread()
WallpaperRenderThread::~WallpaperRenderThread()
{
{
// abort computation
@ -34,15 +49,16 @@ RenderThread::~RenderThread()
wait();
}
void RenderThread::setSize(const QSize& size)
void WallpaperRenderThread::setSize(const QSize& size)
{
QMutexLocker lock(&m_mutex);
m_size = size;
}
int RenderThread::render(const QString &file,
const QColor &color,
Background::ResizeMethod method)
int WallpaperRenderThread::render(const QString &file,
const QSize &size,
Wallpaper::ResizeMethod method,
const QColor &color)
{
int token;
{
@ -50,8 +66,9 @@ int RenderThread::render(const QString &file,
m_file = file;
m_color = color;
m_method = method;
m_size = size;
m_restart = true;
token = ++m_current_token;
token = ++m_currentToken;
}
if (!isRunning()) {
@ -63,13 +80,13 @@ int RenderThread::render(const QString &file,
return token;
}
void RenderThread::run()
void WallpaperRenderThread::run()
{
QString file;
QColor color;
QSize size;
qreal ratio;
Background::ResizeMethod method;
Wallpaper::ResizeMethod method;
int token;
forever {
@ -87,7 +104,7 @@ void RenderThread::run()
m_restart = false;
// load all parameters in nonshared variables
token = m_current_token;
token = m_currentToken;
file = m_file;
color = m_color;
size = m_size;
@ -99,7 +116,7 @@ void RenderThread::run()
result.fill(color.rgba());
if (file.isEmpty() || !QFile::exists(file)) {
emit done(token, result);
emit done(token, result, file, size, method, color);
continue;
}
@ -137,11 +154,11 @@ void RenderThread::run()
// set render parameters according to resize mode
switch (method)
{
case Background::Scale:
case Wallpaper::ScaledResize:
imgSize *= ratio;
scaledSize = size;
break;
case Background::Center:
case Wallpaper::CenteredResize:
scaledSize = imgSize;
pos = QPoint((size.width() - scaledSize.width()) / 2,
(size.height() - scaledSize.height()) / 2);
@ -162,7 +179,7 @@ void RenderThread::run()
}
break;
case Background::Maxpect: {
case Wallpaper::MaxpectResize: {
imgSize *= ratio;
float xratio = (float) size.width() / imgSize.width();
float yratio = (float) size.height() / imgSize.height();
@ -179,7 +196,7 @@ void RenderThread::run()
(size.height() - scaledSize.height()) / 2);
break;
}
case Background::ScaleCrop: {
case Wallpaper::ScaledAndCroppedResize: {
imgSize *= ratio;
float xratio = (float) size.width() / imgSize.width();
float yratio = (float) size.height() / imgSize.height();
@ -196,11 +213,11 @@ void RenderThread::run()
(size.height() - scaledSize.height()) / 2);
break;
}
case Background::Tiled:
case Wallpaper::TiledResize:
scaledSize = imgSize;
tiled = true;
break;
case Background::CenterTiled:
case Wallpaper::CenterTiledResize:
scaledSize = imgSize;
pos = QPoint(
-scaledSize.width() +
@ -215,7 +232,7 @@ void RenderThread::run()
kDebug() << token << scalable << scaledSize << imgSize;
if (scalable) {
// tiling is ignored for scalable wallpapers
KSvgRenderer svg(file);
QSvgRenderer svg(file);
if (m_restart) {
continue;
}
@ -244,7 +261,12 @@ void RenderThread::run()
}
// signal we're done
emit done(token, result);
emit done(token, result, file, size, method, color);
endLoop: continue;
}
}
} // namespace Plasma
#include "wallpaperrenderthread_p.moc"

View File

@ -1,36 +1,55 @@
/*
Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
* Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
* Copyright (c) 2009 Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#ifndef PLASMA_WALLPAPERRENDERTHREAD_H
#define PLASMA_WALLPAPERRENDERTHREAD_H
#ifndef RENDERTHREAD_H
#define RENDERTHREAD_H
#include "backgroundpackage.h"
#include <QColor>
#include <QImage>
#include <QMutex>
#include <QThread>
#include <QWaitCondition>
class RenderThread : public QThread
{
Q_OBJECT
public:
RenderThread();
virtual ~RenderThread();
#include "plasma/wallpaper.h"
int render(const QString &file,
const QColor &color,
Background::ResizeMethod method);
namespace Plasma
{
class WallpaperRenderThread : public QThread
{
Q_OBJECT
public:
WallpaperRenderThread(QObject *parent = 0);
virtual ~WallpaperRenderThread();
int render(const QString &file, const QSize &size,
Wallpaper::ResizeMethod, const QColor &color);
void setSize(const QSize &size);
void setRatio(float ratio);
Q_SIGNALS:
void done(int token, const QImage &pixmap,
const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color);
protected:
virtual void run();
@ -39,18 +58,16 @@ private:
QWaitCondition m_condition;
// protected by mutex
int m_current_token;
int m_currentToken;
QString m_file;
QColor m_color;
QSize m_size;
float m_ratio;
Background::ResizeMethod m_method;
Wallpaper::ResizeMethod m_method;
bool m_abort;
bool m_restart;
signals:
void done(int token, const QImage &pixmap);
};
} // namespace Plasma
#endif // RENDERTHREAD_H

View File

@ -20,12 +20,20 @@
#include "wallpaper.h"
#include <kservicetypetrader.h>
#include <QColor>
#include <QFile>
#include <QImage>
#include <kdebug.h>
#include <kglobal.h>
#include <kservicetypetrader.h>
#include <kstandarddirs.h>
#include <version.h>
#include "plasma/private/dataengineconsumer_p.h"
#include "plasma/private/packages_p.h"
#include "plasma/private/wallpaperrenderthread_p.h"
namespace Plasma
{
@ -36,23 +44,41 @@ public:
WallpaperPrivate(KService::Ptr service, Wallpaper *wallpaper) :
q(wallpaper),
wallpaperDescription(service),
renderColor(0, 0, 0),
renderToken(-1),
renderResizeMethod(Wallpaper::ScaledResize),
cacheRendering(false),
initialized(false),
needsConfig(false)
{
};
~WallpaperPrivate()
{
};
QString cachePath(const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color) const;
void renderComplete(int token, const QImage &image,
const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color);
static WallpaperRenderThread s_renderer;
static PackageStructure::Ptr packageStructure;
Wallpaper *q;
KPluginInfo wallpaperDescription;
QRectF boundingRect;
KServiceAction mode;
QColor renderColor;
int renderToken;
Wallpaper::ResizeMethod renderResizeMethod;
bool cacheRendering : 1;
bool initialized : 1;
bool needsConfig : 1;
};
WallpaperRenderThread WallpaperPrivate::s_renderer;
PackageStructure::Ptr WallpaperPrivate::packageStructure(0);
Wallpaper::Wallpaper(QObject *parentObject, const QVariantList &args)
: d(new WallpaperPrivate(KService::serviceByStorageId(args.count() > 0 ?
args[0].toString() : QString()), this))
@ -65,6 +91,9 @@ Wallpaper::Wallpaper(QObject *parentObject, const QVariantList &args)
mutableArgs.removeFirst();
}
setParent(parentObject);
connect(&WallpaperPrivate::s_renderer, SIGNAL(done(int,QImage,QString,QSize,Wallpaper::ResizeMethod,QColor)),
this, SLOT(renderComplete(int,QImage,QString,QSize,Wallpaper::ResizeMethod,QColor)));
}
Wallpaper::~Wallpaper()
@ -125,6 +154,16 @@ Wallpaper *Wallpaper::load(const KPluginInfo &info, const QVariantList &args)
return load(info.pluginName(), args);
}
PackageStructure::Ptr Wallpaper::packageStructure()
{
if (!WallpaperPrivate::packageStructure) {
WallpaperPrivate::packageStructure = new WallpaperPackage();
}
return WallpaperPrivate::packageStructure;
}
QString Wallpaper::name() const
{
if (!d->wallpaperDescription.isValid()) {
@ -266,6 +305,60 @@ void Wallpaper::setConfigurationRequired(bool needsConfig, const QString &reason
emit configurationRequired(needsConfig);
}
bool Wallpaper::setUseDiskCache() const
{
return d->cacheRendering;
}
void Wallpaper::setUseDiskCache(bool useCache)
{
d->cacheRendering = useCache;
}
void Wallpaper::render(const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color)
{
if (sourceImagePath.isEmpty() || !QFile::exists(sourceImagePath)) {
return;
}
if (d->cacheRendering) {
QString cache = d->cachePath(sourceImagePath, size, resizeMethod, color);
if (QFile::exists(cache)) {
kDebug() << "loading cached wallpaper from" << cache;
QImage img(cache);
emit renderComplete(img);
return;
}
}
d->renderToken = WallpaperPrivate::s_renderer.render(sourceImagePath, size, resizeMethod, color);
}
QString WallpaperPrivate::cachePath(const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color) const
{
const QString id = QString("plasma-wallpapers/%5_%3_%4_%1x%2.png")
.arg(size.width()).arg(size.height()).arg(color.name())
.arg(resizeMethod).arg(sourceImagePath);
return KGlobal::dirs()->locateLocal("cache", id);
}
void WallpaperPrivate::renderComplete(int token, const QImage &image,
const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color)
{
if (token != renderToken) {
return;
}
if (cacheRendering) {
image.save(cachePath(sourceImagePath, size, resizeMethod, color));
}
emit q->renderComplete(image);
}
} // Plasma namespace
#include "wallpaper.moc"

View File

@ -24,6 +24,7 @@
#include <kplugininfo.h>
#include <plasma/plasma.h>
#include <plasma/packagestructure.h>
#include <plasma/version.h>
namespace Plasma
@ -61,6 +62,19 @@ class PLASMA_EXPORT Wallpaper : public QObject
Q_PROPERTY(QList<KServiceAction> listRenderingModes READ listRenderingModes)
public:
/**
* Various resize modes supported by the built in image renderer
*/
enum ResizeMethod {
ScaledResize /**< Scales the image to fit the full area*/,
CenteredResize /**< Centers the image within the area */,
ScaledAndCroppedResize /**< Scales and crops the image, preserving the aspect ratio */,
TiledResize /**< Tiles the image to fill the area */,
CenterTiledResize /**< Tiles the image to fill the area, starting with a centered tile */,
MaxpectResize /**< Best fit resize */
};
Q_ENUMS(ResizeMethod)
~Wallpaper();
/**
@ -96,6 +110,11 @@ class PLASMA_EXPORT Wallpaper : public QObject
**/
static Wallpaper *load(const KPluginInfo &info, const QVariantList &args = QVariantList());
/**
* Returns the Package specialization for wallpapers
*/
static PackageStructure::Ptr packageStructure();
/**
* Returns the user-visible name for the wallpaper, as specified in the
* .desktop file.
@ -230,9 +249,16 @@ class PLASMA_EXPORT Wallpaper : public QObject
/**
* @return true if the wallpaper currently needs to be configured,
* otherwise, false
* @since 4.3
*/
bool configurationRequired() const;
/**
* @return true if disk caching is turned on.
* @since 4.3
*/
bool setUseDiskCache() const;
Q_SIGNALS:
/**
* This signal indicates that wallpaper needs to be repainted.
@ -241,21 +267,30 @@ class PLASMA_EXPORT Wallpaper : public QObject
/**
* Emitted when the user wants to configure/change the wallpaper.
* @since 4.3
*/
void configureRequested();
/**
* Emitted when the state of the wallpaper requiring configuration
* changes.
* @since 4.3
*/
void configurationRequired(bool needsConfig);
/**
* Emitted when the configuration of the wallpaper needs to be saved
* to disk.
* @since 4.3
*/
void configNeedsSaving();
/**
* Emitted when a wallpaper image render is completed.
* @since 4.3
*/
void renderComplete(const QImage &image);
protected:
/**
* This constructor is to be used with the plugin loading systems
@ -285,10 +320,45 @@ class PLASMA_EXPORT Wallpaper : public QObject
* @param reason a translated message for the user explaining that the
* applet needs configuring; this should note what needs
* to be configured
* @since 4.3
*/
void setConfigurationRequired(bool needsConfiguring, const QString &reason = QString());
/**
* Renders the wallpaper asyncronously with the given parameters. When the rendering is
* complete, the renderComplete signal is emitted.
*
* @param sourceImagePath the path to the image file on disk. Common image formats such as
* PNG, JPEG and SVG are supported
* @param size the size to render the image as
* @param resizeMethod the method to use when resizing the image to fit size, @see
* ResizeMethod
* @param color the color to use to pad the rendered image if it doesn't take up the
* entire size with the given ResizeMethod
* @since 4.3
*/
void render(const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod = ScaledResize,
const QColor &color = QColor(0, 0, 0));
/**
* Sets whether or not to cache on disk the results of calls to render. If the wallpaper
* changes often or is innexpensive to render, then it's probably best not to cache them.
*
* The default is not to cache.
*
* @see render
* @param useCache true to cache rendered papers on disk, false not to cache
* @since 4.3
*/
void setUseDiskCache(bool useCache);
private:
Q_PRIVATE_SLOT(d, void renderComplete(int token, const QImage &image,
const QString &sourceImagePath, const QSize &size,
Wallpaper::ResizeMethod resizeMethod, const QColor &color))
friend class WallpaperPrivate;
WallpaperPrivate *const d;
};