1 /* === S Y N F I G ========================================================= */
3 ** \brief Base class for Layers and Value Nodes.
4 ** It defines the base members for the parent - child relationship,
5 ** the times where the node is modified and the handling of
6 ** the GUID on deletion and changing.
11 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
12 ** Copyright (c) 2007 Chris Moore
14 ** This package is free software; you can redistribute it and/or
15 ** modify it under the terms of the GNU General Public License as
16 ** published by the Free Software Foundation; either version 2 of
17 ** the License, or (at your option) any later version.
19 ** This package is distributed in the hope that it will be useful,
20 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ** General Public License for more details.
25 /* ========================================================================= */
27 /* === S T A R T =========================================================== */
29 #ifndef __SYNFIG_PARENTNODE_H
30 #define __SYNFIG_PARENTNODE_H
32 /* === H E A D E R S ======================================================= */
34 #include <sigc++/signal.h>
39 #include "interpolation.h"
42 /* === M A C R O S ========================================================= */
44 // When a PasteCanvas layer has a non-zero 'time offset' parameter, should
45 // the waypoints shown for the canvas be adjusted? This currently only
46 // partially works - see the TODO at the end of layer_pastecanvas.cpp
47 #define ADJUST_WAYPOINTS_FOR_TIME_OFFSET
49 /* === T Y P E D E F S ===================================================== */
51 /* === C L A S S E S & S T R U C T S ======================================= */
55 //!\brief TimePoint class: holds the time and the before and after interpolation mode
57 * It includes a GUID, to make it unique
58 * \see guid.h interpolation.h
64 Interpolation before,after;
67 TimePoint(const Time& x=Time::begin()):
70 before(INTERPOLATION_NIL),
71 after(INTERPOLATION_NIL)
76 const char *c_str()const;
79 const GUID& get_guid()const { return guid; }
80 const Time& get_time()const { return time; }
81 Interpolation get_before()const { return before; }
82 Interpolation get_after()const { return after; }
84 void set_guid(const GUID& x) { guid=x; }
85 void set_time(const Time& x) { time=x; }
86 void set_before(Interpolation x) { before=x; }
87 void set_after(Interpolation x) { after=x; }
89 //! Modify the TimePoint based on the values of \x "merging"
90 //! the interpolations. Used to insert a Time Point in a Time Points Set
91 //! \see TimePointSet::iterator TimePointSet::insert(const TimePoint& x)
92 void absorb(const TimePoint& x);
93 }; // END of class TimePoint
95 inline TimePoint operator+(TimePoint lhs,const Time& rhs)
96 { lhs.set_time(lhs.get_time()+rhs); return lhs; }
98 inline TimePoint operator-(TimePoint lhs,const Time& rhs)
99 { lhs.set_time(lhs.get_time()-rhs); return lhs; }
101 inline bool operator<(const TimePoint& lhs,const TimePoint& rhs)
102 { return lhs.get_time()<rhs.get_time(); }
104 inline bool operator<(const TimePoint& lhs,const Time& rhs)
105 { return lhs.get_time()<rhs; }
107 inline bool operator<(const Time& lhs,const TimePoint& rhs)
108 { return lhs<rhs.get_time(); }
110 inline bool operator==(const TimePoint& lhs,const TimePoint& rhs)
111 { return lhs.get_time()==rhs.get_time(); }
113 inline bool operator!=(const TimePoint& lhs,const TimePoint& rhs)
114 { return lhs.get_time()!=rhs.get_time(); }
116 class TimePointSet : public std::set<TimePoint>
119 iterator insert(const TimePoint& x);
121 template <typename ITER> void insert(ITER begin, ITER end)
122 { for(;begin!=end;++begin) insert(*begin); }
124 }; // END of class TimePointSet
126 class Node : public etl::rshared_object
129 -- ** -- T Y P E S -----------------------------------------------------------
135 typedef TimePointSet time_set;
138 -- ** -- D A T A -------------------------------------------------------------
143 //! \ The GUID of the node
146 //! cached time values for all the children
147 mutable time_set times;
150 mutable bool bchanged;
152 //! The last time the node was modified since the program started
154 mutable int time_last_changed_;
158 mutable RWLock rw_lock_;
160 //! Variable used to remember that a signal_deleted has been thrown
165 //! A set of pointers to parent nodes
166 //! \todo This should really be private
167 std::set<Node*> parent_set;
170 -- ** -- S I G N A L S -------------------------------------------------------
175 //! Node changed signal
176 sigc::signal<void> signal_changed_;
178 //! GUID changed signal
179 /*! \note The second parameter is the *OLD* guid! */
180 sigc::signal<void,GUID> signal_guid_changed_;
182 //! Node deleted signal
183 sigc::signal<void> signal_deleted_;
186 -- ** -- S I G N A L I N T E R F A C E -------------------------------------
191 sigc::signal<void>& signal_deleted() { return signal_deleted_; }
193 sigc::signal<void>& signal_changed() { return signal_changed_; }
196 /*! \note The second parameter is the *OLD* guid! */
197 sigc::signal<void,GUID>& signal_guid_changed() { return signal_guid_changed_; }
200 -- ** -- C O N S T R U C T O R S ---------------------------------------------
207 // This class cannot be copied -- use clone() if necessary
215 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
222 //! Gets the GUID for this Node
223 const GUID& get_guid()const;
225 //! Sets the GUID for this Node
226 void set_guid(const GUID& x);
228 //! Gets the time when the Node was changed
229 int get_time_last_changed()const;
231 //! Adds the parameter \x as the child of the current Node
232 void add_child(Node*x);
234 //! Removes the parameter \x as a child of the current Node
235 void remove_child(Node*x);
237 //!Returns how many parenst has the current Node
238 int parent_count()const;
240 //! Returns the cached times values for all the children
241 const time_set &get_times() const;
244 RWLock& get_rw_lock()const { return rw_lock_; }
251 -- ** -- V I R T U A L F U N C T I O N S -----------------------------------
255 //! Used when the node has changed. Makes changed the parent too.
256 //! To be overloaded by the derivative classes. Emits a signal where the
257 //! the GUI can be connected to.
258 virtual void on_changed();
260 //! Used when the node's GUID has changed.
261 //! To be overloaded by the derivative classes. Emits a signal where the
262 //! the GUI can be connected to.
263 virtual void on_guid_changed(GUID guid);
265 //! Function to be overloaded that fills the Time Point Set with
266 //! all the children Time Points.
267 virtual void get_times_vfunc(time_set &set) const = 0;
268 }; // End of Node class
270 //! Finds a node by its GUID.
271 //! \see global_node_map()
272 synfig::Node* find_node(const synfig::GUID& guid);
274 //! Returns a Handle to the Node by its GUID
275 template<typename T> etl::handle<T>
276 guid_cast(const synfig::GUID& guid)
278 return etl::handle<T>::cast_dynamic(synfig::find_node(guid));
282 template <typename T>
283 synfig::String set_string(T start, T end)
285 synfig::String ret("[");
286 bool started = false;
290 if (started) ret += ", ";
293 ret += synfig::String((*start).c_str());
300 template <typename T>
301 synfig::String set_string(T set)
303 return set_string(set.begin(), set.end());
307 typedef etl::handle<Node> NodeHandle;
309 }; // END of namespace synfig
311 /* === E N D =============================================================== */