Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_03 / synfig-studio / src / synfigapp / actions / activepointadd.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file acitvepointadd.cpp
3 **      \brief Template File
4 **
5 **      $Id: activepointadd.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
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 "activepointadd.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::ActivepointAdd);
46 ACTION_SET_NAME(Action::ActivepointAdd,"activepoint_add");
47 ACTION_SET_LOCAL_NAME(Action::ActivepointAdd,"Add Activepoint");
48 ACTION_SET_TASK(Action::ActivepointAdd,"add");
49 ACTION_SET_CATEGORY(Action::ActivepointAdd,Action::CATEGORY_ACTIVEPOINT);
50 ACTION_SET_PRIORITY(Action::ActivepointAdd,0);
51 ACTION_SET_VERSION(Action::ActivepointAdd,"0.0");
52 ACTION_SET_CVS_ID(Action::ActivepointAdd,"$Id: activepointadd.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
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::ActivepointAdd::ActivepointAdd()
61 {
62         activepoint.set_time(Time::begin()-1);
63         time_set=false;
64         set_dirty(true);
65 }
66
67 Action::ParamVocab
68 Action::ActivepointAdd::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(_("New Activepoint"))
78                 .set_desc(_("Activepoint to be added"))
79                 .set_optional()
80         );
81
82         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
83                 .set_local_name(_("Time"))
84                 .set_desc(_("Time where activepoint is to be added"))
85                 .set_optional()
86         );
87
88         return ret;
89 }
90
91 bool
92 Action::ActivepointAdd::is_canidate(const ParamList &x)
93 {
94         if(canidate_check(get_param_vocab(),x))
95         {
96                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
97                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
98                         return false;
99
100                 // We need either a activepoint or a time.
101                 if(x.count("activepoint") || x.count("time"))
102                         return true;
103         }
104         return false;
105 }
106
107 bool
108 Action::ActivepointAdd::set_param(const synfig::String& name, const Action::Param &param)
109 {
110         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
111         {
112                 ValueDesc value_desc(param.get_value_desc());
113                 
114                 if(!value_desc.parent_is_value_node())
115                         return false;
116                 
117                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
118                 
119                 if(!value_node)
120                         return false;
121                 
122                 index=value_desc.get_index();
123                 
124                 if(time_set)
125                         calc_activepoint();
126                 
127                 return true;
128         }
129         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT && !time_set)
130         {
131                 activepoint=param.get_activepoint();
132                 
133                 return true;
134         }
135         if(name=="time" && param.get_type()==Param::TYPE_TIME && activepoint.get_time()==Time::begin()-1)
136         {
137                 activepoint.set_time(param.get_time());
138                 time_set=true;
139
140                 if(value_node)
141                         calc_activepoint();
142                 
143                 return true;
144         }
145
146         return Action::CanvasSpecific::set_param(name,param);
147 }
148
149 bool
150 Action::ActivepointAdd::is_ready()const
151 {
152         if(!value_node || activepoint.get_time()==(Time::begin()-1))
153                 return false;
154         return Action::CanvasSpecific::is_ready();
155 }
156
157 // This function is called if a time is specified, but not 
158 // a activepoint. In this case, we need to calculate the value
159 // of the activepoint
160 void
161 Action::ActivepointAdd::calc_activepoint()
162 {       
163         const Time time(activepoint.get_time());
164         activepoint.set_state(value_node->list[index].status_at_time(time));
165         activepoint.set_priority(0);
166         
167         // In this case, nothing is really changing, so there will be
168         // no need to redraw the window
169         set_dirty(false);
170 }
171
172 void
173 Action::ActivepointAdd::perform()
174 {       
175         try { value_node->list[index].find(activepoint.get_time()); throw Error(_("A Activepoint already exists at this point in time"));}
176         catch(synfig::Exception::NotFound) { }  
177
178         try { if(value_node->list[index].find(activepoint)!=value_node->list[index].timing_info.end()) throw Error(_("This activepoint is already in the ValueNode"));}
179         catch(synfig::Exception::NotFound) { }  
180         
181         value_node->list[index].add(activepoint);
182         value_node->changed();
183         
184         /*if(get_canvas_interface())
185         {
186                 get_canvas_interface()->signal_value_node_changed()(value_node);
187         }
188         else synfig::warning("CanvasInterface not set on action");
189         */
190 }
191
192 void
193 Action::ActivepointAdd::undo()
194 {
195         value_node->list[index].erase(activepoint);
196         value_node->changed();
197         /*
198         // Signal that a layer has been inserted
199         if(get_canvas_interface())
200         {
201                 get_canvas_interface()->signal_value_node_changed()(value_node);
202         }
203         else synfig::warning("CanvasInterface not set on action");
204         */
205 }