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