initial version
[synfig.git] / synfig-studio / trunk / src / sinfgapp / actions / keyframeremove.cpp
1 /* === S I N F 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 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "keyframeremove.h"
32 #include <sinfgapp/canvasinterface.h>
33 #include <sinfg/valuenode_dynamiclist.h>
34 #include <sinfg/valuenode_animated.h>
35 #include "activepointremove.h"
36 #include "waypointremove.h"
37
38 #endif
39
40 using namespace std;
41 using namespace etl;
42 using namespace sinfg;
43 using namespace sinfgapp;
44 using namespace Action;
45
46 /* === M A C R O S ========================================================= */
47
48 ACTION_INIT(Action::KeyframeRemove);
49 ACTION_SET_NAME(Action::KeyframeRemove,"keyframe_remove");
50 ACTION_SET_LOCAL_NAME(Action::KeyframeRemove,"Remove Keyframe");
51 ACTION_SET_TASK(Action::KeyframeRemove,"remove");
52 ACTION_SET_CATEGORY(Action::KeyframeRemove,Action::CATEGORY_KEYFRAME);
53 ACTION_SET_PRIORITY(Action::KeyframeRemove,0);
54 ACTION_SET_VERSION(Action::KeyframeRemove,"0.0");
55 ACTION_SET_CVS_ID(Action::KeyframeRemove,"$Id: keyframeremove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::KeyframeRemove::KeyframeRemove()
64 {
65         keyframe.set_time(Time::begin()-1);
66         set_dirty(true);
67 }
68
69 Action::ParamVocab
70 Action::KeyframeRemove::get_param_vocab()
71 {
72         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
73         
74         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
75                 .set_local_name(_("Keyframe"))
76                 .set_desc(_("Keyframe to be removed"))
77         );
78
79         return ret;
80 }
81
82 bool
83 Action::KeyframeRemove::is_canidate(const ParamList &x)
84 {
85         return canidate_check(get_param_vocab(),x);
86 }
87
88 bool
89 Action::KeyframeRemove::set_param(const sinfg::String& name, const Action::Param &param)
90 {
91         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
92         {
93                 keyframe=param.get_keyframe();
94                 
95                 return true;
96         }
97
98         return Action::CanvasSpecific::set_param(name,param);
99 }
100
101 bool
102 Action::KeyframeRemove::is_ready()const
103 {
104         if(keyframe.get_time()==(Time::begin()-1))
105                 return false;
106         return Action::CanvasSpecific::is_ready();
107 }
108
109 void
110 Action::KeyframeRemove::prepare()
111 {
112         clear();
113
114         try { get_canvas()->keyframe_list().find(keyframe);}
115         catch(sinfg::Exception::NotFound)
116         {
117                 throw Error(_("Unable to find the given keyframe"));
118         }       
119
120         
121         {
122                 std::vector<sinfgapp::ValueDesc> value_desc_list;
123                 get_canvas_interface()->find_important_value_descs(value_desc_list);
124                 while(!value_desc_list.empty())
125                 {
126                         process_value_desc(value_desc_list.back());
127                         value_desc_list.pop_back();
128                 }
129         }
130 }
131
132 void
133 Action::KeyframeRemove::process_value_desc(const sinfgapp::ValueDesc& value_desc)
134 {       
135         const sinfg::Time time(keyframe.get_time());
136
137         if(value_desc.is_value_node())
138         {
139                 ValueNode::Handle value_node(value_desc.get_value_node());
140         
141                 // If we are a dynamic list, then we need to update the ActivePoints
142                 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
143                 {
144                         ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
145                         int i;
146                         for(i=0;i<value_node->link_count();i++)
147                         try
148                         {
149                                 Activepoint activepoint;
150                                 activepoint=*value_node->list[i].find(time);
151
152                                 sinfgapp::ValueDesc value_desc(value_node,i);
153
154                                 Action::Handle action(ActivepointRemove::create());
155                                 
156                                 action->set_param("canvas",get_canvas());
157                                 action->set_param("canvas_interface",get_canvas_interface());
158                                 action->set_param("value_desc",value_desc);
159                                 action->set_param("activepoint",activepoint);
160
161                                 assert(action->is_ready());
162                                 if(!action->is_ready())
163                                         throw Error(Error::TYPE_NOTREADY);
164                         
165                                 add_action_front(action);                                               
166                         }
167                         catch(...)
168                         {
169                         }
170                 }
171                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
172                 try
173                 {
174                         ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_dynamic(value_node));
175                         Waypoint waypoint;
176                         waypoint=*value_node->find(time);
177                         assert(waypoint.get_time()==time);
178                         
179                         Action::Handle action(WaypointRemove::create());
180                         
181                         action->set_param("canvas",get_canvas());
182                         action->set_param("canvas_interface",get_canvas_interface());
183                         action->set_param("value_node",ValueNode::Handle(value_node));
184                         action->set_param("waypoint",waypoint);
185         
186                         assert(action->is_ready());
187                         if(!action->is_ready())
188                                 throw Error(Error::TYPE_NOTREADY);
189                 
190                         add_action_front(action);                                               
191                 }
192                 catch(...)
193                 {
194                 }
195         }
196 }
197
198
199 void
200 Action::KeyframeRemove::perform()
201 {
202         Action::Super::perform();
203         
204         if(get_canvas_interface())
205         {
206                 get_canvas_interface()->signal_keyframe_removed()(keyframe);
207         }
208         else sinfg::warning("CanvasInterface not set on action");
209
210         get_canvas()->keyframe_list().erase(keyframe);  
211 }
212
213 void
214 Action::KeyframeRemove::undo()
215 {
216         try { get_canvas()->keyframe_list().find(keyframe.get_time()); throw Error(_("A Keyframe already exists at this point in time"));}
217         catch(sinfg::Exception::NotFound) { }   
218
219         try { get_canvas()->keyframe_list().find(keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
220         catch(sinfg::Exception::NotFound) { }   
221
222         Action::Super::undo();
223         
224         get_canvas()->keyframe_list().add(keyframe);
225         
226         if(get_canvas_interface())
227         {
228                 get_canvas_interface()->signal_keyframe_added()(keyframe);
229         }
230         else sinfg::warning("CanvasInterface not set on action");
231 }