From 6c44a1e8a4466e91303c7f81152d5c184516e584 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Thu, 29 Apr 2010 23:01:48 +0000 Subject: [PATCH] access to other animations svn path=/trunk/KDE/kdelibs/; revision=1120776 --- animations/animationscriptengine.cpp | 50 ++++++++++++++++++++++++++++ animations/javascriptanimation.cpp | 17 ++++++++++ animations/javascriptanimation_p.h | 8 +++-- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/animations/animationscriptengine.cpp b/animations/animationscriptengine.cpp index 66fc4296a..b8831386c 100644 --- a/animations/animationscriptengine.cpp +++ b/animations/animationscriptengine.cpp @@ -26,12 +26,17 @@ #include "animationscriptengine_p.h" #include +#include #include +#include #include #include #include +#include +#include "animator.h" +#include "javascriptanimation_p.h" #include "bindings/animationgroup_p.h" namespace Plasma @@ -104,6 +109,51 @@ QScriptValue parallelAnimationGroup(QScriptContext *context, QScriptEngine *engi 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(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() { if (!inst) { diff --git a/animations/javascriptanimation.cpp b/animations/javascriptanimation.cpp index 17f96dc4e..e9d69a647 100644 --- a/animations/javascriptanimation.cpp +++ b/animations/javascriptanimation.cpp @@ -27,6 +27,8 @@ * - support more properties: angle, direction, etc * - 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 { @@ -58,6 +60,21 @@ void JavascriptAnimation::updateState(QAbstractAnimation::State newState, QAbstr kDebug() << "trying for" << m_name << m_instance.isFunction(); m_instance.setProperty("__plasma_javascriptanimation", engine->newQObject(this), 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 m_method = m_instance.property(QString("updateCurrentTime")); diff --git a/animations/javascriptanimation_p.h b/animations/javascriptanimation_p.h index ca86b23dc..0795a6fd3 100644 --- a/animations/javascriptanimation_p.h +++ b/animations/javascriptanimation_p.h @@ -22,8 +22,9 @@ #include -#include -#include +#include "animation.h" +#include "plasma_export.h" +#include "animator.h" class QString; class QScriptEngine; @@ -38,6 +39,9 @@ class JavascriptAnimation: public Animation Q_OBJECT public: + enum { PauseAnimation = Animator::LastAnimation + 1, + PropertyAnimation = Animator::LastAnimation + 2 + }; explicit JavascriptAnimation(const QString &name, QObject *parent = 0);