Use translatable, 'human readable' strings when displaying the valuenode types (eg...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode.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 #define SYNFIG_NO_ANGLE
26
27 //#define HAS_HASH_MAP 1
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include "valuenode.h"
37 #include "general.h"
38 #include "canvas.h"
39
40 #include "valuenode_const.h"
41 #include "valuenode_linear.h"
42 #include "valuenode_composite.h"
43 #include "valuenode_reference.h"
44 #include "valuenode_scale.h"
45 #include "valuenode_segcalctangent.h"
46 #include "valuenode_segcalcvertex.h"
47 #include "valuenode_stripes.h"
48 #include "valuenode_subtract.h"
49 #include "valuenode_timedswap.h"
50 #include "valuenode_twotone.h"
51 #include "valuenode_bline.h"
52 #include "valuenode_dynamiclist.h"
53 #include "valuenode_radialcomposite.h"
54 #include "valuenode_gradientrotate.h"
55 #include "valuenode_sine.h"
56
57 #include "layer.h"
58
59 #endif
60
61 /* === U S I N G =========================================================== */
62
63 using namespace std;
64 using namespace etl;
65 using namespace synfig;
66
67 /* === M A C R O S ========================================================= */
68
69 /* === G L O B A L S ======================================================= */
70
71 static int value_node_count(0);
72
73 static LinkableValueNode::Book *book_;
74
75
76 ValueNode::LooseHandle
77 synfig::find_value_node(const GUID& guid)
78 {
79         return guid_cast<ValueNode>(guid);
80 }
81
82 /* === P R O C E D U R E S ================================================= */
83
84 /* === M E T H O D S ======================================================= */
85
86 bool
87 ValueNode::subsys_init()
88 {
89         book_=new LinkableValueNode::Book();
90
91 #define ADD_VALUENODE(c,n,l)  (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create);      (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=l
92 #define ADD_VALUENODE2(c,n,l) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create_from); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=l
93
94         ADD_VALUENODE(ValueNode_Linear,                 "linear",                       _("Linear")                             );x
95         ADD_VALUENODE(ValueNode_Composite,              "composite",            _("Composite")                  );x
96         ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite",     _("Radial Composite")   );x
97         ADD_VALUENODE(ValueNode_Reference,              "reference",            _("Reference")                  );x
98         ADD_VALUENODE(ValueNode_Scale,                  "scale",                        _("Scale")                              );x
99         ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent",       _("Seg Calc Tangent")   );x
100         ADD_VALUENODE(ValueNode_SegCalcVertex,  "segcalcvertex",        _("Seg Calc Vertex")    );x
101         ADD_VALUENODE(ValueNode_Stripes,                "stripes",                      _("Stripes")                    );x
102         ADD_VALUENODE(ValueNode_Subtract,               "subtract",                     _("Subtract")                   );x
103         //ADD_VALUENODE(ValueNode_TimedSwap,    "timed_swap",           _("Timed Swap")                 );x
104         ADD_VALUENODE(ValueNode_TwoTone,                "twotone",                      _("Two-Tone")                   );x
105         ADD_VALUENODE(ValueNode_BLine,                  "bline",                        _("BLine")                              );x
106         ADD_VALUENODE2(ValueNode_DynamicList,   "dynamic_list",         _("Dynamic List")               );x
107         ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate",      _("Gradient Rotate")    );x
108         ADD_VALUENODE(ValueNode_Sine,                   "sine",                         _("Sine")                               );x
109
110 #undef ADD_VALUENODE
111 #undef ADD_VALUENODE2
112
113         return true;
114 }
115
116 bool
117 ValueNode::subsys_stop()
118 {
119         delete book_;
120 /*      if(global_value_node_map.size() || value_node_count)
121         {
122                 if(value_node_count)
123                         synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
124
125                 if(global_value_node_map.size()!=value_node_count)
126                         synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
127
128                 GlobalValueNodeMap::iterator iter;
129                 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
130                 {
131                         if(!iter->second->is_exported())
132                                 synfig::info("%s: count:%d name:%s type:%s",
133                                         iter->first.get_string().c_str(),
134                                         iter->second->count(),
135                                         iter->second->get_name().c_str(),
136                                         ValueBase::type_name(iter->second->get_type()).c_str()
137                                 );
138                         else
139                                 synfig::info("%s: id:%s count:%d name:%s type:%s",
140                                         iter->first.get_string().c_str(),
141                                         iter->second->get_id().c_str(),
142                                         iter->second->count(),
143                                         iter->second->get_name().c_str(),
144                                         ValueBase::type_name(iter->second->get_type()).c_str()
145                                 );
146                 }
147         }
148 */
149         return true;
150 }
151
152 ValueNode::ValueNode(ValueBase::Type type):type(type)
153 {
154         value_node_count++;
155 }
156
157 LinkableValueNode::Book&
158 LinkableValueNode::book()
159 {
160         return *book_;
161 }
162
163 LinkableValueNode::Handle
164 LinkableValueNode::create(const String &name, const ValueBase& x)
165 {
166         if(!book().count(name))
167                 return 0;
168         return book()[name].factory(x);
169 }
170
171 bool
172 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
173 {
174         if(!book().count(name) || !book()[name].check_type)
175                 return false;
176         return book()[name].check_type(x);
177 }
178
179 bool
180 LinkableValueNode::set_link(int i,ValueNode::Handle x)
181 {
182         ValueNode::Handle previous(get_link(i));
183
184         if(set_link_vfunc(i,x))
185         {
186                 if(previous)
187                         remove_child(previous.get());
188                 add_child(x.get());
189
190                 if(!x->is_exported() && get_parent_canvas())
191                 {
192                         x->set_parent_canvas(get_parent_canvas());
193                 }
194                 changed();
195                 return true;
196         }
197         return false;
198 }
199
200 ValueNode::LooseHandle
201 LinkableValueNode::get_link(int i)const
202 {
203         return get_link_vfunc(i);
204 }
205
206 void
207 LinkableValueNode::unlink_all()
208 {
209         for(int i=0;i<link_count();i++)
210         {
211                 ValueNode::LooseHandle value_node(get_link(i));
212                 if(value_node)
213                         value_node->parent_set.erase(this);
214         }
215 }
216
217 ValueNode::~ValueNode()
218 {
219         value_node_count--;
220
221         begin_delete();
222
223         //DEBUGPOINT();
224 }
225
226 void
227 ValueNode::on_changed()
228 {
229         if(get_parent_canvas())
230                 get_parent_canvas()->signal_value_node_changed()(this);
231         else if(get_root_canvas() && get_parent_canvas())
232                 get_root_canvas()->signal_value_node_changed()(this);
233
234         Node::on_changed();
235 }
236
237 int
238 ValueNode::replace(etl::handle<ValueNode> x)
239 {
240         if(x.get()==this)
241                 return 0;
242
243         while(parent_set.size())
244         {
245                 (*parent_set.begin())->add_child(x.get());
246                 (*parent_set.begin())->remove_child(this);
247                 //x->parent_set.insert(*parent_set.begin());
248                 //parent_set.erase(parent_set.begin());
249         }
250         int r(RHandle(this).replace(x));
251         x->changed();
252         return r;
253 }
254
255 void
256 ValueNode::set_id(const String &x)
257 {
258         if(name!=x)
259         {
260                 name=x;
261                 signal_id_changed_();
262         }
263 }
264
265 ValueNodeList::ValueNodeList():
266         placeholder_count_(0)
267 {
268 }
269
270 bool
271 ValueNodeList::count(const String &id)const
272 {
273         const_iterator iter;
274
275         if(id.empty())
276                 return false;
277
278         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
279
280         if(iter==end())
281                 return false;
282
283         return true;
284 }
285
286 ValueNode::Handle
287 ValueNodeList::find(const String &id)
288 {
289         iterator iter;
290
291         if(id.empty())
292                 throw Exception::IDNotFound("Empty ID");
293
294         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
295
296         if(iter==end())
297                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
298
299         return *iter;
300 }
301
302 ValueNode::ConstHandle
303 ValueNodeList::find(const String &id)const
304 {
305         const_iterator iter;
306
307         if(id.empty())
308                 throw Exception::IDNotFound("Empty ID");
309
310         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
311
312         if(iter==end())
313                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
314
315         return *iter;
316 }
317
318 ValueNode::Handle
319 ValueNodeList::surefind(const String &id)
320 {
321         if(id.empty())
322                 throw Exception::IDNotFound("Empty ID");
323
324         ValueNode::Handle value_node;
325
326         try
327         {
328                 value_node=find(id);
329         }
330         catch(Exception::IDNotFound)
331         {
332                 value_node=PlaceholderValueNode::create();
333                 value_node->set_id(id);
334                 push_back(value_node);
335                 placeholder_count_++;
336         }
337
338         return value_node;
339 }
340
341 bool
342 ValueNodeList::erase(ValueNode::Handle value_node)
343 {
344         assert(value_node);
345
346         iterator iter;
347
348         for(iter=begin();iter!=end();++iter)
349                 if(value_node.get()==iter->get())
350                 {
351                         std::list<ValueNode::RHandle>::erase(iter);
352                         if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
353                                 placeholder_count_--;
354                         return true;
355                 }
356         return false;
357 }
358
359 bool
360 ValueNodeList::add(ValueNode::Handle value_node)
361 {
362         if(!value_node)
363                 return false;
364         if(value_node->get_id().empty())
365                 return false;
366
367         try
368         {
369                 ValueNode::RHandle other_value_node=find(value_node->get_id());
370                 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
371                 {
372                         other_value_node->replace(value_node);
373                         placeholder_count_--;
374                         return true;
375                 }
376
377                 return false;
378         }
379         catch(Exception::IDNotFound)
380         {
381                 push_back(value_node);
382                 return true;
383         }
384
385         return false;
386 }
387
388 void
389 ValueNodeList::audit()
390 {
391         iterator iter,next;
392
393         for(next=begin(),iter=next++;iter!=end();iter=next++)
394                 if(iter->count()==1)
395                         std::list<ValueNode::RHandle>::erase(iter);
396 }
397
398
399 String
400 PlaceholderValueNode::get_name()const
401 {
402         return "placeholder";
403 }
404
405 String
406 PlaceholderValueNode::get_local_name()const
407 {
408         return _("Placeholder");
409 }
410
411 ValueNode*
412 PlaceholderValueNode::clone(const GUID& deriv_guid)const
413 {
414         ValueNode* ret(new PlaceholderValueNode());
415         ret->set_guid(get_guid()^deriv_guid);
416         return ret;
417 }
418
419 PlaceholderValueNode::Handle
420 PlaceholderValueNode::create(ValueBase::Type type)
421 {
422         return new PlaceholderValueNode(type);
423 }
424
425 ValueBase
426 PlaceholderValueNode::operator()(Time t)const
427 {
428         assert(0);
429         return ValueBase();
430 }
431
432 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
433         ValueNode(type)
434 {
435 }
436
437 ValueNode*
438 LinkableValueNode::clone(const GUID& deriv_guid)const
439 {
440         { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
441
442         int i;
443         LinkableValueNode *ret=create_new();
444         ret->set_guid(get_guid()^deriv_guid);
445
446         for(i=0;i<link_count();i++)
447         {
448                 ValueNode::Handle link=get_link_vfunc(i);
449                 if(!link->is_exported())
450                 {
451                         ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
452                         if(!value_node)
453                                 value_node=link->clone(deriv_guid);
454                         ret->set_link(i,value_node);
455                 }
456                 else
457                         ret->set_link(i,link);
458         }
459
460         return ret;
461 }
462
463 String
464 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
465 {
466         assert(is_exported());
467         assert(canvas_);
468
469         if(x.get()==canvas_.get())
470                 return get_id();
471
472         return canvas_->_get_relative_id(x)+':'+get_id();
473 }
474
475 void
476 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
477 {
478         canvas_=x; if(x) root_canvas_=x->get_root();
479 }
480
481 void
482 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
483 {
484         root_canvas_=x->get_root();
485 }
486
487 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
488 {
489         ValueNode::LooseHandle  h;
490
491         int size = link_count();
492
493         //just add it to the set...
494         for(int i=0; i < size; ++i)
495         {
496                 h = get_link(i);
497
498                 if(h)
499                 {
500                         const Node::time_set &tset = h->get_times();
501                         set.insert(tset.begin(),tset.end());
502                 }
503         }
504 }