7c2dfb2ba6e100e0837a8d94e092524c0485ccac
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescconnect.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 "layerparamconnect.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
36
37 #include "valuedescconnect.h"
38 #include <synfigapp/canvasinterface.h>
39
40 #include <synfigapp/general.h>
41
42 #endif
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
49
50 /* === M A C R O S ========================================================= */
51
52 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescConnect);
53 ACTION_SET_NAME(Action::ValueDescConnect,"value_desc_connect");
54 ACTION_SET_LOCAL_NAME(Action::ValueDescConnect,N_("Connect"));
55 ACTION_SET_TASK(Action::ValueDescConnect,"connect");
56 ACTION_SET_CATEGORY(Action::ValueDescConnect,Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
57 ACTION_SET_PRIORITY(Action::ValueDescConnect,0);
58 ACTION_SET_VERSION(Action::ValueDescConnect,"0.0");
59 ACTION_SET_CVS_ID(Action::ValueDescConnect,"$Id$");
60
61 /* === G L O B A L S ======================================================= */
62
63 /* === P R O C E D U R E S ================================================= */
64
65 /* === M E T H O D S ======================================================= */
66
67 Action::ValueDescConnect::ValueDescConnect()
68 {
69 }
70
71 synfig::String
72 Action::ValueDescConnect::get_local_name()const
73 {
74         // TRANSLATORS: This is used in the 'history' dialog when a connection is made.
75         return strprintf(_("Connect '%s' to '%s'"),
76                                          value_desc.get_description(false).c_str(),
77                                          value_node->get_id().c_str());
78 }
79
80 Action::ParamVocab
81 Action::ValueDescConnect::get_param_vocab()
82 {
83         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
84
85         ret.push_back(ParamDesc("dest",Param::TYPE_VALUEDESC)
86                 .set_local_name(_("Destination ValueDesc"))
87         );
88
89         ret.push_back(ParamDesc("src",Param::TYPE_VALUENODE)
90                 .set_local_name(_("Source ValueNode"))
91                 .set_mutual_exclusion("src_name")
92         );
93
94         ret.push_back(ParamDesc("src_name",Param::TYPE_STRING)
95                 .set_local_name(_("Source ValueNode Name"))
96                 .set_mutual_exclusion("src")
97                 .set_user_supplied()
98         );
99
100         return ret;
101 }
102
103 bool
104 Action::ValueDescConnect::is_candidate(const ParamList &x)
105 {
106         if(candidate_check(get_param_vocab(),x))
107         {
108                 // don't show the option of connecting to an existing Index parameter of the Duplicate layer
109                 if(x.count("dest"))
110                 {
111                         ValueDesc value_desc=x.find("dest")->second.get_value_desc();
112
113                         if (value_desc.parent_is_layer_param() &&
114                                 value_desc.get_layer()->get_name() == "duplicate" &&
115                                 value_desc.get_param_name() == "index")
116                                 return false;
117                 }
118
119                 if(x.count("src"))
120                 {
121                         ValueDesc value_desc=x.find("dest")->second.get_value_desc();
122                         ValueNode::Handle value_node=x.find("src")->second.get_value_node();
123                         if(value_desc.get_value_type()==value_node->get_type())
124                                 return true;
125                 }
126                 return true;
127         }
128         return false;
129 }
130
131 bool
132 Action::ValueDescConnect::set_param(const synfig::String& name, const Action::Param &param)
133 {
134         if(name=="dest" && param.get_type()==Param::TYPE_VALUEDESC)
135         {
136                 value_desc=param.get_value_desc();
137
138                 return true;
139         }
140
141         if(name=="src" && param.get_type()==Param::TYPE_VALUENODE)
142         {
143                 value_node=param.get_value_node();
144
145                 return true;
146         }
147
148         if(!value_node_name.empty() && !value_node && name=="canvas" && param.get_type()==Param::TYPE_CANVAS)
149         {
150                 value_node=param.get_canvas()->find_value_node(value_node_name);
151         }
152
153         if(name=="src_name" && param.get_type()==Param::TYPE_STRING)
154         {
155                 value_node_name=param.get_string();
156
157                 if(get_canvas())
158                 {
159                         value_node=get_canvas()->find_value_node(value_node_name);
160                         if(!value_node)
161                                 return false;
162                 }
163
164                 return true;
165         }
166
167         return Action::CanvasSpecific::set_param(name,param);
168 }
169
170 bool
171 Action::ValueDescConnect::is_ready()const
172 {
173         if(!value_desc || !value_node)
174                 return false;
175         return Action::CanvasSpecific::is_ready();
176 }
177
178 void
179 Action::ValueDescConnect::prepare()
180 {
181         clear();
182
183         if(value_desc.parent_is_canvas())
184         {
185                 ValueNode::Handle dest_value_node;
186                 dest_value_node=value_desc.get_value_node();
187
188                 Action::Handle action(ValueNodeReplace::create());
189
190                 action->set_param("canvas",get_canvas());
191                 action->set_param("canvas_interface",get_canvas_interface());
192                 action->set_param("src",value_node);
193                 action->set_param("dest",value_desc.get_value_node());
194
195                 assert(action->is_ready());
196                 if(!action->is_ready())
197                         throw Error(Error::TYPE_NOTREADY);
198
199                 add_action_front(action);
200                 return;
201         }
202         else
203         if(value_desc.parent_is_linkable_value_node())
204         {
205                 Action::Handle action(ValueNodeLinkConnect::create());
206
207                 action->set_param("canvas",get_canvas());
208                 action->set_param("canvas_interface",get_canvas_interface());
209                 action->set_param("parent_value_node",value_desc.get_parent_value_node());
210                 action->set_param("value_node", value_node);
211                 action->set_param("index",value_desc.get_index());
212
213                 assert(action->is_ready());
214                 if(!action->is_ready())
215                         throw Error(Error::TYPE_NOTREADY);
216
217                 add_action_front(action);
218                 return;
219         }
220         else
221         if(value_desc.parent_is_layer_param())
222         {
223                 Action::Handle action(LayerParamConnect::create());
224
225                 action->set_param("canvas",get_canvas());
226                 action->set_param("canvas_interface",get_canvas_interface());
227                 action->set_param("layer",value_desc.get_layer());
228                 action->set_param("param",value_desc.get_param_name());
229                 action->set_param("value_node",value_node);
230
231                 assert(action->is_ready());
232                 if(!action->is_ready())
233                         throw Error(Error::TYPE_NOTREADY);
234
235                 add_action_front(action);
236                 return;
237         }
238
239         throw Error(_("ValueDesc is not recognized or supported."));
240 }