2010-04-26 22:46:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2010-04-26 22:56:15 +02:00
|
|
|
#include "javascriptanimation_p.h"
|
2010-04-29 23:17:43 +02:00
|
|
|
|
|
|
|
#include <kdebug.h>
|
2010-04-26 22:37:43 +02:00
|
|
|
|
2010-04-26 23:00:21 +02:00
|
|
|
#include "animationscriptengine_p.h"
|
2010-04-26 22:37:43 +02:00
|
|
|
/* 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
|
|
|
|
*/
|
2010-04-30 01:01:48 +02:00
|
|
|
#define ADD_ENUM_VALUE(__c__, __ns__, __v__) \
|
|
|
|
__c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__))
|
2010-04-26 22:37:43 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
JavascriptAnimation::JavascriptAnimation(const QString &name, QObject *parent)
|
2010-04-29 23:17:43 +02:00
|
|
|
: Animation(parent),
|
|
|
|
#ifdef PLASMA_JSANIM_FPS
|
|
|
|
m_fps(0),
|
|
|
|
#endif
|
|
|
|
m_name(name)
|
2010-04-26 22:37:43 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
JavascriptAnimation::~JavascriptAnimation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavascriptAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
|
|
|
|
{
|
2010-04-29 23:17:43 +02:00
|
|
|
//kDebug() << ".................. state: " << newState;
|
2010-04-26 22:37:43 +02:00
|
|
|
if (oldState == Stopped && newState == Running) {
|
2010-04-29 23:17:43 +02:00
|
|
|
if (!m_method.isFunction()) {
|
2010-04-26 22:37:43 +02:00
|
|
|
//Define the class and create an instance
|
2010-04-30 00:29:26 +02:00
|
|
|
QScriptEngine *engine = AnimationScriptEngine::globalEngine();
|
2010-04-29 23:17:43 +02:00
|
|
|
QScriptValueList args;
|
2010-04-30 00:29:26 +02:00
|
|
|
args << engine->newQObject(targetWidget()) << duration();
|
2010-04-29 23:17:43 +02:00
|
|
|
m_instance = AnimationScriptEngine::animation(m_name).construct(args);
|
|
|
|
kDebug() << "trying for" << m_name << m_instance.isFunction();
|
2010-04-30 00:29:26 +02:00
|
|
|
m_instance.setProperty("__plasma_javascriptanimation", engine->newQObject(this),
|
|
|
|
QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration);
|
2010-04-30 01:01:48 +02:00
|
|
|
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);
|
2010-04-26 22:37:43 +02:00
|
|
|
|
|
|
|
//Get the method of the object
|
2010-04-29 23:17:43 +02:00
|
|
|
m_method = m_instance.property(QString("updateCurrentTime"));
|
|
|
|
if (!m_method.isFunction()) {
|
|
|
|
qDebug() << "**************** ERROR! ************";
|
|
|
|
m_instance = m_method = QScriptValue();
|
2010-04-26 22:37:43 +02:00
|
|
|
}
|
|
|
|
|
2010-04-29 23:17:43 +02:00
|
|
|
//TODO: this really should be done in the bindings provided
|
2010-04-26 22:37:43 +02:00
|
|
|
//Center the widget for transformation
|
|
|
|
qreal x = targetWidget()->geometry().height()/2;
|
|
|
|
qreal y = targetWidget()->geometry().width()/2;
|
|
|
|
targetWidget()->setTransformOriginPoint(x, y);
|
|
|
|
}
|
|
|
|
|
2010-04-29 23:17:43 +02:00
|
|
|
#ifdef PLASMA_JSANIM_FPS
|
2010-04-26 22:37:43 +02:00
|
|
|
m_fps = 0;
|
|
|
|
} else if (oldState == Running && newState == Stopped) {
|
2010-04-29 23:17:43 +02:00
|
|
|
kDebug() << ".........." << m_name << " fps: " << m_fps * 1000/duration();
|
|
|
|
#endif
|
2010-04-26 22:37:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavascriptAnimation::updateCurrentTime(int currentTime)
|
|
|
|
{
|
2010-04-29 23:17:43 +02:00
|
|
|
if (m_method.isFunction()) {
|
|
|
|
#ifdef PLASMA_JSANIM_FPS
|
2010-04-26 22:37:43 +02:00
|
|
|
++m_fps;
|
2010-04-29 23:17:43 +02:00
|
|
|
#endif
|
2010-04-26 22:37:43 +02:00
|
|
|
QScriptValueList args;
|
|
|
|
args << currentTime;
|
|
|
|
|
2010-04-29 23:17:43 +02:00
|
|
|
m_method.call(m_instance, args);
|
2010-04-26 22:37:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace Plasma
|
|
|
|
|
2010-04-26 23:00:21 +02:00
|
|
|
#include <javascriptanimation_p.moc>
|