Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / layer-manager.cpp
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 "preferences.h"
20 #include "ui/view/view.h"
21 #include "selection.h"
22 #include "sp-object.h"
23 #include "sp-item-group.h"
24 #include "xml/node.h"
25 #include "xml/node-observer.h"
26 #include "util/format.h"
27 // #include "debug/event-tracker.h"
28 // #include "debug/simple-event.h"
30 namespace Inkscape {
33 using Inkscape::XML::Node;
35 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
36 public:
37     LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
38         _mgr(mgr),
39         _obj(obj),
40         _connection(c),
41         _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
42         _labelAttr(g_quark_from_string("inkscape:label"))
43     {}
45     virtual void notifyChildAdded( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
46     virtual void notifyChildRemoved( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
47     virtual void notifyChildOrderChanged( Node &/*node*/, Node &/*child*/, Node */*old_prev*/, Node */*new_prev*/ ) {}
48     virtual void notifyContentChanged( Node &/*node*/, Util::ptr_shared<char> /*old_content*/, Util::ptr_shared<char> /*new_content*/ ) {}
49     virtual void notifyAttributeChanged( Node &/*node*/, GQuark name, Util::ptr_shared<char> /*old_value*/, Util::ptr_shared<char> /*new_value*/ ) {
50         if ( name == _lockedAttr || name == _labelAttr ) {
51             if ( _mgr && _obj ) {
52                 _mgr->_objectModified( _obj, 0 );
53             }
54         }
55     }
57     LayerManager* _mgr;
58     SPObject* _obj;
59     sigc::connection _connection;
60     GQuark _lockedAttr;
61     GQuark _labelAttr;
62 };
64 /*
65 namespace {
67 Util::ptr_shared<char> stringify_node(Node const &node);
69 Util::ptr_shared<char> stringify_obj(SPObject const &obj) {
70     gchar *string;
72     if (obj.id) {
73         string = g_strdup_printf("SPObject(%p)=%s  repr(%p)", &obj, obj.id, obj.repr);
74     } else {
75         string = g_strdup_printf("SPObject(%p) repr(%p)", &obj, obj.repr);
76     }
78     Util::ptr_shared<char> result=Util::share_string(string);
79     g_free(string);
80     return result;
82 }
84 typedef Debug::SimpleEvent<Debug::Event::OTHER> DebugLayer;
86 class DebugLayerNote : public DebugLayer {
87 public:
88     DebugLayerNote(Util::ptr_shared<char> descr)
89         : DebugLayer(Util::share_static_string("layer-note"))
90     {
91         _addProperty("descr", descr);
92     }
93 };
95 class DebugLayerRebuild : public DebugLayer {
96 public:
97     DebugLayerRebuild()
98         : DebugLayer(Util::share_static_string("rebuild-layers"))
99     {
100     }
101 };
103 class DebugLayerObj : public DebugLayer {
104 public:
105     DebugLayerObj(SPObject const& obj, Util::ptr_shared<char> name)
106         : DebugLayer(name)
107     {
108         _addProperty("layer", stringify_obj(obj));
109     }
110 };
112 class DebugAddLayer : public DebugLayerObj {
113 public:
114     DebugAddLayer(SPObject const &obj)
115         : DebugLayerObj(obj, Util::share_static_string("add-layer"))
116     {
117     }
118 };
121 } // end of namespace
122 */
124 LayerManager::LayerManager(SPDesktop *desktop)
125 : _desktop(desktop), _document(NULL)
127     _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
129     sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
131     // This next line has problems on gcc 4.0.2
132     sigc::slot<void, SPDocument*> base2 = first;
134     sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
135     _document_connection = desktop->connectDocumentReplaced( slot2 );
137     _setDocument(desktop->doc());
140 LayerManager::~LayerManager()
142     _layer_connection.disconnect();
143     _document_connection.disconnect();
144     _resource_connection.disconnect();
147 void LayerManager::setCurrentLayer( SPObject* obj )
149     //g_return_if_fail( _desktop->currentRoot() );
150     if ( _desktop->currentRoot() ) {
151         _desktop->setCurrentLayer( obj );
153         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
154         if (prefs->getBool("/options/selection/layerdeselect", true)) {
155             sp_desktop_selection( _desktop )->clear();
156         }
157     }
160 void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify )
162     Glib::ustring incoming( label ? label : "" );
163     Glib::ustring result(incoming);
164     Glib::ustring base(incoming);
165     guint startNum = 1;
167     if (uniquify) {
169         Glib::ustring::size_type pos = base.rfind('#');
170         if ( pos != Glib::ustring::npos ) {
171             gchar* numpart = g_strdup(base.substr(pos+1).c_str());
172             if ( numpart ) {
173                 gchar* endPtr = 0;
174                 guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
175                 if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
176                     base.erase( pos );
177                     result = base;
178                     startNum = static_cast<int>(val);
179                 }
180                 g_free(numpart);
181             }
182         }
184         std::set<Glib::ustring> currentNames;
185         GSList const *layers=sp_document_get_resource_list(_document, "layer");
186         SPObject *root=_desktop->currentRoot();
187         if ( root ) {
188             for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
189                 SPObject *layer=static_cast<SPObject *>(iter->data);
190                 if ( layer != obj ) {
191                     currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
192                 }
193             }
194         }
196         // Not sure if we need to cap it, but we'll just be paranoid for the moment
197         // Intentionally unsigned
198         guint endNum = startNum + 3000;
199         for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
200             gchar* suffix = g_strdup_printf("#%d", i);
201             result = base;
202             result += suffix;
204             g_free(suffix);
205         }
207     }
209     obj->setLabel( result.c_str() );
214 void LayerManager::_setDocument(SPDocument *document) {
215     if (_document) {
216         _resource_connection.disconnect();
217     }
218     _document = document;
219     if (document) {
220         _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
221     }
222     _rebuild();
225 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
227     _details_changed_signal.emit( obj );
230 void LayerManager::_rebuild() {
231 //     Debug::EventTracker<DebugLayerRebuild> tracker1();
233     while ( !_watchers.empty() ) {
234         LayerWatcher* one = _watchers.back();
235         _watchers.pop_back();
236         if ( one->_obj ) {
237             Node* node = SP_OBJECT_REPR(one->_obj);
238             if ( node ) {
239                 node->removeObserver(*one);
240             }
241             one->_connection.disconnect();
242         }
243     }
245     _clear();
247     if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
248         return;
250     GSList const *layers = sp_document_get_resource_list(_document, "layer");
251     SPObject *root=_desktop->currentRoot();
252     if ( root ) {
253         _addOne(root);
255         std::set<SPGroup*> layersToAdd;
257         for ( GSList const *iter = layers; iter; iter = iter->next ) {
258             SPObject *layer = static_cast<SPObject *>(iter->data);
259 //             Debug::EventTracker<DebugLayerNote> tracker(Util::format("Examining %s", layer->label()));
260             bool needsAdd = false;
261             std::set<SPGroup*> additional;
263             if ( root->isAncestorOf(layer) ) {
264                 needsAdd = true;
265                 for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
266                     if ( SP_IS_GROUP(curr) ) {
267                         SPGroup* group = SP_GROUP(curr);
268                         if ( group->layerMode() == SPGroup::LAYER ) {
269                             // If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
270                             needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
271                             if ( (!(group->repr)) || (!(group->repr->parent())) ) {
272                                 needsAdd = false;
273                             }
274                         } else {
275                             // If a non-layer group is a parent of layer groups, then show it also as a layer.
276                             // TODO add the magic Inkscape group mode?
277                             if ( group->repr && group->repr->parent() ) {
278                                 additional.insert(group);
279                             } else {
280                                 needsAdd = false;
281                             }
282                         }
283                     }
284                 }
285             }
286             if ( needsAdd ) {
287                 if ( !includes(layer) ) {
288                     layersToAdd.insert(SP_GROUP(layer));
289                 }
290                 for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
291                     if ( !includes(*it) ) {
292                         layersToAdd.insert(*it);
293                     }
294                 }
295             }
296         }
298         for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
299             SPGroup* layer = *it;
300             // Filter out objects in the middle of being deleted
302             // Such may have been the cause of bug 1339397.
303             // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
305             SPObject const *higher = layer;
306             while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
307                 higher = SP_OBJECT_PARENT(higher);
308             }
309             Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
310             if ( node && node->parent() ) {
311 //                 Debug::EventTracker<DebugAddLayer> tracker(*layer);
313                 sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
315                 LayerWatcher *eye = new LayerWatcher(this, layer, connection);
316                 _watchers.push_back( eye );
317                 SP_OBJECT_REPR(layer)->addObserver(*eye);
318                 
319                 _addOne(layer);
320             }
321         }
322     }
325 // Connected to the desktop's CurrentLayerChanged signal
326 void LayerManager::_selectedLayerChanged(SPObject *layer)
328     // notify anyone who's listening to this instead of directly to the desktop
329     _layer_changed_signal.emit(layer);
334 /*
335   Local Variables:
336   mode:c++
337   c-file-style:"stroustrup"
338   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
339   indent-tabs-mode:nil
340   fill-column:99
341   End:
342 */
343 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :