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 * Abhishek Sharma
7 *
8 * Released under GNU GPL, read the file 'COPYING' for more information
9 */
11 #include <set>
12 #include <sigc++/functors/mem_fun.h>
13 #include <sigc++/adaptors/hide.h>
14 #include "gc-managed.h"
15 #include "gc-finalized.h"
16 #include "document.h"
17 #include "desktop.h"
18 #include "desktop-handles.h"
19 #include "layer-manager.h"
20 #include "preferences.h"
21 #include "ui/view/view.h"
22 #include "selection.h"
23 #include "sp-object.h"
24 #include "sp-item-group.h"
25 #include "xml/node.h"
26 #include "xml/node-observer.h"
27 #include "util/format.h"
28 // #include "debug/event-tracker.h"
29 // #include "debug/simple-event.h"
31 namespace Inkscape {
34 using Inkscape::XML::Node;
36 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
37 public:
38 LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
39 _mgr(mgr),
40 _obj(obj),
41 _connection(c),
42 _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
43 _labelAttr(g_quark_from_string("inkscape:label"))
44 {}
46 virtual void notifyChildAdded( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
47 virtual void notifyChildRemoved( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
48 virtual void notifyChildOrderChanged( Node &/*node*/, Node &/*child*/, Node */*old_prev*/, Node */*new_prev*/ ) {}
49 virtual void notifyContentChanged( Node &/*node*/, Util::ptr_shared<char> /*old_content*/, Util::ptr_shared<char> /*new_content*/ ) {}
50 virtual void notifyAttributeChanged( Node &/*node*/, GQuark name, Util::ptr_shared<char> /*old_value*/, Util::ptr_shared<char> /*new_value*/ ) {
51 if ( name == _lockedAttr || name == _labelAttr ) {
52 if ( _mgr && _obj ) {
53 _mgr->_objectModified( _obj, 0 );
54 }
55 }
56 }
58 LayerManager* _mgr;
59 SPObject* _obj;
60 sigc::connection _connection;
61 GQuark _lockedAttr;
62 GQuark _labelAttr;
63 };
65 /*
66 namespace {
68 Util::ptr_shared<char> stringify_node(Node const &node);
70 Util::ptr_shared<char> stringify_obj(SPObject const &obj) {
71 gchar *string;
73 if (obj.id) {
74 string = g_strdup_printf("SPObject(%p)=%s repr(%p)", &obj, obj.id, obj.repr);
75 } else {
76 string = g_strdup_printf("SPObject(%p) repr(%p)", &obj, obj.repr);
77 }
79 Util::ptr_shared<char> result=Util::share_string(string);
80 g_free(string);
81 return result;
83 }
85 typedef Debug::SimpleEvent<Debug::Event::OTHER> DebugLayer;
87 class DebugLayerNote : public DebugLayer {
88 public:
89 DebugLayerNote(Util::ptr_shared<char> descr)
90 : DebugLayer(Util::share_static_string("layer-note"))
91 {
92 _addProperty("descr", descr);
93 }
94 };
96 class DebugLayerRebuild : public DebugLayer {
97 public:
98 DebugLayerRebuild()
99 : DebugLayer(Util::share_static_string("rebuild-layers"))
100 {
101 }
102 };
104 class DebugLayerObj : public DebugLayer {
105 public:
106 DebugLayerObj(SPObject const& obj, Util::ptr_shared<char> name)
107 : DebugLayer(name)
108 {
109 _addProperty("layer", stringify_obj(obj));
110 }
111 };
113 class DebugAddLayer : public DebugLayerObj {
114 public:
115 DebugAddLayer(SPObject const &obj)
116 : DebugLayerObj(obj, Util::share_static_string("add-layer"))
117 {
118 }
119 };
122 } // end of namespace
123 */
125 LayerManager::LayerManager(SPDesktop *desktop)
126 : _desktop(desktop), _document(NULL)
127 {
128 _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
130 sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
132 // This next line has problems on gcc 4.0.2
133 sigc::slot<void, SPDocument*> base2 = first;
135 sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
136 _document_connection = desktop->connectDocumentReplaced( slot2 );
138 _setDocument(desktop->doc());
139 }
141 LayerManager::~LayerManager()
142 {
143 _layer_connection.disconnect();
144 _document_connection.disconnect();
145 _resource_connection.disconnect();
146 _document = 0;
147 }
149 void LayerManager::setCurrentLayer( SPObject* obj )
150 {
151 //g_return_if_fail( _desktop->currentRoot() );
152 if ( _desktop->currentRoot() ) {
153 _desktop->setCurrentLayer( obj );
155 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
156 if (prefs->getBool("/options/selection/layerdeselect", true)) {
157 sp_desktop_selection( _desktop )->clear();
158 }
159 }
160 }
162 void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify )
163 {
164 Glib::ustring incoming( label ? label : "" );
165 Glib::ustring result(incoming);
166 Glib::ustring base(incoming);
167 guint startNum = 1;
169 if (uniquify) {
171 Glib::ustring::size_type pos = base.rfind('#');
172 if ( pos != Glib::ustring::npos ) {
173 gchar* numpart = g_strdup(base.substr(pos+1).c_str());
174 if ( numpart ) {
175 gchar* endPtr = 0;
176 guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
177 if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
178 base.erase( pos );
179 result = base;
180 startNum = static_cast<int>(val);
181 }
182 g_free(numpart);
183 }
184 }
186 std::set<Glib::ustring> currentNames;
187 GSList const *layers=_document->getResourceList("layer");
188 SPObject *root=_desktop->currentRoot();
189 if ( root ) {
190 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
191 SPObject *layer=static_cast<SPObject *>(iter->data);
192 if ( layer != obj ) {
193 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
194 }
195 }
196 }
198 // Not sure if we need to cap it, but we'll just be paranoid for the moment
199 // Intentionally unsigned
200 guint endNum = startNum + 3000;
201 for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
202 gchar* suffix = g_strdup_printf("#%d", i);
203 result = base;
204 result += suffix;
206 g_free(suffix);
207 }
209 }
211 obj->setLabel( result.c_str() );
212 }
216 void LayerManager::_setDocument(SPDocument *document) {
217 if (_document) {
218 _resource_connection.disconnect();
219 }
220 _document = document;
221 if (document) {
222 _resource_connection = document->connectResourcesChanged("layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
223 }
224 _rebuild();
225 }
227 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
228 {
229 _details_changed_signal.emit( obj );
230 }
232 void LayerManager::_rebuild() {
233 // Debug::EventTracker<DebugLayerRebuild> tracker1();
235 while ( !_watchers.empty() ) {
236 LayerWatcher* one = _watchers.back();
237 _watchers.pop_back();
238 if ( one->_obj ) {
239 Node* node = SP_OBJECT_REPR(one->_obj);
240 if ( node ) {
241 node->removeObserver(*one);
242 }
243 one->_connection.disconnect();
244 }
245 }
247 _clear();
249 if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
250 return;
252 GSList const *layers = _document->getResourceList("layer");
253 SPObject *root=_desktop->currentRoot();
254 if ( root ) {
255 _addOne(root);
257 std::set<SPGroup*> layersToAdd;
259 for ( GSList const *iter = layers; iter; iter = iter->next ) {
260 SPObject *layer = static_cast<SPObject *>(iter->data);
261 // Debug::EventTracker<DebugLayerNote> tracker(Util::format("Examining %s", layer->label()));
262 bool needsAdd = false;
263 std::set<SPGroup*> additional;
265 if ( root->isAncestorOf(layer) ) {
266 needsAdd = true;
267 for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
268 if ( SP_IS_GROUP(curr) ) {
269 SPGroup* group = SP_GROUP(curr);
270 if ( group->layerMode() == SPGroup::LAYER ) {
271 // If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
272 needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
273 // XML Tree being used here directly while it shouldn't be...
274 if ( (!(group->getRepr())) || (!(group->getRepr()->parent())) ) {
275 needsAdd = false;
276 }
277 } else {
278 // If a non-layer group is a parent of layer groups, then show it also as a layer.
279 // TODO add the magic Inkscape group mode?
280 // XML Tree being used directly while it shouldn't be...
281 if ( group->getRepr() && group->getRepr()->parent() ) {
282 additional.insert(group);
283 } else {
284 needsAdd = false;
285 }
286 }
287 }
288 }
289 }
290 if ( needsAdd ) {
291 if ( !includes(layer) ) {
292 layersToAdd.insert(SP_GROUP(layer));
293 }
294 for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
295 if ( !includes(*it) ) {
296 layersToAdd.insert(*it);
297 }
298 }
299 }
300 }
302 for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
303 SPGroup* layer = *it;
304 // Filter out objects in the middle of being deleted
306 // Such may have been the cause of bug 1339397.
307 // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
309 SPObject const *higher = layer;
310 while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
311 higher = SP_OBJECT_PARENT(higher);
312 }
313 Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
314 if ( node && node->parent() ) {
315 // Debug::EventTracker<DebugAddLayer> tracker(*layer);
317 sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
319 LayerWatcher *eye = new LayerWatcher(this, layer, connection);
320 _watchers.push_back( eye );
321 SP_OBJECT_REPR(layer)->addObserver(*eye);
323 _addOne(layer);
324 }
325 }
326 }
327 }
329 // Connected to the desktop's CurrentLayerChanged signal
330 void LayerManager::_selectedLayerChanged(SPObject *layer)
331 {
332 // notify anyone who's listening to this instead of directly to the desktop
333 _layer_changed_signal.emit(layer);
334 }
336 }
338 /*
339 Local Variables:
340 mode:c++
341 c-file-style:"stroustrup"
342 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
343 indent-tabs-mode:nil
344 fill-column:99
345 End:
346 */
347 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :