1 | /* |
2 | Copyright (C) 2009 Adenilson Cavalcanti <adenilson.silva@idnt.org.br> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "animator.h" |
19 | #include "private/animator_p.h" |
20 | |
21 | #include <kdebug.h> |
22 | |
23 | #include "animations/animation.h" |
24 | #include "animations/animationscriptengine_p.h" |
25 | #include "animations/fade_p.h" |
26 | #include "animations/grow_p.h" |
27 | #include "animations/pulser_p.h" |
28 | #include "animations/rotation_p.h" |
29 | #include "animations/slide_p.h" |
30 | #include "animations/rotationstacked_p.h" |
31 | #include "animations/geometry_p.h" |
32 | #include "animations/zoom_p.h" |
33 | #include "animations/pixmaptransition_p.h" |
34 | #include "animations/water_p.h" |
35 | #include "animations/pendulumcurve_p.h" |
36 | #include "animations/javascriptanimation_p.h" |
37 | #include "theme.h" |
38 | |
39 | namespace Plasma |
40 | { |
41 | |
42 | QHash<Animator::Animation, Animator::Animation> AnimatorPrivate::s_stockAnimMappings; |
43 | QHash<Animator::Animation, QString> AnimatorPrivate::s_loadableAnimMappings; |
44 | |
45 | void AnimatorPrivate::mapAnimation(Animator::Animation from, Animator::Animation to) |
46 | { |
47 | if (from == to) { |
48 | return; |
49 | } |
50 | |
51 | s_loadableAnimMappings.remove(from); |
52 | s_stockAnimMappings.insert(from, to); |
53 | } |
54 | |
55 | void AnimatorPrivate::mapAnimation(Animator::Animation from, const QString &to) |
56 | { |
57 | s_stockAnimMappings.remove(from); |
58 | s_loadableAnimMappings.insert(from, to); |
59 | } |
60 | |
61 | Plasma::Animation* Animator::create(Animator::Animation type, QObject *parent) |
62 | { |
63 | if (AnimatorPrivate::s_stockAnimMappings.contains(type)) { |
64 | return create(AnimatorPrivate::s_stockAnimMappings.value(type)); |
65 | } else if (AnimatorPrivate::s_loadableAnimMappings.contains(type)) { |
66 | const QString anim = AnimatorPrivate::s_loadableAnimMappings.value(type); |
67 | return create(anim, parent); |
68 | } |
69 | |
70 | Plasma::Animation *result = 0; |
71 | |
72 | switch (type) { |
73 | case FadeAnimation: |
74 | result = create("FadeAnimation" , parent); |
75 | if (!result) { |
76 | result = new Plasma::FadeAnimation(parent); |
77 | } |
78 | break; |
79 | |
80 | case GrowAnimation: |
81 | result = create("GrowAnimation" , parent); |
82 | if (!result) { |
83 | result = new Plasma::GrowAnimation(parent); |
84 | } |
85 | break; |
86 | |
87 | case PulseAnimation: |
88 | result = create("PulseAnimation" , parent); |
89 | if (!result) { |
90 | result = new Plasma::PulseAnimation(parent); |
91 | } |
92 | break; |
93 | |
94 | case RotationAnimation: |
95 | result = create("RotationAnimation" , parent); |
96 | if (!result) { |
97 | result = new Plasma::RotationAnimation(parent); |
98 | } |
99 | break; |
100 | |
101 | case RotationStackedAnimation: |
102 | result = create("RotationStackedAnimation" , parent); |
103 | if (!result) { |
104 | result = new Plasma::RotationStackedAnimation(parent); |
105 | } |
106 | break; |
107 | |
108 | case SlideAnimation: |
109 | result = create("SlideAnimation" , parent); |
110 | if (!result) { |
111 | result = new Plasma::SlideAnimation(parent); |
112 | } |
113 | break; |
114 | |
115 | case GeometryAnimation: |
116 | result = create("GeometryAnimation" , parent); |
117 | if (!result) { |
118 | result = new Plasma::GeometryAnimation(parent); |
119 | } |
120 | break; |
121 | |
122 | case ZoomAnimation: |
123 | result = create("ZoomAnimation" , parent); |
124 | if (!result) { |
125 | result = new Plasma::ZoomAnimation(parent); |
126 | } |
127 | break; |
128 | |
129 | case PixmapTransitionAnimation: |
130 | result = create("PixmapTransitionAnimation" , parent); |
131 | if (!result) { |
132 | result = new Plasma::PixmapTransition(parent); |
133 | } |
134 | break; |
135 | |
136 | case WaterAnimation: |
137 | result = create("WaterAnimation" , parent); |
138 | if (!result) { |
139 | result = new Plasma::WaterAnimation(parent); |
140 | } |
141 | break; |
142 | |
143 | default: |
144 | //kDebug() << "Unsupported animation type."; |
145 | break; |
146 | } |
147 | |
148 | return result; |
149 | } |
150 | |
151 | QEasingCurve Animator::create(Animator::CurveShape type) |
152 | { |
153 | QEasingCurve result; |
154 | |
155 | switch (type) { |
156 | case EaseInCurve: |
157 | result.setType(QEasingCurve::InQuad); |
158 | break; |
159 | |
160 | case EaseOutCurve: |
161 | result.setType(QEasingCurve::OutQuad); |
162 | break; |
163 | |
164 | case EaseInOutCurve: |
165 | result.setType(QEasingCurve::InOutQuad); |
166 | break; |
167 | |
168 | case LinearCurve: |
169 | result.setType(QEasingCurve::Linear); |
170 | break; |
171 | |
172 | case PendularCurve: |
173 | result = PendulumCurve(); |
174 | break; |
175 | |
176 | default: |
177 | kDebug() << "Unsupported easing curve type." ; |
178 | break; |
179 | } |
180 | |
181 | return result; |
182 | } |
183 | |
184 | Plasma::Animation *Animator::create(const QString &anim, QObject *parent) |
185 | { |
186 | if (AnimationScriptEngine::animationFailedToLoad(anim)) { |
187 | return 0; |
188 | } |
189 | |
190 | if (!AnimationScriptEngine::isAnimationRegistered(anim)) { |
191 | const QString path = Theme::defaultTheme()->animationPath(anim); |
192 | if (path.isEmpty()) { |
193 | AnimationScriptEngine::addToLoadFailures(anim); |
194 | //kError() << "************ failed to find script file for animation" << anim; |
195 | return 0; |
196 | } |
197 | |
198 | if (!AnimationScriptEngine::loadScript(path)) { |
199 | AnimationScriptEngine::addToLoadFailures(anim); |
200 | return 0; |
201 | } |
202 | |
203 | if (!AnimationScriptEngine::isAnimationRegistered(anim)) { |
204 | //kError() << "successfully loaded script file" << path << ", but did not get animation object for" << anim; |
205 | AnimationScriptEngine::addToLoadFailures(anim); |
206 | return 0; |
207 | } |
208 | } |
209 | |
210 | return new Plasma::JavascriptAnimation(anim, parent); |
211 | } |
212 | |
213 | } // namespace Plasma |
214 | |
215 | #include <animator.moc> |
216 | |
217 | |