Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / 0.61.08 / 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 #include <synfigapp/general.h>
40
41 #endif
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace synfigapp;
47 using namespace Action;
48
49 /* === M A C R O S ========================================================= */
50
51 ACTION_INIT(Action::KeyframeDuplicate);
52 ACTION_SET_NAME(Action::KeyframeDuplicate,"keyframe_duplicate");
53 ACTION_SET_LOCAL_NAME(Action::KeyframeDuplicate,N_("Duplicate Keyframe"));
54 ACTION_SET_TASK(Action::KeyframeDuplicate,"duplicate");
55 ACTION_SET_CATEGORY(Action::KeyframeDuplicate,Action::CATEGORY_KEYFRAME);
56 ACTION_SET_PRIORITY(Action::KeyframeDuplicate,0);
57 ACTION_SET_VERSION(Action::KeyframeDuplicate,"0.0");
58 ACTION_SET_CVS_ID(Action::KeyframeDuplicate,"$Id$");
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 Action::KeyframeDuplicate::KeyframeDuplicate()
67 {
68         new_keyframe.set_time(Time::begin()-1);
69         keyframe.set_time(Time::begin()-1);
70         set_dirty(true);
71 }
72
73 Action::ParamVocab
74 Action::KeyframeDuplicate::get_param_vocab()
75 {
76         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
77
78         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
79                 .set_local_name(_("Keyframe"))
80                 .set_desc(_("Keyframe to be duplicated"))
81         );
82
83         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
84                 .set_local_name(_("Time"))
85         );
86
87         return ret;
88 }
89
90 bool
91 Action::KeyframeDuplicate::is_candidate(const ParamList &x)
92 {
93         return candidate_check(get_param_vocab(),x);
94 }
95
96 bool
97 Action::KeyframeDuplicate::set_param(const synfig::String& name, const Action::Param &param)
98 {
99         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
100         {
101                 keyframe=param.get_keyframe();
102                 new_keyframe.set_description(keyframe.get_description()+_(" (Duplicate)"));
103
104                 return true;
105         }
106         if(name=="time" && param.get_type()==Param::TYPE_TIME)
107         {
108                 new_keyframe.set_time(param.get_time());
109
110                 return true;
111         }
112
113         return Action::CanvasSpecific::set_param(name,param);
114 }
115
116 bool
117 Action::KeyframeDuplicate::is_ready()const
118 {
119         if(keyframe.get_time()==(Time::begin()-1) || new_keyframe.get_time()==(Time::begin()-1))
120                 return false;
121         return Action::CanvasSpecific::is_ready();
122 }
123
124 void
125 Action::KeyframeDuplicate::prepare()
126 {
127         clear();
128
129         const synfig::Time old_time=keyframe.get_time();
130         const synfig::Time new_time=new_keyframe.get_time();
131
132         try { get_canvas()->keyframe_list().find(keyframe);}
133         catch(synfig::Exception::NotFound)
134         {
135                 throw Error(_("Unable to find the given keyframe"));
136         }
137
138         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"));}
139         catch(...) { }
140
141         // If the times are different, then we
142         // will need to romp through the valuenodes
143         // and add actions to update their values.
144         if(new_time!=old_time)
145         {
146                 std::vector<synfigapp::ValueDesc> value_desc_list;
147                 get_canvas_interface()->find_important_value_descs(value_desc_list);
148                 while(!value_desc_list.empty())
149                 {
150                         process_value_desc(value_desc_list.back());
151                         value_desc_list.pop_back();
152                 }
153         }
154 }
155
156 void
157 Action::KeyframeDuplicate::process_value_desc(const synfigapp::ValueDesc& value_desc)
158 {
159         const synfig::Time old_time=keyframe.get_time();
160         const synfig::Time new_time=new_keyframe.get_time();
161
162         if(value_desc.is_value_node())
163         {
164                 ValueNode::Handle value_node(value_desc.get_value_node());
165
166                 // If we are a dynamic list, then we need to update the ActivePoints
167                 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
168                 {
169                         ValueNode_DynamicList::Handle value_node_dynamic(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
170                         int i;
171
172                         for(i=0;i<value_node_dynamic->link_count();i++)
173                         {
174                                 synfigapp::ValueDesc value_desc(value_node_dynamic,i);
175                                 Activepoint activepoint(value_node_dynamic->list[i].new_activepoint_at_time(old_time));
176                                 activepoint.set_time(new_time);
177
178                                 Action::Handle action(ActivepointSetSmart::create());
179
180                                 action->set_param("canvas",get_canvas());
181                                 action->set_param("canvas_interface",get_canvas_interface());
182                                 action->set_param("value_desc",value_desc);
183                                 action->set_param("activepoint",activepoint);
184
185                                 assert(action->is_ready());
186                                 if(!action->is_ready())
187                                         throw Error(Error::TYPE_NOTREADY);
188
189                                 add_action_front(action);
190                         }
191                 }
192                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
193                 {
194                         ValueNode_Animated::Handle value_node_animated(ValueNode_Animated::Handle::cast_dynamic(value_node));
195                         Waypoint waypoint(value_node_animated->new_waypoint_at_time(old_time));
196                         waypoint.set_time(new_time);
197
198                         Action::Handle action(WaypointSetSmart::create());
199
200                         action->set_param("canvas",get_canvas());
201                         action->set_param("canvas_interface",get_canvas_interface());
202                         action->set_param("value_node",ValueNode::Handle(value_node_animated));
203                         action->set_param("waypoint",waypoint);
204
205                         assert(action->is_ready());
206                         if(!action->is_ready())
207                                 throw Error(Error::TYPE_NOTREADY);
208
209                         add_action_front(action);
210                 }
211         }
212 }
213
214 void
215 Action::KeyframeDuplicate::perform()
216 {
217         try { get_canvas()->keyframe_list().find(new_keyframe.get_time()); throw Error(_("A Keyframe already exists at this point in time"));}
218         catch(synfig::Exception::NotFound) { }
219
220         try { get_canvas()->keyframe_list().find(new_keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
221         catch(synfig::Exception::NotFound) { }
222
223         Action::Super::perform();
224
225         get_canvas()->keyframe_list().add(new_keyframe);
226
227         if(get_canvas_interface())
228         {
229                 get_canvas_interface()->signal_keyframe_added()(new_keyframe);
230         }
231         else synfig::warning("CanvasInterface not set on action");
232 }
233
234 void
235 Action::KeyframeDuplicate::undo()
236 {
237         Action::Super::undo();
238
239         if(get_canvas_interface())
240         {
241                 get_canvas_interface()->signal_keyframe_removed()(new_keyframe);
242         }
243         else synfig::warning("CanvasInterface not set on action");
244
245         get_canvas()->keyframe_list().erase(new_keyframe);
246 }