2010-04-26 22:46:36 +02:00
|
|
|
/*
|
2010-04-28 19:51:46 +02:00
|
|
|
* Copyright 2010 Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2010 Adenilson Cavalcanti <cavalcantii@gmail.com>
|
2010-04-26 22:37:43 +02:00
|
|
|
*
|
2010-04-26 22:46:36 +02:00
|
|
|
* 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.
|
2010-04-26 22:37:43 +02:00
|
|
|
*
|
2010-04-26 22:46:36 +02:00
|
|
|
* 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
|
2010-04-26 22:37:43 +02:00
|
|
|
*
|
2010-04-26 22:46:36 +02:00
|
|
|
* 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.
|
2010-04-26 22:37:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
*
|
|
|
|
* - cleanup debug messages
|
|
|
|
*/
|
|
|
|
|
2010-04-26 23:00:21 +02:00
|
|
|
#include "animationscriptengine_p.h"
|
2010-04-26 22:37:43 +02:00
|
|
|
|
2010-04-28 19:51:46 +02:00
|
|
|
#include <QFile>
|
2010-04-30 01:01:48 +02:00
|
|
|
#include <QMetaEnum>
|
2010-04-30 00:29:26 +02:00
|
|
|
#include <QParallelAnimationGroup>
|
2010-04-30 01:01:48 +02:00
|
|
|
#include <QPauseAnimation>
|
2010-04-30 00:29:26 +02:00
|
|
|
#include <QSequentialAnimationGroup>
|
2010-04-28 19:51:46 +02:00
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
#include <kdebug.h>
|
2012-03-18 17:45:41 +01:00
|
|
|
#include <klocalizedstring.h>
|
2010-04-28 19:51:46 +02:00
|
|
|
|
2010-04-30 01:01:48 +02:00
|
|
|
#include "animator.h"
|
|
|
|
#include "javascriptanimation_p.h"
|
2010-04-30 00:29:26 +02:00
|
|
|
#include "bindings/animationgroup_p.h"
|
|
|
|
|
2010-04-26 22:56:15 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2010-04-26 22:37:43 +02:00
|
|
|
|
2010-04-30 02:13:44 +02:00
|
|
|
QScriptValue constructEasingCurveClass(QScriptEngine *engine);
|
|
|
|
|
2010-04-26 22:56:15 +02:00
|
|
|
namespace AnimationScriptEngine
|
|
|
|
{
|
|
|
|
|
|
|
|
QScriptEngine* inst = 0;
|
|
|
|
QHash<QString, QScriptValue> s_animFuncs;
|
2010-06-15 20:02:44 +02:00
|
|
|
QSet<QString> s_animFailures;
|
2010-05-13 02:52:36 +02:00
|
|
|
QString s_prefix;
|
2010-04-26 22:56:15 +02:00
|
|
|
|
|
|
|
QScriptValue animation(const QString &anim)
|
2010-04-26 22:37:43 +02:00
|
|
|
{
|
|
|
|
return s_animFuncs.value(anim);
|
|
|
|
}
|
|
|
|
|
2010-04-28 19:38:27 +02:00
|
|
|
bool isAnimationRegistered(const QString &anim)
|
|
|
|
{
|
|
|
|
return s_animFuncs.contains(anim);
|
|
|
|
}
|
|
|
|
|
2010-06-15 20:02:44 +02:00
|
|
|
void addToLoadFailures(const QString &anim)
|
|
|
|
{
|
|
|
|
s_animFailures.insert(anim);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool animationFailedToLoad(const QString &anim)
|
|
|
|
{
|
|
|
|
return s_animFailures.contains(anim);
|
|
|
|
}
|
|
|
|
|
2010-04-28 19:38:27 +02:00
|
|
|
void clearAnimations()
|
|
|
|
{
|
|
|
|
s_animFuncs.clear();
|
2010-06-15 20:02:44 +02:00
|
|
|
s_animFailures.clear();
|
2010-04-28 19:38:27 +02:00
|
|
|
delete inst;
|
|
|
|
inst = 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 22:56:15 +02:00
|
|
|
QScriptValue registerAnimation(QScriptContext *context, QScriptEngine *engine)
|
2010-04-26 22:37:43 +02:00
|
|
|
{
|
|
|
|
if (context->argumentCount() > 1) {
|
2010-05-13 02:52:36 +02:00
|
|
|
const QString name = s_prefix + context->argument(0).toString();
|
2010-04-28 19:38:27 +02:00
|
|
|
|
|
|
|
if (!s_animFuncs.contains(name)) {
|
|
|
|
const QScriptValue func = context->argument(1);
|
|
|
|
if (func.isFunction()) {
|
|
|
|
s_animFuncs.insert(name, func);
|
|
|
|
}
|
2010-04-26 22:37:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:29:26 +02:00
|
|
|
QObject *extractParent(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
return context->thisObject().property("__plasma_javascriptanimation").toQObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue animationGroup(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QObject *parent = extractParent(context, engine);
|
|
|
|
if (!parent) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSequentialAnimationGroup *group = new SequentialAnimationGroup(parent);
|
|
|
|
return engine->newQObject(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue parallelAnimationGroup(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QObject *parent = extractParent(context, engine);
|
|
|
|
if (!parent) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParallelAnimationGroup *group = new ParallelAnimationGroup(parent);
|
|
|
|
return engine->newQObject(group);
|
|
|
|
}
|
|
|
|
|
2010-04-30 01:01:48 +02:00
|
|
|
void registerEnums(QScriptValue &scriptValue, const QMetaObject &meta)
|
|
|
|
{
|
|
|
|
//manually create enum values. ugh
|
|
|
|
QScriptEngine *engine = scriptValue.engine();
|
|
|
|
for (int i = 0; i < meta.enumeratorCount(); ++i) {
|
|
|
|
QMetaEnum e = meta.enumerator(i);
|
|
|
|
//kDebug() << e.name();
|
|
|
|
for (int i=0; i < e.keyCount(); ++i) {
|
|
|
|
//kDebug() << e.key(i) << e.value(i);
|
|
|
|
scriptValue.setProperty(e.key(i), QScriptValue(engine, e.value(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue animation(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("animation() takes one argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *parent = extractParent(context, engine);
|
|
|
|
QAbstractAnimation *anim = 0;
|
|
|
|
if (context->argument(0).isString()) {
|
|
|
|
const QString animName = context->argument(0).toString();
|
|
|
|
anim = Plasma::Animator::create(animName, parent);
|
|
|
|
} else {
|
|
|
|
int animId = context->argument(0).toInt32();
|
|
|
|
if (animId == JavascriptAnimation::PauseAnimation) {
|
|
|
|
anim = new QPauseAnimation(parent);
|
|
|
|
} else if (animId == JavascriptAnimation::PropertyAnimation) {
|
|
|
|
anim = new QPropertyAnimation(parent);
|
|
|
|
} else {
|
|
|
|
anim = Plasma::Animator::create(static_cast<Animator::Animation>(animId), parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anim) {
|
|
|
|
QScriptValue value = engine->newQObject(anim);
|
|
|
|
registerEnums(value, *anim->metaObject());
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context->throwError(i18n("%1 is not a known animation type", context->argument(0).isString()));
|
|
|
|
}
|
|
|
|
|
2010-04-26 22:56:15 +02:00
|
|
|
QScriptEngine *globalEngine()
|
2010-04-26 22:37:43 +02:00
|
|
|
{
|
|
|
|
if (!inst) {
|
|
|
|
inst = new QScriptEngine;
|
|
|
|
QScriptValue global = inst->globalObject();
|
2010-04-26 22:56:15 +02:00
|
|
|
global.setProperty("registerAnimation", inst->newFunction(AnimationScriptEngine::registerAnimation));
|
2010-04-30 00:29:26 +02:00
|
|
|
global.setProperty("AnimationGroup", inst->newFunction(AnimationScriptEngine::animationGroup));
|
|
|
|
global.setProperty("ParallelAnimationGroup", inst->newFunction(AnimationScriptEngine::parallelAnimationGroup));
|
2010-04-30 02:13:44 +02:00
|
|
|
global.setProperty("QEasingCurve", constructEasingCurveClass(inst));
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2010-04-30 02:13:44 +02:00
|
|
|
kDebug() << "........... first js animation, creating the engine!";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2010-04-26 22:37:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2010-05-13 02:52:36 +02:00
|
|
|
bool loadScript(const QString &path, const QString &prefix)
|
2010-04-28 19:51:46 +02:00
|
|
|
{
|
|
|
|
QFile file(path);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
kError() << "failed to open script file" << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream buffer(&file);
|
|
|
|
QString tmp(buffer.readAll());
|
|
|
|
|
|
|
|
QScriptEngine *engine = AnimationScriptEngine::globalEngine();
|
2010-08-06 03:26:28 +02:00
|
|
|
engine->pushContext();
|
2010-05-13 02:52:36 +02:00
|
|
|
s_prefix = prefix;
|
2010-04-28 19:51:46 +02:00
|
|
|
QScriptValue def(engine->evaluate(tmp, path));
|
2010-05-13 02:52:36 +02:00
|
|
|
s_prefix.clear();
|
2010-08-06 03:26:28 +02:00
|
|
|
engine->popContext();
|
2010-04-28 19:51:46 +02:00
|
|
|
if (engine->hasUncaughtException()) {
|
|
|
|
const QScriptValue error = engine->uncaughtException();
|
|
|
|
QString file = error.property("fileName").toString();
|
|
|
|
const QString failureMsg = QString("Error in %1 on line %2.\n%3")
|
|
|
|
.arg(file)
|
|
|
|
.arg(error.property("lineNumber").toString())
|
|
|
|
.arg(error.toString());
|
|
|
|
kError() << failureMsg;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-26 22:56:15 +02:00
|
|
|
} // namespace AnimationEngine
|
|
|
|
} // namespace Plasma
|
|
|
|
|