65252cfac62763423b9b1395ccd3720134163b46
[synfig.git] / synfig-studio / 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 #include "layerparamset.h"
39
40 #include <synfigapp/canvasinterface.h>
41 #include <synfig/valuenode_const.h>
42
43 #include <synfigapp/general.h>
44
45 #endif
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50 using namespace synfigapp;
51 using namespace Action;
52
53 /* === M A C R O S ========================================================= */
54
55 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescExport);
56 ACTION_SET_NAME(Action::ValueDescExport,"ValueDescExport");
57 ACTION_SET_LOCAL_NAME(Action::ValueDescExport,N_("Export"));
58 ACTION_SET_TASK(Action::ValueDescExport,"export");
59 ACTION_SET_CATEGORY(Action::ValueDescExport,Action::CATEGORY_VALUEDESC);
60 ACTION_SET_PRIORITY(Action::ValueDescExport,0);
61 ACTION_SET_VERSION(Action::ValueDescExport,"0.0");
62 ACTION_SET_CVS_ID(Action::ValueDescExport,"$Id$");
63
64 /* === G L O B A L S ======================================================= */
65
66 /* === P R O C E D U R E S ================================================= */
67
68 /* === M E T H O D S ======================================================= */
69
70 Action::ValueDescExport::ValueDescExport()
71 {
72 }
73
74 synfig::String
75 Action::ValueDescExport::get_local_name()const
76 {
77         // 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.
78         return strprintf(_("Export '%s' as '%s'"),
79                                          value_desc.get_description(false).c_str(),
80                                          name.c_str());
81 }
82
83 Action::ParamVocab
84 Action::ValueDescExport::get_param_vocab()
85 {
86         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
87
88         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
89                 .set_local_name(_("ValueDesc"))
90         );
91
92         ret.push_back(ParamDesc("name",Param::TYPE_STRING)
93                 .set_local_name(_("Name"))
94                 .set_desc(_("The name that you want this value to be exported as"))
95                 .set_user_supplied()
96         );
97
98         return ret;
99 }
100
101 bool
102 Action::ValueDescExport::is_candidate(const ParamList &x)
103 {
104         if(candidate_check(get_param_vocab(),x))
105         {
106                 ValueDesc value_desc=x.find("value_desc")->second.get_value_desc();
107                 if(!value_desc ||
108                    value_desc.parent_is_canvas() ||
109                    (value_desc.is_value_node() && value_desc.get_value_node()->is_exported()) ||
110                    (value_desc.get_value_type()==ValueBase::TYPE_CANVAS && !value_desc.get_value().get(Canvas::Handle())->is_inline()))
111                 {
112 //                      if (!value_desc)
113 //                              synfig::info("%s:%d no export because no value_desc", __FILE__, __LINE__);
114 //                      else if (value_desc.parent_is_canvas())
115 //                              synfig::info("%s:%d no export because parent is canvas", __FILE__, __LINE__);
116 //                      else if (value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
117 //                              synfig::info("%s:%d no export because exported value node", __FILE__, __LINE__);
118 //                      else if (value_desc.get_value_type()==ValueBase::TYPE_CANVAS && !value_desc.get_value().get(Canvas::Handle())->is_inline())
119 //                              synfig::info("%s:%d no export because canvas isn't inline", __FILE__, __LINE__);
120
121                         return false;
122                 }
123                 return true;
124         }
125         return false;
126 }
127
128 bool
129 Action::ValueDescExport::set_param(const synfig::String& param_name, const Action::Param &param)
130 {
131         if(param_name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
132         {
133                 value_desc=param.get_value_desc();
134
135                 return true;
136         }
137
138         if(param_name=="name" && param.get_type()==Param::TYPE_STRING)
139         {
140                 name=param.get_string();
141
142                 return true;
143         }
144
145         return Action::CanvasSpecific::set_param(param_name,param);
146 }
147
148 bool
149 Action::ValueDescExport::is_ready()const
150 {
151         if(!value_desc || name.empty())
152                 return false;
153         return Action::CanvasSpecific::is_ready();
154 }
155
156 void
157 Action::ValueDescExport::prepare()
158 {
159         clear();
160
161         ValueNode::Handle value_node;
162
163         if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
164         {
165                 // action: CanvasAdd
166                 if(!value_desc.is_const())
167                         throw Error(_("Can only export Canvas when used as constant parameter"));
168                 Canvas::Handle canvas(value_desc.get_value().get(Canvas::Handle()));
169                 if (canvas) canvas=canvas->clone();
170
171                 Action::Handle action(CanvasAdd::create());
172
173                 action->set_param("canvas",get_canvas());
174                 action->set_param("canvas_interface",get_canvas_interface());
175                 action->set_param("src",canvas);
176                 action->set_param("id",name);
177
178                 assert(action->is_ready());
179                 if(!action->is_ready())
180                         throw Error(Error::TYPE_NOTREADY);
181
182                 add_action_front(action);
183
184                 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
185                 {
186                         // action: LayerParamSet
187                         Action::Handle action(LayerParamSet::create());
188
189                         action->set_param("canvas",get_canvas());
190                         action->set_param("canvas_interface",get_canvas_interface());
191                         action->set_param("layer",value_desc.get_layer());
192                         action->set_param("param",value_desc.get_param_name());
193                         action->set_param("new_value",ValueBase(canvas));
194
195                         assert(action->is_ready());
196                         if(!action->is_ready())
197                                 throw Error(Error::TYPE_NOTREADY);
198
199                         add_action_front(action);
200                 }
201
202                 return;
203         }
204
205         if(value_desc.is_value_node())
206         {
207                 if(value_desc.get_value_node()->is_exported())
208                         throw Error(_("ValueBase is already exported"));
209
210                 value_node=value_desc.get_value_node();
211         }
212         else
213         {
214                 // action: LayerParamConnect
215                 if(!value_desc.parent_is_layer_param())
216                         throw Error(_("Unable to export parameter. (Bug?)"));
217
218                 value_node=ValueNode_Const::create(value_desc.get_value());
219
220                 Action::Handle action(LayerParamConnect::create());
221
222                 action->set_param("canvas",get_canvas());
223                 action->set_param("canvas_interface",get_canvas_interface());
224                 action->set_param("layer",value_desc.get_layer());
225                 action->set_param("param",value_desc.get_param_name());
226                 action->set_param("value_node",value_node);
227
228                 assert(action->is_ready());
229                 if(!action->is_ready())
230                         throw Error(Error::TYPE_NOTREADY);
231
232                 add_action_front(action);
233         }
234
235         // action: ValueNodeAdd
236         Action::Handle action(ValueNodeAdd::create());
237
238         action->set_param("canvas",get_canvas());
239         action->set_param("canvas_interface",get_canvas_interface());
240         action->set_param("new",value_node);
241         action->set_param("name",name);
242
243         assert(action->is_ready());
244         if(!action->is_ready())
245                 throw Error(Error::TYPE_NOTREADY);
246
247         add_action_front(action);
248 }