Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / src / synfig / canvas.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file canvas.h
3 **      \brief Canvas Class Implementation
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 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_CANVAS_H
27 #define __SYNFIG_CANVAS_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include <map>
32 #include <list>
33 #include <ETL/handle>
34 #include <sigc++/signal.h>
35 #include <sigc++/connection.h>
36
37 #include "vector.h"
38 #include "string.h"
39 #include "canvasbase.h"
40 #include "valuenode.h"
41 #include "keyframe.h"
42 #include "renddesc.h"
43 #include "node.h"
44 #include "guid.h"
45
46 /* === M A C R O S ========================================================= */
47
48 /* version change history:
49  *
50  * 0.1: the original version
51  *
52  *      if a waypoint goes from -179 to 179 degrees, that is a 2
53  *      degree change.  there's no way to express a 720 degree
54  *      rotation with a single pair of waypoints
55  *
56  * 0.2: svn r1227
57  *
58  *      angles no longer wrap at -180 degrees back to 180 degrees; if
59  *      a waypoint goes from -179 to 179 degrees, that is a rotation
60  *      of 358 degrees.  loading a version 0.1 canvas will modify
61  *      constant angle waypoints to that they are within 180 degrees
62  *      of the previous waypoint's value
63  *
64  *      the 'straight' blend method didn't used to work properly.  it
65  *      didn't work at all on transparent pixels in layers other than
66  *      the PasteCanvas layer.  for example, the examples/japan.sifz
67  *      file has a red circle (straight, amount=1.0) on top of a
68  *      striped conical gradient.  if 'straight' was working, the
69  *      conical gradient would be entirely obscured by the circle
70  *      layer (even by its transparent pixels)
71  *
72  * 0.3: svn r1422
73  *
74  *      the 'straight' blend method was fixed.  loading a version 0.2
75  *      or older canvas will replace the 'straight' blend method in
76  *      non-pastecanvas layers with 'composite', unless they're
77  *      completely transparent, in which case it will replace them
78  *      with an  'alpha over' blend instead.  Images like
79  *      examples/logo.sifz use transparent straight blends to do
80  *      masking, which no longer works now that 'straight' blending is
81  *      fixed.
82  *
83  *      Tangent lengths calculated by the "Segment Tangent" and "BLine
84  *      Tangent" ValueNodes were scaled by a factor of 0.5.
85  *
86  * 0.4: svn r1856
87  *
88  *      Stop scaling tangents by 0.5.
89  *
90  * 0.5: svn r1863
91  *
92  *      Added "offset", "scale", and "fixed_length" links to the
93  *      "BLine Tangent" ValueNode.
94  *
95  * 0.6: svn r2067
96  *
97  *      Added "scale" link to the "BLine Width" ValueNode in svn r1872.
98  *
99  *      Added "loop" link to the "Gradient Color" ValueNode in svn r1901.
100  */
101
102 #define CURRENT_CANVAS_VERSION "0.6"
103
104 /* === T Y P E D E F S ===================================================== */
105
106 /* === C L A S S E S & S T R U C T S ======================================= */
107
108 namespace synfig {
109
110 class Context;
111 class GUID;
112
113 /*!     \class Canvas
114 **      \todo writeme
115 */
116 class Canvas : public CanvasBase, public Node
117 {
118         /*
119  --     ** -- T Y P E S -----------------------------------------------------------
120         */
121
122 public:
123         typedef etl::handle<Canvas> Handle;
124         typedef etl::loose_handle<Canvas> LooseHandle;
125         typedef etl::handle<const Canvas> ConstHandle;
126
127         typedef std::list<Handle> Children;
128
129         friend void synfig::optimize_layers(Time, Context, Canvas::Handle, bool seen_motion_blur);
130
131         /*
132  --     ** -- D A T A -------------------------------------------------------------
133         */
134
135 private:
136
137         //! Contains the ID string for the Canvas
138         /*!     \see get_id(), set_id() */
139         String id_;
140
141         //! Contains the name of the Canvas
142         /*!     \see set_name(), get_name() */
143         String name_;
144
145         //! Contains a description of the Canvas
146         /*!     \see set_description(), get_description() */
147         String description_;
148
149         //! Contains the canvas' version string
150         /*!     \see set_version(), get_version() */
151         String version_;
152
153         //! Contains the author's name
154         /*!     \see set_author(), get_author() */
155         String author_;
156
157         //! Contains the author's email address
158         /*!     \todo This private parameter has no binding, so it's unusable at the moment */
159         String email_;
160
161         //! File name of Canvas
162         /*! \see get_file_name(), set_file_name() */
163         String file_name_;
164
165         //! Metadata map for Canvas.
166         /*! \see get_meta_data(), set_meta_data(), erase_meta_data() */
167         std::map<String, String> meta_data_;
168
169         //! Contains a list of ValueNodes that are in this Canvas
170         /*!     \see value_node_list(), find_value_node() */
171         ValueNodeList value_node_list_;
172
173         //! \writeme
174         KeyframeList keyframe_list_;
175
176         //! A handle to the parent canvas of this canvas.
177         /*!     If canvas is a root canvas, then this handle is empty
178         **      \see parent()
179         */
180         LooseHandle parent_;
181
182         //! List containing any child Canvases
183         /*!     \see children() */
184         Children children_;
185
186         //! Render Description for Canvas
187         /*!     \see rend_desc() */
188     RendDesc desc_;
189
190         //! Contains the value of the last call to set_time()
191         Time cur_time_;
192
193         //! \writeme
194         mutable std::map<String,Handle> externals_;
195
196         //! This flag is set if this canvas is "inline"
197         bool is_inline_;
198
199         mutable bool is_dirty_;
200
201         bool op_flag_;
202
203         //! Layer Group database
204         std::map<String,std::set<etl::handle<Layer> > > group_db_;
205
206         //! Layer Connection database
207         std::map<etl::loose_handle<Layer>,std::vector<sigc::connection> > connections_;
208
209         /*
210  -- ** -- S I G N A L S -------------------------------------------------------
211         */
212
213 private:
214
215         //!     Group Added
216         sigc::signal<void,String> signal_group_added_;
217
218         //!     Group Removed
219         sigc::signal<void,String> signal_group_removed_;
220
221         //! Group Changed
222         sigc::signal<void,String> signal_group_changed_;
223
224         sigc::signal<void,String,etl::handle<synfig::Layer> > signal_group_pair_added_;
225         sigc::signal<void,String,etl::handle<synfig::Layer> > signal_group_pair_removed_;
226
227         //!     Layers Reordered
228         sigc::signal<void,int*> signal_layers_reordered_;
229
230         //!     RendDesc Changed
231         sigc::signal<void> signal_rend_desc_changed_;
232
233         //!     ID Changed
234         sigc::signal<void> signal_id_changed_;
235
236         //!     Dirty
237         //sigc::signal<void> signal_dirty_;
238
239         //!     FileName Changed
240         sigc::signal<void> signal_file_name_changed_;
241
242         //!     Metadata Changed
243         sigc::signal<void, String> signal_meta_data_changed_;
244
245         //! Key-Specific meta data changed signals
246         std::map<String, sigc::signal<void> > signal_map_meta_data_changed_;
247
248
249         //!     ValueBasenode Changed
250         sigc::signal<void, etl::handle<ValueNode> > signal_value_node_changed_;
251
252         sigc::signal<void, etl::handle<ValueNode> > signal_value_node_renamed_;
253
254         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> > signal_value_node_child_added_;
255
256         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> > signal_value_node_child_removed_;
257
258         /*
259  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
260         */
261
262 public:
263
264         sigc::signal<void,String,etl::handle<synfig::Layer> >& signal_group_pair_added() { return signal_group_pair_added_; }
265         sigc::signal<void,String,etl::handle<synfig::Layer> >& signal_group_pair_removed() { return signal_group_pair_removed_; }
266
267         //!     Group Added
268         sigc::signal<void,String>& signal_group_added() { return signal_group_added_; }
269
270         //!     Group Removed
271         sigc::signal<void,String>& signal_group_removed() { return signal_group_removed_; }
272
273         //! Group Changed
274         sigc::signal<void,String>& signal_group_changed() { return signal_group_changed_; }
275
276         //!     Layers Reordered
277         sigc::signal<void,int*>& signal_layers_reordered() { return signal_layers_reordered_; }
278
279         //!     RendDesc Changed
280         sigc::signal<void>& signal_rend_desc_changed() { return signal_rend_desc_changed_; }
281
282         //!     ID Changed
283         sigc::signal<void>& signal_id_changed() { return signal_id_changed_; }
284
285         //!     File name Changed
286         sigc::signal<void>& signal_file_name_changed();
287
288         //!     Metadata Changed
289         sigc::signal<void, String>& signal_meta_data_changed() { return signal_meta_data_changed_; }
290
291         //!     Metadata Changed
292         sigc::signal<void>& signal_meta_data_changed(const String& key) { return signal_map_meta_data_changed_[key]; }
293
294
295         sigc::signal<void, etl::handle<ValueNode> >& signal_value_node_changed() { return signal_value_node_changed_; }
296
297         sigc::signal<void, etl::handle<ValueNode> >& signal_value_node_renamed() { return signal_value_node_renamed_; }
298
299         //!     Dirty
300         sigc::signal<void>& signal_dirty() { return signal_changed();   }
301
302         //! \writeme
303         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> >& signal_value_node_child_added() { return signal_value_node_child_added_; }
304
305         //! \writeme
306         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> >& signal_value_node_child_removed() { return signal_value_node_child_removed_; }
307
308         /*
309  --     ** -- C O N S T R U C T O R S ---------------------------------------------
310         */
311
312 protected:
313
314         Canvas(const String &name);
315
316 public:
317
318         ~Canvas();
319
320         /*
321  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
322         */
323
324 public:
325
326         //! Returns the set of layers in group
327         std::set<etl::handle<Layer> > get_layers_in_group(const String&group);
328
329         //! Gets all the groups
330         std::set<String> get_groups()const;
331
332         //! Gets the number of groups in this canvas
333         int get_group_count()const;
334
335         //! Renames the given group
336         void rename_group(const String&old_name,const String&new_name);
337
338         //! \writeme
339         bool is_inline()const { return is_inline_; }
340
341         //! Returns a handle to the RendDesc for this Canvas
342         RendDesc &rend_desc() { return desc_; }
343
344         //! Returns a handle to the RendDesc for this Canvas
345         const RendDesc &rend_desc()const { return desc_; }
346
347         //! Gets the name of the canvas
348         const String & get_name()const { return name_; }
349
350         //! Sets the name of the canvas
351         void set_name(const String &x);
352
353         //! Gets the version string of the canvas
354         const String get_version()const { return version_; }
355
356         //! Sets the version string of the canvas
357         void set_version(const String &x) { version_ = x; }
358
359         //! Gets the author of the canvas
360         const String & get_author()const { return author_; }
361
362         //! Sets the author of the canvas
363         void set_author(const String &x);
364
365         //! Gets the description of the canvas
366         const String & get_description()const { return description_; }
367
368         //! Sets the name of the canvas
369         void set_description(const String &x);
370
371         //! Gets the ID of the canvas
372         const String & get_id()const { return id_; }
373
374         //! Sets the ID of the canvas
375         void set_id(const String &x);
376
377         //!     Returns the data string for the given meta data key
378         String get_meta_data(const String& key)const;
379
380         //!     Returns a list of meta data keys
381         std::list<String> get_meta_data_keys()const;
382
383         //! Sets a meta data key to a specific string
384         void set_meta_data(const String& key, const String& data);
385
386         //! Removes a meta data key
387         void erase_meta_data(const String& key);
388
389         //! \writeme
390         String get_relative_id(etl::loose_handle<const Canvas> x)const;
391
392         //! \internal \writeme
393         String _get_relative_id(etl::loose_handle<const Canvas> x)const;
394
395         //! Returns \c true if the Canvas is a root Canvas. \c false otherwise
396         bool is_root()const { return !parent_; }
397
398         //! Returns a handle to the parent Canvas.
399         /*! The returned handle will be empty if this is a root canvas */
400         LooseHandle parent()const { return parent_; }
401
402         LooseHandle get_root()const;
403
404         //! Returns a list of all child canvases in this canvas
405         std::list<Handle> &children() { return children_; }
406
407         //! Returns a list of all child canvases in this canvas
408         const std::list<Handle> &children()const { return children_; }
409
410         //! Gets the color at the specified point
411         //Color get_color(const Point &pos)const;
412
413         //! Sets the time for all the layers in the canvas
414         void set_time(Time t)const;
415
416         //! \writeme
417         Time get_time()const { return cur_time_; }
418
419         //! Returns the number of layers in the canvas
420         int size()const;
421
422         //! Removes all the layers from the canvas
423         void clear();
424
425         //! Returns true if the canvas has no layers
426         bool empty()const;
427
428         //! Returns a reference to the ValueNodeList for this Canvas
429         // ValueNodeList &value_node_list() { return value_node_list_; }
430
431         //! Returns a reference to the ValueNodeList for this Canvas
432         const ValueNodeList &value_node_list()const;
433
434         //! Returns a reference to the KeyframeList for this Canvas
435         KeyframeList &keyframe_list();
436
437         //! Returns a reference to the KeyframeList for this Canvas
438         const KeyframeList &keyframe_list()const;
439
440         //! Finds the ValueNode in the Canvas with the given \a id
441         /*!     \return If found, returns a handle to the ValueNode.
442         **              Otherwise, returns an empty handle.
443         */
444         ValueNode::Handle find_value_node(const String &id);
445
446         //! \internal \writeme
447         ValueNode::Handle surefind_value_node(const String &id);
448
449         //! Finds the ValueNode in the Canvas with the given \a id
450         /*!     \return If found, returns a handle to the ValueNode.
451         **              Otherwise, returns an empty handle.
452         */
453         ValueNode::ConstHandle find_value_node(const String &id)const;
454
455         //! \writeme
456         void add_value_node(ValueNode::Handle x, const String &id);
457
458         //! writeme
459         //void rename_value_node(ValueNode::Handle x, const String &id);
460
461         //! \writeme
462         void remove_value_node(ValueNode::Handle x);
463
464         //! \writeme
465         void remove_value_node(const String &id) { remove_value_node(find_value_node(id)); }
466
467         //! Finds a child Canvas in the Canvas with the given \a name
468         /*!     \return If found, returns a handle to the child Canvas.
469         **              If not found, it creates a new Canvas and returns it
470         **              If an error occurs, it returns an empty handle
471         */
472         Handle surefind_canvas(const String &id);
473
474         //! Finds a child Canvas in the Canvas with the given \a id
475         /*!     \return If found, returns a handle to the child Canvas.
476         **              Otherwise, returns an empty handle.
477         */
478         Handle find_canvas(const String &id);
479
480         //! Finds a child Canvas in the Canvas with the given \a id
481         /*!     \return If found, returns a handle to the child Canvas.
482         **              Otherwise, returns an empty handle.
483         */
484         ConstHandle find_canvas(const String &id)const;
485
486         //! Sets the file path for the Canvas
487         //void set_file_path(const String &);
488
489         //! Returns the file path from the file name
490         String get_file_path()const;
491
492         //! Sets the filename (with path)
493         void set_file_name(const String &);
494
495         //! Gets the filename (with path)
496         String get_file_name()const;
497
498         //! Creates a new child canvas, and returns its handle
499         Handle new_child_canvas();
500
501         //! Creates a new child canvas with an ID of \a id, and returns its handle
502         Handle new_child_canvas(const String &id);
503
504         //! Adds the given canvas as a child
505         Handle add_child_canvas(Handle child_canvas, const String &id);
506
507         void remove_child_canvas(Handle child_canvas);
508
509         etl::handle<Layer> find_layer(const Point &pos);
510
511         int get_depth(etl::handle<Layer>)const;
512
513         Context get_context()const;
514
515         iterator end();
516
517         const_iterator end()const;
518
519         reverse_iterator rbegin();
520
521         const_reverse_iterator rbegin()const;
522
523         etl::handle<Layer> &back();
524
525         void push_back(etl::handle<Layer> x);
526
527         void push_front(etl::handle<Layer> x);
528
529         void push_back_simple(etl::handle<Layer> x);
530
531         void insert(iterator iter,etl::handle<Layer> x);
532         void erase(iterator iter);
533
534         const etl::handle<Layer> &back()const;
535
536         void set_inline(LooseHandle parent);
537
538         static Handle create();
539
540         static Handle create_inline(Handle parent);
541
542         Handle clone(const GUID& deriv_guid=GUID())const;
543
544 private:
545         void add_group_pair(String group, etl::handle<Layer> layer);
546         void remove_group_pair(String group, etl::handle<Layer> layer);
547         void add_connection(etl::loose_handle<Layer> layer, sigc::connection connection);
548         void disconnect_connections(etl::loose_handle<Layer> layer);
549
550 protected:
551         virtual void on_changed();
552         virtual void get_times_vfunc(Node::time_set &set) const;
553 }; // END of class Canvas
554
555 void optimize_layers(Time time, Context context, Canvas::Handle op_canvas, bool seen_motion_blur=false);
556
557
558 }; // END of namespace synfig
559
560 /* === E N D =============================================================== */
561
562 #endif