Don't show the option of connecting to an existing Index parameter of the Duplicate...
[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(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 Action::ParamVocab
71 Action::ValueDescConnect::get_param_vocab()
72 {
73         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74
75         ret.push_back(ParamDesc("dest",Param::TYPE_VALUEDESC)
76                 .set_local_name(_("Destination ValueDesc"))
77         );
78
79         ret.push_back(ParamDesc("src",Param::TYPE_VALUENODE)
80                 .set_local_name(_("Source ValueNode"))
81                 .set_mutual_exclusion("src_name")
82         );
83
84         ret.push_back(ParamDesc("src_name",Param::TYPE_STRING)
85                 .set_local_name(_("Source ValueNode Name"))
86                 .set_mutual_exclusion("src")
87                 .set_user_supplied()
88         );
89
90         return ret;
91 }
92
93 bool
94 Action::ValueDescConnect::is_candidate(const ParamList &x)
95 {
96         if(candidate_check(get_param_vocab(),x))
97         {
98                 // don't show the option of connecting to an existing Index parameter of the Duplicate layer
99                 if(x.count("dest"))
100                 {
101                         ValueDesc value_desc=x.find("dest")->second.get_value_desc();
102
103                         if (value_desc.parent_is_layer_param() &&
104                                 value_desc.get_layer()->get_name() == "duplicate" &&
105                                 value_desc.get_param_name() == "index")
106                                 return false;
107                 }
108
109                 if(x.count("src"))
110                 {
111                         ValueDesc value_desc=x.find("dest")->second.get_value_desc();
112                         ValueNode::Handle value_node=x.find("src")->second.get_value_node();
113                         if(value_desc.get_value_type()==value_node->get_type())
114                         {
115                                 printf("%s:%d\n", __FILE__, __LINE__);
116                                 return true;
117                         }
118                 }
119                 printf("%s:%d\n", __FILE__, __LINE__);
120                 return true;
121         }
122         printf("%s:%d\n", __FILE__, __LINE__);
123         return false;
124 }
125
126 bool
127 Action::ValueDescConnect::set_param(const synfig::String& name, const Action::Param &param)
128 {
129         if(name=="dest" && param.get_type()==Param::TYPE_VALUEDESC)
130         {
131                 value_desc=param.get_value_desc();
132
133                 return true;
134         }
135
136         if(name=="src" && param.get_type()==Param::TYPE_VALUENODE)
137         {
138                 value_node=param.get_value_node();
139
140                 return true;
141         }
142
143         if(!value_node_name.empty() && !value_node && name=="canvas" && param.get_type()==Param::TYPE_CANVAS)
144         {
145                 value_node=param.get_canvas()->find_value_node(value_node_name);
146         }
147
148         if(name=="src_name" && param.get_type()==Param::TYPE_STRING)
149         {
150                 value_node_name=param.get_string();
151
152                 if(get_canvas())
153                 {
154                         value_node=get_canvas()->find_value_node(value_node_name);
155                         if(!value_node)
156                                 return false;
157                 }
158
159                 return true;
160         }
161
162         return Action::CanvasSpecific::set_param(name,param);
163 }
164
165 bool
166 Action::ValueDescConnect::is_ready()const
167 {
168         if(!value_desc || !value_node)
169                 return false;
170         return Action::CanvasSpecific::is_ready();
171 }
172
173 void
174 Action::ValueDescConnect::prepare()
175 {
176         clear();
177
178         if(value_desc.parent_is_canvas())
179         {
180                 ValueNode::Handle dest_value_node;
181                 dest_value_node=value_desc.get_value_node();
182
183                 Action::Handle action(ValueNodeReplace::create());
184
185                 action->set_param("canvas",get_canvas());
186                 action->set_param("canvas_interface",get_canvas_interface());
187                 action->set_param("src",value_node);
188                 action->set_param("dest",value_desc.get_value_node());
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_linkable_value_node())
199         {
200                 Action::Handle action(ValueNodeLinkConnect::create());
201
202                 action->set_param("canvas",get_canvas());
203                 action->set_param("canvas_interface",get_canvas_interface());
204                 action->set_param("parent_value_node",value_desc.get_parent_value_node());
205                 action->set_param("value_node", value_node);
206                 action->set_param("index",value_desc.get_index());
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         else
216         if(value_desc.parent_is_layer_param())
217         {
218                 Action::Handle action(LayerParamConnect::create());
219
220                 action->set_param("canvas",get_canvas());
221                 action->set_param("canvas_interface",get_canvas_interface());
222                 action->set_param("layer",value_desc.get_layer());
223                 action->set_param("param",value_desc.get_param_name());
224                 action->set_param("value_node",value_node);
225
226                 assert(action->is_ready());
227                 if(!action->is_ready())
228                         throw Error(Error::TYPE_NOTREADY);
229
230                 add_action_front(action);
231                 return;
232         }
233
234         throw Error(_("ValueDesc is not recognized or supported."));
235 }