Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc3 / src / synfigapp / actions / valuenodereplace.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenodereplace.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 "valuenodereplace.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44
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$");
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 void swap_guid(const ValueNode::Handle& a,const ValueNode::Handle& b)
59 {
60         GUID old_a(a->get_guid());
61         a->set_guid(GUID());
62
63         GUID old_b(b->get_guid());
64         b->set_guid(GUID());
65
66         a->set_guid(old_b);
67         b->set_guid(old_a);
68 }
69
70 /* === M E T H O D S ======================================================= */
71
72 Action::ValueNodeReplace::ValueNodeReplace():
73         is_undoable(true)
74 {
75 }
76
77 Action::ParamVocab
78 Action::ValueNodeReplace::get_param_vocab()
79 {
80         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
81
82         ret.push_back(ParamDesc("dest",Param::TYPE_VALUENODE)
83                 .set_local_name(_("Destination ValueNode"))
84                 .set_desc(_("ValueNode to replaced"))
85         );
86
87         ret.push_back(ParamDesc("src",Param::TYPE_VALUENODE)
88                 .set_local_name(_("Source ValueNode"))
89                 .set_desc(_("ValueNode that will replace the destination"))
90         );
91
92         return ret;
93 }
94
95 bool
96 Action::ValueNodeReplace::is_candidate(const ParamList &x)
97 {
98         return candidate_check(get_param_vocab(),x);
99 }
100
101 bool
102 Action::ValueNodeReplace::set_param(const synfig::String& name, const Action::Param &param)
103 {
104         if(name=="dest" && param.get_type()==Param::TYPE_VALUENODE)
105         {
106                 dest_value_node=param.get_value_node();
107
108                 return true;
109         }
110
111         if(name=="src" && param.get_type()==Param::TYPE_VALUENODE)
112         {
113                 src_value_node=param.get_value_node();
114
115                 return true;
116         }
117
118         return Action::CanvasSpecific::set_param(name,param);
119 }
120
121 bool
122 Action::ValueNodeReplace::is_ready()const
123 {
124         if(!dest_value_node || !src_value_node)
125                 return false;
126         return Action::CanvasSpecific::is_ready();
127 }
128
129 void
130 Action::ValueNodeReplace::perform()
131 {
132         set_dirty(true);
133
134         if(dest_value_node == src_value_node)
135                 throw Error(_("Attempted to replace valuenode with itself"));
136
137         if(dest_value_node->get_type() != src_value_node->get_type())
138                 throw Error(_("You cannot replace ValueNodes with different types!"));
139
140         is_undoable=true;
141
142         if(!src_value_node->is_exported())
143         {
144                 src_value_node->set_id(dest_value_node->get_id());
145                 src_value_node->set_parent_canvas(dest_value_node->get_parent_canvas());
146
147                 ValueNode::RHandle value_node(src_value_node);
148
149                 if(!value_node.runique() && value_node.rcount()>1)
150                         is_undoable=false;      // !!!
151         }
152         else
153                 is_undoable=false;      // !!!
154
155         if(!is_undoable)
156                 synfig::warning("ValueNodeReplace: Circumstances make undoing this action impossible at the current time. :(");
157
158         ValueNode::RHandle value_node(dest_value_node);
159
160         if(value_node.runique() || value_node.rcount()<=1)
161                 throw Error(_("Nothing to replace."));
162
163         int replacements;
164
165         replacements=value_node->replace(src_value_node);
166         assert(replacements);
167         if(!replacements)
168                 throw Error(_("Action Failure. This is a bug. Please report it."));
169         swap_guid(dest_value_node,src_value_node);
170
171         //src_value_node->parent_set.swap(dest_value_node->parent_set);
172
173         // Signal that a layer has been inserted
174         if(get_canvas_interface())
175         {
176                 get_canvas_interface()->signal_value_node_replaced()(dest_value_node,src_value_node);
177         }
178         else synfig::warning("CanvasInterface not set on action");
179
180 }
181
182 void
183 Action::ValueNodeReplace::undo()
184 {
185         if(!is_undoable)
186                 throw Error(_("This action cannot be undone under these circumstances."));
187
188         set_dirty(true);
189
190         if(dest_value_node == src_value_node)
191                 throw Error(_("Attempted to replace valuenode with itself"));
192
193         if(dest_value_node->get_type() != src_value_node->get_type())
194                 throw Error(_("You cannot replace ValueNodes with different types!"));
195
196         ValueNode::RHandle value_node(src_value_node);
197
198         if(value_node.runique() || value_node.rcount()<=1)
199                 throw Error(_("Nothing to replace."));
200
201         int replacements;
202
203         replacements=value_node->replace(dest_value_node);
204         assert(replacements);
205         if(!replacements)
206                 throw Error(_("Action Failure. This is a bug. Please report it."));
207         swap_guid(dest_value_node,src_value_node);
208
209         //src_value_node->parent_set.swap(dest_value_node->parent_set);
210
211         synfig::info(get_name()+_(": (Undo) ")+strprintf("Replaced %d ValueNode instances",replacements));
212
213         src_value_node->set_id(String());
214         src_value_node->set_parent_canvas(0);
215
216         // Signal that a layer has been inserted
217         if(get_canvas_interface())
218         {
219                 get_canvas_interface()->signal_value_node_replaced()(src_value_node,dest_value_node);
220         }
221         else synfig::warning("CanvasInterface not set on action");
222
223 }