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>
35 #include <synfigapp/general.h>
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
45 /* === M A C R O S ========================================================= */
47 ACTION_INIT(Action::ValueNodeReplace);
48 ACTION_SET_NAME(Action::ValueNodeReplace,"value_node_replace");
49 ACTION_SET_LOCAL_NAME(Action::ValueNodeReplace,N_("Replace ValueNode"));
50 ACTION_SET_TASK(Action::ValueNodeReplace,"replace");
51 ACTION_SET_CATEGORY(Action::ValueNodeReplace,Action::CATEGORY_VALUENODE|Action::CATEGORY_DRAG);
52 ACTION_SET_PRIORITY(Action::ValueNodeReplace,0);
53 ACTION_SET_VERSION(Action::ValueNodeReplace,"0.0");
54 ACTION_SET_CVS_ID(Action::ValueNodeReplace,"$Id$");
56 /* === G L O B A L S ======================================================= */
58 /* === P R O C E D U R E S ================================================= */
60 void swap_guid(const ValueNode::Handle& a,const ValueNode::Handle& b)
62 GUID old_a(a->get_guid());
65 GUID old_b(b->get_guid());
72 /* === M E T H O D S ======================================================= */
74 Action::ValueNodeReplace::ValueNodeReplace():
80 Action::ValueNodeReplace::get_param_vocab()
82 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
84 ret.push_back(ParamDesc("dest",Param::TYPE_VALUENODE)
85 .set_local_name(_("Destination ValueNode"))
86 .set_desc(_("ValueNode to replaced"))
89 ret.push_back(ParamDesc("src",Param::TYPE_VALUENODE)
90 .set_local_name(_("Source ValueNode"))
91 .set_desc(_("ValueNode that will replace the destination"))
98 Action::ValueNodeReplace::is_candidate(const ParamList &x)
100 return candidate_check(get_param_vocab(),x);
104 Action::ValueNodeReplace::set_param(const synfig::String& name, const Action::Param ¶m)
106 if(name=="dest" && param.get_type()==Param::TYPE_VALUENODE)
108 dest_value_node=param.get_value_node();
113 if(name=="src" && param.get_type()==Param::TYPE_VALUENODE)
115 src_value_node=param.get_value_node();
120 return Action::CanvasSpecific::set_param(name,param);
124 Action::ValueNodeReplace::is_ready()const
126 if(!dest_value_node || !src_value_node)
128 return Action::CanvasSpecific::is_ready();
132 Action::ValueNodeReplace::perform()
136 if(dest_value_node == src_value_node)
137 throw Error(_("Attempted to replace valuenode with itself"));
139 if(dest_value_node->get_type() != src_value_node->get_type())
140 throw Error(_("You cannot replace ValueNodes with different types!"));
144 if(!src_value_node->is_exported())
146 src_value_node->set_id(dest_value_node->get_id());
147 src_value_node->set_parent_canvas(dest_value_node->get_parent_canvas());
149 ValueNode::RHandle value_node(src_value_node);
151 if(!value_node.runique() && value_node.rcount()>1)
152 is_undoable=false; // !!!
155 is_undoable=false; // !!!
158 synfig::warning("ValueNodeReplace: Circumstances make undoing this action impossible at the current time. :(");
160 ValueNode::RHandle value_node(dest_value_node);
162 if(value_node.runique() || value_node.rcount()<=1)
163 throw Error(_("Nothing to replace."));
167 replacements=value_node->replace(src_value_node);
168 assert(replacements);
170 throw Error(_("Action Failure. This is a bug. Please report it."));
171 swap_guid(dest_value_node,src_value_node);
173 //src_value_node->parent_set.swap(dest_value_node->parent_set);
175 // Signal that a layer has been inserted
176 if(get_canvas_interface())
178 get_canvas_interface()->signal_value_node_replaced()(dest_value_node,src_value_node);
180 else synfig::warning("CanvasInterface not set on action");
185 Action::ValueNodeReplace::undo()
188 throw Error(_("This action cannot be undone under these circumstances."));
192 if(dest_value_node == src_value_node)
193 throw Error(_("Attempted to replace valuenode with itself"));
195 if(dest_value_node->get_type() != src_value_node->get_type())
196 throw Error(_("You cannot replace ValueNodes with different types!"));
198 ValueNode::RHandle value_node(src_value_node);
200 if(value_node.runique() || value_node.rcount()<=1)
201 throw Error(_("Nothing to replace."));
205 replacements=value_node->replace(dest_value_node);
206 assert(replacements);
208 throw Error(_("Action Failure. This is a bug. Please report it."));
209 swap_guid(dest_value_node,src_value_node);
211 //src_value_node->parent_set.swap(dest_value_node->parent_set);
213 synfig::info(get_name()+_(": (Undo) ")+strprintf("Replaced %d ValueNode instances",replacements));
215 src_value_node->set_id(String());
216 src_value_node->set_parent_canvas(0);
218 // Signal that a layer has been inserted
219 if(get_canvas_interface())
221 get_canvas_interface()->signal_value_node_replaced()(src_value_node,dest_value_node);
223 else synfig::warning("CanvasInterface not set on action");