Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_03 / synfig-studio / src / synfigapp / actions / keyframeremove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframeremove.cpp
3 **      \brief Template File
4 **
5 **      $Id: keyframeremove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
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 "keyframeremove.h"
33 #include <synfigapp/canvasinterface.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfig/valuenode_animated.h>
36 #include "activepointremove.h"
37 #include "waypointremove.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::KeyframeRemove);
50 ACTION_SET_NAME(Action::KeyframeRemove,"keyframe_remove");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeRemove,"Remove Keyframe");
52 ACTION_SET_TASK(Action::KeyframeRemove,"remove");
53 ACTION_SET_CATEGORY(Action::KeyframeRemove,Action::CATEGORY_KEYFRAME);
54 ACTION_SET_PRIORITY(Action::KeyframeRemove,0);
55 ACTION_SET_VERSION(Action::KeyframeRemove,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeRemove,"$Id: keyframeremove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
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::KeyframeRemove::KeyframeRemove()
65 {
66         keyframe.set_time(Time::begin()-1);
67         set_dirty(true);
68 }
69
70 Action::ParamVocab
71 Action::KeyframeRemove::get_param_vocab()
72 {
73         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74         
75         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
76                 .set_local_name(_("Keyframe"))
77                 .set_desc(_("Keyframe to be removed"))
78         );
79
80         return ret;
81 }
82
83 bool
84 Action::KeyframeRemove::is_canidate(const ParamList &x)
85 {
86         return canidate_check(get_param_vocab(),x);
87 }
88
89 bool
90 Action::KeyframeRemove::set_param(const synfig::String& name, const Action::Param &param)
91 {
92         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
93         {
94                 keyframe=param.get_keyframe();
95                 
96                 return true;
97         }
98
99         return Action::CanvasSpecific::set_param(name,param);
100 }
101
102 bool
103 Action::KeyframeRemove::is_ready()const
104 {
105         if(keyframe.get_time()==(Time::begin()-1))
106                 return false;
107         return Action::CanvasSpecific::is_ready();
108 }
109
110 void
111 Action::KeyframeRemove::prepare()
112 {
113         clear();
114
115         try { get_canvas()->keyframe_list().find(keyframe);}
116         catch(synfig::Exception::NotFound)
117         {
118                 throw Error(_("Unable to find the given keyframe"));
119         }       
120
121         
122         {
123                 std::vector<synfigapp::ValueDesc> value_desc_list;
124                 get_canvas_interface()->find_important_value_descs(value_desc_list);
125                 while(!value_desc_list.empty())
126                 {
127                         process_value_desc(value_desc_list.back());
128                         value_desc_list.pop_back();
129                 }
130         }
131 }
132
133 void
134 Action::KeyframeRemove::process_value_desc(const synfigapp::ValueDesc& value_desc)
135 {       
136         const synfig::Time time(keyframe.get_time());
137
138         if(value_desc.is_value_node())
139         {
140                 ValueNode::Handle value_node(value_desc.get_value_node());
141         
142                 // If we are a dynamic list, then we need to update the ActivePoints
143                 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
144                 {
145                         ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
146                         int i;
147                         for(i=0;i<value_node->link_count();i++)
148                         try
149                         {
150                                 Activepoint activepoint;
151                                 activepoint=*value_node->list[i].find(time);
152
153                                 synfigapp::ValueDesc value_desc(value_node,i);
154
155                                 Action::Handle action(ActivepointRemove::create());
156                                 
157                                 action->set_param("canvas",get_canvas());
158                                 action->set_param("canvas_interface",get_canvas_interface());
159                                 action->set_param("value_desc",value_desc);
160                                 action->set_param("activepoint",activepoint);
161
162                                 assert(action->is_ready());
163                                 if(!action->is_ready())
164                                         throw Error(Error::TYPE_NOTREADY);
165                         
166                                 add_action_front(action);                                               
167                         }
168                         catch(...)
169                         {
170                         }
171                 }
172                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
173                 try
174                 {
175                         ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_dynamic(value_node));
176                         Waypoint waypoint;
177                         waypoint=*value_node->find(time);
178                         assert(waypoint.get_time()==time);
179                         
180                         Action::Handle action(WaypointRemove::create());
181                         
182                         action->set_param("canvas",get_canvas());
183                         action->set_param("canvas_interface",get_canvas_interface());
184                         action->set_param("value_node",ValueNode::Handle(value_node));
185                         action->set_param("waypoint",waypoint);
186         
187                         assert(action->is_ready());
188                         if(!action->is_ready())
189                                 throw Error(Error::TYPE_NOTREADY);
190                 
191                         add_action_front(action);                                               
192                 }
193                 catch(...)
194                 {
195                 }
196         }
197 }
198
199
200 void
201 Action::KeyframeRemove::perform()
202 {
203         Action::Super::perform();
204         
205         if(get_canvas_interface())
206         {
207                 get_canvas_interface()->signal_keyframe_removed()(keyframe);
208         }
209         else synfig::warning("CanvasInterface not set on action");
210
211         get_canvas()->keyframe_list().erase(keyframe);  
212 }
213
214 void
215 Action::KeyframeRemove::undo()
216 {
217         try { get_canvas()->keyframe_list().find(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(keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
221         catch(synfig::Exception::NotFound) { }  
222
223         Action::Super::undo();
224         
225         get_canvas()->keyframe_list().add(keyframe);
226         
227         if(get_canvas_interface())
228         {
229                 get_canvas_interface()->signal_keyframe_added()(keyframe);
230         }
231         else synfig::warning("CanvasInterface not set on action");
232 }