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