File: javascriptanimation.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,316 kB
  • sloc: cpp: 761,810; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (124 lines) | stat: -rw-r--r-- 4,795 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * 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.
 */

#include "javascriptanimation_p.h"

#include <kdebug.h>

#include "animationscriptengine_p.h"
/* 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
 */
#define ADD_ENUM_VALUE(__c__, __ns__, __v__) \
    __c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__))

namespace Plasma
{

JavascriptAnimation::JavascriptAnimation(const QString &name, QObject *parent)
    : EasingAnimation(parent),
#ifdef PLASMA_JSANIM_FPS
      m_fps(0),
#endif
      m_name(name)
{

}

JavascriptAnimation::~JavascriptAnimation()
{
}

void JavascriptAnimation::prepInstance()
{
    QScriptEngine *engine = AnimationScriptEngine::globalEngine();
    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);
}

void JavascriptAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
    //kDebug() << ".................. state: " << newState;
    if (oldState == Stopped && newState == Running) {
        if (!m_method.isFunction()) {
            //Define the class and create an instance
            m_instance = AnimationScriptEngine::animation(m_name).construct();
            kDebug() << "trying for" << m_name << m_instance.isFunction();

            //Get the method of the object
            m_method = m_instance.property(QString("updateCurrentTime"));
            if (!m_method.isFunction()) {
                qDebug() << "**************** ERROR! Name: " << m_name << " ************";
                m_instance = m_method = QScriptValue();
            } else {
                prepInstance();

                //TODO: this really should be done in the bindings provided
                //Center the widget for transformation
                qreal x = targetWidget()->geometry().height()/2;
                qreal y = targetWidget()->geometry().width()/2;
                targetWidget()->setTransformOriginPoint(x, y);
            }
        }

        if (m_method.isFunction()) {
            m_instance.setProperty("duration", duration(), QScriptValue::ReadOnly);
            m_instance.setProperty("target", m_instance.engine()->newQObject(targetWidget()), QScriptValue::ReadOnly);
        }
#ifdef PLASMA_JSANIM_FPS
        m_fps = 0;
    } else if (oldState == Running && newState == Stopped) {
        kDebug() << ".........." << m_name << " fps: " << m_fps * 1000/duration();
#endif
    }
}

void JavascriptAnimation::updateEffectiveTime(int currentTime)
{
    if (m_method.isFunction()) {
#ifdef PLASMA_JSANIM_FPS
        ++m_fps;
#endif
        QScriptValueList args;
        args << currentTime;

        m_method.call(m_instance, args);
    }
}

} //namespace Plasma

#include "javascriptanimation_p.moc"