Code

switch to sigc++ signal for "release"
[inkscape.git] / src / document-subset.cpp
1 /*
2  * Inkscape::DocumentSubset - view of a document including only a subset
3  *                            of nodes
4  *
5  * Copyright 2006  MenTaLguY  <mental@rydia.net>
6  *
7  * Released under GNU GPL, read the file 'COPYING' for more information
8  */
10 #include "gc-finalized.h"
11 #include "document-subset.h"
12 #include "document.h"
13 #include "sp-object.h"
15 #include <glib/gmessages.h>
17 #include <sigc++/signal.h>
18 #include <sigc++/functors/mem_fun.h>
20 #include "util/list.h"
21 #include "util/reverse-list.h"
23 #include <vector>
24 #include <map>
25 #include <algorithm>
26 #include <iterator>
28 namespace Inkscape {
30 struct DocumentSubset::Relations : public GC::Managed<GC::ATOMIC>,
31                                    public GC::Finalized
32 {
33     typedef std::vector<SPObject *> Siblings;
35     struct Record {
36         SPObject *parent;
37         Siblings children;
39         sigc::connection release_connection;
40         sigc::connection position_changed_connection;
42         Record() : parent(NULL) {}
44         unsigned childIndex(SPObject *obj) {
45             Siblings::iterator found;
46             found = std::find(children.begin(), children.end(), obj);
47             if ( found != children.end() ) {
48                 return found - children.begin();
49             } else {
50                 return 0;
51             }
52         }
54         unsigned findInsertIndex(SPObject *obj) const {
55             if (children.empty()) {
56                 return 0;
57             } else {
58                 Siblings::const_iterator first=children.begin();
59                 Siblings::const_iterator last=children.end() - 1;
61                 while ( first != last ) {
62                     Siblings::const_iterator mid = first + ( last - first + 1 ) / 2;
63                     int pos = sp_object_compare_position(*mid, obj);
64                     if ( pos < 0 ) {
65                         first = mid;
66                     } else if ( pos > 0 ) {
67                         if ( last == mid ) {
68                             last = mid - 1; // already at the top limit
69                         } else {
70                             last = mid;
71                         }
72                     } else {
73                         g_assert_not_reached();
74                     }
75                 }
77                 if ( first == last ) {
78                     // compare to the single possiblity left
79                     int pos = sp_object_compare_position(*last, obj);
80                     if ( pos < 0 ) {
81                         last++;
82                     }
83                 }
85                 return last - children.begin();
86             }
87         }
89         void addChild(SPObject *obj) {
90             unsigned index=findInsertIndex(obj);
91             children.insert(children.begin()+index, obj);
92         }
94         template <typename OutputIterator>
95         void extractDescendants(OutputIterator descendants,
96                                 SPObject *obj)
97         {
98             Siblings new_children;
99             bool found_one=false;
100             for ( Siblings::iterator iter=children.begin()
101                 ; iter != children.end() ; iter++ )
102             {
103                 if (obj->isAncestorOf(*iter)) {
104                     if (!found_one) {
105                         found_one = true;
106                         new_children.insert(new_children.end(),
107                                             children.begin(), iter);
108                     }
109                     *descendants++ = *iter;
110                 } else if (found_one) {
111                     new_children.push_back(*iter);
112                 }
113             }
114             if (found_one) {
115                 children.swap(new_children);
116             }
117         }
119         unsigned removeChild(SPObject *obj) {
120             Siblings::iterator found;
121             found = std::find(children.begin(), children.end(), obj);
122             unsigned index = found - children.begin();
123             if ( found != children.end() ) {
124                 children.erase(found);
125             }
126             return index;
127         }
128     };
130     typedef std::map<SPObject *, Record> Map;
131     Map records;
133     sigc::signal<void> changed_signal;
134     sigc::signal<void, SPObject *> added_signal;
135     sigc::signal<void, SPObject *> removed_signal;
137     Relations() { records[NULL]; }
139     ~Relations() {
140         for ( Map::iterator iter=records.begin()
141             ; iter != records.end() ; ++iter )
142         {
143             sp_object_unref((*iter).first);
144         }
145     }
147     Record *get(SPObject *obj) {
148         Map::iterator found=records.find(obj);
149         if ( found != records.end() ) {
150             return &(*found).second;
151         } else {
152             return NULL;
153         }
154     }
156     void addOne(SPObject *obj);
157     void remove(SPObject *obj, bool subtree);
158     void reorder(SPObject *obj);
159     void clear();
161 private:
162     Record &_doAdd(SPObject *obj) {
163         sp_object_ref(obj);
164         Record &record=records[obj];
165         record.release_connection
166           = obj->connectRelease(
167               sigc::mem_fun(this, &Relations::_release_object)
168             );
169         record.position_changed_connection
170           = obj->connectPositionChanged(
171               sigc::mem_fun(this, &Relations::reorder)
172             );
173         return record;
174     }
176     void _notifyAdded(SPObject *obj) {
177         added_signal.emit(obj);
178     }
180     void _doRemove(SPObject *obj) {
181         Record &record=records[obj];
182         record.release_connection.disconnect();
183         record.position_changed_connection.disconnect();
184         records.erase(obj);
186         if ( record.parent == NULL ) {
187             Record &root = records[NULL];
188             for ( Siblings::iterator it = root.children.begin(); it != root.children.end(); ++it ) {
189                 if ( *it == obj ) {
190                     root.children.erase( it );
191                     break;
192                 }
193             }
194         }
196         removed_signal.emit(obj);
197         sp_object_unref(obj);
198     }
200     void _doRemoveSubtree(SPObject *obj) {
201         Record *record=get(obj);
202         if (record) {
203             Siblings &children=record->children;
204             for ( Siblings::iterator iter=children.begin()
205                 ; iter != children.end() ; ++iter )
206             {
207                 _doRemoveSubtree(*iter);
208             }
209             _doRemove(obj);
210         }
211     }
213     void _release_object(SPObject *obj) {
214         if (get(obj)) {
215             remove(obj, true);
216         }
217     }
218 };
220 DocumentSubset::DocumentSubset()
221 : _relations(new DocumentSubset::Relations())
225 void DocumentSubset::Relations::addOne(SPObject *obj) {
226     g_return_if_fail( obj != NULL );
227     g_return_if_fail( get(obj) == NULL );
229     Record &record=_doAdd(obj);
231     /* find the nearest ancestor in the subset */
232     Record *parent_record=NULL;
233     for ( SPObject::ParentIterator parent_iter=obj->parent
234         ; !parent_record && parent_iter ; ++parent_iter )
235     {
236         parent_record = get(parent_iter);
237         if (parent_record) {
238             record.parent = parent_iter;
239         }
240     }
241     if (!parent_record) {
242         parent_record = get(NULL);
243         g_assert( parent_record != NULL );
244     }
246     Siblings &children=record.children;
248     /* reparent descendants of obj to obj */
249     parent_record->extractDescendants(
250         std::back_insert_iterator<Siblings>(children),
251         obj
252     );
253     for ( Siblings::iterator iter=children.begin()
254         ; iter != children.end() ; ++iter )
255     {
256         Record *child_record=get(*iter);
257         g_assert( child_record != NULL );
258         child_record->parent = obj;
259     }
261     /* add obj to the child list */
262     parent_record->addChild(obj);
264     _notifyAdded(obj);
265     changed_signal.emit();
268 void DocumentSubset::Relations::remove(SPObject *obj, bool subtree) {
269     g_return_if_fail( obj != NULL );
271     Record *record=get(obj);
272     g_return_if_fail( record != NULL );
274     Record *parent_record=get(record->parent);
275     g_assert( parent_record != NULL );
277     unsigned index=parent_record->removeChild(obj);
279     if (subtree) {
280         _doRemoveSubtree(obj);
281     } else {
282         /* reparent obj's orphaned children to their grandparent */
283         Siblings &siblings=parent_record->children;
284         Siblings &children=record->children;
285         siblings.insert(siblings.begin()+index,
286                         children.begin(), children.end());
288         for ( Siblings::iterator iter=children.begin()
289             ; iter != children.end() ; iter++ )
290         {
291             Record *child_record=get(*iter);
292             g_assert( child_record != NULL );
293             child_record->parent = record->parent;
294         }
296         /* remove obj's record */
297         _doRemove(obj);
298     }
299     
300     changed_signal.emit();
303 void DocumentSubset::Relations::clear() {
304     Record &root=records[NULL];
306     while (!root.children.empty()) {
307         _doRemoveSubtree(root.children.front());
308     }
310     changed_signal.emit();
313 void DocumentSubset::Relations::reorder(SPObject *obj) {
314     SPObject::ParentIterator parent=obj;
316     /* find nearest ancestor in the subset */
317     Record *parent_record=NULL;
318     while (!parent_record) {
319         parent_record = get(++parent);
320     }
322     if (get(obj)) {
323         /* move the object if it's in the subset */
324         parent_record->removeChild(obj);
325         parent_record->addChild(obj);
326         changed_signal.emit();
327     } else {
328         /* otherwise, move any top-level descendants */
329         Siblings descendants;
330         parent_record->extractDescendants(
331             std::back_insert_iterator<Siblings>(descendants),
332             obj
333         );
334         if (!descendants.empty()) {
335             unsigned index=parent_record->findInsertIndex(obj);
336             Siblings &family=parent_record->children;
337             family.insert(family.begin()+index,
338                           descendants.begin(), descendants.end());
339             changed_signal.emit();
340         }
341     }
344 void DocumentSubset::_addOne(SPObject *obj) {
345     _relations->addOne(obj);
348 void DocumentSubset::_remove(SPObject *obj, bool subtree) {
349     _relations->remove(obj, subtree);
352 void DocumentSubset::_clear() {
353     _relations->clear();
356 bool DocumentSubset::includes(SPObject *obj) const {
357     return _relations->get(obj);
360 SPObject *DocumentSubset::parentOf(SPObject *obj) const {
361     Relations::Record *record=_relations->get(obj);
362     return ( record ? record->parent : NULL );
365 unsigned DocumentSubset::childCount(SPObject *obj) const {
366     Relations::Record *record=_relations->get(obj);
367     return ( record ? record->children.size() : 0 );
370 unsigned DocumentSubset::indexOf(SPObject *obj) const {
371     SPObject *parent=parentOf(obj);
372     Relations::Record *record=_relations->get(parent);
373     return ( record ? record->childIndex(obj) : 0 );
376 SPObject *DocumentSubset::nthChildOf(SPObject *obj, unsigned n) const {
377     Relations::Record *record=_relations->get(obj);
378     return ( record ? record->children[n] : NULL );
381 sigc::connection DocumentSubset::connectChanged(sigc::slot<void> slot) const {
382     return _relations->changed_signal.connect(slot);
385 sigc::connection
386 DocumentSubset::connectAdded(sigc::slot<void, SPObject *> slot) const {
387     return _relations->added_signal.connect(slot);
390 sigc::connection
391 DocumentSubset::connectRemoved(sigc::slot<void, SPObject *> slot) const {
392     return _relations->removed_signal.connect(slot);
397 /*
398   Local Variables:
399   mode:c++
400   c-file-style:"stroustrup"
401   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
402   indent-tabs-mode:nil
403   fill-column:99
404   End:
405 */
406 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :