[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/base/plasma/libs/jsanimation
From:       Aaron J. Seigo <aseigo () kde ! org>
Date:       2010-04-01 0:45:43
Message-ID: 20100401004543.B1B42AC888 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1109754 by aseigo:

allow animations to register themselves from the js, no more parsing of the js \
needed, and the animation js is only processed once by the engine. each JS file now \
makes a call to registerAnimation() from inside the javascript. this can be called \
multiple times, in fact. this results (in engine.cpp) in the animation function being \
stored as a QScriptValue. animations can then request this object and use it to \
construct a new animation. voila. CCMAIL:cavalcantii@gmail.com


 M  +7 -2      CMakeLists.txt  
 M  +3 -0      a_file_path.js  
 A             engine.cpp   engine.h#1109748 [License: LGPL (v2.1+)]
 M  +3 -14     engine.h  
 M  +36 -10    factory.cpp  
 M  +2 -0      factory.h  
 M  +3 -0      fade.js  
 M  +11 -35    javascriptanimation.cpp  
 M  +2 -10     javascriptanimation_p.h  
 M  +3 -0      rotation.js  
 M  +3 -0      slide.js  
 M  +2 -0      zoom.js  


--- trunk/playground/base/plasma/libs/jsanimation/CMakeLists.txt #1109753:1109754
@@ -12,8 +12,13 @@
    ${KDE4_INCLUDES}
    )
 
-set(jsanimation_SRCS factory.cpp javascriptanimation.cpp
-javascriptanimationtest.cpp red.cpp animation.cpp)
+set(jsanimation_SRCS
+   engine.cpp
+   factory.cpp
+   javascriptanimation.cpp
+   javascriptanimationtest.cpp 
+   red.cpp
+   animation.cpp)
 kde4_add_executable(javascriptanimationtest ${jsanimation_SRCS})
 target_link_libraries(javascriptanimationtest ${KDE4_KDEUI_LIBS} \
${QT_QTSCRIPT_LIBRARY})  
--- trunk/playground/base/plasma/libs/jsanimation/a_file_path.js #1109753:1109754
@@ -10,3 +10,6 @@
 	target.y = target.y + delta * 20
     }
 }
+
+registerAnimation("a_file_path.js", SeveralAnimation)
+
--- trunk/playground/base/plasma/libs/jsanimation/engine.h #1109753:1109754
@@ -43,26 +43,15 @@
 {
 public:
     static QScriptEngine* globalEngine();
+    static QScriptValue animation(const QString &anim);
 
 protected:
     AnimationEngine() { }
 
 private:
     static QScriptEngine *inst;
-
+    static QScriptValue registerAnimation(QScriptContext *context, QScriptEngine \
*engine); +    static QHash<QString, QScriptValue> s_animFuncs;
 };
 
-QScriptEngine* AnimationEngine::inst = 0;
-
-
-QScriptEngine *AnimationEngine::globalEngine()
-{
-    if (!inst) {
-        inst = new QScriptEngine;
-	qDebug() << "........... first js animation, creating the engine!";
-    }
-
-    return inst;
-}
-
 #endif
--- trunk/playground/base/plasma/libs/jsanimation/factory.cpp #1109753:1109754
@@ -1,28 +1,54 @@
 #include "factory.h"
 #include "animation.h"
+#include "engine.h"
 #include "javascriptanimation_p.h"
 #include <QFile>
 #include <QTextStream>
 #include <QDebug>
 
+QSet<QString> Factory::s_paths;
+
 Plasma::Animation *Factory::create(QString &path)
 {
     Plasma::Animation *result = 0;
     /* Here this will be executed in the Animator::factory() and
      * the path is retrieved from the Theme class
      */
-    qDebug() << path;
-    QFile file(path);
-    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
-        qDebug() << "failed to open js......";
-        return result;
+    //qDebug() << path;
+
+    /**
+     * FIXME: in future, the path and the animation name will not match 1:1
+     * so we need a way to map animations to path names. trivial would be to map
+     * "Zoom" to "zoom.js" (e.g. anim.toLower() + 'js') but that's also a bit lame.
+     * I think we may need a mapping, much as we use KService to do for applet
+     * names to libraries. Maybe we should, in fact, use KSycoca for this?
+     * In any case, the method used below even allows for all anims to be defined in
+     * one file!
+     */
+    if (!s_paths.contains(path)) {
+        QFile file(path);
+        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+            qDebug() << "failed to open js......";
+            return result;
+        }
+
+        QTextStream buffer(&file);
+        QString tmp(buffer.readAll());
+        s_paths.insert(path);
+
+        QScriptEngine *engine = AnimationEngine::globalEngine();
+        QScriptValue def(engine->evaluate(tmp, path));
+        if (engine->hasUncaughtException()) {
+            const QScriptValue error = engine->uncaughtException();
+            QString file = error.property("fileName").toString();
+            const QString failureMsg = QString("Error in %1 on line \
%2.<br><br>%3").arg( +                    \
file).arg(error.property("lineNumber").toString()).arg(error.toString()); +           \
qDebug() << "fail!" << failureMsg; +        }
     }
-
-    QTextStream buffer(&file);
-    QString tmp(buffer.readAll());
     //m_anim = new Plasma::JavascriptAnimation(this, tmp);
-    qDebug() << tmp;
-    result = new Plasma::JavascriptAnimation(0, tmp);
+    //qDebug() << tmp;
+    result = new Plasma::JavascriptAnimation(path);
 
     return result;
 }
--- trunk/playground/base/plasma/libs/jsanimation/factory.h #1109753:1109754
@@ -44,6 +44,8 @@
 public:
     static Plasma::Animation* create(QString &path);
 
+private:
+    static QSet<QString> s_paths;
 };
 
 
--- trunk/playground/base/plasma/libs/jsanimation/fade.js #1109753:1109754
@@ -13,3 +13,6 @@
 
     }
 }
+
+registerAnimation("fade.js", FadeAnimation)
+
--- trunk/playground/base/plasma/libs/jsanimation/javascriptanimation.cpp \
#1109753:1109754 @@ -33,12 +33,18 @@
 namespace Plasma
 {
 
-JavascriptAnimation::JavascriptAnimation(QObject *parent, QString &file):
-    Animation(parent), m_js(file), engine(0), m_instance(0), m_method(0)
+JavascriptAnimation::JavascriptAnimation(const QString &name, QObject *parent)
+    : Animation(parent), m_name(name), engine(0), m_instance(0), m_method(0)
 {
 
 }
 
+JavascriptAnimation::~JavascriptAnimation()
+{
+    delete m_instance;
+    delete m_method;
+}
+
 void JavascriptAnimation::updateState(QAbstractAnimation::State newState, \
QAbstractAnimation::State oldState)  {
     qDebug() << ".................. state: " << newState;
@@ -47,13 +53,12 @@
             engine = AnimationEngine::globalEngine();
 
             //Define the class and create an instance
-            QScriptValue def(engine->evaluate(m_js, "js class"));
             if (!m_instance) {
                 m_instance = new QScriptValue;
-                *m_instance = \
engine->evaluate(uglyHackJSName()).construct(QScriptValueList() +                \
                *m_instance = \
                AnimationEngine::animation(m_name).construct(QScriptValueList()
                                                << engine->newQObject(targetWidget())
                                                << duration());
-
+                qDebug( )<< "trying for" << m_name << m_instance->isFunction();
             }
 
             //Get the method of the object
@@ -85,35 +90,6 @@
     }
 }
 
-JavascriptAnimation::~JavascriptAnimation()
-{
-    /* TODO: maybe use a QWeakPointer here */
-    if (m_instance)
-        delete m_instance;
-    if (m_method)
-        delete m_method;
-}
-
-QString JavascriptAnimation::javascript() const
-{
-
-    return m_js;
-}
-
-void JavascriptAnimation::setJavascript(QString &string)
-{
-    m_js = string;
-}
-
-QString JavascriptAnimation::uglyHackJSName()
-{
-    /* XXX for while parse the function/constructor js name from the string */
-    QString marker("function");
-    int start = m_js.indexOf(marker);
-    int end = m_js.indexOf(QString("("));
-    return m_js.mid(start + marker.length() + 1, end - 1 - marker.length());
-}
-
 } //namespace Plasma
 
-#include <../javascriptanimation_p.moc>
+#include <javascriptanimation_p.moc>
--- trunk/playground/base/plasma/libs/jsanimation/javascriptanimation_p.h \
#1109753:1109754 @@ -35,27 +35,19 @@
 class JavascriptAnimation: public Animation
 {
     Q_OBJECT
-    Q_PROPERTY(QString javascript READ javascript WRITE setJavascript)
 
 public:
 
-    explicit JavascriptAnimation(QObject *parent, QString &file);
+    explicit JavascriptAnimation(const QString &name, QObject *parent = 0);
 
     ~JavascriptAnimation();
 
-    QString javascript() const;
-
-    void setJavascript(QString &string);
-
 protected:
     void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State \
oldState);  void updateCurrentTime(int currentTime);
 
 private:
-
-    QString uglyHackJSName();
-
-    QString m_js;
+    QString m_name;
     QScriptEngine *engine;
     QScriptValue *m_instance;
     QScriptValue *m_method;
--- trunk/playground/base/plasma/libs/jsanimation/rotation.js #1109753:1109754
@@ -12,3 +12,6 @@
 
     }
 }
+
+registerAnimation("rotation.js", RotationAnimation)
+
--- trunk/playground/base/plasma/libs/jsanimation/slide.js #1109753:1109754
@@ -10,3 +10,6 @@
 
     }
 }
+
+registerAnimation("slide.js", SlideAnimation)
+
--- trunk/playground/base/plasma/libs/jsanimation/zoom.js #1109753:1109754
@@ -9,3 +9,5 @@
 
     }
 }
+
+registerAnimation("zoom.js", ZoomAnimation)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic