Code

090c967146312f2e7b342e370341f5b08131f4b3
[inkscape.git] / src / layer-manager.cpp
1 /*
2  * Inkscape::LayerManager - a view of a document's layers, relative
3  *                          to a particular desktop
4  *
5  * Copyright 2006  MenTaLguY  <mental@rydia.net>
6  *
7  * Released under GNU GPL, read the file 'COPYING' for more information
8  */
10 #include <sigc++/functors/mem_fun.h>
11 #include <sigc++/adaptors/hide.h>
12 #include "gc-managed.h"
13 #include "gc-finalized.h"
14 #include "document.h"
15 #include "desktop.h"
16 #include "layer-manager.h"
17 #include "ui/view/view.h"
18 #include "sp-object.h"
19 #include "xml/node.h"
21 namespace Inkscape {
23 LayerManager::LayerManager(SPDesktop *desktop)
24 : _desktop(desktop), _document(NULL)
25 {
26     sigc::slot<void> base = sigc::mem_fun(*this, &LayerManager::_rebuild);
27     sigc::slot<void, SPObject *> slot = sigc::hide<0>(base);
28     _layer_connection = desktop->connectCurrentLayerChanged(slot);
30     sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
32     // This next line has problems on gcc 4.0.2
33     sigc::slot<void, SPDocument*> base2 = first;
35     sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
36     _document_connection = desktop->connectDocumentReplaced( slot2 );
38     _setDocument(desktop->doc());
39 }
41 void LayerManager::_setDocument(SPDocument *document) {
42     if (_document) {
43         _resource_connection.disconnect();
44     }
45     _document = document;
46     if (document) {
47         _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
48     }
49     _rebuild();
50 }
52 void LayerManager::_rebuild() {
53     _clear();
54     GSList const *layers=sp_document_get_resource_list(_document, "layer");
55     SPObject *root=_desktop->currentRoot();
56     if ( root ) {
57         _addOne(root);
59         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
60             SPObject *layer=static_cast<SPObject *>(iter->data);
62             for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
63                 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
64                     // Filter out objects in the middle of being deleted
66                     // Such may have been the cause of bug 1339397.
67                     // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
68                     SPObject const *higher = curr;
69                     while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
70                         higher = SP_OBJECT_PARENT(higher);
71                     }
72                     Inkscape::XML::Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
73                     if ( node && node->parent() ) {
74                         _addOne(curr);
75                     }
76                 }
77             }
78         }
79     }
80 }
82 }
84 /*
85   Local Variables:
86   mode:c++
87   c-file-style:"stroustrup"
88   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
89   indent-tabs-mode:nil
90   fill-column:99
91   End:
92 */
93 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :