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