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