Added a \todo note questioning a part of the code.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / timepointsdelete.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file timepointsdelete.cpp
3 **      \brief Delete the Time Points File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2004 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 "timepointsdelete.h"
33 #include <synfig/layer_pastecanvas.h>
34 #include <synfigapp/canvasinterface.h>
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfig/valuenode_animated.h>
37
38 #include "activepointremove.h"
39 #include "waypointremove.h"
40 #include <synfigapp/timegather.h>
41
42 #include <typeinfo>
43
44 #endif
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace synfigapp;
50 using namespace Action;
51
52 /* === M A C R O S ========================================================= */
53
54 ACTION_INIT(Action::TimepointsDelete);
55 ACTION_SET_NAME(Action::TimepointsDelete,"timepoint_delete");
56 ACTION_SET_LOCAL_NAME(Action::TimepointsDelete,"Delete Time Points");
57 ACTION_SET_TASK(Action::TimepointsDelete,"delete");
58 ACTION_SET_CATEGORY(Action::TimepointsDelete,Action::CATEGORY_WAYPOINT|Action::CATEGORY_ACTIVEPOINT);
59 ACTION_SET_PRIORITY(Action::TimepointsDelete,0);
60 ACTION_SET_VERSION(Action::TimepointsDelete,"0.0");
61 ACTION_SET_CVS_ID(Action::TimepointsDelete,"$Id$");
62
63 /* === G L O B A L S ======================================================= */
64
65 /* === P R O C E D U R E S ================================================= */
66
67 /* === M E T H O D S ======================================================= */
68
69 Action::TimepointsDelete::TimepointsDelete()
70 {
71         set_dirty(false);
72 }
73
74 Action::ParamVocab
75 Action::TimepointsDelete::get_param_vocab()
76 {
77         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
78
79         ret.push_back(ParamDesc("addlayer",Param::TYPE_VALUE)
80                 .set_local_name(_("New Selected Layer"))
81                 .set_desc(_("A layer to add to our selected list"))
82                 .set_supports_multiple()
83                 .set_optional()
84         );
85
86         ret.push_back(ParamDesc("addcanvas",Param::TYPE_CANVAS)
87                 .set_local_name(_("New Selected Canvas"))
88                 .set_desc(_("A canvas to add to our selected list"))
89                 .set_supports_multiple()
90                 .set_optional()
91         );
92
93         ret.push_back(ParamDesc("addvaluedesc",Param::TYPE_VALUEDESC)
94                 .set_local_name(_("New Selected ValueBase"))
95                 .set_desc(_("A valuenode's description to add to our selected list"))
96                 .set_supports_multiple()
97                 .set_optional()
98         );
99
100         ret.push_back(ParamDesc("addtime",Param::TYPE_TIME)
101                 .set_local_name(_("New Selected Time Point"))
102                 .set_desc(_("A time point to add to our selected list"))
103                 .set_supports_multiple()
104         );
105
106         return ret;
107 }
108
109 bool
110 Action::TimepointsDelete::is_candidate(const ParamList &x)
111 {
112         if(!candidate_check(get_param_vocab(),x))
113                 return false;
114
115         if(     x.find("addlayer") == x.end() &&
116                 x.find("addcanvas") == x.end() &&
117                 x.find("addvaluedesc") == x.end())
118                 return false;
119         return true;
120 }
121
122 bool
123 Action::TimepointsDelete::set_param(const synfig::String& name, const Action::Param &param)
124 {
125         if(name=="addlayer" && param.get_type()==Param::TYPE_LAYER)
126         {
127                 //add a layer to the list
128                 sel_layers.push_back(param.get_layer());
129
130                 return true;
131         }
132
133         if(name=="addcanvas" && param.get_type()==Param::TYPE_CANVAS)
134         {
135                 //add a layer to the list
136                 sel_canvases.push_back(param.get_canvas());
137
138                 return true;
139         }
140
141         if(name=="addvaluedesc" && param.get_type()==Param::TYPE_VALUEDESC)
142         {
143                 //add a layer to the list
144                 sel_values.push_back(param.get_value_desc());
145
146                 return true;
147         }
148
149         if(name=="addtime" && param.get_type()==Param::TYPE_TIME)
150         {
151                 //add a layer to the list
152                 sel_times.insert(param.get_time());
153
154                 return true;
155         }
156
157         return Action::CanvasSpecific::set_param(name,param);
158 }
159
160 bool
161 Action::TimepointsDelete::is_ready()const
162 {
163         if((sel_layers.empty() && sel_canvases.empty() && sel_values.empty()) || sel_times.empty())
164                 return false;
165         return Action::CanvasSpecific::is_ready();
166 }
167
168 void
169 Action::TimepointsDelete::prepare()
170 {
171         clear();
172
173         if(sel_times.empty()) return;
174
175         //all our lists should be set correctly...
176
177         //build our sub-action list
178         //      and yes we do need to store it temporarily so we don't duplicate
179         //              an operation on a specific valuenode, etc....
180         timepoints_ref  match;
181
182         Time fps = get_canvas()->rend_desc().get_frame_rate();
183
184         //std::vector<synfig::Layer::Handle>
185         //synfig::info("Layers %d", sel_layers.size());
186         {
187                 std::vector<synfig::Layer::Handle>::iterator i = sel_layers.begin(),
188                                                                                                         end = sel_layers.end();
189
190                 for(; i != end; ++i)
191                 {
192                         //synfig::info("Recurse through a layer");
193                         recurse_layer(*i,sel_times,match);
194                 }
195         }
196
197         //std::vector<synfig::Canvas::Handle>   sel_canvases;
198         //synfig::info("Canvases %d", sel_canvases.size());
199         {
200                 std::vector<synfig::Canvas::Handle>::iterator   i = sel_canvases.begin(),
201                                                                                                                 end = sel_canvases.end();
202
203                 for(; i != end; ++i)
204                 {
205                         //synfig::info("Recurse through a canvas");
206                         recurse_canvas(*i,sel_times,match);
207                 }
208         }
209
210         //std::vector<synfigapp::ValueDesc>
211         //synfig::info("ValueBasedescs %d", sel_values.size());
212         {
213                 std::vector<synfigapp::ValueDesc>::iterator     i = sel_values.begin(),
214                                                                                                         end = sel_values.end();
215
216                 for(; i != end; ++i)
217                 {
218                         //synfig::info("Recurse through a valuedesc");
219                         recurse_valuedesc(*i,sel_times,match);
220                 }
221         }
222
223         //process the hell out of em...
224         {
225                 //must build from both lists
226                 timepoints_ref::waytracker::const_iterator      i = match.waypointbiglist.begin(),
227                                                                                                         end = match.waypointbiglist.end();
228                 for(; i != end; ++i)
229                 {
230                         //iterate through each waypoint for this specific valuenode
231                         std::set<synfig::Waypoint>::const_iterator      j = i->waypoints.begin(),
232                                                                                                                 end = i->waypoints.end();
233                         for(; j != end; ++j)
234                         {
235                                 Action::Handle action(WaypointRemove::create());
236
237                                 action->set_param("canvas",get_canvas());
238                                 action->set_param("canvas_interface",get_canvas_interface());
239                                 action->set_param("value_node",ValueNode::Handle(i->val));
240                                 action->set_param("waypoint",*j);
241
242                                 //run the action now that we've added everything
243                                 assert(action->is_ready());
244                                 if(!action->is_ready())
245                                         throw Error(Error::TYPE_NOTREADY);
246
247                                 add_action_front(action);
248                         }
249                 }
250         }
251         {
252                 //must build from both lists
253                 timepoints_ref::acttracker::const_iterator      i = match.actpointbiglist.begin(),
254                                                                                                         end = match.actpointbiglist.end();
255                 for(; i != end; ++i)
256                 {
257                         //iterate through each activepoint for this specific valuenode
258                         std::set<synfig::Activepoint>::const_iterator   j = i->activepoints.begin(),
259                                                                                                                         jend = i->activepoints.end();
260                         for(; j != jend; ++j)
261                         {
262                                 Action::Handle action(ActivepointRemove::create());
263
264                                 action->set_param("canvas",get_canvas());
265                                 action->set_param("canvas_interface",get_canvas_interface());
266                                 action->set_param("value_desc",i->val);
267                                 action->set_param("activepoint",*j);
268
269                                 //run the action now that everything should be in order
270                                 assert(action->is_ready());
271                                 if(!action->is_ready())
272                                 {
273                                         throw Error(Error::TYPE_NOTREADY);
274                                 }
275
276                                 add_action_front(action);
277                         }
278                 }
279         }
280 }
281
282 void
283 Action::TimepointsDelete::perform()
284 {
285         Action::Super::perform();
286 }