1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenodereplace.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "valuenodereplace.h"
33 #include <synfigapp/canvasinterface.h>
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
43 /* === M A C R O S ========================================================= */
45 ACTION_INIT(Action::ValueNodeReplace);
46 ACTION_SET_NAME(Action::ValueNodeReplace,"value_node_replace");
47 ACTION_SET_LOCAL_NAME(Action::ValueNodeReplace,"Replace ValueNode");
48 ACTION_SET_TASK(Action::ValueNodeReplace,"replace");
49 ACTION_SET_CATEGORY(Action::ValueNodeReplace,Action::CATEGORY_VALUENODE|Action::CATEGORY_DRAG);
50 ACTION_SET_PRIORITY(Action::ValueNodeReplace,0);
51 ACTION_SET_VERSION(Action::ValueNodeReplace,"0.0");
52 ACTION_SET_CVS_ID(Action::ValueNodeReplace,"$Id$");
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 void swap_guid(const ValueNode::Handle& a,const ValueNode::Handle& b)
60 GUID old_a(a->get_guid());
63 GUID old_b(b->get_guid());
70 /* === M E T H O D S ======================================================= */
72 Action::ValueNodeReplace::ValueNodeReplace():
78 Action::ValueNodeReplace::get_param_vocab()
80 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
82 ret.push_back(ParamDesc("dest",Param::TYPE_VALUENODE)
83 .set_local_name(_("Destination ValueNode"))
84 .set_desc(_("ValueNode to replaced"))
87 ret.push_back(ParamDesc("src",Param::TYPE_VALUENODE)
88 .set_local_name(_("Source ValueNode"))
89 .set_desc(_("ValueNode that will replace the destination"))
96 Action::ValueNodeReplace::is_candidate(const ParamList &x)
98 return candidate_check(get_param_vocab(),x);
102 Action::ValueNodeReplace::set_param(const synfig::String& name, const Action::Param ¶m)
104 if(name=="dest" && param.get_type()==Param::TYPE_VALUENODE)
106 dest_value_node=param.get_value_node();
111 if(name=="src" && param.get_type()==Param::TYPE_VALUENODE)
113 src_value_node=param.get_value_node();
118 return Action::CanvasSpecific::set_param(name,param);
122 Action::ValueNodeReplace::is_ready()const
124 if(!dest_value_node || !src_value_node)
126 return Action::CanvasSpecific::is_ready();
130 Action::ValueNodeReplace::perform()
134 if(dest_value_node == src_value_node)
135 throw Error(_("Attempted to replace valuenode with itself"));
137 if(dest_value_node->get_type() != src_value_node->get_type())
138 throw Error(_("You cannot replace ValueNodes with different types!"));
142 if(!src_value_node->is_exported())
144 src_value_node->set_id(dest_value_node->get_id());
145 src_value_node->set_parent_canvas(dest_value_node->get_parent_canvas());
147 ValueNode::RHandle value_node(src_value_node);
149 if(!value_node.runique() && value_node.rcount()>1)
150 is_undoable=false; // !!!
153 is_undoable=false; // !!!
156 synfig::warning("ValueNodeReplace: Circumstances make undoing this action impossible at the current time. :(");
158 ValueNode::RHandle value_node(dest_value_node);
160 if(value_node.runique() || value_node.rcount()<=1)
161 throw Error(_("Nothing to replace."));
165 replacements=value_node->replace(src_value_node);
166 assert(replacements);
168 throw Error(_("Action Failure. This is a bug. Please report it."));
169 swap_guid(dest_value_node,src_value_node);
171 //src_value_node->parent_set.swap(dest_value_node->parent_set);
173 // Signal that a layer has been inserted
174 if(get_canvas_interface())
176 get_canvas_interface()->signal_value_node_replaced()(dest_value_node,src_value_node);
178 else synfig::warning("CanvasInterface not set on action");
183 Action::ValueNodeReplace::undo()
186 throw Error(_("This action cannot be undone under these circumstances."));
190 if(dest_value_node == src_value_node)
191 throw Error(_("Attempted to replace valuenode with itself"));
193 if(dest_value_node->get_type() != src_value_node->get_type())
194 throw Error(_("You cannot replace ValueNodes with different types!"));
196 ValueNode::RHandle value_node(src_value_node);
198 if(value_node.runique() || value_node.rcount()<=1)
199 throw Error(_("Nothing to replace."));
203 replacements=value_node->replace(dest_value_node);
204 assert(replacements);
206 throw Error(_("Action Failure. This is a bug. Please report it."));
207 swap_guid(dest_value_node,src_value_node);
209 //src_value_node->parent_set.swap(dest_value_node->parent_set);
211 synfig::info(get_name()+_(": (Undo) ")+strprintf("Replaced %d ValueNode instances",replacements));
213 src_value_node->set_id(String());
214 src_value_node->set_parent_canvas(0);
216 // Signal that a layer has been inserted
217 if(get_canvas_interface())
219 get_canvas_interface()->signal_value_node_replaced()(src_value_node,dest_value_node);
221 else synfig::warning("CanvasInterface not set on action");