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"
20 namespace Inkscape {
22 LayerManager::LayerManager(SPDesktop *desktop)
23 : _desktop(desktop), _document(NULL)
24 {
25 sigc::slot<void> base = sigc::mem_fun(*this, &LayerManager::_rebuild);
26 sigc::slot<void, SPObject *> slot = sigc::hide<0>(base);
27 _layer_connection = desktop->connectCurrentLayerChanged(slot);
29 sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
31 // This next line has problems on gcc 4.0.2
32 sigc::slot<void, SPDocument*> base2 = first;
34 sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
35 _document_connection = desktop->connectDocumentReplaced( slot2 );
37 _setDocument(desktop->doc());
38 }
40 void LayerManager::_setDocument(SPDocument *document) {
41 if (_document) {
42 _resource_connection.disconnect();
43 }
44 _document = document;
45 if (document) {
46 _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
47 }
48 _rebuild();
49 }
51 void LayerManager::_rebuild() {
52 _clear();
53 GSList const *layers=sp_document_get_resource_list(_document, "layer");
54 SPObject *root=_desktop->currentRoot();
55 if ( root ) {
56 _addOne(root);
57 }
58 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
59 SPObject *layer=static_cast<SPObject *>(iter->data);
61 for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
62 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
63 // Filter out objects in the middle of being deleted
64 SPObject const *higher = curr;
65 while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
66 higher = SP_OBJECT_PARENT(higher);
67 }
68 Inkscape::XML::Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
69 if ( node && node->parent() ) {
70 _addOne(curr);
71 }
72 }
73 }
74 }
75 }
77 }
79 /*
80 Local Variables:
81 mode:c++
82 c-file-style:"stroustrup"
83 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
84 indent-tabs-mode:nil
85 fill-column:99
86 End:
87 */
88 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :