Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc1 / src / synfigapp / actions / valuedesclink.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedesclink.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) 2007 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 "valuedesclink.h"
34
35 #include <synfigapp/canvasinterface.h>
36 #include <synfig/valuenode_const.h>
37
38 #endif
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
45
46 /* === M A C R O S ========================================================= */
47
48 ACTION_INIT(Action::ValueDescLink);
49 ACTION_SET_NAME(Action::ValueDescLink,"value_desc_link");
50 ACTION_SET_LOCAL_NAME(Action::ValueDescLink,"Link");
51 ACTION_SET_TASK(Action::ValueDescLink,"connect");
52 ACTION_SET_CATEGORY(Action::ValueDescLink,Action::CATEGORY_VALUEDESC);
53 ACTION_SET_PRIORITY(Action::ValueDescLink,0);
54 ACTION_SET_VERSION(Action::ValueDescLink,"0.0");
55 ACTION_SET_CVS_ID(Action::ValueDescLink,"$Id$");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::ValueDescLink::ValueDescLink(): poison(false), status_level(0)
64 {
65 }
66
67 Action::ParamVocab
68 Action::ValueDescLink::get_param_vocab()
69 {
70         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
71
72         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
73                 .set_local_name(_("ValueDesc to link"))
74                 .set_requires_multiple()
75         );
76
77         return ret;
78 }
79
80 bool
81 Action::ValueDescLink::is_candidate(const ParamList &x)
82 {
83         return candidate_check(get_param_vocab(),x);
84 }
85
86 bool
87 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param &param)
88 {
89         if(name=="time" && param.get_type()==Param::TYPE_TIME)
90         {
91                 time=param.get_time();
92                 return true;
93         }
94
95         // don't bother looking for the best value to use if there's already been an error
96         if (poison==true) return false;
97
98         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
99         {
100                 ValueDesc value_desc(param.get_value_desc());
101
102                 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
103                 {
104                         if(link_value_node==value_desc.get_value_node())
105                                 return true;
106
107                         if(link_value_node && link_value_node->is_exported())
108                         {
109                                 poison=true;
110                                 status_message = (_("Cannot link two different exported values ('") +
111                                                                   value_desc.get_value_node()->get_id() + _("' and '") +
112                                                                   link_value_node->get_id()) + _("')");
113                                 return false;
114                         }
115
116                         link_value_node=value_desc.get_value_node();
117                         status_message = _("Used exported ValueNode ('") + link_value_node->get_id() + _("').");
118                 }
119                 else if(value_desc.is_value_node())
120                 {
121                         if(!link_value_node)
122                         {
123                                 status_level = 1;
124                                 status_message = _("Using the only available ValueNode.");
125                                 link_value_node=value_desc.get_value_node();
126                         }
127                         else if(link_value_node->is_exported())
128                         {
129                                 // we've already seen an exported value, so use that rather than the current value
130                         }
131                         // Use the one that is referenced more
132                         else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
133                         {
134                                 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
135                                 {
136                                         status_level = 2;
137                                         status_message = _("Using the most referenced ValueNode.");
138                                         link_value_node=value_desc.get_value_node();
139                                 }
140                                 else if (status_level <= 2)
141                                 {
142                                         status_level = 2;
143                                         status_message = _("Using the most referenced ValueNode.");
144                                 }
145                         }
146                         // If the current link value node is a constant and
147                         // this one isn't, then give preference to the exotic
148                         else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
149                         {
150                                 status_level = 3;
151                                 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
152                                 link_value_node=value_desc.get_value_node();
153                         }
154                         else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
155                         {
156                                 if (status_level <= 3)
157                                 {
158                                         status_level = 3;
159                                         status_message = _("There's a tie for most referenced; using the animated ValueNode.");
160                                 }
161                         }
162                         // If both are animated, and this one has more waypoints, then use the one with more waypoints
163                         else if(ValueNode_Animated::Handle::cast_dynamic(link_value_node) &&
164                                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()) &&
165                                         ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() !=
166                                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
167                         {
168                                 if (ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() <
169                                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
170                                 {
171                                         status_level = 4;
172                                         status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
173                                         link_value_node=value_desc.get_value_node();
174                                 }
175                                 else if (status_level <= 4)
176                                 {
177                                         status_level = 4;
178                                         status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
179                                 }
180                         }
181                         // Use the one that was least recently changed
182                         else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
183                         {
184                                 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
185                                 {
186                                         status_level = 5;
187                                         status_message = _("Everything is tied; using the least recently modified value.");
188                                         link_value_node=value_desc.get_value_node();
189                                 }
190                                 else if (status_level <= 5)
191                                 {
192                                         status_level = 5;
193                                         status_message = _("Everything is tied; using the least recently modified value.");
194                                 }
195                         }
196                         else
197                         {
198                                 status_level = 6;
199                                 status_message = _("Absolutely everything is tied.");
200                         }
201                 }
202
203                 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
204                 {
205                         // Everything must be of the same type
206                         poison=true;
207                         status_message = (_("Cannot link two values of different types ('") +
208                                                           value_desc.get_value().type_name() + _("' and '") +
209                                                           value_desc_list.front().get_value().type_name() +_("')"));
210                         return false;
211                 }
212
213                 value_desc_list.push_back(value_desc);
214
215                 return true;
216         }
217
218         return Action::CanvasSpecific::set_param(name,param);
219 }
220
221 bool
222 Action::ValueDescLink::is_ready()const
223 {
224         if(poison)
225                 return true;
226         if(value_desc_list.size()<=1)
227                 return false;
228         return Action::CanvasSpecific::is_ready();
229 }
230
231 void
232 Action::ValueDescLink::prepare()
233 {
234         if(poison)
235                 throw Error(status_message.c_str());
236
237         if(value_desc_list.empty())
238                 throw Error(Error::TYPE_NOTREADY);
239
240         clear();
241
242         if(!link_value_node)
243         {
244                 status_message = _("No ValueNodes were available, so one was created.");
245                 ValueDesc& value_desc(value_desc_list.front());
246
247                 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
248
249                 Action::Handle action(Action::create("value_desc_connect"));
250
251                 action->set_param("canvas",get_canvas());
252                 action->set_param("canvas_interface",get_canvas_interface());
253                 action->set_param("src",link_value_node);
254                 action->set_param("dest",value_desc);
255
256                 assert(action->is_ready());
257                 if(!action->is_ready())
258                         throw Error(Error::TYPE_NOTREADY);
259
260                 add_action_front(action);
261         }
262
263         /*
264         if(!link_value_node->is_exported())
265         {
266                 Action::Handle action(Action::create("value_node_add"));
267
268                 action->set_param("canvas",get_canvas());
269                 action->set_param("canvas_interface",get_canvas_interface());
270                 action->set_param("new",link_value_node);
271                 action->set_param("name",strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
272
273                 assert(action->is_ready());
274                 if(!action->is_ready())
275                         throw Error(Error::TYPE_NOTREADY);
276
277                 add_action_front(action);
278         }
279         */
280
281         std::list<ValueDesc>::iterator iter;
282         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
283         {
284                 ValueDesc& value_desc(*iter);
285
286                 Action::Handle action(Action::create("value_desc_connect"));
287
288                 action->set_param("canvas",get_canvas());
289                 action->set_param("canvas_interface",get_canvas_interface());
290                 action->set_param("src",link_value_node);
291                 action->set_param("dest",value_desc);
292
293                 assert(action->is_ready());
294                 if(!action->is_ready())
295                         throw Error(Error::TYPE_NOTREADY);
296
297                 add_action_front(action);
298         }
299
300         synfig::info(status_message);
301 }