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