1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedesclink.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "valuedesclink.h"
35 #include <synfigapp/canvasinterface.h>
36 #include <synfig/valuenode_const.h>
38 #include <synfigapp/general.h>
44 using namespace synfig;
45 using namespace synfigapp;
46 using namespace Action;
48 /* === M A C R O S ========================================================= */
50 ACTION_INIT(Action::ValueDescLink);
51 ACTION_SET_NAME(Action::ValueDescLink,"ValueDescLink");
52 ACTION_SET_LOCAL_NAME(Action::ValueDescLink,N_("Link"));
53 ACTION_SET_TASK(Action::ValueDescLink,"connect");
54 ACTION_SET_CATEGORY(Action::ValueDescLink,Action::CATEGORY_VALUEDESC);
55 ACTION_SET_PRIORITY(Action::ValueDescLink,0);
56 ACTION_SET_VERSION(Action::ValueDescLink,"0.0");
57 ACTION_SET_CVS_ID(Action::ValueDescLink,"$Id$");
59 /* === G L O B A L S ======================================================= */
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 Action::ValueDescLink::ValueDescLink(): poison(false), status_level(0)
70 Action::ValueDescLink::get_param_vocab()
72 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
75 .set_local_name(_("ValueDesc to link"))
76 .set_requires_multiple()
83 Action::ValueDescLink::is_candidate(const ParamList &x)
85 return candidate_check(get_param_vocab(),x);
89 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param ¶m)
91 if(name=="time" && param.get_type()==Param::TYPE_TIME)
93 time=param.get_time();
97 // don't bother looking for the best value to use if there's already been an error
98 if (poison==true) return false;
100 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
102 ValueDesc value_desc(param.get_value_desc());
104 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
106 if(link_value_node==value_desc.get_value_node())
109 if(link_value_node && link_value_node->is_exported())
112 status_message = (_("Cannot link two different exported values ('") +
113 value_desc.get_value_node()->get_id() + _("' and '") +
114 link_value_node->get_id()) + _("')");
118 link_value_node=value_desc.get_value_node();
119 status_message = _("Used exported ValueNode ('") + link_value_node->get_id() + _("').");
121 else if(value_desc.is_value_node())
126 status_message = _("Using the only available ValueNode.");
127 link_value_node=value_desc.get_value_node();
129 else if(link_value_node->is_exported())
131 // we've already seen an exported value, so use that rather than the current value
133 // Use the one that is referenced more
134 else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
136 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
139 status_message = _("Using the most referenced ValueNode.");
140 link_value_node=value_desc.get_value_node();
142 else if (status_level <= 2)
145 status_message = _("Using the most referenced ValueNode.");
148 // If the current link value node is a constant and
149 // this one isn't, then give preference to the exotic
150 else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
153 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
154 link_value_node=value_desc.get_value_node();
156 else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
158 if (status_level <= 3)
161 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
164 // If both are animated, and this one has more waypoints, then use the one with more waypoints
165 else if(ValueNode_Animated::Handle::cast_dynamic(link_value_node) &&
166 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()) &&
167 ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() !=
168 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
170 if (ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() <
171 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
174 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
175 link_value_node=value_desc.get_value_node();
177 else if (status_level <= 4)
180 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
183 // Use the one that was least recently changed
184 else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
186 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
189 status_message = _("Everything is tied; using the least recently modified value.");
190 link_value_node=value_desc.get_value_node();
192 else if (status_level <= 5)
195 status_message = _("Everything is tied; using the least recently modified value.");
201 status_message = _("Absolutely everything is tied.");
205 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
207 // Everything must be of the same type
209 status_message = (strprintf(_("Cannot link two values of different types ('%s' and '%s')"),
210 ValueBase::type_local_name(value_desc.get_value_type()).c_str(),
211 ValueBase::type_local_name(value_desc_list.front().get_value_type()).c_str()));
215 value_desc_list.push_back(value_desc);
220 return Action::CanvasSpecific::set_param(name,param);
224 Action::ValueDescLink::is_ready()const
228 if(value_desc_list.size()<=1)
230 return Action::CanvasSpecific::is_ready();
234 Action::ValueDescLink::prepare()
237 throw Error(status_message.c_str());
239 if(value_desc_list.empty())
240 throw Error(Error::TYPE_NOTREADY);
246 status_message = _("No ValueNodes were available, so one was created.");
247 ValueDesc& value_desc(value_desc_list.back());
249 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
251 Action::Handle action(Action::create("ValueDescConnect"));
253 action->set_param("canvas",get_canvas());
254 action->set_param("canvas_interface",get_canvas_interface());
255 action->set_param("src",link_value_node);
256 action->set_param("dest",value_desc);
258 assert(action->is_ready());
259 if(!action->is_ready())
260 throw Error(Error::TYPE_NOTREADY);
262 add_action_front(action);
266 if(!link_value_node->is_exported())
268 Action::Handle action(Action::create("ValueNodeAdd"));
270 action->set_param("canvas",get_canvas());
271 action->set_param("canvas_interface",get_canvas_interface());
272 action->set_param("new",link_value_node);
273 action->set_param("name",strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
275 assert(action->is_ready());
276 if(!action->is_ready())
277 throw Error(Error::TYPE_NOTREADY);
279 add_action_front(action);
283 std::list<ValueDesc>::iterator iter;
284 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
286 ValueDesc& value_desc(*iter);
288 // only one of the selected items can be exported - that's the one we're linking to
289 // don't link it to itself
290 if (value_desc.is_exported())
293 Action::Handle action(Action::create("ValueDescConnect"));
295 action->set_param("canvas",get_canvas());
296 action->set_param("canvas_interface",get_canvas_interface());
297 action->set_param("src",link_value_node);
298 action->set_param("dest",value_desc);
300 assert(action->is_ready());
301 if(!action->is_ready())
302 throw Error(Error::TYPE_NOTREADY);
304 add_action_front(action);
307 synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());