Added a \todo note questioning a part of the code.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointset.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointset.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 "activepointset.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44
45 ACTION_INIT(Action::ActivepointSet);
46 ACTION_SET_NAME(Action::ActivepointSet,"activepoint_set");
47 ACTION_SET_LOCAL_NAME(Action::ActivepointSet,"Set Activepoint");
48 ACTION_SET_TASK(Action::ActivepointSet,"set");
49 ACTION_SET_CATEGORY(Action::ActivepointSet,Action::CATEGORY_ACTIVEPOINT);
50 ACTION_SET_PRIORITY(Action::ActivepointSet,0);
51 ACTION_SET_VERSION(Action::ActivepointSet,"0.0");
52 ACTION_SET_CVS_ID(Action::ActivepointSet,"$Id$");
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 Action::ActivepointSet::ActivepointSet()
61 {
62         set_dirty(true);
63 }
64
65 Action::ParamVocab
66 Action::ActivepointSet::get_param_vocab()
67 {
68         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
69
70         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
71                 .set_local_name(_("ValueDesc"))
72         );
73
74         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
75                 .set_local_name(_("Activepoint"))
76                 .set_desc(_("Activepoint to be changed"))
77                 .set_supports_multiple()
78         );
79
80         return ret;
81 }
82
83 bool
84 Action::ActivepointSet::is_candidate(const ParamList &x)
85 {
86         if(candidate_check(get_param_vocab(),x))
87         {
88                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
89                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
90                         return false;
91
92                 return true;
93         }
94         return false;
95 }
96
97 bool
98 Action::ActivepointSet::set_param(const synfig::String& name, const Action::Param &param)
99 {
100         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
101         {
102                 ValueDesc value_desc(param.get_value_desc());
103
104                 if(!value_desc.parent_is_value_node())
105                         return false;
106
107                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
108
109                 if(!value_node)
110                         return false;
111
112                 index=value_desc.get_index();
113
114                 return true;
115         }
116         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT)
117         {
118                 //NOTE: there is no duplication checking at the moment
119                 activepoints.push_back(param.get_activepoint());
120
121                 return true;
122         }
123
124         return Action::CanvasSpecific::set_param(name,param);
125 }
126
127 bool
128 Action::ActivepointSet::is_ready()const
129 {
130         if(!value_node || activepoints.empty())
131                 return false;
132         return Action::CanvasSpecific::is_ready();
133 }
134
135 void
136 Action::ActivepointSet::perform()
137 {
138         typedef ValueNode_DynamicList::ListEntry::ActivepointList AList;
139         AList::iterator iter;
140
141 #if 1
142         vector<AList::iterator> iters;
143         vector<Activepoint>::iterator i = activepoints.begin(), end = activepoints.end();
144
145         for(; i != end; ++i)
146         {
147                 try { iters.push_back(value_node->list[index].find(*i)); }
148                 catch(synfig::Exception::NotFound)
149                 {
150                         throw Error(_("Unable to find activepoint"));
151                 }
152         }
153
154         //check to see which valuenodes are going to override because of the time...
155         ValueNode_DynamicList::ListEntry::findresult timeiter;
156
157         for(i = activepoints.begin(); i != end; ++i)
158         {
159                 timeiter = value_node->list[index].find_time(i->get_time());
160
161                 bool candelete = timeiter.second;
162
163                 //we only want to track overwrites (not activepoints that are also being modified)
164                 if(candelete)
165                 {
166                         for(vector<AList::iterator>::iterator ii = iters.begin(); ii != iters.end(); ++ii)
167                         {
168                                 if(timeiter.first == *ii)
169                                 {
170                                         candelete = false;
171                                         break;
172                                 }
173                         }
174                 }
175
176                 //if we can still delete it after checking, record it, and then remove them all later
177                 if(candelete)
178                 {
179                         Activepoint a = *timeiter.first;
180                         overwritten_activepoints.push_back(a);
181                 }
182         }
183
184         //overwrite all the valuenodes we're supposed to set
185         {
186                 i = activepoints.begin();
187                 for(vector<AList::iterator>::iterator ii = iters.begin(); ii != iters.end() && i != end; ++ii, ++i)
188                 {
189                         old_activepoints.push_back(**ii);
190                         **ii = *i; //set the point to the corresponding point in the normal waypoint list
191                 }
192         }
193
194         //remove all the points we're supposed to be overwriting
195         {
196                 vector<Activepoint>::iterator   oi = overwritten_activepoints.begin(),
197                                                                                 oend = overwritten_activepoints.end();
198                 for(; oi != oend; ++oi)
199                 {
200                         value_node->list[index].erase(*oi);
201                 }
202         }
203
204 #else
205         try { iter=value_node->list[index].find(activepoint); }
206         catch(synfig::Exception::NotFound)
207         {
208                 throw Error(_("Unable to find activepoint"));
209         }
210
211         //find the value at the old time before we replace it
212         ValueNode_DynamicList::ListEntry::findresult timeiter;
213         timeiter = value_node->list[index].find_time(activepoint.get_time());
214
215         //we only want to track overwrites (not inplace modifications)
216         if(timeiter.second && activepoint.get_uid() == timeiter.first->get_uid())
217         {
218                 timeiter.second = false;
219         }
220
221         old_activepoint=*iter;
222         *iter=activepoint;
223
224         if(timeiter.second)
225         {
226                 synfig::info("Erasing the found activepoint");
227                 time_overwrite = true;
228                 overwritten_ap = *timeiter.first;
229
230                 value_node->list[index].erase(overwritten_ap);
231         }
232
233 #endif
234
235         value_node->list[index].timing_info.sort();
236
237         // Signal that a valuenode has been changed
238         value_node->changed();
239 }
240
241 void
242 Action::ActivepointSet::undo()
243 {
244         ValueNode_DynamicList::ListEntry::ActivepointList::iterator iter;
245
246 #if 1
247         vector<Activepoint>::iterator i = old_activepoints.begin(), end = old_activepoints.end();
248
249         for(; i != end; ++i)
250         {
251                 try { iter = value_node->list[index].find(*i); }
252                 catch(synfig::Exception::NotFound)
253                 {
254                         throw Error(_("Unable to find activepoint"));
255                 }
256
257                 //overwrite with old one
258                 *iter = *i;
259         }
260
261         //add back in all the points that we removed before...
262         {
263                 vector<Activepoint>::iterator   oi = overwritten_activepoints.begin(),
264                                                                                 oend = overwritten_activepoints.end();
265                 for(; oi != oend; ++oi)
266                 {
267                         value_node->list[index].add(*oi);
268                 }
269         }
270
271 #else
272         try { iter=value_node->list[index].find(activepoint); }
273         catch(synfig::Exception::NotFound)
274         {
275                 throw Error(_("Unable to find activepoint"));
276         }
277
278         *iter=old_activepoint;
279
280         if(time_overwrite)
281         {
282                 value_node->list[index].add(overwritten_ap);
283         }
284 #endif
285
286         value_node->list[index].timing_info.sort();
287
288         // Signal that a valuenode has been changed
289         value_node->changed();
290 }