Changed the "tagrelease" and "tagstable" make targets to use subversion. Also increme...
[synfig.git] / synfig-studio / tags / stable / src / sinfgapp / actions / valuenodereplace.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file valuenodereplace.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenodereplace.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "valuenodereplace.h"
32 #include <sinfgapp/canvasinterface.h>
33
34 #endif
35
36 using namespace std;
37 using namespace etl;
38 using namespace sinfg;
39 using namespace sinfgapp;
40 using namespace Action;
41
42 /* === M A C R O S ========================================================= */
43
44 ACTION_INIT(Action::ValueNodeReplace);
45 ACTION_SET_NAME(Action::ValueNodeReplace,"value_node_replace");
46 ACTION_SET_LOCAL_NAME(Action::ValueNodeReplace,"Replace ValueNode");
47 ACTION_SET_TASK(Action::ValueNodeReplace,"replace");
48 ACTION_SET_CATEGORY(Action::ValueNodeReplace,Action::CATEGORY_VALUENODE|Action::CATEGORY_DRAG);
49 ACTION_SET_PRIORITY(Action::ValueNodeReplace,0);
50 ACTION_SET_VERSION(Action::ValueNodeReplace,"0.0");
51 ACTION_SET_CVS_ID(Action::ValueNodeReplace,"$Id: valuenodereplace.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 void swap_guid(const ValueNode::Handle& a,const ValueNode::Handle& b)
58 {
59         GUID old_a(a->get_guid());
60         a->set_guid(GUID());
61
62         GUID old_b(b->get_guid());
63         b->set_guid(GUID());
64         
65         a->set_guid(old_b);
66         b->set_guid(old_a);
67 }
68
69 /* === M E T H O D S ======================================================= */
70
71 Action::ValueNodeReplace::ValueNodeReplace():
72         is_undoable(true)
73 {
74 }
75
76 Action::ParamVocab
77 Action::ValueNodeReplace::get_param_vocab()
78 {
79         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
80         
81         ret.push_back(ParamDesc("dest",Param::TYPE_VALUENODE)
82                 .set_local_name(_("Destination ValueNode"))
83                 .set_desc(_("ValueNode to replaced"))
84         );
85
86         ret.push_back(ParamDesc("src",Param::TYPE_VALUENODE)
87                 .set_local_name(_("Source ValueNode"))
88                 .set_desc(_("ValueNode that will replace the destination"))
89         );
90         
91         return ret;
92 }
93
94 bool
95 Action::ValueNodeReplace::is_canidate(const ParamList &x)
96 {
97         return canidate_check(get_param_vocab(),x);
98 }
99
100 bool
101 Action::ValueNodeReplace::set_param(const sinfg::String& name, const Action::Param &param)
102 {
103         if(name=="dest" && param.get_type()==Param::TYPE_VALUENODE)
104         {
105                 dest_value_node=param.get_value_node();
106                 
107                 return true;
108         }
109
110         if(name=="src" && param.get_type()==Param::TYPE_VALUENODE)
111         {
112                 src_value_node=param.get_value_node();
113                 
114                 return true;
115         }
116
117         return Action::CanvasSpecific::set_param(name,param);
118 }
119
120 bool
121 Action::ValueNodeReplace::is_ready()const
122 {
123         if(!dest_value_node || !src_value_node)
124                 return false;
125         return Action::CanvasSpecific::is_ready();
126 }
127
128 void
129 Action::ValueNodeReplace::perform()
130 {
131         set_dirty(true);
132
133         if(dest_value_node == src_value_node)
134                 throw Error(_("Attempted to replace valuenode with itself"));
135
136         if(dest_value_node->get_type() != src_value_node->get_type())
137                 throw Error(_("You cannot replace ValueNodes with different types!"));
138         
139         is_undoable=true;
140         
141         if(!src_value_node->is_exported())
142         {
143                 src_value_node->set_id(dest_value_node->get_id());
144                 src_value_node->set_parent_canvas(dest_value_node->get_parent_canvas());
145
146                 ValueNode::RHandle value_node(src_value_node);
147                 
148                 if(!value_node.runique() && value_node.rcount()>1)
149                         is_undoable=false;      // !!!
150         }
151         else
152                 is_undoable=false;      // !!!
153         
154         if(!is_undoable)
155                 sinfg::warning("ValueNodeReplace: Circumstances make undoing this action impossible at the current time. :(");
156         
157         ValueNode::RHandle value_node(dest_value_node);
158         
159         if(value_node.runique() || value_node.rcount()<=1)
160                 throw Error(_("Nothing to replace."));
161         
162         int replacements;
163                 
164         replacements=value_node->replace(src_value_node);
165         assert(replacements);
166         if(!replacements)
167                 throw Error(_("Action Failure. This is a bug. Please report it."));
168         swap_guid(dest_value_node,src_value_node);
169         
170         //src_value_node->parent_set.swap(dest_value_node->parent_set);
171         
172         // Signal that a layer has been inserted
173         if(get_canvas_interface())
174         {
175                 get_canvas_interface()->signal_value_node_replaced()(dest_value_node,src_value_node);
176         }
177         else sinfg::warning("CanvasInterface not set on action");
178         
179 }
180
181 void
182 Action::ValueNodeReplace::undo()
183 {
184         if(!is_undoable)
185                 throw Error(_("This action cannot be undone under these circumstances."));
186                 
187         set_dirty(true);
188
189         if(dest_value_node == src_value_node)
190                 throw Error(_("Attempted to replace valuenode with itself"));
191
192         if(dest_value_node->get_type() != src_value_node->get_type())
193                 throw Error(_("You cannot replace ValueNodes with different types!"));
194                 
195         ValueNode::RHandle value_node(src_value_node);
196         
197         if(value_node.runique() || value_node.rcount()<=1)
198                 throw Error(_("Nothing to replace."));
199         
200         int replacements;
201         
202         replacements=value_node->replace(dest_value_node);
203         assert(replacements);
204         if(!replacements)
205                 throw Error(_("Action Failure. This is a bug. Please report it."));
206         swap_guid(dest_value_node,src_value_node);
207
208         //src_value_node->parent_set.swap(dest_value_node->parent_set);
209         
210         sinfg::info(get_name()+_(": (Undo) ")+strprintf("Replaced %d ValueNode instances",replacements));
211
212         src_value_node->set_id(String());
213         src_value_node->set_parent_canvas(0);
214         
215         // Signal that a layer has been inserted
216         if(get_canvas_interface())
217         {
218                 get_canvas_interface()->signal_value_node_replaced()(src_value_node,dest_value_node);
219         }
220         else sinfg::warning("CanvasInterface not set on action");
221         
222 }