Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_06 / src / synfigapp / actions / waypointremove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file waypointremove.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 "waypointremove.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::WaypointRemove);
46 ACTION_SET_NAME(Action::WaypointRemove,"waypoint_remove");
47 ACTION_SET_LOCAL_NAME(Action::WaypointRemove,"Remove Waypoint");
48 ACTION_SET_TASK(Action::WaypointRemove,"remove");
49 ACTION_SET_CATEGORY(Action::WaypointRemove,Action::CATEGORY_WAYPOINT);
50 ACTION_SET_PRIORITY(Action::WaypointRemove,0);
51 ACTION_SET_VERSION(Action::WaypointRemove,"0.0");
52 ACTION_SET_CVS_ID(Action::WaypointRemove,"$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::WaypointRemove::WaypointRemove()
61 {
62         waypoint.set_time(Time::begin()-1);
63         set_dirty(true);
64 }
65
66 Action::ParamVocab
67 Action::WaypointRemove::get_param_vocab()
68 {
69         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
70
71         ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
72                 .set_local_name(_("ValueNode (Animated)"))
73         );
74
75         ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
76                 .set_local_name(_("Waypoint"))
77                 .set_desc(_("Waypoint to be Removed"))
78         );
79
80         return ret;
81 }
82
83 bool
84 Action::WaypointRemove::is_candidate(const ParamList &x)
85 {
86         return candidate_check(get_param_vocab(),x);
87 }
88
89 bool
90 Action::WaypointRemove::set_param(const synfig::String& name, const Action::Param &param)
91 {
92         if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
93         {
94                 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
95
96                 return static_cast<bool>(value_node);
97         }
98         if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT)
99         {
100                 waypoint=param.get_waypoint();
101
102                 return true;
103         }
104
105         return Action::CanvasSpecific::set_param(name,param);
106 }
107
108 bool
109 Action::WaypointRemove::is_ready()const
110 {
111         if(!value_node || waypoint.get_time()==(Time::begin()-1))
112                 return false;
113         return Action::CanvasSpecific::is_ready();
114 }
115
116 void
117 Action::WaypointRemove::perform()
118 {
119         WaypointList::iterator iter(value_node->find(waypoint));
120
121         if((UniqueID)*iter!=(UniqueID)waypoint)
122                 throw Error(_("UniqueID mismatch, iter=%d, waypoint=%d"),iter->get_uid(),waypoint.get_uid());
123
124         if(iter->get_time()!=waypoint.get_time())
125                 throw Error(_("Time mismatch iter=%s, waypoint=%s"),iter->get_time().get_string().c_str(),waypoint.get_time().get_string().c_str());
126
127         waypoint=*iter;
128
129         value_node->erase(waypoint);
130
131         // In this case, we need to convert this to a
132         // constant value node
133         if(value_node->waypoint_list().size()==0)
134         {
135                 if(!value_node_ref)
136                 {
137                         value_node_ref=waypoint.get_value_node();
138                         if(!value_node_ref)
139                                 throw Error(_("Unable to create ValueNode_Reference"));
140                 }
141
142                 value_node->replace(value_node_ref);
143                 value_node->waypoint_list().clear();
144
145                 if(get_canvas_interface())
146                 {
147                         get_canvas_interface()->signal_value_node_replaced()(value_node,value_node_ref);
148                 }
149         }
150
151         value_node->changed();
152 }
153
154 void
155 Action::WaypointRemove::undo()
156 {
157         if(value_node_ref)
158         {
159                 if(value_node->waypoint_list().size()!=0)
160                         throw Error(_("This animated value node should be empty, but for some reason it isn't. This is a bug. (1)"));
161
162                 value_node_ref->replace(value_node);
163
164                 waypoint.set_value_node(value_node_ref);
165
166                 if(get_canvas_interface())
167                         get_canvas_interface()->signal_value_node_replaced()(value_node_ref,value_node);
168
169                 if(value_node->waypoint_list().size()!=0)
170                         throw Error(_("This animated value node should be empty, but for some reason it isn't. This is a bug. (2)"));
171         }
172
173         if(value_node->waypoint_list().size()!=0)
174         {
175                 try { value_node->find(waypoint.get_time()); throw Error(_("A Waypoint already exists at this point in time"));}
176                 catch(synfig::Exception::NotFound) { }
177
178                 try { if(value_node->find(waypoint)!=value_node->waypoint_list().end()) throw Error(_("This waypoint is already in the ValueNode"));}
179                 catch(synfig::Exception::NotFound) { }
180         }
181
182         value_node->add(waypoint);
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 }