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 <set>
11 #include <sigc++/functors/mem_fun.h>
12 #include <sigc++/adaptors/hide.h>
13 #include "gc-managed.h"
14 #include "gc-finalized.h"
15 #include "document.h"
16 #include "desktop.h"
17 #include "layer-manager.h"
18 #include "ui/view/view.h"
19 #include "sp-object.h"
20 #include "xml/node.h"
21 #include "xml/node-observer.h"
23 namespace Inkscape {
26 using Inkscape::XML::Node;
28 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
29 public:
30 LayerWatcher(LayerManager* mgr, SPObject* obj) :
31 _mgr(mgr),
32 _obj(obj),
33 _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
34 _labelAttr(g_quark_from_string("inkscape:label"))
35 {}
37 virtual void notifyChildAdded( Node &node, Node &child, Node *prev ) {}
38 virtual void notifyChildRemoved( Node &node, Node &child, Node *prev ) {}
39 virtual void notifyChildOrderChanged( Node &node, Node &child, Node *old_prev, Node *new_prev ) {}
40 virtual void notifyContentChanged( Node &node, Util::ptr_shared<char> old_content, Util::ptr_shared<char> new_content ) {}
41 virtual void notifyAttributeChanged( Node &node, GQuark name, Util::ptr_shared<char> old_value, Util::ptr_shared<char> new_value ) {
42 if ( name == _lockedAttr || name == _labelAttr ) {
43 if ( _mgr && _obj ) {
44 _mgr->_objectModified( _obj, 0 );
45 }
46 }
47 }
49 LayerManager* _mgr;
50 SPObject* _obj;
51 GQuark _lockedAttr;
52 GQuark _labelAttr;
53 };
56 LayerManager::LayerManager(SPDesktop *desktop)
57 : _desktop(desktop), _document(NULL)
58 {
59 _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
61 sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
63 // This next line has problems on gcc 4.0.2
64 sigc::slot<void, SPDocument*> base2 = first;
66 sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
67 _document_connection = desktop->connectDocumentReplaced( slot2 );
69 _setDocument(desktop->doc());
70 }
73 void LayerManager::renameLayer( SPObject* obj, gchar const *label )
74 {
75 Glib::ustring incoming( label ? label : "" );
76 Glib::ustring result(incoming);
77 Glib::ustring base(incoming);
78 guint startNum = 1;
80 size_t pos = base.rfind('#');
81 if ( pos != Glib::ustring::npos ) {
82 gchar* numpart = g_strdup(base.substr(pos+1).c_str());
83 if ( numpart ) {
84 gchar* endPtr = 0;
85 guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
86 if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
87 base.erase( pos );
88 result = base;
89 startNum = static_cast<int>(val);
90 }
91 g_free(numpart);
92 }
93 }
95 std::set<Glib::ustring> currentNames;
96 GSList const *layers=sp_document_get_resource_list(_document, "layer");
97 SPObject *root=_desktop->currentRoot();
98 if ( root ) {
99 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
100 SPObject *layer=static_cast<SPObject *>(iter->data);
101 if ( layer != obj ) {
102 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
103 }
104 }
105 }
107 // Not sure if we need to cap it, but we'll just be paranoid for the moment
108 // Intentionally unsigned
109 guint endNum = startNum + 3000;
110 for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
111 gchar* suffix = g_strdup_printf("#%d", i);
112 result = base;
113 result += suffix;
115 g_free(suffix);
116 }
118 obj->setLabel( result.c_str() );
119 }
123 void LayerManager::_setDocument(SPDocument *document) {
124 if (_document) {
125 _resource_connection.disconnect();
126 }
127 _document = document;
128 if (document) {
129 _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
130 }
131 _rebuild();
132 }
135 void LayerManager::_objectModifiedCB( SPObject* obj, guint flags, LayerManager* mgr )
136 {
137 mgr->_objectModified( obj, flags );
138 }
140 void LayerManager::_objectModified( SPObject* obj, guint flags )
141 {
142 _details_changed_signal.emit( obj );
143 }
145 void LayerManager::_rebuild() {
146 while ( !_watchers.empty() ) {
147 LayerWatcher* one = _watchers.back();
148 _watchers.pop_back();
149 if ( one->_obj ) {
150 Node* node = SP_OBJECT_REPR(one->_obj);
151 if ( node ) {
152 node->removeObserver(*one);
153 }
154 }
155 }
157 _clear();
159 GSList const *layers=sp_document_get_resource_list(_document, "layer");
160 SPObject *root=_desktop->currentRoot();
161 if ( root ) {
162 _addOne(root);
164 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
165 SPObject *layer=static_cast<SPObject *>(iter->data);
167 for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
168 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
169 // Filter out objects in the middle of being deleted
171 // Such may have been the cause of bug 1339397.
172 // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
173 SPObject const *higher = curr;
174 while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
175 higher = SP_OBJECT_PARENT(higher);
176 }
177 Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
178 if ( node && node->parent() ) {
179 g_signal_connect( G_OBJECT(curr), "modified", G_CALLBACK( _objectModifiedCB ), this );
181 LayerWatcher* eye = new LayerWatcher(this, curr);
182 _watchers.push_back( eye );
183 SP_OBJECT_REPR(curr)->addObserver(*eye);
185 _addOne(curr);
186 }
187 }
188 }
189 }
190 }
191 }
193 // Connected to the desktop's CurrentLayerChanged signal
194 void LayerManager::_selectedLayerChanged(SPObject *layer)
195 {
196 // notify anyone who's listening to this instead of directly to the desktop
197 _layer_changed_signal.emit(layer);
198 }
200 }
202 /*
203 Local Variables:
204 mode:c++
205 c-file-style:"stroustrup"
206 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207 indent-tabs-mode:nil
208 fill-column:99
209 End:
210 */
211 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :