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);
145 void clear();
147 private:
148 Record &_doAdd(SPObject *obj) {
149 sp_object_ref(obj);
150 Record &record=records[obj];
151 record.release_connection
152 = g_signal_connect(obj, "release",
153 (GCallback)&Relations::_release_object, this);
154 record.position_changed_connection
155 = obj->connectPositionChanged(
156 sigc::mem_fun(this, &Relations::reorder)
157 );
158 return record;
159 }
161 void _notifyAdded(SPObject *obj) {
162 added_signal.emit(obj);
163 }
165 void _doRemove(SPObject *obj) {
166 Record &record=records[obj];
167 if (record.release_connection) {
168 g_signal_handler_disconnect(obj, record.release_connection);
169 record.release_connection = 0;
170 }
171 record.position_changed_connection.disconnect();
172 records.erase(obj);
173 removed_signal.emit(obj);
174 sp_object_unref(obj);
175 }
177 void _doRemoveSubtree(SPObject *obj) {
178 Record *record=get(obj);
179 if (record) {
180 Siblings &children=record->children;
181 for ( Siblings::iterator iter=children.begin()
182 ; iter != children.end() ; ++iter )
183 {
184 _doRemoveSubtree(*iter);
185 }
186 _doRemove(obj);
187 }
188 }
190 static void _release_object(SPObject *obj, void *relations_p) {
191 Relations &relations=*static_cast<Relations *>(relations_p);
192 if (relations.get(obj)) {
193 relations.remove(obj, true);
194 }
195 }
196 };
198 DocumentSubset::DocumentSubset()
199 : _relations(new DocumentSubset::Relations())
200 {
201 }
203 void DocumentSubset::Relations::addOne(SPObject *obj) {
204 g_return_if_fail( obj != NULL );
205 g_return_if_fail( get(obj) != NULL );
207 Record &record=_doAdd(obj);
209 /* find the nearest ancestor in the subset */
210 Record *parent_record=NULL;
211 for ( SPObject::ParentIterator parent_iter=obj->parent
212 ; !parent_record && parent_iter ; ++parent_iter )
213 {
214 parent_record = get(parent_iter);
215 if (parent_record) {
216 record.parent = parent_iter;
217 }
218 }
219 if (!parent_record) {
220 parent_record = get(NULL);
221 g_assert( parent_record != NULL );
222 }
224 Siblings &children=record.children;
226 /* reparent descendants of obj to obj */
227 parent_record->extractDescendants(
228 std::back_insert_iterator<Siblings>(children),
229 obj
230 );
231 for ( Siblings::iterator iter=children.begin()
232 ; iter != children.end() ; ++iter )
233 {
234 Record *child_record=get(*iter);
235 g_assert( child_record != NULL );
236 child_record->parent = obj;
237 }
239 /* add obj to the child list */
240 parent_record->addChild(obj);
242 _notifyAdded(obj);
243 changed_signal.emit();
244 }
246 void DocumentSubset::Relations::remove(SPObject *obj, bool subtree) {
247 g_return_if_fail( obj != NULL );
249 Record *record=get(obj);
250 g_return_if_fail( record != NULL );
252 Record *parent_record=get(record->parent);
253 g_assert( parent_record != NULL );
255 unsigned index=parent_record->removeChild(obj);
257 if (subtree) {
258 _doRemoveSubtree(obj);
259 } else {
260 /* reparent obj's orphaned children to their grandparent */
261 Siblings &siblings=parent_record->children;
262 Siblings &children=record->children;
263 siblings.insert(siblings.begin()+index,
264 children.begin(), children.end());
266 for ( Siblings::iterator iter=children.begin()
267 ; iter != children.end() ; iter++ )
268 {
269 Record *child_record=get(*iter);
270 g_assert( child_record != NULL );
271 child_record->parent = record->parent;
272 }
274 /* remove obj's record */
275 _doRemove(obj);
276 }
278 changed_signal.emit();
279 }
281 void DocumentSubset::Relations::clear() {
282 Record &root=records[NULL];
284 while (!root.children.empty()) {
285 _doRemoveSubtree(root.children.front());
286 }
288 changed_signal.emit();
289 }
291 void DocumentSubset::Relations::reorder(SPObject *obj) {
292 SPObject::ParentIterator parent=obj;
294 /* find nearest ancestor in the subset */
295 Record *parent_record=NULL;
296 while (!parent_record) {
297 parent_record = get(++parent);
298 }
300 if (get(obj)) {
301 /* move the object if it's in the subset */
302 parent_record->removeChild(obj);
303 parent_record->addChild(obj);
304 changed_signal.emit();
305 } else {
306 /* otherwise, move any top-level descendants */
307 Siblings descendants;
308 parent_record->extractDescendants(
309 std::back_insert_iterator<Siblings>(descendants),
310 obj
311 );
312 if (!descendants.empty()) {
313 unsigned index=parent_record->findInsertIndex(obj);
314 Siblings &family=parent_record->children;
315 family.insert(family.begin()+index,
316 descendants.begin(), descendants.end());
317 changed_signal.emit();
318 }
319 }
320 }
322 void DocumentSubset::_addOne(SPObject *obj) {
323 _relations->addOne(obj);
324 }
326 void DocumentSubset::_remove(SPObject *obj, bool subtree) {
327 _relations->remove(obj, subtree);
328 }
330 void DocumentSubset::_clear() {
331 _relations->clear();
332 }
334 bool DocumentSubset::includes(SPObject *obj) const {
335 return _relations->get(obj);
336 }
338 SPObject *DocumentSubset::parentOf(SPObject *obj) const {
339 Relations::Record *record=_relations->get(obj);
340 return ( record ? record->parent : NULL );
341 }
343 unsigned DocumentSubset::childCount(SPObject *obj) const {
344 Relations::Record *record=_relations->get(obj);
345 return ( record ? record->children.size() : 0 );
346 }
348 unsigned DocumentSubset::indexOf(SPObject *obj) const {
349 SPObject *parent=parentOf(obj);
350 Relations::Record *record=_relations->get(parent);
351 return ( record ? record->childIndex(obj) : 0 );
352 }
354 SPObject *DocumentSubset::nthChildOf(SPObject *obj, unsigned n) const {
355 Relations::Record *record=_relations->get(obj);
356 return ( record ? record->children[n] : NULL );
357 }
359 sigc::connection DocumentSubset::connectChanged(sigc::slot<void> slot) const {
360 return _relations->changed_signal.connect(slot);
361 }
363 sigc::connection
364 DocumentSubset::connectAdded(sigc::slot<void, SPObject *> slot) const {
365 return _relations->added_signal.connect(slot);
366 }
368 sigc::connection
369 DocumentSubset::connectRemoved(sigc::slot<void, SPObject *> slot) const {
370 return _relations->removed_signal.connect(slot);
371 }
373 }
375 /*
376 Local Variables:
377 mode:c++
378 c-file-style:"stroustrup"
379 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
380 indent-tabs-mode:nil
381 fill-column:99
382 End:
383 */
384 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :