Adding jsanim classes (next, change object factory to address jsanim

object creation).


svn path=/trunk/KDE/kdelibs/; revision=1119231
This commit is contained in:
Adenilson Cavalcanti Da Silva 2010-04-26 20:37:43 +00:00
parent 2fa6d4204c
commit 058713d07a
4 changed files with 291 additions and 0 deletions

71
animations/engine.cpp Normal file
View File

@ -0,0 +1,71 @@
/***********************************************************************/
/* engine.h */
/* */
/* Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA */
/* 02110-1301 USA */
/***********************************************************************/
/**
* @file engine.h
* @author Adenilson Cavalcanti
* @date Wed Feb 24 21:49:45 2010
*
* @brief
*
*
*/
/* TODO:
*
* - cleanup debug messages
*/
#include "engine.h"
QScriptEngine* AnimationEngine::inst = 0;
QHash<QString, QScriptValue> AnimationEngine::s_animFuncs;
QScriptValue AnimationEngine::animation(const QString &anim)
{
return s_animFuncs.value(anim);
}
QScriptValue AnimationEngine::registerAnimation(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() > 1) {
const QString name = context->argument(0).toString();
const QScriptValue func = context->argument(1);
if (func.isFunction()) {
s_animFuncs.insert(name, func);
}
}
return engine->undefinedValue();
}
QScriptEngine *AnimationEngine::globalEngine()
{
if (!inst) {
inst = new QScriptEngine;
QScriptValue global = inst->globalObject();
global.setProperty("registerAnimation", inst->newFunction(AnimationEngine::registerAnimation));
qDebug() << "........... first js animation, creating the engine!";
}
return inst;
}

57
animations/engine.h Normal file
View File

@ -0,0 +1,57 @@
/***********************************************************************/
/* engine.h */
/* */
/* Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA */
/* 02110-1301 USA */
/***********************************************************************/
/**
* @file engine.h
* @author Adenilson Cavalcanti
* @date Wed Feb 24 21:49:45 2010
*
* @brief
*
*
*/
#ifndef __ENGINE__
#define __ENGINE__
#include <QScriptEngine>
#include <QScriptString>
#include <QScriptValue>
#include <QScriptContext>
#include <QDebug>
/* Plasma-shell will have an engine */
class AnimationEngine
{
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;
};
#endif

103
animations/jsanim.cpp Normal file
View File

@ -0,0 +1,103 @@
/////////////////////////////////////////////////////////////////////////
// jsanim.cpp //
// //
// Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> //
// //
// This library is free software; you can redistribute it and/or //
// modify it under the terms of the GNU Lesser General Public //
// License as published by the Free Software Foundation; either //
// version 2.1 of the License, or (at your option) any later version. //
// //
// This library 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 //
// Lesser General Public License for more details. //
// //
// You should have received a copy of the GNU Lesser General Public //
// License along with this library; if not, write to the Free Software //
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
// 02110-1301 USA //
/////////////////////////////////////////////////////////////////////////
#include "jsanim_p.h"
#include "engine.h"
#include <QDebug>
/* TODO:
* - support passing more parameters to the js animation object
* - support more properties: angle, direction, etc
* - support calling a 'resetAnimation' in js class when animation is stopped
*/
namespace Plasma
{
JavascriptAnimation::JavascriptAnimation(const QString &name, QObject *parent)
: Animation(parent), m_fps(0), 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;
if (oldState == Stopped && newState == Running) {
if (!engine) {
engine = AnimationEngine::globalEngine();
//Define the class and create an instance
if (!m_instance) {
m_instance = new QScriptValue;
*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
if (!m_method) {
m_method = new QScriptValue;
*m_method = m_instance->property(QString("updateCurrentTime"));
if (!m_method->isFunction()) {
qDebug() << "**************** ERROR! ************";
} else {
qDebug() << ".................. success js instance creation!";
}
}
//Center the widget for transformation
qreal x = targetWidget()->geometry().height()/2;
qreal y = targetWidget()->geometry().width()/2;
targetWidget()->setTransformOriginPoint(x, y);
}
m_fps = 0;
} else if (oldState == Running && newState == Stopped) {
qDebug() << ".........." << m_name << " fps: " << m_fps * 1000/duration();
}
}
void JavascriptAnimation::updateCurrentTime(int currentTime)
{
if (m_method->isFunction()) {
++m_fps;
QScriptValueList args;
args << currentTime;
m_method->call(*m_instance, args);
}
}
} //namespace Plasma
#include <../jsanim_p.moc>

60
animations/jsanim_p.h Normal file
View File

@ -0,0 +1,60 @@
/***********************************************************************/
/* jsanim_p.h */
/* */
/* Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA */
/* 02110-1301 USA */
/***********************************************************************/
#ifndef PLASMA_ANIMATIONS_JS_P_H
#define PLASMA_ANIMATIONS_JS_P_H
#include <plasma/animations/animation.h>
#include <plasma/plasma_export.h>
class QString;
class QScriptEngine;
class QScriptValue;
namespace Plasma
{
class JavascriptAnimation: public Animation
{
Q_OBJECT
public:
explicit JavascriptAnimation(const QString &name, QObject *parent = 0);
~JavascriptAnimation();
protected:
void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
void updateCurrentTime(int currentTime);
private:
int m_fps;
QString m_name;
QScriptEngine *engine;
QScriptValue *m_instance;
QScriptValue *m_method;
};
}
#endif