slightly optimize bounding box computation for ArdourCanvas::PolyItem by avoiding...
[ardour.git] / libs / canvas / group.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <iostream>
21 #include <cairomm/context.h>
22
23 #include "pbd/stacktrace.h"
24 #include "pbd/compose.h"
25
26 #include "canvas/group.h"
27 #include "canvas/types.h"
28 #include "canvas/debug.h"
29 #include "canvas/item.h"
30 #include "canvas/canvas.h"
31
32 using namespace std;
33 using namespace ArdourCanvas;
34
35 int Group::default_items_per_cell = 64;
36
37
38 Group::Group (Canvas* canvas)
39         : Item (canvas)
40         , _lut (0)
41 {
42         
43 }
44
45 Group::Group (Group* parent)
46         : Item (parent)
47         , _lut (0)
48 {
49         
50 }
51
52 Group::Group (Group* parent, Duple position)
53         : Item (parent, position)
54         , _lut (0)
55 {
56         
57 }
58
59 Group::~Group ()
60 {
61         clear_items (true);
62 }
63
64 /** @param area Area to draw in this group's coordinates.
65  *  @param context Context, set up with its origin at this group's position.
66  */
67 void
68 Group::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
69 {
70         ensure_lut ();
71         vector<Item*> items = _lut->get (area);
72
73 #ifdef CANVAS_DEBUG
74         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
75                 cerr << string_compose ("%1GROUP %2 @ %7 render %5 @ %6 %3 items out of %4\n", 
76                                         _canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size(), area, _position, this);
77         }
78 #endif
79
80         ++render_depth;
81                 
82         for (vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {
83
84                 if (!(*i)->visible ()) {
85 #ifdef CANVAS_DEBUG
86                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
87                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] invisible - skipped\n";
88                         }
89 #endif
90                         continue;
91                 }
92                 
93                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
94
95                 if (!item_bbox) {
96 #ifdef CANVAS_DEBUG
97                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
98                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] empty - skipped\n";
99                         }
100 #endif
101                         continue;
102                 }
103                 
104                 Rect item = (*i)->item_to_window (item_bbox.get());
105                 boost::optional<Rect> d = item.intersection (area);
106                 
107                 if (d) {
108                         Rect draw = d.get();
109                         if (draw.width() && draw.height()) {
110 #ifdef CANVAS_DEBUG
111                                 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
112                                         if (dynamic_cast<Group*>(*i) == 0) {
113                                                 cerr << _canvas->render_indent() << "render "
114                                                      << ' ' 
115                                                      << (*i)
116                                                      << ' '
117                                                      << (*i)->whatami()
118                                                      << ' '
119                                                      << (*i)->name
120                                                      << " item = " 
121                                                      << item
122                                                      << " intersect = "
123                                                      << draw
124                                                      << " @ " 
125                                                      << _position
126                                                      << endl;
127                                         }
128                                 }
129 #endif
130
131                                 (*i)->render (area, context);
132                                 ++render_count;
133                         }
134
135                 } else {
136
137 #ifdef CANVAS_DEBUG
138                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
139                                 cerr << string_compose ("%1skip render of %2 %3, no intersection\n", _canvas->render_indent(), (*i)->whatami(),
140                                                         (*i)->name);
141                         }
142 #endif
143
144                 }
145         }
146
147         --render_depth;
148 }
149
150 void
151 Group::compute_bounding_box () const
152 {
153         Rect bbox;
154         bool have_one = false;
155
156         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
157
158                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
159
160                 if (!item_bbox) {
161                         continue;
162                 }
163
164                 Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
165                 if (have_one) {
166                         bbox = bbox.extend (group_bbox);
167                 } else {
168                         bbox = group_bbox;
169                         have_one = true;
170                 }
171         }
172
173         if (!have_one) {
174                 _bounding_box = boost::optional<Rect> ();
175         } else {
176                 _bounding_box = bbox;
177         }
178
179         _bounding_box_dirty = false;
180 }
181
182 void
183 Group::add (Item* i)
184 {
185         /* XXX should really notify canvas about this */
186
187         _items.push_back (i);
188         invalidate_lut ();
189         _bounding_box_dirty = true;
190
191         
192 }
193
194 void
195 Group::remove (Item* i)
196 {
197
198         if (i->parent() != this) {
199                 return;
200         }
201
202         /* we cannot call bounding_box() here because that will iterate over
203            _items, one of which (the argument, i) may be in the middle of
204            deletion, making it impossible to call compute_bounding_box()
205            on it.
206         */
207
208         if (_bounding_box) {
209                 _pre_change_bounding_box = _bounding_box;
210         } else {
211                 _pre_change_bounding_box = Rect();
212         }
213
214         i->unparent ();
215         _items.remove (i);
216         invalidate_lut ();
217         _bounding_box_dirty = true;
218         
219         end_change ();
220 }
221
222 void
223 Group::clear (bool with_delete)
224 {
225         begin_change ();
226
227         clear_items (with_delete);
228
229         invalidate_lut ();
230         _bounding_box_dirty = true;
231
232         end_change ();
233 }
234
235 void
236 Group::clear_items (bool with_delete)
237 {
238         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ) {
239
240                 list<Item*>::iterator tmp = i;
241                 Item *item = *i;
242
243                 ++tmp;
244
245                 /* remove from list before doing anything else, because we
246                  * don't want to find the item in _items during any activity
247                  * driven by unparent-ing or deletion.
248                  */
249
250                 _items.erase (i);
251                 item->unparent ();
252                 
253                 if (with_delete) {
254                         delete item;
255                 }
256
257                 i = tmp;
258         }
259 }
260
261 void
262 Group::raise_child_to_top (Item* i)
263 {
264         if (!_items.empty()) {
265                 if (_items.back() == i) {
266                         return;
267                 }
268         }
269
270         _items.remove (i);
271         _items.push_back (i);
272         invalidate_lut ();
273 }
274
275 void
276 Group::raise_child (Item* i, int levels)
277 {
278         list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
279         assert (j != _items.end ());
280
281         ++j;
282         _items.remove (i);
283
284         while (levels > 0 && j != _items.end ()) {
285                 ++j;
286                 --levels;
287         }
288
289         _items.insert (j, i);
290         invalidate_lut ();
291 }
292
293 void
294 Group::lower_child_to_bottom (Item* i)
295 {
296         if (!_items.empty()) {
297                 if (_items.front() == i) {
298                         return;
299                 }
300         }
301         _items.remove (i);
302         _items.push_front (i);
303         invalidate_lut ();
304 }
305
306 void
307 Group::ensure_lut () const
308 {
309         if (!_lut) {
310                 _lut = new DumbLookupTable (*this);
311         }
312 }
313
314 void
315 Group::invalidate_lut () const
316 {
317         delete _lut;
318         _lut = 0;
319 }
320
321 void
322 Group::child_changed ()
323 {
324         invalidate_lut ();
325         _bounding_box_dirty = true;
326
327         if (_parent) {
328                 _parent->child_changed ();
329         }
330 }
331
332 void
333 Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
334 {
335         boost::optional<Rect> const bbox = bounding_box ();
336
337         /* Point is in canvas coordinate system */
338
339         if (!bbox || !item_to_canvas (bbox.get()).contains (point)) {
340                 return;
341         }
342
343         /* now recurse and add any items within our group that contain point */
344
345         ensure_lut ();
346         vector<Item*> our_items = _lut->items_at_point (point);
347
348         if (!our_items.empty()) {
349                 /* this adds this group itself to the list of items at point */
350                 Item::add_items_at_point (point, items);
351         }
352
353         for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
354                 (*i)->add_items_at_point (point, items);
355         }
356 }
357
358 void
359 Group::dump (ostream& o) const
360 {
361 #ifdef CANVAS_DEBUG
362         o << _canvas->indent();
363         o << "Group " << this << " [" << name << ']';
364         o << " @ " << position();
365         o << " Items: " << _items.size();
366         o << " Visible ? " << _visible;
367
368         boost::optional<Rect> bb = bounding_box();
369
370         if (bb) {
371                 o << endl << _canvas->indent() << "  bbox: " << bb.get();
372                 o << endl << _canvas->indent() << "  CANVAS bbox: " << item_to_canvas (bb.get());
373         } else {
374                 o << "  bbox unset";
375         }
376
377         o << endl;
378 #endif
379
380         ArdourCanvas::dump_depth++;
381
382         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
383                 o << **i;
384         }
385
386         ArdourCanvas::dump_depth--;
387 }