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 //g_return_if_fail( _desktop->currentRoot() );
79 if ( _desktop->currentRoot() ) {
80 _desktop->setCurrentLayer( obj );
82 if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
83 sp_desktop_selection( _desktop )->clear();
84 }
85 }
86 }
88 void LayerManager::renameLayer( SPObject* obj, gchar const *label )
89 {
90 Glib::ustring incoming( label ? label : "" );
91 Glib::ustring result(incoming);
92 Glib::ustring base(incoming);
93 guint startNum = 1;
95 size_t pos = base.rfind('#');
96 if ( pos != Glib::ustring::npos ) {
97 gchar* numpart = g_strdup(base.substr(pos+1).c_str());
98 if ( numpart ) {
99 gchar* endPtr = 0;
100 guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
101 if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
102 base.erase( pos );
103 result = base;
104 startNum = static_cast<int>(val);
105 }
106 g_free(numpart);
107 }
108 }
110 std::set<Glib::ustring> currentNames;
111 GSList const *layers=sp_document_get_resource_list(_document, "layer");
112 SPObject *root=_desktop->currentRoot();
113 if ( root ) {
114 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
115 SPObject *layer=static_cast<SPObject *>(iter->data);
116 if ( layer != obj ) {
117 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
118 }
119 }
120 }
122 // Not sure if we need to cap it, but we'll just be paranoid for the moment
123 // Intentionally unsigned
124 guint endNum = startNum + 3000;
125 for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
126 gchar* suffix = g_strdup_printf("#%d", i);
127 result = base;
128 result += suffix;
130 g_free(suffix);
131 }
133 obj->setLabel( result.c_str() );
134 }
138 void LayerManager::_setDocument(SPDocument *document) {
139 if (_document) {
140 _resource_connection.disconnect();
141 }
142 _document = document;
143 if (document) {
144 _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
145 }
146 _rebuild();
147 }
150 void LayerManager::_objectModifiedCB( SPObject* obj, guint flags, LayerManager* mgr )
151 {
152 mgr->_objectModified( obj, flags );
153 }
155 void LayerManager::_objectModified( SPObject* obj, guint flags )
156 {
157 _details_changed_signal.emit( obj );
158 }
160 void LayerManager::_rebuild() {
161 while ( !_watchers.empty() ) {
162 LayerWatcher* one = _watchers.back();
163 _watchers.pop_back();
164 if ( one->_obj ) {
165 Node* node = SP_OBJECT_REPR(one->_obj);
166 if ( node ) {
167 node->removeObserver(*one);
168 }
169 }
170 }
172 _clear();
174 GSList const *layers=sp_document_get_resource_list(_document, "layer");
175 SPObject *root=_desktop->currentRoot();
176 if ( root ) {
177 _addOne(root);
179 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
180 SPObject *layer=static_cast<SPObject *>(iter->data);
182 for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
183 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
184 // Filter out objects in the middle of being deleted
186 // Such may have been the cause of bug 1339397.
187 // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
188 SPObject const *higher = curr;
189 while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
190 higher = SP_OBJECT_PARENT(higher);
191 }
192 Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
193 if ( node && node->parent() ) {
194 g_signal_connect( G_OBJECT(curr), "modified", G_CALLBACK( _objectModifiedCB ), this );
196 LayerWatcher* eye = new LayerWatcher(this, curr);
197 _watchers.push_back( eye );
198 SP_OBJECT_REPR(curr)->addObserver(*eye);
200 _addOne(curr);
201 }
202 }
203 }
204 }
205 }
206 }
208 // Connected to the desktop's CurrentLayerChanged signal
209 void LayerManager::_selectedLayerChanged(SPObject *layer)
210 {
211 // notify anyone who's listening to this instead of directly to the desktop
212 _layer_changed_signal.emit(layer);
213 }
215 }
217 /*
218 Local Variables:
219 mode:c++
220 c-file-style:"stroustrup"
221 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
222 indent-tabs-mode:nil
223 fill-column:99
224 End:
225 */
226 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :