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