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