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