Plasma
javascriptanimation.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "javascriptanimation_p.h" 00021 00022 #include <kdebug.h> 00023 00024 #include "animationscriptengine_p.h" 00025 /* TODO: 00026 * - support passing more parameters to the js animation object 00027 * - support more properties: angle, direction, etc 00028 * - support calling a 'resetAnimation' in js class when animation is stopped 00029 */ 00030 #define ADD_ENUM_VALUE(__c__, __ns__, __v__) \ 00031 __c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__)) 00032 00033 namespace Plasma 00034 { 00035 00036 JavascriptAnimation::JavascriptAnimation(const QString &name, QObject *parent) 00037 : EasingAnimation(parent), 00038 #ifdef PLASMA_JSANIM_FPS 00039 m_fps(0), 00040 #endif 00041 m_name(name) 00042 { 00043 00044 } 00045 00046 JavascriptAnimation::~JavascriptAnimation() 00047 { 00048 } 00049 00050 void JavascriptAnimation::prepInstance() 00051 { 00052 QScriptEngine *engine = AnimationScriptEngine::globalEngine(); 00053 m_instance.setProperty("__plasma_javascriptanimation", engine->newQObject(this), 00054 QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); 00055 ADD_ENUM_VALUE(m_instance, Plasma::Animator, FadeAnimation); 00056 ADD_ENUM_VALUE(m_instance, Plasma::Animator, AppearAnimation); 00057 ADD_ENUM_VALUE(m_instance, Plasma::Animator, DisappearAnimation); 00058 ADD_ENUM_VALUE(m_instance, Plasma::Animator, ActivateAnimation); 00059 ADD_ENUM_VALUE(m_instance, Plasma::Animator, FadeAnimation); 00060 ADD_ENUM_VALUE(m_instance, Plasma::Animator, GrowAnimation); 00061 ADD_ENUM_VALUE(m_instance, Plasma::Animator, PulseAnimation); 00062 ADD_ENUM_VALUE(m_instance, Plasma::Animator, RotationAnimation); 00063 ADD_ENUM_VALUE(m_instance, Plasma::Animator, RotationStackedAnimation); 00064 ADD_ENUM_VALUE(m_instance, Plasma::Animator, SlideAnimation); 00065 ADD_ENUM_VALUE(m_instance, Plasma::Animator, GeometryAnimation); 00066 ADD_ENUM_VALUE(m_instance, Plasma::Animator, ZoomAnimation); 00067 ADD_ENUM_VALUE(m_instance, Plasma::Animator, PixmapTransitionAnimation); 00068 ADD_ENUM_VALUE(m_instance, JavascriptAnimation, PauseAnimation); 00069 ADD_ENUM_VALUE(m_instance, JavascriptAnimation, PropertyAnimation); 00070 } 00071 00072 void JavascriptAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) 00073 { 00074 //kDebug() << ".................. state: " << newState; 00075 if (oldState == Stopped && newState == Running) { 00076 if (!m_method.isFunction()) { 00077 //Define the class and create an instance 00078 m_instance = AnimationScriptEngine::animation(m_name).construct(); 00079 kDebug() << "trying for" << m_name << m_instance.isFunction(); 00080 00081 //Get the method of the object 00082 m_method = m_instance.property(QString("updateCurrentTime")); 00083 if (!m_method.isFunction()) { 00084 qDebug() << "**************** ERROR! Name: " << m_name << " ************"; 00085 m_instance = m_method = QScriptValue(); 00086 } else { 00087 prepInstance(); 00088 00089 //TODO: this really should be done in the bindings provided 00090 //Center the widget for transformation 00091 qreal x = targetWidget()->geometry().height()/2; 00092 qreal y = targetWidget()->geometry().width()/2; 00093 targetWidget()->setTransformOriginPoint(x, y); 00094 } 00095 } 00096 00097 if (m_method.isFunction()) { 00098 m_instance.setProperty("duration", duration(), QScriptValue::ReadOnly); 00099 m_instance.setProperty("target", m_instance.engine()->newQObject(targetWidget()), QScriptValue::ReadOnly); 00100 } 00101 #ifdef PLASMA_JSANIM_FPS 00102 m_fps = 0; 00103 } else if (oldState == Running && newState == Stopped) { 00104 kDebug() << ".........." << m_name << " fps: " << m_fps * 1000/duration(); 00105 #endif 00106 } 00107 } 00108 00109 void JavascriptAnimation::updateEffectiveTime(int currentTime) 00110 { 00111 if (m_method.isFunction()) { 00112 #ifdef PLASMA_JSANIM_FPS 00113 ++m_fps; 00114 #endif 00115 QScriptValueList args; 00116 args << currentTime; 00117 00118 m_method.call(m_instance, args); 00119 } 00120 } 00121 00122 } //namespace Plasma 00123 00124 #include "javascriptanimation_p.moc"
KDE 4.6 API Reference