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"
20 #include "xml/node-observer.h"
22 namespace Inkscape {
25 using Inkscape::XML::Node;
27 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
28 public:
29 LayerWatcher(LayerManager* mgr, SPObject* obj) :
30 _mgr(mgr),
31 _obj(obj),
32 _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
33 _labelAttr(g_quark_from_string("inkscape:label"))
34 {}
36 virtual void notifyChildAdded( Node &node, Node &child, Node *prev ) {}
37 virtual void notifyChildRemoved( Node &node, Node &child, Node *prev ) {}
38 virtual void notifyChildOrderChanged( Node &node, Node &child, Node *old_prev, Node *new_prev ) {}
39 virtual void notifyContentChanged( Node &node, Util::ptr_shared<char> old_content, Util::ptr_shared<char> new_content ) {}
40 virtual void notifyAttributeChanged( Node &node, GQuark name, Util::ptr_shared<char> old_value, Util::ptr_shared<char> new_value ) {
41 if ( name == _lockedAttr || name == _labelAttr ) {
42 if ( _mgr && _obj ) {
43 _mgr->_objectModified( _obj, 0 );
44 }
45 }
46 }
48 LayerManager* _mgr;
49 SPObject* _obj;
50 GQuark _lockedAttr;
51 GQuark _labelAttr;
52 };
55 LayerManager::LayerManager(SPDesktop *desktop)
56 : _desktop(desktop), _document(NULL)
57 {
58 _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
60 sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
62 // This next line has problems on gcc 4.0.2
63 sigc::slot<void, SPDocument*> base2 = first;
65 sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
66 _document_connection = desktop->connectDocumentReplaced( slot2 );
68 _setDocument(desktop->doc());
69 }
71 void LayerManager::_setDocument(SPDocument *document) {
72 if (_document) {
73 _resource_connection.disconnect();
74 }
75 _document = document;
76 if (document) {
77 _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
78 }
79 _rebuild();
80 }
83 void LayerManager::_objectModifiedCB( SPObject* obj, guint flags, LayerManager* mgr )
84 {
85 mgr->_objectModified( obj, flags );
86 }
88 void LayerManager::_objectModified( SPObject* obj, guint flags )
89 {
90 _details_changed_signal.emit( obj );
91 }
93 void LayerManager::_rebuild() {
94 while ( !_watchers.empty() ) {
95 LayerWatcher* one = _watchers.back();
96 _watchers.pop_back();
97 if ( one->_obj ) {
98 Node* node = SP_OBJECT_REPR(one->_obj);
99 if ( node ) {
100 node->removeObserver(*one);
101 }
102 }
103 }
105 _clear();
107 GSList const *layers=sp_document_get_resource_list(_document, "layer");
108 SPObject *root=_desktop->currentRoot();
109 if ( root ) {
110 _addOne(root);
112 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
113 SPObject *layer=static_cast<SPObject *>(iter->data);
115 for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
116 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
117 // Filter out objects in the middle of being deleted
119 // Such may have been the cause of bug 1339397.
120 // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
121 SPObject const *higher = curr;
122 while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
123 higher = SP_OBJECT_PARENT(higher);
124 }
125 Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
126 if ( node && node->parent() ) {
127 g_signal_connect( G_OBJECT(curr), "modified", G_CALLBACK( _objectModifiedCB ), this );
129 LayerWatcher* eye = new LayerWatcher(this, curr);
130 _watchers.push_back( eye );
131 SP_OBJECT_REPR(curr)->addObserver(*eye);
133 _addOne(curr);
134 }
135 }
136 }
137 }
138 }
139 }
141 // Connected to the desktop's CurrentLayerChanged signal
142 void LayerManager::_selectedLayerChanged(SPObject *layer)
143 {
144 // notify anyone who's listening to this instead of directly to the desktop
145 _layer_changed_signal.emit(layer);
146 }
148 }
150 /*
151 Local Variables:
152 mode:c++
153 c-file-style:"stroustrup"
154 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155 indent-tabs-mode:nil
156 fill-column:99
157 End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :