Added a \todo note questioning a part of the code.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointsimpleadd.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointsimpleadd.cpp
3 **      \brief Simple add activepoint 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 "activepointsimpleadd.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::ActivepointSimpleAdd);
46 ACTION_SET_NAME(Action::ActivepointSimpleAdd,"waypoint_simpleadd");
47 ACTION_SET_LOCAL_NAME(Action::ActivepointSimpleAdd,"Simply Add Waypoint");
48 ACTION_SET_TASK(Action::ActivepointSimpleAdd,"add");
49 ACTION_SET_CATEGORY(Action::ActivepointSimpleAdd,Action::CATEGORY_WAYPOINT);
50 ACTION_SET_PRIORITY(Action::ActivepointSimpleAdd,0);
51 ACTION_SET_VERSION(Action::ActivepointSimpleAdd,"0.0");
52 ACTION_SET_CVS_ID(Action::ActivepointSimpleAdd,"$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::ActivepointSimpleAdd::ActivepointSimpleAdd()
61 {
62         set_dirty(true);
63         activepoint.set_time(Time::begin()-1);
64 }
65
66 Action::ParamVocab
67 Action::ActivepointSimpleAdd::get_param_vocab()
68 {
69         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
70
71         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
72                 .set_local_name(_("Destination ValueNode (Animated)"))
73         );
74
75         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
76                 .set_local_name(_("Activepoint"))
77                 .set_desc(_("Activepoint to be added"))
78         );
79
80         return ret;
81 }
82
83 bool
84 Action::ActivepointSimpleAdd::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 candidate_check(get_param_vocab(),x);
95 }
96
97 bool
98 Action::ActivepointSimpleAdd::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                 activepoint = param.get_activepoint();
119
120                 return true;
121         }
122
123         return Action::CanvasSpecific::set_param(name,param);
124 }
125
126 bool
127 Action::ActivepointSimpleAdd::is_ready()const
128 {
129         if(!value_node && activepoint.get_time() != (Time::begin()-1))
130                 return false;
131         return Action::CanvasSpecific::is_ready();
132 }
133
134 void
135 Action::ActivepointSimpleAdd::perform()
136 {
137         //remove any pretenders that lie at our destination
138         ValueNode_DynamicList::ListEntry::findresult iter = value_node->list[index]
139                                                                                                                         .find_time(activepoint.get_time());
140
141         time_overwrite = false;
142         if(iter.second)
143         {
144                 overwritten_ap = *iter.first;
145                 time_overwrite = true;
146                 value_node->list[index].erase(overwritten_ap);
147         }
148
149         //add the value node in since it's safe
150         value_node->list[index].add(activepoint);
151
152         //sort them...
153         value_node->list[index].timing_info.sort();
154
155         // Signal that a valuenode has been changed
156         value_node->changed();
157 }
158
159 void
160 Action::ActivepointSimpleAdd::undo()
161 {
162         //remove our old version...
163         ValueNode_DynamicList::ListEntry::findresult iter = value_node->list[index].find_uid(activepoint);
164
165         if(!iter.second)
166         {
167                 throw Error(_("The activepoint to remove no longer exists"));
168         }
169
170         //remove the offending value
171         value_node->list[index].erase(*iter.first); //could also just use waypoint
172
173         if(time_overwrite)
174         {
175                 value_node->list[index].add(overwritten_ap);
176         }
177
178         //sort them...
179         value_node->list[index].timing_info.sort();
180
181         // Signal that a valuenode has been changed
182         value_node->changed();
183 }