91c58ca7b7c5774ebf9e453023cdb12d0d28ece2
[synfig.git] /
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 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layerparamdisconnect.h"
34 #include "valuenodelinkdisconnect.h"
35 #include "valuenodereplace.h"
36
37 #include "valuedescdisconnect.h"
38 #include <synfigapp/canvasinterface.h>
39 #include <synfig/valuenode_const.h>
40 #include <synfig/valuenode_duplicate.h>
41
42 #include <synfigapp/general.h>
43
44 #endif
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace synfigapp;
50 using namespace Action;
51
52 /* === M A C R O S ========================================================= */
53
54 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescDisconnect);
55 ACTION_SET_NAME(Action::ValueDescDisconnect,"value_desc_disconnect");
56 ACTION_SET_LOCAL_NAME(Action::ValueDescDisconnect,N_("Disconnect"));
57 ACTION_SET_TASK(Action::ValueDescDisconnect,"disconnect");
58 ACTION_SET_CATEGORY(Action::ValueDescDisconnect,Action::CATEGORY_VALUEDESC);
59 ACTION_SET_PRIORITY(Action::ValueDescDisconnect,-100);
60 ACTION_SET_VERSION(Action::ValueDescDisconnect,"0.0");
61 ACTION_SET_CVS_ID(Action::ValueDescDisconnect,"$Id$");
62
63 /* === G L O B A L S ======================================================= */
64
65 /* === P R O C E D U R E S ================================================= */
66
67 /* === M E T H O D S ======================================================= */
68
69 Action::ValueDescDisconnect::ValueDescDisconnect():
70         time(0)
71 {
72 }
73
74 synfig::String
75 Action::ValueDescDisconnect::get_local_name()const
76 {
77   // TRANSLATORS: This is used in the History dialog when a ValueNode is disconnected.
78   return strprintf(_("Disconnect %s"),
79                    value_desc
80                    ? value_desc.get_description().c_str()
81                    : _("ValueDesc"));
82 }
83
84 Action::ParamVocab
85 Action::ValueDescDisconnect::get_param_vocab()
86 {
87         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
88
89         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
90                 .set_local_name(_("ValueDesc"))
91         );
92
93         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
94                 .set_local_name(_("Time"))
95                 .set_optional()
96         );
97
98         return ret;
99 }
100
101 bool
102 Action::ValueDescDisconnect::is_candidate(const ParamList &x)
103 {
104         if (!candidate_check(get_param_vocab(),x))
105                 return false;
106
107         ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
108
109         // don't allow the Index parameter of the Duplicate layer to be disconnected
110         if(value_desc.parent_is_layer_param() && value_desc.get_layer()->get_name() == "duplicate" && value_desc.get_param_name() == "index")
111                 return false;
112         if(!value_desc.parent_is_canvas() && value_desc.is_value_node() && value_desc.get_value_node()->rcount()>1)
113                 return true;
114         if(value_desc.is_const())
115                 return false;
116         if(value_desc.is_value_node() && ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
117                 return false;
118         // don't allow Duplicate ValueNodes in the Children dialog to be disconnected
119         if(value_desc.is_value_node() &&
120            ValueNode_Duplicate::Handle::cast_dynamic(value_desc.get_value_node()) &&
121            !value_desc.parent_is_layer_param() &&
122            !value_desc.parent_is_value_node())
123                 return false;
124         return true;
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 }