Add an option "Browser Command" to allow the user to specify which web browser to...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuedescconvert.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescconvert.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) 2007, 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 /*
34 #include "valuenodereplace.h"
35 #include "layerparamconnect.h"
36 #include "valuenodelinkconnect.h"
37 */
38
39 #include "valuedescconnect.h"
40
41 #include "valuedescconvert.h"
42
43 #include <synfigapp/canvasinterface.h>
44 #include <synfig/valuenode_const.h>
45
46 #include <synfigapp/general.h>
47
48 #endif
49
50 using namespace std;
51 using namespace etl;
52 using namespace synfig;
53 using namespace synfigapp;
54 using namespace Action;
55
56 /* === M A C R O S ========================================================= */
57
58 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescConvert);
59 ACTION_SET_NAME(Action::ValueDescConvert,"value_desc_convert");
60 ACTION_SET_LOCAL_NAME(Action::ValueDescConvert,N_("Convert"));
61 ACTION_SET_TASK(Action::ValueDescConvert,"convert");
62 ACTION_SET_CATEGORY(Action::ValueDescConvert,Action::CATEGORY_VALUEDESC);
63 ACTION_SET_PRIORITY(Action::ValueDescConvert,0);
64 ACTION_SET_VERSION(Action::ValueDescConvert,"0.0");
65 ACTION_SET_CVS_ID(Action::ValueDescConvert,"$Id$");
66
67 /* === G L O B A L S ======================================================= */
68
69 /* === P R O C E D U R E S ================================================= */
70
71 /* === M E T H O D S ======================================================= */
72
73 Action::ValueDescConvert::ValueDescConvert()
74 {
75         time=(Time::begin()-1);
76 }
77
78 synfig::String
79 Action::ValueDescConvert::get_local_name()const
80 {
81         // TRANSLATORS: This is used in the 'history' dialog when a ValueNode is converted.  The first %s is what is converted, the 2nd is the local name of the ValueNode's type.
82         return strprintf(_("Convert '%s' to ValueNode type '%s'"),
83                                          value_desc.get_description().c_str(),
84                                          LinkableValueNode::book()[type].local_name.c_str());
85 }
86
87 Action::ParamVocab
88 Action::ValueDescConvert::get_param_vocab()
89 {
90         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
91
92         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
93                 .set_local_name(_("ValueDesc"))
94         );
95
96         ret.push_back(ParamDesc("type",Param::TYPE_STRING)
97                 .set_local_name(_("Type"))
98                 .set_desc(_("The type of ValueNode that you want to be converted to"))
99         );
100
101         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
102                 .set_local_name(_("Time"))
103         );
104
105         return ret;
106 }
107
108 bool
109 Action::ValueDescConvert::is_candidate(const ParamList &x)
110 {
111         return candidate_check(get_param_vocab(),x);
112 }
113
114 bool
115 Action::ValueDescConvert::set_param(const synfig::String& name, const Action::Param &param)
116 {
117         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
118         {
119                 value_desc=param.get_value_desc();
120
121                 return true;
122         }
123
124         if(name=="type" && param.get_type()==Param::TYPE_STRING)
125         {
126                 type=param.get_string();
127
128                 return true;
129         }
130
131         if(name=="time" && param.get_type()==Param::TYPE_TIME)
132         {
133                 time=param.get_time();
134
135                 return true;
136         }
137
138         return Action::CanvasSpecific::set_param(name,param);
139 }
140
141 bool
142 Action::ValueDescConvert::is_ready()const
143 {
144         if(!value_desc || type.empty())
145                 return false;
146         if(time==(Time::begin()-1))
147         {
148                 synfig::error("Missing time");
149                 return false;
150         }
151         return Action::CanvasSpecific::is_ready();
152 }
153
154 void
155 Action::ValueDescConvert::prepare()
156 {
157         clear();
158
159         ValueBase value;
160
161         if(value_desc.is_const())
162                 value=value_desc.get_value();
163         else if(value_desc.is_value_node())
164                 value=(*value_desc.get_value_node())(time);
165         else
166                 throw Error(_("Unable to decipher ValueDesc (Bug?)"));
167
168         ValueNode::Handle src_value_node(LinkableValueNode::create(type,value));
169
170         if(!src_value_node)
171                 throw Error(_("Unable to create new value node"));
172
173
174         ValueNode::Handle dest_value_node;
175         dest_value_node=value_desc.get_value_node();
176
177         Action::Handle action(ValueDescConnect::create());
178
179         action->set_param("canvas",get_canvas());
180         action->set_param("canvas_interface",get_canvas_interface());
181         action->set_param("src",src_value_node);
182         action->set_param("dest",value_desc);
183
184         assert(action->is_ready());
185         if(!action->is_ready())
186                 throw Error(Error::TYPE_NOTREADY);
187
188         add_action_front(action);
189
190 /*
191         return;
192
193
194         if(value_desc.parent_is_canvas())
195         {
196                 ValueNode::Handle dest_value_node;
197                 dest_value_node=value_desc.get_value_node();
198
199                 Action::Handle action(ValueNodeReplace::create());
200
201                 action->set_param("canvas",get_canvas());
202                 action->set_param("canvas_interface",get_canvas_interface());
203                 action->set_param("src",src_value_node);
204                 action->set_param("dest",dest_value_node);
205
206                 assert(action->is_ready());
207                 if(!action->is_ready())
208                         throw Error(Error::TYPE_NOTREADY);
209
210                 add_action_front(action);
211                 return;
212         }
213         else
214         if(value_desc.parent_is_linkable_value_node())
215         {
216                 Action::Handle action(ValueNodeLinkConnect::create());
217
218                 action->set_param("canvas",get_canvas());
219                 action->set_param("canvas_interface",get_canvas_interface());
220                 action->set_param("parent_value_node",value_desc.get_parent_value_node());
221                 action->set_param("value_node", src_value_node);
222                 action->set_param("index",value_desc.get_index());
223
224                 assert(action->is_ready());
225                 if(!action->is_ready())
226                         throw Error(Error::TYPE_NOTREADY);
227
228                 add_action_front(action);
229                 return;
230         }
231         else
232         if(value_desc.parent_is_layer_param())
233         {
234                 Action::Handle action(LayerParamConnect::create());
235
236                 action->set_param("canvas",get_canvas());
237                 action->set_param("canvas_interface",get_canvas_interface());
238                 action->set_param("layer",value_desc.get_layer());
239                 action->set_param("param",value_desc.get_param_name());
240                 action->set_param("value_node",src_value_node);
241
242                 assert(action->is_ready());
243                 if(!action->is_ready())
244                         throw Error(Error::TYPE_NOTREADY);
245
246                 add_action_front(action);
247                 return;
248         }
249
250
251
252         throw Error(_("ValueDesc is not recognized or supported."));
253 */
254 }