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