Code

ab2dff10762028c4244684fedfddc6c4e45936c0
[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     _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
28     sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
30     // This next line has problems on gcc 4.0.2
31     sigc::slot<void, SPDocument*> base2 = first;
33     sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
34     _document_connection = desktop->connectDocumentReplaced( slot2 );
36     _setDocument(desktop->doc());
37 }
39 void LayerManager::_setDocument(SPDocument *document) {
40     if (_document) {
41         _resource_connection.disconnect();
42     }
43     _document = document;
44     if (document) {
45         _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
46     }
47     _rebuild();
48 }
51 void LayerManager::_objectModifiedCB( SPObject* obj, guint flags, LayerManager* mgr )
52 {
53     mgr->_objectModified( obj, flags );
54 }
56 void LayerManager::_objectModified( SPObject* obj, guint flags )
57 {
58     _details_changed_signal.emit( obj );
59 }
61 void LayerManager::_rebuild() {
62     _clear();
63     GSList const *layers=sp_document_get_resource_list(_document, "layer");
64     SPObject *root=_desktop->currentRoot();
65     if ( root ) {
66         _addOne(root);
68         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
69             SPObject *layer=static_cast<SPObject *>(iter->data);
71             for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
72                 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
73                     // Filter out objects in the middle of being deleted
75                     // Such may have been the cause of bug 1339397.
76                     // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
77                     SPObject const *higher = curr;
78                     while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
79                         higher = SP_OBJECT_PARENT(higher);
80                     }
81                     Inkscape::XML::Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
82                     if ( node && node->parent() ) {
83                         g_signal_connect( G_OBJECT(curr), "modified", G_CALLBACK( _objectModifiedCB ), this );
85                         _addOne(curr);
86                     }
87                 }
88             }
89         }
90     }
91 }
93 // Connected to the desktop's CurrentLayerChanged signal
94 void LayerManager::_selectedLayerChanged(SPObject *layer)
95 {
96     // notify anyone who's listening to this instead of directly to the desktop
97     _layer_changed_signal.emit(layer);
98 }
102 /*
103   Local Variables:
104   mode:c++
105   c-file-style:"stroustrup"
106   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
107   indent-tabs-mode:nil
108   fill-column:99
109   End:
110 */
111 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :