Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / 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  * 0.7: svn r2315
102  *
103  *      Added "loop" link to the "Random" ValueNode in svn r2315.
104  */
105
106 #define CURRENT_CANVAS_VERSION "0.7"
107
108 /* === T Y P E D E F S ===================================================== */
109
110 /* === C L A S S E S & S T R U C T S ======================================= */
111
112 namespace synfig {
113
114 class Context;
115 class GUID;
116
117 /*!     \class Canvas
118 **      \todo writeme
119 */
120 class Canvas : public CanvasBase, public Node
121 {
122         /*
123  --     ** -- T Y P E S -----------------------------------------------------------
124         */
125
126 public:
127         typedef etl::handle<Canvas> Handle;
128         typedef etl::loose_handle<Canvas> LooseHandle;
129         typedef etl::handle<const Canvas> ConstHandle;
130
131         typedef std::list<Handle> Children;
132
133         friend void synfig::optimize_layers(Time, Context, Canvas::Handle, bool seen_motion_blur);
134
135         /*
136  --     ** -- D A T A -------------------------------------------------------------
137         */
138
139 private:
140
141         //! Contains the ID string for the Canvas
142         /*!     \see get_id(), set_id() */
143         String id_;
144
145         //! Contains the name of the Canvas
146         /*!     \see set_name(), get_name() */
147         String name_;
148
149         //! Contains a description of the Canvas
150         /*!     \see set_description(), get_description() */
151         String description_;
152
153         //! Contains the canvas' version string
154         /*!     \see set_version(), get_version() */
155         String version_;
156
157         //! Contains the author's name
158         /*!     \see set_author(), get_author() */
159         String author_;
160
161         //! Contains the author's email address
162         /*!     \todo This private parameter has no binding, so it's unusable at the moment */
163         String email_;
164
165         //! File name of Canvas
166         /*! \see get_file_name(), set_file_name() */
167         String file_name_;
168
169         //! Metadata map for Canvas.
170         /*! \see get_meta_data(), set_meta_data(), erase_meta_data() */
171         std::map<String, String> meta_data_;
172
173         //! Contains a list of ValueNodes that are in this Canvas
174         /*!     \see value_node_list(), find_value_node() */
175         ValueNodeList value_node_list_;
176
177         //! \writeme
178         KeyframeList keyframe_list_;
179
180         //! A handle to the parent canvas of this canvas.
181         /*!     If canvas is a root canvas, then this handle is empty
182         **      \see parent()
183         */
184         LooseHandle parent_;
185
186         //! List containing any child Canvases
187         /*!     \see children() */
188         Children children_;
189
190         //! Render Description for Canvas
191         /*!     \see rend_desc() */
192     RendDesc desc_;
193
194         //! Contains the value of the last call to set_time()
195         Time cur_time_;
196
197         //! \writeme
198         mutable std::map<String,Handle> externals_;
199
200         //! This flag is set if this canvas is "inline"
201         bool is_inline_;
202
203         mutable bool is_dirty_;
204
205         bool op_flag_;
206
207         //! Layer Group database
208         std::map<String,std::set<etl::handle<Layer> > > group_db_;
209
210         //! Layer Connection database
211         std::map<etl::loose_handle<Layer>,std::vector<sigc::connection> > connections_;
212
213         /*
214  -- ** -- S I G N A L S -------------------------------------------------------
215         */
216
217 private:
218
219         //!     Group Added
220         sigc::signal<void,String> signal_group_added_;
221
222         //!     Group Removed
223         sigc::signal<void,String> signal_group_removed_;
224
225         //! Group Changed
226         sigc::signal<void,String> signal_group_changed_;
227
228         sigc::signal<void,String,etl::handle<synfig::Layer> > signal_group_pair_added_;
229         sigc::signal<void,String,etl::handle<synfig::Layer> > signal_group_pair_removed_;
230
231         //!     Layers Reordered
232         sigc::signal<void,int*> signal_layers_reordered_;
233
234         //!     RendDesc Changed
235         sigc::signal<void> signal_rend_desc_changed_;
236
237         //!     ID Changed
238         sigc::signal<void> signal_id_changed_;
239
240         //!     Dirty
241         //sigc::signal<void> signal_dirty_;
242
243         //!     FileName Changed
244         sigc::signal<void> signal_file_name_changed_;
245
246         //!     Metadata Changed
247         sigc::signal<void, String> signal_meta_data_changed_;
248
249         //! Key-Specific meta data changed signals
250         std::map<String, sigc::signal<void> > signal_map_meta_data_changed_;
251
252
253         //!     ValueBasenode Changed
254         sigc::signal<void, etl::handle<ValueNode> > signal_value_node_changed_;
255
256         sigc::signal<void, etl::handle<ValueNode> > signal_value_node_renamed_;
257
258         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> > signal_value_node_child_added_;
259
260         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> > signal_value_node_child_removed_;
261
262         /*
263  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
264         */
265
266 public:
267
268         sigc::signal<void,String,etl::handle<synfig::Layer> >& signal_group_pair_added() { return signal_group_pair_added_; }
269         sigc::signal<void,String,etl::handle<synfig::Layer> >& signal_group_pair_removed() { return signal_group_pair_removed_; }
270
271         //!     Group Added
272         sigc::signal<void,String>& signal_group_added() { return signal_group_added_; }
273
274         //!     Group Removed
275         sigc::signal<void,String>& signal_group_removed() { return signal_group_removed_; }
276
277         //! Group Changed
278         sigc::signal<void,String>& signal_group_changed() { return signal_group_changed_; }
279
280         //!     Layers Reordered
281         sigc::signal<void,int*>& signal_layers_reordered() { return signal_layers_reordered_; }
282
283         //!     RendDesc Changed
284         sigc::signal<void>& signal_rend_desc_changed() { return signal_rend_desc_changed_; }
285
286         //!     ID Changed
287         sigc::signal<void>& signal_id_changed() { return signal_id_changed_; }
288
289         //!     File name Changed
290         sigc::signal<void>& signal_file_name_changed();
291
292         //!     Metadata Changed
293         sigc::signal<void, String>& signal_meta_data_changed() { return signal_meta_data_changed_; }
294
295         //!     Metadata Changed
296         sigc::signal<void>& signal_meta_data_changed(const String& key) { return signal_map_meta_data_changed_[key]; }
297
298
299         sigc::signal<void, etl::handle<ValueNode> >& signal_value_node_changed() { return signal_value_node_changed_; }
300
301         sigc::signal<void, etl::handle<ValueNode> >& signal_value_node_renamed() { return signal_value_node_renamed_; }
302
303         //!     Dirty
304         sigc::signal<void>& signal_dirty() { return signal_changed();   }
305
306         //! \writeme
307         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> >& signal_value_node_child_added() { return signal_value_node_child_added_; }
308
309         //! \writeme
310         sigc::signal<void, etl::handle<ValueNode>, etl::handle<ValueNode> >& signal_value_node_child_removed() { return signal_value_node_child_removed_; }
311
312         /*
313  --     ** -- C O N S T R U C T O R S ---------------------------------------------
314         */
315
316 protected:
317
318         Canvas(const String &name);
319
320 public:
321
322         ~Canvas();
323
324         /*
325  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
326         */
327
328 public:
329
330         //! Returns the set of layers in group
331         std::set<etl::handle<Layer> > get_layers_in_group(const String&group);
332
333         //! Gets all the groups
334         std::set<String> get_groups()const;
335
336         //! Gets the number of groups in this canvas
337         int get_group_count()const;
338
339         //! Renames the given group
340         void rename_group(const String&old_name,const String&new_name);
341
342         //! \writeme
343         bool is_inline()const { return is_inline_; }
344
345         //! Returns a handle to the RendDesc for this Canvas
346         RendDesc &rend_desc() { return desc_; }
347
348         //! Returns a handle to the RendDesc for this Canvas
349         const RendDesc &rend_desc()const { return desc_; }
350
351         //! Gets the name of the canvas
352         const String & get_name()const { return name_; }
353
354         //! Sets the name of the canvas
355         void set_name(const String &x);
356
357         //! Gets the version string of the canvas
358         const String get_version()const { return version_; }
359
360         //! Sets the version string of the canvas
361         void set_version(const String &x) { version_ = x; }
362
363         //! Gets the author of the canvas
364         const String & get_author()const { return author_; }
365
366         //! Sets the author of the canvas
367         void set_author(const String &x);
368
369         //! Gets the description of the canvas
370         const String & get_description()const { return description_; }
371
372         //! Sets the name of the canvas
373         void set_description(const String &x);
374
375         //! Gets the ID of the canvas
376         const String & get_id()const { return id_; }
377
378         //! Sets the ID of the canvas
379         void set_id(const String &x);
380
381         //!     Returns the data string for the given meta data key
382         String get_meta_data(const String& key)const;
383
384         //!     Returns a list of meta data keys
385         std::list<String> get_meta_data_keys()const;
386
387         //! Sets a meta data key to a specific string
388         void set_meta_data(const String& key, const String& data);
389
390         //! Removes a meta data key
391         void erase_meta_data(const String& key);
392
393         //! \writeme
394         String get_relative_id(etl::loose_handle<const Canvas> x)const;
395
396         //! \internal \writeme
397         String _get_relative_id(etl::loose_handle<const Canvas> x)const;
398
399         //! Returns \c true if the Canvas is a root Canvas. \c false otherwise
400         bool is_root()const { return !parent_; }
401
402         //! Returns a handle to the parent Canvas.
403         /*! The returned handle will be empty if this is a root canvas */
404         LooseHandle parent()const { return parent_; }
405
406         LooseHandle get_root()const;
407
408         //! Returns a list of all child canvases in this canvas
409         std::list<Handle> &children() { return children_; }
410
411         //! Returns a list of all child canvases in this canvas
412         const std::list<Handle> &children()const { return children_; }
413
414         //! Gets the color at the specified point
415         //Color get_color(const Point &pos)const;
416
417         //! Sets the time for all the layers in the canvas
418         void set_time(Time t)const;
419
420         //! \writeme
421         Time get_time()const { return cur_time_; }
422
423         //! Returns the number of layers in the canvas
424         int size()const;
425
426         //! Removes all the layers from the canvas
427         void clear();
428
429         //! Returns true if the canvas has no layers
430         bool empty()const;
431
432         //! Returns a reference to the ValueNodeList for this Canvas
433         // ValueNodeList &value_node_list() { return value_node_list_; }
434
435         //! Returns a reference to the ValueNodeList for this Canvas
436         const ValueNodeList &value_node_list()const;
437
438         //! Returns a reference to the KeyframeList for this Canvas
439         KeyframeList &keyframe_list();
440
441         //! Returns a reference to the KeyframeList for this Canvas
442         const KeyframeList &keyframe_list()const;
443
444         //! Finds the ValueNode in the Canvas with the given \a id
445         /*!     \return If found, returns a handle to the ValueNode.
446         **              Otherwise, returns an empty handle.
447         */
448         ValueNode::Handle find_value_node(const String &id);
449
450         //! \internal \writeme
451         ValueNode::Handle surefind_value_node(const String &id);
452
453         //! Finds the ValueNode in the Canvas with the given \a id
454         /*!     \return If found, returns a handle to the ValueNode.
455         **              Otherwise, returns an empty handle.
456         */
457         ValueNode::ConstHandle find_value_node(const String &id)const;
458
459         //! \writeme
460         void add_value_node(ValueNode::Handle x, const String &id);
461
462         //! writeme
463         //void rename_value_node(ValueNode::Handle x, const String &id);
464
465         //! \writeme
466         void remove_value_node(ValueNode::Handle x);
467
468         //! \writeme
469         void remove_value_node(const String &id) { remove_value_node(find_value_node(id)); }
470
471         //! Finds a child Canvas in the Canvas with the given \a name
472         /*!     \return If found, returns a handle to the child Canvas.
473         **              If not found, it creates a new Canvas and returns it
474         **              If an error occurs, it returns an empty handle
475         */
476         Handle surefind_canvas(const String &id,String &warnings);
477
478         //! Finds a child Canvas in the Canvas with the given \a id
479         /*!     \return If found, returns a handle to the child Canvas.
480         **              Otherwise, returns an empty handle.
481         */
482         Handle find_canvas(const String &id, String &warnings);
483
484         //! Finds a child Canvas in the Canvas with the given \a id
485         /*!     \return If found, returns a handle to the child Canvas.
486         **              Otherwise, returns an empty handle.
487         */
488         ConstHandle find_canvas(const String &id, String &warnings)const;
489
490         //! Sets the file path for the Canvas
491         //void set_file_path(const String &);
492
493         //! Returns the file path from the file name
494         String get_file_path()const;
495
496         //! Sets the filename (with path)
497         void set_file_name(const String &);
498
499         //! Gets the filename (with path)
500         String get_file_name()const;
501
502         //! Creates a new child canvas, and returns its handle
503         Handle new_child_canvas();
504
505         //! Creates a new child canvas with an ID of \a id, and returns its handle
506         Handle new_child_canvas(const String &id);
507
508         //! Adds the given canvas as a child
509         Handle add_child_canvas(Handle child_canvas, const String &id);
510
511         void remove_child_canvas(Handle child_canvas);
512
513         etl::handle<Layer> find_layer(const Point &pos);
514
515         int get_depth(etl::handle<Layer>)const;
516
517         Context get_context()const;
518
519         iterator end();
520
521         const_iterator end()const;
522
523         reverse_iterator rbegin();
524
525         const_reverse_iterator rbegin()const;
526
527         etl::handle<Layer> &back();
528
529         void push_back(etl::handle<Layer> x);
530
531         void push_front(etl::handle<Layer> x);
532
533         void push_back_simple(etl::handle<Layer> x);
534
535         void insert(iterator iter,etl::handle<Layer> x);
536         void erase(iterator iter);
537
538         const etl::handle<Layer> &back()const;
539
540         void set_inline(LooseHandle parent);
541
542         static Handle create();
543
544         static Handle create_inline(Handle parent);
545
546         Handle clone(const GUID& deriv_guid=GUID())const;
547
548         void register_external_canvas(String file, Handle canvas);
549
550 #ifdef _DEBUG
551         void show_externals(String file, int line, String text) const;
552 #endif  // _DEBUG
553
554 private:
555         void add_group_pair(String group, etl::handle<Layer> layer);
556         void remove_group_pair(String group, etl::handle<Layer> layer);
557         void add_connection(etl::loose_handle<Layer> layer, sigc::connection connection);
558         void disconnect_connections(etl::loose_handle<Layer> layer);
559
560 protected:
561         virtual void on_changed();
562         virtual void get_times_vfunc(Node::time_set &set) const;
563 }; // END of class Canvas
564
565 void optimize_layers(Time time, Context context, Canvas::Handle op_canvas, bool seen_motion_blur=false);
566
567
568 }; // END of namespace synfig
569
570 /* === E N D =============================================================== */
571
572 #endif