Use get_layer_descriptions() to format the layer list rather than including a copy...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuedescdisconnect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescdisconnect.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 "layerparamdisconnect.h"
33 #include "valuenodelinkdisconnect.h"
34 #include "valuenodereplace.h"
35
36 #include "valuedescdisconnect.h"
37 #include <synfigapp/canvasinterface.h>
38 #include <synfig/valuenode_const.h>
39 #include <synfig/valuenode_duplicate.h>
40
41 #include <synfigapp/general.h>
42
43 #endif
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48 using namespace synfigapp;
49 using namespace Action;
50
51 /* === M A C R O S ========================================================= */
52
53 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescDisconnect);
54 ACTION_SET_NAME(Action::ValueDescDisconnect,"value_desc_disconnect");
55 ACTION_SET_LOCAL_NAME(Action::ValueDescDisconnect,N_("Disconnect"));
56 ACTION_SET_TASK(Action::ValueDescDisconnect,"disconnect");
57 ACTION_SET_CATEGORY(Action::ValueDescDisconnect,Action::CATEGORY_VALUEDESC);
58 ACTION_SET_PRIORITY(Action::ValueDescDisconnect,-100);
59 ACTION_SET_VERSION(Action::ValueDescDisconnect,"0.0");
60 ACTION_SET_CVS_ID(Action::ValueDescDisconnect,"$Id$");
61
62 /* === G L O B A L S ======================================================= */
63
64 /* === P R O C E D U R E S ================================================= */
65
66 /* === M E T H O D S ======================================================= */
67
68 Action::ValueDescDisconnect::ValueDescDisconnect():
69         time(0)
70 {
71 }
72
73 synfig::String
74 Action::ValueDescDisconnect::get_local_name()const
75 {
76   // TRANSLATORS: This is used in the History dialog when a ValueNode is disconnected.
77   return strprintf(_("Disconnect %s"),
78                    value_desc
79                    ? value_desc.get_description().c_str()
80                    : _("ValueDesc"));
81 }
82
83 Action::ParamVocab
84 Action::ValueDescDisconnect::get_param_vocab()
85 {
86         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
87
88         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
89                 .set_local_name(_("ValueDesc"))
90         );
91
92         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
93                 .set_local_name(_("Time"))
94                 .set_optional()
95         );
96
97         return ret;
98 }
99
100 bool
101 Action::ValueDescDisconnect::is_candidate(const ParamList &x)
102 {
103         if(candidate_check(get_param_vocab(),x))
104         {
105                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
106
107                 // don't allow the Index parameter of the Duplicate layer to be disconnected
108                 if(value_desc.parent_is_layer_param() && value_desc.get_layer()->get_name() == "duplicate" && value_desc.get_param_name() == "index")
109                         return false;
110                 if(!value_desc.parent_is_canvas() && value_desc.is_value_node() && value_desc.get_value_node()->rcount()>1)
111                         return true;
112                 if(value_desc.is_const())
113                         return false;
114                 if(value_desc.is_value_node() && ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
115                         return false;
116                 // don't allow Duplicate ValueNodes in the Children dialog to be disconnected
117                 if(value_desc.is_value_node() &&
118                    ValueNode_Duplicate::Handle::cast_dynamic(value_desc.get_value_node()) &&
119                    !value_desc.parent_is_layer_param() &&
120                    !value_desc.parent_is_value_node())
121                         return false;
122                 return true;
123         }
124         return false;
125 }
126
127 bool
128 Action::ValueDescDisconnect::set_param(const synfig::String& name, const Action::Param &param)
129 {
130         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
131         {
132                 value_desc=param.get_value_desc();
133
134                 return true;
135         }
136
137         if(name=="time" && param.get_type()==Param::TYPE_TIME)
138         {
139                 time=param.get_time();
140
141                 return true;
142         }
143
144         return Action::CanvasSpecific::set_param(name,param);
145 }
146
147 bool
148 Action::ValueDescDisconnect::is_ready()const
149 {
150         if(!value_desc)
151                 return false;
152         return Action::CanvasSpecific::is_ready();
153 }
154
155 void
156 Action::ValueDescDisconnect::prepare()
157 {
158         clear();
159
160         if(value_desc.parent_is_canvas())
161         {
162                 ValueNode::Handle src_value_node;
163                 src_value_node=ValueNode_Const::create((*value_desc.get_value_node())(time));
164
165                 Action::Handle action(ValueNodeReplace::create());
166
167                 action->set_param("canvas",get_canvas());
168                 action->set_param("canvas_interface",get_canvas_interface());
169                 action->set_param("src",src_value_node);
170                 action->set_param("dest",value_desc.get_value_node());
171
172                 assert(action->is_ready());
173                 if(!action->is_ready())
174                         throw Error(Error::TYPE_NOTREADY);
175
176                 add_action_front(action);
177                 return;
178         }
179         else
180         if(value_desc.parent_is_linkable_value_node())
181         {
182                 Action::Handle action(ValueNodeLinkDisconnect::create());
183
184                 action->set_param("canvas",get_canvas());
185                 action->set_param("canvas_interface",get_canvas_interface());
186                 action->set_param("parent_value_node",value_desc.get_parent_value_node());
187                 action->set_param("index",value_desc.get_index());
188                 action->set_param("time",time);
189
190                 assert(action->is_ready());
191                 if(!action->is_ready())
192                         throw Error(Error::TYPE_NOTREADY);
193
194                 add_action_front(action);
195                 return;
196         }
197         else
198         if(value_desc.parent_is_layer_param())
199         {
200                 Action::Handle action(LayerParamDisconnect::create());
201
202                 action->set_param("canvas",get_canvas());
203                 action->set_param("canvas_interface",get_canvas_interface());
204                 action->set_param("layer",value_desc.get_layer());
205                 action->set_param("param",value_desc.get_param_name());
206                 action->set_param("time",time);
207
208                 assert(action->is_ready());
209                 if(!action->is_ready())
210                         throw Error(Error::TYPE_NOTREADY);
211
212                 add_action_front(action);
213                 return;
214         }
215
216         throw Error(_("ValueDesc is not recognized or supported."));
217 }