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