Fix 1501383: "del key deletes object instead of character". Changed the LayerRemove...
[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 **  Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "activepointset.h"
34 #include <synfigapp/canvasinterface.h>
35
36 #include <synfigapp/general.h>
37
38 #endif
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
45
46 /* === M A C R O S ========================================================= */
47
48 ACTION_INIT(Action::ActivepointSet);
49 ACTION_SET_NAME(Action::ActivepointSet,"ActivepointSet");
50 ACTION_SET_LOCAL_NAME(Action::ActivepointSet,N_("Set Activepoint"));
51 ACTION_SET_TASK(Action::ActivepointSet,"set");
52 ACTION_SET_CATEGORY(Action::ActivepointSet,Action::CATEGORY_ACTIVEPOINT);
53 ACTION_SET_PRIORITY(Action::ActivepointSet,0);
54 ACTION_SET_VERSION(Action::ActivepointSet,"0.0");
55 ACTION_SET_CVS_ID(Action::ActivepointSet,"$Id$");
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::ActivepointSet::ActivepointSet()
64 {
65         set_dirty(true);
66 }
67
68 Action::ParamVocab
69 Action::ActivepointSet::get_param_vocab()
70 {
71         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
72
73         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
74                 .set_local_name(_("ValueDesc"))
75         );
76
77         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
78                 .set_local_name(_("Activepoint"))
79                 .set_desc(_("Activepoint to be changed"))
80                 .set_supports_multiple()
81         );
82
83         return ret;
84 }
85
86 bool
87 Action::ActivepointSet::is_candidate(const ParamList &x)
88 {
89         if (!candidate_check(get_param_vocab(),x))
90                 return false;
91
92         ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
93
94         return (value_desc.parent_is_value_node() &&
95                         // We need a dynamic list.
96                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()));
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 }