summaryrefslogtreecommitdiff
path: root/libs/canvas/group.cc
blob: 7d0d61308f4d61438f966a4958d65a5ada0eec56 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <cairomm/context.h>
#include "pbd/stacktrace.h"
#include "pbd/xml++.h"

#include "canvas/group.h"
#include "canvas/types.h"
#include "canvas/debug.h"
#include "canvas/item_factory.h"
#include "canvas/item.h"
#include "canvas/canvas.h"

using namespace std;
using namespace ArdourCanvas;

int Group::default_items_per_cell = 64;


Group::Group (Canvas* canvas)
	: Item (canvas)
	, _lut (0)
{
	
}

Group::Group (Group* parent)
	: Item (parent)
	, _lut (0)
{
	
}

Group::Group (Group* parent, Duple position)
	: Item (parent, position)
	, _lut (0)
{
	
}

Group::~Group ()
{
	for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
		(*i)->unparent ();
	}

	_items.clear ();
}

/** @param area Area to draw in this group's coordinates.
 *  @param context Context, set up with its origin at this group's position.
 */
void
Group::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
{
	ensure_lut ();
	vector<Item*> items = _lut->get (area);

	for (vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {

		if (!(*i)->visible ()) {
			continue;
		}
		
		boost::optional<Rect> item_bbox = (*i)->bounding_box ();

		if (!item_bbox) {
			continue;
		}

		/* convert the render area to our child's coordinates */
		Rect const item_area = (*i)->parent_to_item (area);

		/* intersect the child's render area with the child's bounding box */
		boost::optional<Rect> r = item_bbox.get().intersection (item_area);

		if (r) {
			/* render the intersection */
			context->save ();
			context->translate ((*i)->position().x, (*i)->position().y);
			(*i)->render (r.get(), context);
			context->restore ();
			++render_count;
		}
	}
}

void
Group::compute_bounding_box () const
{
	Rect bbox;
	bool have_one = false;

	for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
		boost::optional<Rect> item_bbox = (*i)->bounding_box ();
		if (!item_bbox) {
			continue;
		}

		Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
		if (have_one) {
			bbox = bbox.extend (group_bbox);
		} else {
			bbox = group_bbox;
			have_one = true;
		}
	}

	if (!have_one) {
		_bounding_box = boost::optional<Rect> ();
	} else {
		_bounding_box = bbox;
	}

	_bounding_box_dirty = false;
}

void
Group::add (Item* i)
{
	_items.push_back (i);
	invalidate_lut ();
	_bounding_box_dirty = true;
	
	DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: group add\n");
}

void
Group::remove (Item* i)
{
	_items.remove (i);
	invalidate_lut ();
	_bounding_box_dirty = true;
	
	DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: group remove\n");
}

void
Group::raise_child_to_top (Item* i)
{
	_items.remove (i);
	_items.push_back (i);
	invalidate_lut ();
}

void
Group::raise_child (Item* i, int levels)
{
	list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
	assert (j != _items.end ());

	++j;
	_items.remove (i);

	while (levels > 0 && j != _items.end ()) {
		++j;
		--levels;
	}

	_items.insert (j, i);
	invalidate_lut ();
}

void
Group::lower_child_to_bottom (Item* i)
{
	_items.remove (i);
	_items.push_front (i);
	invalidate_lut ();
}

void
Group::ensure_lut () const
{
	if (!_lut) {
		_lut = new DumbLookupTable (*this);
	}
}

void
Group::invalidate_lut () const
{
	delete _lut;
	_lut = 0;
}

void
Group::child_changed ()
{
	invalidate_lut ();
	_bounding_box_dirty = true;

	if (_parent) {
		_parent->child_changed ();
	}
}

void
Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
{
	boost::optional<Rect> const bbox = bounding_box ();

	if (!bbox || !bbox.get().contains (point)) {
		return;
	}

	Item::add_items_at_point (point, items);
	
	ensure_lut ();
	
	vector<Item*> our_items = _lut->items_at_point (point);
	for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
		(*i)->add_items_at_point (point - (*i)->position(), items);
	}
}

XMLNode *
Group::get_state () const
{
	XMLNode* node = new XMLNode ("Group");
	for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
		node->add_child_nocopy (*(*i)->get_state ());
	}

	add_item_state (node);
	return node;
}

void
Group::set_state (XMLNode const * node)
{
	set_item_state (node);

	XMLNodeList const & children = node->children ();
	for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
		/* this will create the item and add it to this group */
		create_item (this, *i);
	}
}

void
Group::dump (ostream& o) const
{
	o << _canvas->indent();
	o << "Group " << this;
	o << " Items: " << _items.size();

	boost::optional<Rect> bb = bounding_box();

	if (bb) {
		o << endl << _canvas->indent() << "  bbox: " << bb.get();
	} else {
		o << "  bbox unset";
	}

	o << endl;

	ArdourCanvas::dump_depth++;

	for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
		o << **i;
	}

	ArdourCanvas::dump_depth--;
}