summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/export_multiplication.h
blob: cb6af793f11f1f04e16893f08523a9ecfc768613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
    Copyright (C) 2012 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/* This file is not used at the moment. It includes code related to export a
 * multiplication graph system that can be used together with the ExportMultiplicator
 * class in the gtk2_ardour folder.
 * - Sakari Bergen 6.8.2008 -
 */

/*** Graph classes ***/
  public:

	/// A node in the hierarchical graph that represents a multiplicatable export item
	class GraphNode {
	  public:
		GraphNode ();
		virtual ~GraphNode ();

		uint32_t id() const { return _id; }

		/* Children and parents. Note: only children are kept in order! */

		list<GraphNode *> const & get_parents () const { return parents; }

		void add_child (GraphNode * child, GraphNode * left_sibling);
		void remove_child (GraphNode * child);
		GraphNode * first_child () const { return children.front(); }
		GraphNode * last_child () const { return children.back(); }
		list<GraphNode *> const & get_children () const { return children; }

		/* Relation functions */

		bool is_ancestor_of (GraphNode const * node) const;
		bool is_descendant_of (GraphNode const * node) const;
		bool equals (GraphNode const * node) const { return node == this; }

		/* Selection functions */

		bool selected () const { return _selected; }
		void select (bool value);

		PBD::Signal1<void,bool> SelectChanged;

	  protected:

		/* Parent manipulation functions should be used only from child manipulation functions! */

		void add_parent (GraphNode * parent);
		void remove_parent (GraphNode * parent);

		list<GraphNode *> parents;
		list<GraphNode *> children;

		bool           _selected;
		uint32_t       _id;
		static uint32_t id_counter;
	};

	/// A graph node that contains data
	template <typename T>
	class DataNode : public GraphNode {
	  private:
		typedef boost::shared_ptr<T> DataPtr;
		typedef boost::shared_ptr<DataNode<T> > SelfPtr;
		typedef boost::weak_ptr<DataNode<T> > WeakSelfPtr;

		DataNode (DataPtr data) : _data (data) {}
		void set_self_ptr (boost::shared_ptr<DataNode<T> > ptr) { _self_ptr = ptr; }

	  public:
		static SelfPtr create (T * data)
		{
			SelfPtr ptr = SelfPtr (new DataNode<T> (DataPtr (data)));
			ptr->set_self_ptr (ptr);
			return ptr;
		}

		static SelfPtr create (DataPtr data)
		{
			SelfPtr ptr = SelfPtr (new DataNode<T> (data));
			ptr->set_self_ptr (ptr);
			return ptr;
		}

		DataPtr data() { return _data; }
		SelfPtr self_ptr () { return _self_ptr.lock(); }

		template<typename P> // Parent's data type
		void sort_parents (list<boost::shared_ptr<DataNode<P> > > const & sort_list)
		{
			parents.sort (NodeSorter<P> (sort_list));
		}

	  private:
		DataPtr     _data;
		WeakSelfPtr _self_ptr;
	};

  private:
	/* Sorts GraphNodes according to a list of DataNodes */

	template<typename T>
	class NodeSorter {
	  public:
		typedef list<boost::shared_ptr<DataNode<T> > > ListType;

		NodeSorter (ListType const & list) : list (list) {}

		bool operator() (GraphNode * one, GraphNode * other) // '<' operator
		{
			if (one == other) { return false; } // Strict weak ordering
			for (typename ListType::const_iterator it = list.begin(); it != list.end(); ++it) {
				if (it->get() == one) {
					return true;
				}
				if (it->get() == other) {
					return false;
				}
			}

			std::cerr << "Invalid comparison list given to NodeSorter" << std::endl;

			abort();
		}

	  private:
		ListType const & list;
	};

/*** Multiplication management ***/
  public:

	typedef DataNode<TimespanState> TimespanNode;
	typedef boost::shared_ptr<TimespanNode> TimespanNodePtr;

	typedef DataNode<ChannelConfigState> ChannelConfigNode;
	typedef boost::shared_ptr<ChannelConfigNode> ChannelConfigNodePtr;

	typedef DataNode<FormatState> FormatNode;
	typedef boost::shared_ptr<FormatNode> FormatNodePtr;

	typedef DataNode<FilenameState> FilenameNode;
	typedef boost::shared_ptr<FilenameNode> FilenameNodePtr;

	struct MultiplicationGraph {
		list<TimespanNodePtr>      timespans;
		list<ChannelConfigNodePtr> channel_configs;
		list<FormatNodePtr>        formats;
		list<FilenameNodePtr>      filenames;
	};

	MultiplicationGraph const & get_graph () { return graph; }

	void split_node (GraphNode * node, float position);
	void remove_node (GraphNode * node);

	PBD::Signal0<void> GraphChanged;

  private:

	void purge_graph ();

	template<typename T>
	static void insert_after (list<T> & the_list, T const & position, T const & element);

	template<typename T>
	static void remove_by_element (list<T> & the_list, T const & element);

	bool nodes_have_one_common_child (list<GraphNode *> const & the_list);
	list<GraphNode *>::const_iterator end_of_common_child_range (list<GraphNode *> const & the_list, list<GraphNode *>::const_iterator beginning);
	void split_node_at_position (GraphNode * old_node, GraphNode * new_node, float position);

	void split_timespan (TimespanNodePtr node, float position = 0.5);
	void split_channel_config (ChannelConfigNodePtr node, float position = 0.5);
	void split_format (FormatNodePtr node, float position = 0.5);
	void split_filename (FilenameNodePtr node, float position = 0.5);

	void duplicate_timespan_children (TimespanNodePtr source, TimespanNodePtr target, GraphNode * insertion_point = 0);
	void duplicate_channel_config_children (ChannelConfigNodePtr source, ChannelConfigNodePtr target, GraphNode * insertion_point = 0);
	void duplicate_format_children (FormatNodePtr source, FormatNodePtr target, GraphNode * insertion_point = 0);

	TimespanNodePtr duplicate_timespan_node (TimespanNodePtr node);
	ChannelConfigNodePtr duplicate_channel_config_node (ChannelConfigNodePtr node);
	FormatNodePtr duplicate_format_node (FormatNodePtr node);
	FilenameNodePtr duplicate_filename_node (FilenameNodePtr node);

	void remove_timespan (TimespanNodePtr node);
	void remove_channel_config (ChannelConfigNodePtr node);
	void remove_format (FormatNodePtr node);
	void remove_filename (FilenameNodePtr node);

	MultiplicationGraph graph;