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