Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_04 / synfig-core / src / synfig / valuenode.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file value_node.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenode.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
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) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=n
92 #define ADD_VALUENODE2(c,n) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create_from); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=n
93
94         ADD_VALUENODE(ValueNode_Linear,"linear");
95         ADD_VALUENODE(ValueNode_Composite,"composite");
96         ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite");
97         ADD_VALUENODE(ValueNode_Reference,"reference");
98         ADD_VALUENODE(ValueNode_Scale,"scale");
99         ADD_VALUENODE(ValueNode_SegCalcTangent,"segcalctangent");
100         ADD_VALUENODE(ValueNode_SegCalcVertex,"segcalcvertex");
101         ADD_VALUENODE(ValueNode_Stripes,"stripes");
102         ADD_VALUENODE(ValueNode_Subtract,"subtract");
103         //ADD_VALUENODE(ValueNode_TimedSwap,"timed_swap");
104         ADD_VALUENODE(ValueNode_TwoTone,"twotone");
105         ADD_VALUENODE(ValueNode_BLine,"bline");
106         ADD_VALUENODE2(ValueNode_DynamicList,"dynamic_list");
107         ADD_VALUENODE(ValueNode_GradientRotate,"gradient_rotate");
108         ADD_VALUENODE(ValueNode_Sine,"sine");
109         
110 #undef ADD_VALUENODE
111         return true;
112 }
113
114 bool
115 ValueNode::subsys_stop()
116 {
117         delete book_;
118 /*      if(global_value_node_map.size() || value_node_count)
119         {
120                 if(value_node_count)
121                         synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
122
123                 if(global_value_node_map.size()!=value_node_count)
124                         synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
125                 
126                 GlobalValueNodeMap::iterator iter;              
127                 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
128                 {
129                         if(!iter->second->is_exported())
130                                 synfig::info("%s: count:%d name:%s type:%s",
131                                         iter->first.get_string().c_str(),
132                                         iter->second->count(),
133                                         iter->second->get_name().c_str(),
134                                         ValueBase::type_name(iter->second->get_type()).c_str()
135                                 );
136                         else
137                                 synfig::info("%s: id:%s count:%d name:%s type:%s",
138                                         iter->first.get_string().c_str(),
139                                         iter->second->get_id().c_str(),
140                                         iter->second->count(),
141                                         iter->second->get_name().c_str(),
142                                         ValueBase::type_name(iter->second->get_type()).c_str()
143                                 );
144                 }
145         }
146 */
147         return true;
148 }
149
150 ValueNode::ValueNode(ValueBase::Type type):type(type)
151 {
152         value_node_count++;
153 }
154
155 LinkableValueNode::Book&
156 LinkableValueNode::book()
157 {
158         return *book_;
159 }
160
161 LinkableValueNode::Handle
162 LinkableValueNode::create(const String &name, const ValueBase& x)
163 {
164         if(!book().count(name))
165                 return 0;
166         return book()[name].factory(x);
167 }
168
169 bool
170 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
171 {
172         if(!book().count(name) || !book()[name].check_type)
173                 return false;
174         return book()[name].check_type(x);
175 }
176
177 bool
178 LinkableValueNode::set_link(int i,ValueNode::Handle x)
179 {
180         ValueNode::Handle previous(get_link(i));
181         
182         if(set_link_vfunc(i,x))
183         {
184                 if(previous)
185                         remove_child(previous.get());
186                 add_child(x.get());
187                 
188                 if(!x->is_exported() && get_parent_canvas())
189                 {
190                         x->set_parent_canvas(get_parent_canvas());
191                 }
192                 changed();
193                 return true;
194         }
195         return false;
196 }
197
198 ValueNode::LooseHandle
199 LinkableValueNode::get_link(int i)const
200 {
201         return get_link_vfunc(i);
202 }
203
204 void
205 LinkableValueNode::unlink_all()
206 {
207         for(int i=0;i<link_count();i++)
208         {
209                 ValueNode::LooseHandle value_node(get_link(i));
210                 if(value_node)
211                         value_node->parent_set.erase(this);
212         }
213 }
214
215 ValueNode::~ValueNode()
216 {
217         value_node_count--;
218         
219         begin_delete();
220
221         //DEBUGPOINT();
222 }
223
224 void
225 ValueNode::on_changed()
226 {
227         if(get_parent_canvas())
228                 get_parent_canvas()->signal_value_node_changed()(this);
229         else if(get_root_canvas() && get_parent_canvas())
230                 get_root_canvas()->signal_value_node_changed()(this);
231         
232         Node::on_changed();
233 }
234
235 int
236 ValueNode::replace(etl::handle<ValueNode> x)
237 {
238         if(x.get()==this)
239                 return 0;
240         
241         while(parent_set.size())
242         {
243                 (*parent_set.begin())->add_child(x.get());
244                 (*parent_set.begin())->remove_child(this);
245                 //x->parent_set.insert(*parent_set.begin());
246                 //parent_set.erase(parent_set.begin());
247         }
248         int r(RHandle(this).replace(x));
249         x->changed();
250         return r;
251 }
252
253 void
254 ValueNode::set_id(const String &x)
255 {
256         if(name!=x)
257         {
258                 name=x;
259                 signal_id_changed_();
260         }
261 }
262
263 ValueNodeList::ValueNodeList():
264         placeholder_count_(0)
265 {
266 }
267
268 bool
269 ValueNodeList::count(const String &id)const
270 {
271         const_iterator iter;
272
273         if(id.empty())
274                 return false;
275
276         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
277
278         if(iter==end())
279                 return false;
280
281         return true;
282 }
283
284 ValueNode::Handle
285 ValueNodeList::find(const String &id)
286 {
287         iterator iter;
288
289         if(id.empty())
290                 throw Exception::IDNotFound("Empty ID");
291
292         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
293
294         if(iter==end())
295                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
296
297         return *iter;
298 }
299
300 ValueNode::ConstHandle
301 ValueNodeList::find(const String &id)const
302 {
303         const_iterator iter;
304
305         if(id.empty())
306                 throw Exception::IDNotFound("Empty ID");
307
308         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
309
310         if(iter==end())
311                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
312
313         return *iter;
314 }
315
316 ValueNode::Handle
317 ValueNodeList::surefind(const String &id)
318 {
319         if(id.empty())
320                 throw Exception::IDNotFound("Empty ID");
321         
322         ValueNode::Handle value_node;
323         
324         try
325         {
326                 value_node=find(id);
327         }
328         catch(Exception::IDNotFound)
329         {
330                 value_node=PlaceholderValueNode::create();
331                 value_node->set_id(id);
332                 push_back(value_node);
333                 placeholder_count_++;
334         }
335
336         return value_node;
337 }
338
339 bool
340 ValueNodeList::erase(ValueNode::Handle value_node)
341 {
342         assert(value_node);
343
344         iterator iter;
345
346         for(iter=begin();iter!=end();++iter)
347                 if(value_node.get()==iter->get())
348                 {
349                         std::list<ValueNode::RHandle>::erase(iter);
350                         if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
351                                 placeholder_count_--;
352                         return true;
353                 }
354         return false;
355 }
356
357 bool
358 ValueNodeList::add(ValueNode::Handle value_node)
359 {
360         if(!value_node)
361                 return false;
362         if(value_node->get_id().empty())
363                 return false;
364
365         try
366         {
367                 ValueNode::RHandle other_value_node=find(value_node->get_id());
368                 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
369                 {
370                         other_value_node->replace(value_node);
371                         placeholder_count_--;
372                         return true;
373                 }
374
375                 return false;
376         }
377         catch(Exception::IDNotFound)
378         {       
379                 push_back(value_node);
380                 return true;
381         }
382         
383         return false;
384 }
385
386 void
387 ValueNodeList::audit()
388 {
389         iterator iter,next;
390
391         for(next=begin(),iter=next++;iter!=end();iter=next++)
392                 if(iter->count()==1)
393                         std::list<ValueNode::RHandle>::erase(iter);
394 }
395
396
397 String
398 PlaceholderValueNode::get_name()const
399 {
400         return "placeholder";
401 }
402
403 String
404 PlaceholderValueNode::get_local_name()const
405 {
406         return _("Placeholder");
407 }
408
409 ValueNode*
410 PlaceholderValueNode::clone(const GUID& deriv_guid)const
411 {
412         ValueNode* ret(new PlaceholderValueNode());
413         ret->set_guid(get_guid()^deriv_guid);
414         return ret;
415 }
416
417 PlaceholderValueNode::Handle
418 PlaceholderValueNode::create(ValueBase::Type type)
419 {
420         return new PlaceholderValueNode(type);
421 }
422
423 ValueBase
424 PlaceholderValueNode::operator()(Time t)const
425 {
426         assert(0);
427         return ValueBase();
428 }
429
430 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
431         ValueNode(type)
432 {
433 }
434
435 ValueNode*
436 LinkableValueNode::clone(const GUID& deriv_guid)const
437 {
438         { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
439
440         int i;
441         LinkableValueNode *ret=create_new();
442         ret->set_guid(get_guid()^deriv_guid);
443         
444         for(i=0;i<link_count();i++)
445         {
446                 ValueNode::Handle link=get_link_vfunc(i);
447                 if(!link->is_exported())
448                 {
449                         ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
450                         if(!value_node)
451                                 value_node=link->clone(deriv_guid);
452                         ret->set_link(i,value_node);
453                 }
454                 else
455                         ret->set_link(i,link);
456         }
457
458         return ret;
459 }
460
461 String
462 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
463 {
464         assert(is_exported());
465         assert(canvas_);
466         
467         if(x.get()==canvas_.get())
468                 return get_id();
469         
470         return canvas_->_get_relative_id(x)+':'+get_id();
471 }
472
473 void
474 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
475 {
476         canvas_=x; if(x) root_canvas_=x->get_root();
477 }
478
479 void
480 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
481 {
482         root_canvas_=x->get_root();
483 }
484
485 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
486 {
487         ValueNode::LooseHandle  h;
488         
489         int size = link_count();
490                 
491         //just add it to the set...
492         for(int i=0; i < size; ++i)
493         {
494                 h = get_link(i);
495
496                 if(h)
497                 {
498                         const Node::time_set &tset = h->get_times();
499                         set.insert(tset.begin(),tset.end());
500                 }
501         }
502 }