Fix 1501383: "del key deletes object instead of character". Changed the LayerRemove...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuedescexport.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescexport.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuenodeadd.h"
34
35 #include "canvasadd.h"
36 #include "valuedescexport.h"
37 #include "layerparamconnect.h"
38
39 #include <synfigapp/canvasinterface.h>
40 #include <synfig/valuenode_const.h>
41
42 #include <synfigapp/general.h>
43
44 #endif
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace synfigapp;
50 using namespace Action;
51
52 /* === M A C R O S ========================================================= */
53
54 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescExport);
55 ACTION_SET_NAME(Action::ValueDescExport,"ValueDescExport");
56 ACTION_SET_LOCAL_NAME(Action::ValueDescExport,N_("Export"));
57 ACTION_SET_TASK(Action::ValueDescExport,"export");
58 ACTION_SET_CATEGORY(Action::ValueDescExport,Action::CATEGORY_VALUEDESC);
59 ACTION_SET_PRIORITY(Action::ValueDescExport,0);
60 ACTION_SET_VERSION(Action::ValueDescExport,"0.0");
61 ACTION_SET_CVS_ID(Action::ValueDescExport,"$Id$");
62
63 /* === G L O B A L S ======================================================= */
64
65 /* === P R O C E D U R E S ================================================= */
66
67 /* === M E T H O D S ======================================================= */
68
69 Action::ValueDescExport::ValueDescExport()
70 {
71 }
72
73 synfig::String
74 Action::ValueDescExport::get_local_name()const
75 {
76         // TRANSLATORS: This is used in the 'history' dialog when a ValueNode is exported.  The first %s is what is exported, the 2nd is the name it is given.
77         return strprintf(_("Export '%s' as '%s'"),
78                                          value_desc.get_description(false).c_str(),
79                                          name.c_str());
80 }
81
82 Action::ParamVocab
83 Action::ValueDescExport::get_param_vocab()
84 {
85         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
86
87         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
88                 .set_local_name(_("ValueDesc"))
89         );
90
91         ret.push_back(ParamDesc("name",Param::TYPE_STRING)
92                 .set_local_name(_("Name"))
93                 .set_desc(_("The name that you want this value to be exported as"))
94                 .set_user_supplied()
95         );
96
97         return ret;
98 }
99
100 bool
101 Action::ValueDescExport::is_candidate(const ParamList &x)
102 {
103         if(candidate_check(get_param_vocab(),x))
104         {
105                 ValueDesc value_desc=x.find("value_desc")->second.get_value_desc();
106                 if(!value_desc ||
107                    value_desc.parent_is_canvas() ||
108                    (value_desc.is_value_node() && value_desc.get_value_node()->is_exported()) ||
109                    (value_desc.get_value_type()==ValueBase::TYPE_CANVAS && value_desc.get_value_node()))
110                 {
111 //                      if (!value_desc)
112 //                              synfig::info("%s:%d no export because no value_desc", __FILE__, __LINE__);
113 //                      else if (value_desc.parent_is_canvas())
114 //                              synfig::info("%s:%d no export because parent is canvas", __FILE__, __LINE__);
115 //                      else if (value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
116 //                              synfig::info("%s:%d no export because exported value node", __FILE__, __LINE__);
117 //                      else if (value_desc.get_value_type()==ValueBase::TYPE_CANVAS && value_desc.is_value_node())
118 //                              synfig::info("%s:%d no export because canvas value node", __FILE__, __LINE__);
119                         
120                         return false;
121                 }
122                 return true;
123         }
124         return false;
125 }
126
127 bool
128 Action::ValueDescExport::set_param(const synfig::String& param_name, const Action::Param &param)
129 {
130         if(param_name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
131         {
132                 value_desc=param.get_value_desc();
133
134                 return true;
135         }
136
137         if(param_name=="name" && param.get_type()==Param::TYPE_STRING)
138         {
139                 name=param.get_string();
140
141                 return true;
142         }
143
144         return Action::CanvasSpecific::set_param(param_name,param);
145 }
146
147 bool
148 Action::ValueDescExport::is_ready()const
149 {
150         if(!value_desc || name.empty())
151                 return false;
152         return Action::CanvasSpecific::is_ready();
153 }
154
155 void
156 Action::ValueDescExport::prepare()
157 {
158         clear();
159
160         ValueNode::Handle value_node;
161
162         if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
163         {
164                 // action: CanvasAdd
165                 if(!value_desc.is_const())
166                         throw Error(_("Can only export Canvas when used as constant parameter"));
167                 Canvas::Handle canvas(value_desc.get_value().get(Canvas::Handle()));
168                 if (canvas) canvas=canvas->clone();
169
170                 Action::Handle action(CanvasAdd::create());
171
172                 action->set_param("canvas",get_canvas());
173                 action->set_param("canvas_interface",get_canvas_interface());
174                 action->set_param("src",canvas);
175                 action->set_param("id",name);
176
177                 assert(action->is_ready());
178                 if(!action->is_ready())
179                         throw Error(Error::TYPE_NOTREADY);
180
181                 add_action_front(action);
182
183                 if(value_desc.is_value_node())
184                 {
185                         assert(0);                      // we shouldn't get here I don't think
186                         if(value_desc.get_value_node()->is_exported())
187                                 throw Error(_("ValueBase is already exported"));
188
189                         value_node=value_desc.get_value_node();
190                 }
191                 else
192                 {
193                         // action: LayerParamConnect
194                         if(!value_desc.parent_is_layer_param())
195                                 throw Error(_("Unable to export parameter. (Bug?)"));
196
197                         value_node=ValueNode_Const::create(canvas);
198
199                         Action::Handle action(LayerParamConnect::create());
200
201                         action->set_param("canvas",get_canvas());
202                         action->set_param("canvas_interface",get_canvas_interface());
203                         action->set_param("layer",value_desc.get_layer());
204                         action->set_param("param",value_desc.get_param_name());
205                         action->set_param("value_node",value_node);
206
207                         assert(action->is_ready());
208                         if(!action->is_ready())
209                                 throw Error(Error::TYPE_NOTREADY);
210
211                         add_action_front(action);
212                 }
213
214                 return;
215         }
216
217         if(value_desc.is_value_node())
218         {
219                 if(value_desc.get_value_node()->is_exported())
220                         throw Error(_("ValueBase is already exported"));
221
222                 value_node=value_desc.get_value_node();
223         }
224         else
225         {
226                 // action: LayerParamConnect
227                 if(!value_desc.parent_is_layer_param())
228                         throw Error(_("Unable to export parameter. (Bug?)"));
229
230                 value_node=ValueNode_Const::create(value_desc.get_value());
231
232                 Action::Handle action(LayerParamConnect::create());
233
234                 action->set_param("canvas",get_canvas());
235                 action->set_param("canvas_interface",get_canvas_interface());
236                 action->set_param("layer",value_desc.get_layer());
237                 action->set_param("param",value_desc.get_param_name());
238                 action->set_param("value_node",value_node);
239
240                 assert(action->is_ready());
241                 if(!action->is_ready())
242                         throw Error(Error::TYPE_NOTREADY);
243
244                 add_action_front(action);
245         }
246
247         // action: ValueNodeAdd
248         Action::Handle action(ValueNodeAdd::create());
249
250         action->set_param("canvas",get_canvas());
251         action->set_param("canvas_interface",get_canvas_interface());
252         action->set_param("new",value_node);
253         action->set_param("name",name);
254
255         assert(action->is_ready());
256         if(!action->is_ready())
257                 throw Error(Error::TYPE_NOTREADY);
258
259         add_action_front(action);
260 }