Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc1 / src / synfigapp / actions / keyframeduplicate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframeduplicate.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "keyframeduplicate.h"
33 #include <synfigapp/canvasinterface.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfig/valuenode_animated.h>
36 #include "activepointsetsmart.h"
37 #include "waypointsetsmart.h"
38
39 #endif
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace synfigapp;
45 using namespace Action;
46
47 /* === M A C R O S ========================================================= */
48
49 ACTION_INIT(Action::KeyframeDuplicate);
50 ACTION_SET_NAME(Action::KeyframeDuplicate,"keyframe_duplicate");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeDuplicate,"Duplicate Keyframe");
52 ACTION_SET_TASK(Action::KeyframeDuplicate,"duplicate");
53 ACTION_SET_CATEGORY(Action::KeyframeDuplicate,Action::CATEGORY_KEYFRAME);
54 ACTION_SET_PRIORITY(Action::KeyframeDuplicate,0);
55 ACTION_SET_VERSION(Action::KeyframeDuplicate,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeDuplicate,"$Id$");
57
58 /* === G L O B A L S ======================================================= */
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Action::KeyframeDuplicate::KeyframeDuplicate()
65 {
66         new_keyframe.set_time(Time::begin()-1);
67         keyframe.set_time(Time::begin()-1);
68         set_dirty(true);
69 }
70
71 Action::ParamVocab
72 Action::KeyframeDuplicate::get_param_vocab()
73 {
74         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
75
76         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
77                 .set_local_name(_("Keyframe"))
78                 .set_desc(_("Keyframe to be duplicated"))
79         );
80
81         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
82                 .set_local_name(_("Time"))
83         );
84
85         return ret;
86 }
87
88 bool
89 Action::KeyframeDuplicate::is_candidate(const ParamList &x)
90 {
91         return candidate_check(get_param_vocab(),x);
92 }
93
94 bool
95 Action::KeyframeDuplicate::set_param(const synfig::String& name, const Action::Param &param)
96 {
97         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
98         {
99                 keyframe=param.get_keyframe();
100                 new_keyframe.set_description(keyframe.get_description()+_(" (Duplicate)"));
101
102                 return true;
103         }
104         if(name=="time" && param.get_type()==Param::TYPE_TIME)
105         {
106                 new_keyframe.set_time(param.get_time());
107
108                 return true;
109         }
110
111         return Action::CanvasSpecific::set_param(name,param);
112 }
113
114 bool
115 Action::KeyframeDuplicate::is_ready()const
116 {
117         if(keyframe.get_time()==(Time::begin()-1) || new_keyframe.get_time()==(Time::begin()-1))
118                 return false;
119         return Action::CanvasSpecific::is_ready();
120 }
121
122 void
123 Action::KeyframeDuplicate::prepare()
124 {
125         clear();
126
127         const synfig::Time old_time=keyframe.get_time();
128         const synfig::Time new_time=new_keyframe.get_time();
129
130         try { get_canvas()->keyframe_list().find(keyframe);}
131         catch(synfig::Exception::NotFound)
132         {
133                 throw Error(_("Unable to find the given keyframe"));
134         }
135
136         try { if(get_canvas()->keyframe_list().find(new_time)!=get_canvas()->keyframe_list().end()) throw Error(_("A Keyframe already exists at this point in time"));}
137         catch(...) { }
138
139         // If the times are different, then we
140         // will need to romp through the valuenodes
141         // and add actions to update their values.
142         if(new_time!=old_time)
143         {
144                 std::vector<synfigapp::ValueDesc> value_desc_list;
145                 get_canvas_interface()->find_important_value_descs(value_desc_list);
146                 while(!value_desc_list.empty())
147                 {
148                         process_value_desc(value_desc_list.back());
149                         value_desc_list.pop_back();
150                 }
151         }
152 }
153
154 void
155 Action::KeyframeDuplicate::process_value_desc(const synfigapp::ValueDesc& value_desc)
156 {
157         const synfig::Time old_time=keyframe.get_time();
158         const synfig::Time new_time=new_keyframe.get_time();
159
160         if(value_desc.is_value_node())
161         {
162                 ValueNode::Handle value_node(value_desc.get_value_node());
163
164                 // If we are a dynamic list, then we need to update the ActivePoints
165                 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
166                 {
167                         ValueNode_DynamicList::Handle value_node_dynamic(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
168                         int i;
169
170                         for(i=0;i<value_node_dynamic->link_count();i++)
171                         {
172                                 synfigapp::ValueDesc value_desc(value_node_dynamic,i);
173                                 Activepoint activepoint(value_node_dynamic->list[i].new_activepoint_at_time(old_time));
174                                 activepoint.set_time(new_time);
175
176                                 Action::Handle action(ActivepointSetSmart::create());
177
178                                 action->set_param("canvas",get_canvas());
179                                 action->set_param("canvas_interface",get_canvas_interface());
180                                 action->set_param("value_desc",value_desc);
181                                 action->set_param("activepoint",activepoint);
182
183                                 assert(action->is_ready());
184                                 if(!action->is_ready())
185                                         throw Error(Error::TYPE_NOTREADY);
186
187                                 add_action_front(action);
188                         }
189                 }
190                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
191                 {
192                         ValueNode_Animated::Handle value_node_animated(ValueNode_Animated::Handle::cast_dynamic(value_node));
193                         Waypoint waypoint(value_node_animated->new_waypoint_at_time(old_time));
194                         waypoint.set_time(new_time);
195
196                         Action::Handle action(WaypointSetSmart::create());
197
198                         action->set_param("canvas",get_canvas());
199                         action->set_param("canvas_interface",get_canvas_interface());
200                         action->set_param("value_node",ValueNode::Handle(value_node_animated));
201                         action->set_param("waypoint",waypoint);
202
203                         assert(action->is_ready());
204                         if(!action->is_ready())
205                                 throw Error(Error::TYPE_NOTREADY);
206
207                         add_action_front(action);
208                 }
209         }
210 }
211
212 void
213 Action::KeyframeDuplicate::perform()
214 {
215         try { get_canvas()->keyframe_list().find(new_keyframe.get_time()); throw Error(_("A Keyframe already exists at this point in time"));}
216         catch(synfig::Exception::NotFound) { }
217
218         try { get_canvas()->keyframe_list().find(new_keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
219         catch(synfig::Exception::NotFound) { }
220
221         Action::Super::perform();
222
223         get_canvas()->keyframe_list().add(new_keyframe);
224
225         if(get_canvas_interface())
226         {
227                 get_canvas_interface()->signal_keyframe_added()(new_keyframe);
228         }
229         else synfig::warning("CanvasInterface not set on action");
230 }
231
232 void
233 Action::KeyframeDuplicate::undo()
234 {
235         Action::Super::undo();
236
237         if(get_canvas_interface())
238         {
239                 get_canvas_interface()->signal_keyframe_removed()(new_keyframe);
240         }
241         else synfig::warning("CanvasInterface not set on action");
242
243         get_canvas()->keyframe_list().erase(new_keyframe);
244 }