Improve the description of a ValueNode to include its exported name if requested...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode.cpp
3 **      \brief Implementation of the "Placeholder" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 #define SYNFIG_NO_ANGLE
27
28 //#define HAS_HASH_MAP 1
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include "valuenode.h"
38 #include "general.h"
39 #include "canvas.h"
40 #include "paramdesc.h"
41
42 #include "valuenode_const.h"
43 #include "valuenode_linear.h"
44 #include "valuenode_composite.h"
45 #include "valuenode_reference.h"
46 #include "valuenode_scale.h"
47 #include "valuenode_blinecalctangent.h"
48 #include "valuenode_blinecalcvertex.h"
49 #include "valuenode_blinereversetangent.h"
50 #include "valuenode_segcalctangent.h"
51 #include "valuenode_segcalcvertex.h"
52 #include "valuenode_repeat_gradient.h"
53 #include "valuenode_stripes.h"
54 #include "valuenode_range.h"
55 #include "valuenode_add.h"
56 #include "valuenode_subtract.h"
57 #include "valuenode_timedswap.h"
58 #include "valuenode_twotone.h"
59 #include "valuenode_bline.h"
60 #include "valuenode_dynamiclist.h"
61 #include "valuenode_radialcomposite.h"
62 #include "valuenode_gradientrotate.h"
63 #include "valuenode_sine.h"
64 #include "valuenode_cos.h"
65 #include "valuenode_atan2.h"
66 #include "valuenode_exp.h"
67 #include "valuenode_switch.h"
68 #include "valuenode_timeloop.h"
69 #include "valuenode_reciprocal.h"
70 #include "valuenode_duplicate.h"
71 #include "valuenode_integer.h"
72
73 #include "layer.h"
74
75 #endif
76
77 /* === U S I N G =========================================================== */
78
79 using namespace std;
80 using namespace etl;
81 using namespace synfig;
82
83 /* === M A C R O S ========================================================= */
84
85 /* === G L O B A L S ======================================================= */
86
87 static int value_node_count(0);
88
89 static LinkableValueNode::Book *book_;
90
91
92 ValueNode::LooseHandle
93 synfig::find_value_node(const GUID& guid)
94 {
95         return guid_cast<ValueNode>(guid);
96 }
97
98 /* === P R O C E D U R E S ================================================= */
99
100 /* === M E T H O D S ======================================================= */
101
102 bool
103 ValueNode::subsys_init()
104 {
105         book_=new LinkableValueNode::Book();
106
107 #define ADD_VALUENODE(class,name,local)                                                                                                                 \
108         (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create);            \
109         (*book_)[name].check_type=&class::check_type;                                                                                           \
110         (*book_)[name].local_name=local
111
112 #define ADD_VALUENODE2(class,name,local)                                                                                                                \
113         (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from);       \
114         (*book_)[name].check_type=&class::check_type;                                                                                           \
115         (*book_)[name].local_name=local
116
117         ADD_VALUENODE(ValueNode_Linear,                 "linear",                       _("Linear")                             );
118         ADD_VALUENODE(ValueNode_Composite,              "composite",            _("Composite")                  );
119         ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite",     _("Radial Composite")   );
120         ADD_VALUENODE(ValueNode_Reference,              "reference",            _("Reference")                  );
121         ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient",      _("Repeat Gradient")    );
122         ADD_VALUENODE(ValueNode_Scale,                  "scale",                        _("Scale")                              );
123         ADD_VALUENODE(ValueNode_BLineCalcTangent,"blinecalctangent",_("BLine Tangent")          );
124         ADD_VALUENODE(ValueNode_BLineCalcVertex,"blinecalcvertex",      _("BLine Vertex")               );
125         ADD_VALUENODE(ValueNode_BLineRevTangent,"blinerevtangent",      _("Reverse Tangent")    );
126         ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent",       _("Segment Tangent")    );
127         ADD_VALUENODE(ValueNode_SegCalcVertex,  "segcalcvertex",        _("Segment Vertex")             );
128         ADD_VALUENODE(ValueNode_Stripes,                "stripes",                      _("Stripes")                    );
129         ADD_VALUENODE(ValueNode_Range,                  "range",                        _("Range")                              );
130         ADD_VALUENODE(ValueNode_Add,                    "add",                          _("Add")                                );
131         ADD_VALUENODE(ValueNode_Subtract,               "subtract",                     _("Subtract")                   );
132         ADD_VALUENODE(ValueNode_TimedSwap,              "timed_swap",           _("Timed Swap")                 );
133         ADD_VALUENODE(ValueNode_TwoTone,                "twotone",                      _("Two-Tone")                   );
134         ADD_VALUENODE(ValueNode_BLine,                  "bline",                        _("BLine")                              );
135         ADD_VALUENODE2(ValueNode_DynamicList,   "dynamic_list",         _("Dynamic List")               );
136         ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate",      _("Gradient Rotate")    );
137         ADD_VALUENODE(ValueNode_Sine,                   "sine",                         _("Sine")                               );
138         ADD_VALUENODE(ValueNode_Cos,                    "cos",                          _("Cos")                                );
139         ADD_VALUENODE(ValueNode_Atan2,                  "atan2",                        _("aTan2")                              );
140         ADD_VALUENODE(ValueNode_Exp,                    "exp",                          _("Exponential")                );
141         ADD_VALUENODE(ValueNode_Switch,                 "switch",                       _("Switch")                             );
142         ADD_VALUENODE(ValueNode_TimeLoop,               "timeloop",                     _("Time Loop")                  );
143         ADD_VALUENODE(ValueNode_Reciprocal,             "reciprocal",           _("Reciprocal")                 );
144         ADD_VALUENODE(ValueNode_Duplicate,              "duplicate",            _("Duplicate")                  );
145         ADD_VALUENODE(ValueNode_Integer,                "fromint",                      _("From Integer")               );
146
147 #undef ADD_VALUENODE
148 #undef ADD_VALUENODE2
149
150         return true;
151 }
152
153 bool
154 ValueNode::subsys_stop()
155 {
156         delete book_;
157 /*      if(global_value_node_map.size() || value_node_count)
158         {
159                 if(value_node_count)
160                         synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
161
162                 if(global_value_node_map.size()!=value_node_count)
163                         synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
164
165                 GlobalValueNodeMap::iterator iter;
166                 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
167                 {
168                         if(!iter->second->is_exported())
169                                 synfig::info(_("%s: count:%d name:%s type:%s"),
170                                         iter->first.get_string().c_str(),
171                                         iter->second->count(),
172                                         iter->second->get_name().c_str(),
173                                         ValueBase::type_local_name(iter->second->get_type()).c_str()
174                                 );
175                         else
176                                 synfig::info(_("%s: id:%s count:%d name:%s type:%s"),
177                                         iter->first.get_string().c_str(),
178                                         iter->second->get_id().c_str(),
179                                         iter->second->count(),
180                                         iter->second->get_name().c_str(),
181                                         ValueBase::type_local_name(iter->second->get_type()).c_str()
182                                 );
183                 }
184         }
185 */
186         return true;
187 }
188
189 ValueNode::ValueNode(ValueBase::Type type):type(type)
190 {
191         value_node_count++;
192 }
193
194 LinkableValueNode::Book&
195 LinkableValueNode::book()
196 {
197         return *book_;
198 }
199
200 LinkableValueNode::Handle
201 LinkableValueNode::create(const String &name, const ValueBase& x)
202 {
203         if(!book().count(name))
204                 return 0;
205
206         if (!check_type(name, x.get_type()) &&
207                 // the Duplicate ValueNode is an exception - we don't want the
208                 // user creating it for themselves, so check_type() fails for
209                 // it even when it is valid
210                 !(name == "duplicate" && x.get_type() == ValueBase::TYPE_REAL))
211         {
212                 error(_("Bad type: ValueNode '%s' doesn't accept type '%s'"), book()[name].local_name.c_str(), ValueBase::type_local_name(x.get_type()).c_str());
213                 return 0;
214         }
215
216         return book()[name].factory(x);
217 }
218
219 bool
220 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
221 {
222         if(!book().count(name) || !book()[name].check_type)
223                 return false;
224         return book()[name].check_type(x);
225 }
226
227 bool
228 LinkableValueNode::set_link(int i,ValueNode::Handle x)
229 {
230         ValueNode::Handle previous(get_link(i));
231
232         if(set_link_vfunc(i,x))
233         {
234                 if(previous)
235                         remove_child(previous.get());
236                 add_child(x.get());
237
238                 if(!x->is_exported() && get_parent_canvas())
239                 {
240                         x->set_parent_canvas(get_parent_canvas());
241                 }
242                 changed();
243                 return true;
244         }
245         return false;
246 }
247
248 ValueNode::LooseHandle
249 LinkableValueNode::get_link(int i)const
250 {
251         return get_link_vfunc(i);
252 }
253
254 void
255 LinkableValueNode::unlink_all()
256 {
257         for(int i=0;i<link_count();i++)
258         {
259                 ValueNode::LooseHandle value_node(get_link(i));
260                 if(value_node)
261                         value_node->parent_set.erase(this);
262         }
263 }
264
265 ValueNode::~ValueNode()
266 {
267         value_node_count--;
268
269         begin_delete();
270 }
271
272 void
273 ValueNode::on_changed()
274 {
275         etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
276         if(parent_canvas)
277                 do                                              // signal to all the ancestor canvases
278                         parent_canvas->signal_value_node_changed()(this);
279                 while (parent_canvas = parent_canvas->parent());
280         else if(get_root_canvas())
281                 get_root_canvas()->signal_value_node_changed()(this);
282
283         Node::on_changed();
284 }
285
286 int
287 ValueNode::replace(etl::handle<ValueNode> x)
288 {
289         if(x.get()==this)
290                 return 0;
291
292         while(parent_set.size())
293         {
294                 (*parent_set.begin())->add_child(x.get());
295                 (*parent_set.begin())->remove_child(this);
296                 //x->parent_set.insert(*parent_set.begin());
297                 //parent_set.erase(parent_set.begin());
298         }
299         int r(RHandle(this).replace(x));
300         x->changed();
301         return r;
302 }
303
304 void
305 ValueNode::set_id(const String &x)
306 {
307         if(name!=x)
308         {
309                 name=x;
310                 signal_id_changed_();
311         }
312 }
313
314 String
315 ValueNode::get_description(bool show_exported_name)const
316 {
317         String ret(_("ValueNode"));
318
319         if (dynamic_cast<const LinkableValueNode*>(this))
320                 return (dynamic_cast<const LinkableValueNode*>(this))->get_description(-1, show_exported_name);
321
322         if (show_exported_name && !is_exported())
323                 show_exported_name = false;
324
325         if (show_exported_name)
326                 ret += strprintf(" (%s)", get_id().c_str());
327
328         return ret;
329 }
330
331 ValueNodeList::ValueNodeList():
332         placeholder_count_(0)
333 {
334 }
335
336 bool
337 ValueNodeList::count(const String &id)const
338 {
339         const_iterator iter;
340
341         if(id.empty())
342                 return false;
343
344         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
345                 ;
346
347         if(iter==end())
348                 return false;
349
350         return true;
351 }
352
353 ValueNode::Handle
354 ValueNodeList::find(const String &id)
355 {
356         iterator iter;
357
358         if(id.empty())
359                 throw Exception::IDNotFound("Empty ID");
360
361         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
362                 ;
363
364         if(iter==end())
365                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
366
367         return *iter;
368 }
369
370 ValueNode::ConstHandle
371 ValueNodeList::find(const String &id)const
372 {
373         const_iterator iter;
374
375         if(id.empty())
376                 throw Exception::IDNotFound("Empty ID");
377
378         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
379                 ;
380
381         if(iter==end())
382                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
383
384         return *iter;
385 }
386
387 ValueNode::Handle
388 ValueNodeList::surefind(const String &id)
389 {
390         if(id.empty())
391                 throw Exception::IDNotFound("Empty ID");
392
393         ValueNode::Handle value_node;
394
395         try
396         {
397                 value_node=find(id);
398         }
399         catch(Exception::IDNotFound)
400         {
401                 value_node=PlaceholderValueNode::create();
402                 value_node->set_id(id);
403                 push_back(value_node);
404                 placeholder_count_++;
405         }
406
407         return value_node;
408 }
409
410 bool
411 ValueNodeList::erase(ValueNode::Handle value_node)
412 {
413         assert(value_node);
414
415         iterator iter;
416
417         for(iter=begin();iter!=end();++iter)
418                 if(value_node.get()==iter->get())
419                 {
420                         std::list<ValueNode::RHandle>::erase(iter);
421                         if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
422                                 placeholder_count_--;
423                         return true;
424                 }
425         return false;
426 }
427
428 bool
429 ValueNodeList::add(ValueNode::Handle value_node)
430 {
431         if(!value_node)
432                 return false;
433         if(value_node->get_id().empty())
434                 return false;
435
436         try
437         {
438                 ValueNode::RHandle other_value_node=find(value_node->get_id());
439                 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
440                 {
441                         other_value_node->replace(value_node);
442                         placeholder_count_--;
443                         return true;
444                 }
445
446                 return false;
447         }
448         catch(Exception::IDNotFound)
449         {
450                 push_back(value_node);
451                 return true;
452         }
453
454         return false;
455 }
456
457 void
458 ValueNodeList::audit()
459 {
460         iterator iter,next;
461
462         for(next=begin(),iter=next++;iter!=end();iter=next++)
463                 if(iter->count()==1)
464                         std::list<ValueNode::RHandle>::erase(iter);
465 }
466
467
468 String
469 PlaceholderValueNode::get_name()const
470 {
471         return "placeholder";
472 }
473
474 String
475 PlaceholderValueNode::get_local_name()const
476 {
477         return _("Placeholder");
478 }
479
480 ValueNode*
481 PlaceholderValueNode::clone(const GUID& deriv_guid)const
482 {
483         ValueNode* ret(new PlaceholderValueNode());
484         ret->set_guid(get_guid()^deriv_guid);
485         return ret;
486 }
487
488 PlaceholderValueNode::Handle
489 PlaceholderValueNode::create(ValueBase::Type type)
490 {
491         return new PlaceholderValueNode(type);
492 }
493
494 ValueBase
495 PlaceholderValueNode::operator()(Time /*t*/)const
496 {
497         assert(0);
498         return ValueBase();
499 }
500
501 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
502         ValueNode(type)
503 {
504 }
505
506 ValueNode*
507 LinkableValueNode::clone(const GUID& deriv_guid)const
508 {
509         // printf("%s:%d clone()\n", __FILE__, __LINE__);
510         { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)
511                                                                                                                                   {
512                                                                                                                                           printf("VALUENODE FOUND VALUENODE\n");
513                                                                                                                                           return x;
514                                                                                                                                   }}
515
516         int i;
517         LinkableValueNode *ret=create_new();
518         ret->set_guid(get_guid()^deriv_guid);
519
520         for(i=0;i<link_count();i++)
521         {
522                 ValueNode::Handle link=get_link_vfunc(i);
523                 if(!link->is_exported())
524                 {
525                         ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
526                         if(!value_node)
527                                 value_node=link->clone(deriv_guid);
528                         ret->set_link(i,value_node);
529                 }
530                 else
531                         ret->set_link(i,link);
532         }
533
534         return ret;
535 }
536
537 String
538 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
539 {
540         assert(is_exported());
541         assert(canvas_);
542
543         if(x.get()==canvas_.get())
544                 return get_id();
545
546         return canvas_->_get_relative_id(x)+':'+get_id();
547 }
548
549 void
550 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
551 {
552         canvas_=x; if(x) root_canvas_=x->get_root();
553 }
554
555 void
556 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
557 {
558         root_canvas_=x->get_root();
559 }
560
561 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
562 {
563         ValueNode::LooseHandle  h;
564
565         int size = link_count();
566
567         //just add it to the set...
568         for(int i=0; i < size; ++i)
569         {
570                 h = get_link(i);
571
572                 if(h)
573                 {
574                         const Node::time_set &tset = h->get_times();
575                         set.insert(tset.begin(),tset.end());
576                 }
577         }
578 }
579
580 String
581 LinkableValueNode::get_description(int index, bool show_exported_name)const
582 {
583         String description;
584
585         if (show_exported_name && !is_exported())
586                 show_exported_name = false;
587
588         if (index != -1)
589                 description = String(":") + link_local_name(index);
590
591         const synfig::Node* node = this;
592         LinkableValueNode::ConstHandle parent_linkable_vn = 0;
593
594         // walk up through the valuenodes trying to find the layer at the top
595         while (!node->parent_set.empty() && !dynamic_cast<const Layer*>(node))
596         {
597                 LinkableValueNode::ConstHandle linkable_value_node(dynamic_cast<const LinkableValueNode*>(node));
598                 if (linkable_value_node)
599                 {
600                         String link;
601                         int cnt = linkable_value_node->link_count();
602                         for (int i = 0; i < cnt; i++)
603                                 if (linkable_value_node->get_link(i) == parent_linkable_vn)
604                                 {
605                                         link = String(":") + linkable_value_node->link_local_name(i);
606                                         break;
607                                 }
608
609                         description = linkable_value_node->get_local_name() + link + (parent_linkable_vn?">":"") + description;
610                 }
611                 node = *node->parent_set.begin();
612                 parent_linkable_vn = linkable_value_node;
613         }
614
615         Layer::ConstHandle parent_layer(dynamic_cast<const Layer*>(node));
616         if(parent_layer)
617         {
618                 String param;
619                 const Layer::DynamicParamList &dynamic_param_list(parent_layer->dynamic_param_list());
620                 // loop to find the parameter in the dynamic parameter list - this gives us its name
621                 for (Layer::DynamicParamList::const_iterator iter = dynamic_param_list.begin(); iter != dynamic_param_list.end(); iter++)
622                         if (iter->second == parent_linkable_vn)
623                                 param = String(":") + parent_layer->get_param_local_name(iter->first);
624                 description = strprintf("(%s)%s>%s",
625                                                                 parent_layer->get_non_empty_description().c_str(),
626                                                                 param.c_str(),
627                                                                 description.c_str());
628         }
629
630         if (show_exported_name)
631                 description += strprintf(" (%s)", get_id().c_str());
632
633         return description;
634 }