Code

d156b045b23dab2d2f58525e24f600ddb65f8995
[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         gulong release_connection;
40         sigc::connection position_changed_connection;
42         Record() : parent(NULL), release_connection(0) {}
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;
60                 while ( first != last ) {
61                     Siblings::const_iterator mid=first + ( last - first + 1 ) / 2;
62                     int pos=sp_object_compare_position(*mid, obj);
63                     if ( pos < 0 ) {
64                         first = mid;
65                     } else if ( pos > 0 ) {
66                         last = mid;
67                     } else {
68                         g_assert_not_reached();
69                     }
70                 }
71                 return last - children.begin();
72             }
73         }
75         void addChild(SPObject *obj) {
76             unsigned index=findInsertIndex(obj);
77             children.insert(children.begin()+index, obj);
78         }
80         template <typename OutputIterator>
81         void extractDescendants(OutputIterator descendants,
82                                 SPObject *obj)
83         {
84             Siblings new_children;
85             bool found_one=false;
86             for ( Siblings::iterator iter=children.begin()
87                 ; iter != children.end() ; iter++ )
88             {
89                 if (obj->isAncestorOf(*iter)) {
90                     if (!found_one) {
91                         found_one = true;
92                         new_children.insert(new_children.end(),
93                                             children.begin(), iter);
94                     }
95                     *descendants++ = *iter;
96                 } else if (found_one) {
97                     new_children.push_back(*iter);
98                 }
99             }
100             if (found_one) {
101                 children.swap(new_children);
102             }
103         }
105         unsigned removeChild(SPObject *obj) {
106             Siblings::iterator found;
107             found = std::find(children.begin(), children.end(), obj);
108             unsigned index = found - children.begin();
109             if ( found != children.end() ) {
110                 children.erase(found);
111             }
112             return index;
113         }
114     };
116     typedef std::map<SPObject *, Record> Map;
117     Map records;
119     sigc::signal<void> changed_signal;
120     sigc::signal<void, SPObject *> added_signal;
121     sigc::signal<void, SPObject *> removed_signal;
123     Relations() { records[NULL]; }
125     ~Relations() {
126         for ( Map::iterator iter=records.begin()
127             ; iter != records.end() ; ++iter )
128         {
129             sp_object_unref((*iter).first);
130         }
131     }
133     Record *get(SPObject *obj) {
134         Map::iterator found=records.find(obj);
135         if ( found != records.end() ) {
136             return &(*found).second;
137         } else {
138             return NULL;
139         }
140     }
142     void addOne(SPObject *obj);
143     void remove(SPObject *obj, bool subtree);
144     void reorder(SPObject *obj);
146 private:
147     Record &_doAdd(SPObject *obj) {
148         sp_object_ref(obj);
149         Record &record=records[obj];
150         record.release_connection
151           = g_signal_connect(obj, "release",
152                              (GCallback)&Relations::_release_object, this);
153         record.position_changed_connection
154           = obj->connectPositionChanged(
155               sigc::mem_fun(this, &Relations::reorder)
156             );
157         return record;
158     }
160     void _notifyAdded(SPObject *obj) {
161         added_signal.emit(obj);
162     }
164     void _doRemove(SPObject *obj) {
165         Record &record=records[obj];
166         if (record.release_connection) {
167             g_signal_handler_disconnect(obj, record.release_connection);
168             record.release_connection = 0;
169         }
170         record.position_changed_connection.disconnect();
171         records.erase(obj);
172         removed_signal.emit(obj);
173         sp_object_unref(obj);
174     }
176     void _doRemoveSubtree(SPObject *obj) {
177         Record *record=get(obj);
178         if (record) {
179             Siblings &children=record->children;
180             for ( Siblings::iterator iter=children.begin()
181                 ; iter != children.end() ; ++iter )
182             {
183                 _doRemoveSubtree(*iter);
184             }
185             _doRemove(obj);
186         }
187     }
189     static void _release_object(SPObject *obj, void *relations_p) {
190         Relations &relations=*static_cast<Relations *>(relations_p);
191         if (relations.get(obj)) {
192             relations.remove(obj, true);
193         }
194     }
195 };
197 DocumentSubset::DocumentSubset(SPDocument *document)
198 : _document(document), _relations(new DocumentSubset::Relations())
202 void DocumentSubset::Relations::addOne(SPObject *obj) {
203     g_return_if_fail( obj != NULL );
204     g_return_if_fail( get(obj) != NULL );
206     Record &record=_doAdd(obj);
208     /* find the nearest ancestor in the subset */
209     Record *parent_record=NULL;
210     for ( SPObject::ParentIterator parent_iter=obj->parent
211         ; !parent_record && parent_iter ; ++parent_iter )
212     {
213         parent_record = get(parent_iter);
214         if (parent_record) {
215             record.parent = parent_iter;
216         }
217     }
218     if (!parent_record) {
219         parent_record = get(NULL);
220         g_assert( parent_record != NULL );
221     }
223     Siblings &children=record.children;
225     /* reparent descendants of obj to obj */
226     parent_record->extractDescendants(
227         std::back_insert_iterator<Siblings>(children),
228         obj
229     );
230     for ( Siblings::iterator iter=children.begin()
231         ; iter != children.end() ; ++iter )
232     {
233         Record *child_record=get(*iter);
234         g_assert( child_record != NULL );
235         child_record->parent = obj;
236     }
238     /* add obj to the child list */
239     parent_record->addChild(obj);
241     _notifyAdded(obj);
242     changed_signal.emit();
245 void DocumentSubset::Relations::remove(SPObject *obj, bool subtree) {
246     g_return_if_fail( obj != NULL );
248     Record *record=get(obj);
249     g_return_if_fail( record != NULL );
251     Record *parent_record=get(record->parent);
252     g_assert( parent_record != NULL );
254     unsigned index=parent_record->removeChild(obj);
256     if (subtree) {
257         _doRemoveSubtree(obj);
258     } else {
259         /* reparent obj's orphaned children to their grandparent */
260         Siblings &siblings=parent_record->children;
261         Siblings &children=record->children;
262         siblings.insert(siblings.begin()+index,
263                         children.begin(), children.end());
265         for ( Siblings::iterator iter=children.begin()
266             ; iter != children.end() ; iter++ )
267         {
268             Record *child_record=get(*iter);
269             g_assert( child_record != NULL );
270             child_record->parent = record->parent;
271         }
273         /* remove obj's record */
274         _doRemove(obj);
275     }
276     
277     changed_signal.emit();
280 void DocumentSubset::Relations::reorder(SPObject *obj) {
281     SPObject::ParentIterator parent=obj;
283     /* find nearest ancestor in the subset */
284     Record *parent_record=NULL;
285     while (!parent_record) {
286         parent_record = get(++parent);
287     }
289     if (get(obj)) {
290         /* move the object if it's in the subset */
291         parent_record->removeChild(obj);
292         parent_record->addChild(obj);
293         changed_signal.emit();
294     } else {
295         /* otherwise, move any top-level descendants */
296         Siblings descendants;
297         parent_record->extractDescendants(
298             std::back_insert_iterator<Siblings>(descendants),
299             obj
300         );
301         if (!descendants.empty()) {
302             unsigned index=parent_record->findInsertIndex(obj);
303             Siblings &family=parent_record->children;
304             family.insert(family.begin()+index,
305                           descendants.begin(), descendants.end());
306             changed_signal.emit();
307         }
308     }
311 void DocumentSubset::_addOne(SPObject *obj) {
312     _relations->addOne(obj);
315 void DocumentSubset::_remove(SPObject *obj, bool subtree) {
316     _relations->remove(obj, subtree);
319 bool DocumentSubset::includes(SPObject *obj) const {
320     return _relations->get(obj);
323 SPObject *DocumentSubset::parentOf(SPObject *obj) const {
324     Relations::Record *record=_relations->get(obj);
325     return ( record ? record->parent : NULL );
328 unsigned DocumentSubset::childCount(SPObject *obj) const {
329     Relations::Record *record=_relations->get(obj);
330     return ( record ? record->children.size() : 0 );
333 unsigned DocumentSubset::indexOf(SPObject *obj) const {
334     SPObject *parent=parentOf(obj);
335     Relations::Record *record=_relations->get(parent);
336     return ( record ? record->childIndex(obj) : 0 );
339 SPObject *DocumentSubset::nthChildOf(SPObject *obj, unsigned n) const {
340     Relations::Record *record=_relations->get(obj);
341     return ( record ? record->children[n] : NULL );
344 sigc::connection DocumentSubset::connectChanged(sigc::slot<void> slot) const {
345     return _relations->changed_signal.connect(slot);
348 sigc::connection
349 DocumentSubset::connectAdded(sigc::slot<void, SPObject *> slot) const {
350     return _relations->added_signal.connect(slot);
353 sigc::connection
354 DocumentSubset::connectRemoved(sigc::slot<void, SPObject *> slot) const {
355     return _relations->removed_signal.connect(slot);
360 /*
361   Local Variables:
362   mode:c++
363   c-file-style:"stroustrup"
364   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
365   indent-tabs-mode:nil
366   fill-column:99
367   End:
368 */
369 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :