Tidy up some is_candidate() methods.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointremove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointremove.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 "activepointremove.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::ActivepointRemove);
48 ACTION_SET_NAME(Action::ActivepointRemove,"activepoint_remove");
49 ACTION_SET_LOCAL_NAME(Action::ActivepointRemove,N_("Remove Activepoint"));
50 ACTION_SET_TASK(Action::ActivepointRemove,"remove");
51 ACTION_SET_CATEGORY(Action::ActivepointRemove,Action::CATEGORY_ACTIVEPOINT);
52 ACTION_SET_PRIORITY(Action::ActivepointRemove,0);
53 ACTION_SET_VERSION(Action::ActivepointRemove,"0.0");
54 ACTION_SET_CVS_ID(Action::ActivepointRemove,"$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::ActivepointRemove::ActivepointRemove()
63 {
64         activepoint.set_time(Time::begin()-1);
65         set_dirty(true);
66 }
67
68 Action::ParamVocab
69 Action::ActivepointRemove::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         );
81
82         return ret;
83 }
84
85 bool
86 Action::ActivepointRemove::is_candidate(const ParamList &x)
87 {
88         if (!candidate_check(get_param_vocab(),x))
89                 return false;
90
91         ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
92
93         return (value_desc.parent_is_value_node() &&
94                         // We need a dynamic list.
95                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()));
96 }
97
98 bool
99 Action::ActivepointRemove::set_param(const synfig::String& name, const Action::Param &param)
100 {
101         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
102         {
103                 ValueDesc value_desc(param.get_value_desc());
104
105                 if(!value_desc.parent_is_value_node())
106                         return false;
107
108                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
109
110                 if(!value_node)
111                         return false;
112
113                 index=value_desc.get_index();
114
115                 return true;
116         }
117         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT)
118         {
119                 activepoint=param.get_activepoint();
120
121                 return true;
122         }
123
124         return Action::CanvasSpecific::set_param(name,param);
125 }
126
127 bool
128 Action::ActivepointRemove::is_ready()const
129 {
130         if(!value_node || activepoint.get_time()==(Time::begin()-1))
131                 return false;
132         return Action::CanvasSpecific::is_ready();
133 }
134
135 void
136 Action::ActivepointRemove::perform()
137 {
138         ValueNode_DynamicList::ListEntry::ActivepointList::iterator iter;
139
140         try { iter=value_node->list[index].find(activepoint); }
141         catch(synfig::Exception::NotFound)
142         {
143                 throw Error(_("Unable to find activepoint"));
144         }
145
146         value_node->list[index].erase(activepoint);
147         value_node->changed();
148
149         /*
150         // Signal that a layer has been inserted
151         if(get_canvas_interface())
152         {
153                 get_canvas_interface()->signal_value_node_changed()(value_node);
154         }
155         else synfig::warning("CanvasInterface not set on action");
156         */
157 }
158
159 void
160 Action::ActivepointRemove::undo()
161 {
162         try { value_node->list[index].find(activepoint.get_time()); throw Error(_("A Activepoint already exists at this point in time"));}
163         catch(synfig::Exception::NotFound) { }
164
165         try { if(value_node->list[index].find(activepoint)!=value_node->list[index].timing_info.end()) throw Error(_("This activepoint is already in the ValueNode"));}
166         catch(synfig::Exception::NotFound) { }
167
168         value_node->list[index].add(activepoint);
169         value_node->changed();
170         /*
171         // Signal that a layer has been inserted
172         if(get_canvas_interface())
173         {
174                 get_canvas_interface()->signal_value_node_changed()(value_node);
175         }
176         else synfig::warning("CanvasInterface not set on action");
177         */
178 }