access to other animations

svn path=/trunk/KDE/kdelibs/; revision=1120776
This commit is contained in:
Aaron J. Seigo 2010-04-29 23:01:48 +00:00
parent a06b2bf70d
commit 6c44a1e8a4
3 changed files with 73 additions and 2 deletions

View File

@ -26,12 +26,17 @@
#include "animationscriptengine_p.h" #include "animationscriptengine_p.h"
#include <QFile> #include <QFile>
#include <QMetaEnum>
#include <QParallelAnimationGroup> #include <QParallelAnimationGroup>
#include <QPauseAnimation>
#include <QSequentialAnimationGroup> #include <QSequentialAnimationGroup>
#include <QTextStream> #include <QTextStream>
#include <kdebug.h> #include <kdebug.h>
#include <klocale.h>
#include "animator.h"
#include "javascriptanimation_p.h"
#include "bindings/animationgroup_p.h" #include "bindings/animationgroup_p.h"
namespace Plasma namespace Plasma
@ -104,6 +109,51 @@ QScriptValue parallelAnimationGroup(QScriptContext *context, QScriptEngine *engi
return engine->newQObject(group); return engine->newQObject(group);
} }
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()));
}
QScriptEngine *globalEngine() QScriptEngine *globalEngine()
{ {
if (!inst) { if (!inst) {

View File

@ -27,6 +27,8 @@
* - support more properties: angle, direction, etc * - support more properties: angle, direction, etc
* - support calling a 'resetAnimation' in js class when animation is stopped * - support calling a 'resetAnimation' in js class when animation is stopped
*/ */
#define ADD_ENUM_VALUE(__c__, __ns__, __v__) \
__c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__))
namespace Plasma namespace Plasma
{ {
@ -58,6 +60,21 @@ void JavascriptAnimation::updateState(QAbstractAnimation::State newState, QAbstr
kDebug() << "trying for" << m_name << m_instance.isFunction(); kDebug() << "trying for" << m_name << m_instance.isFunction();
m_instance.setProperty("__plasma_javascriptanimation", engine->newQObject(this), m_instance.setProperty("__plasma_javascriptanimation", engine->newQObject(this),
QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, FadeAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, AppearAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, DisappearAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, ActivateAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, FadeAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, GrowAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, PulseAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, RotationAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, RotationStackedAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, SlideAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, GeometryAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, ZoomAnimation);
ADD_ENUM_VALUE(m_instance, Plasma::Animator, PixmapTransitionAnimation);
ADD_ENUM_VALUE(m_instance, JavascriptAnimation, PauseAnimation);
ADD_ENUM_VALUE(m_instance, JavascriptAnimation, PropertyAnimation);
//Get the method of the object //Get the method of the object
m_method = m_instance.property(QString("updateCurrentTime")); m_method = m_instance.property(QString("updateCurrentTime"));

View File

@ -22,8 +22,9 @@
#include <QScriptValue> #include <QScriptValue>
#include <plasma/animations/animation.h> #include "animation.h"
#include <plasma/plasma_export.h> #include "plasma_export.h"
#include "animator.h"
class QString; class QString;
class QScriptEngine; class QScriptEngine;
@ -38,6 +39,9 @@ class JavascriptAnimation: public Animation
Q_OBJECT Q_OBJECT
public: public:
enum { PauseAnimation = Animator::LastAnimation + 1,
PropertyAnimation = Animator::LastAnimation + 2
};
explicit JavascriptAnimation(const QString &name, QObject *parent = 0); explicit JavascriptAnimation(const QString &name, QObject *parent = 0);