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