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