Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / layer-manager.cpp
index 4d92d37c846edf4030e495f9f0bdc24884b1222c..95cefc229f44351203035c37a2136e61a7bf4d39 100644 (file)
@@ -3,6 +3,7 @@
  *                          to a particular desktop
  *
  * Copyright 2006  MenTaLguY  <mental@rydia.net>
+ *   Abhishek Sharma
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 #include "desktop.h"
 #include "desktop-handles.h"
 #include "layer-manager.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "ui/view/view.h"
 #include "selection.h"
 #include "sp-object.h"
+#include "sp-item-group.h"
 #include "xml/node.h"
 #include "xml/node-observer.h"
+#include "util/format.h"
+// #include "debug/event-tracker.h"
+// #include "debug/simple-event.h"
 
 namespace Inkscape {
 
@@ -38,11 +43,11 @@ public:
         _labelAttr(g_quark_from_string("inkscape:label"))
     {}
 
-    virtual void notifyChildAdded( Node &node, Node &child, Node *prev ) {}
-    virtual void notifyChildRemoved( Node &node, Node &child, Node *prev ) {}
-    virtual void notifyChildOrderChanged( Node &node, Node &child, Node *old_prev, Node *new_prev ) {}
-    virtual void notifyContentChanged( Node &node, Util::ptr_shared<char> old_content, Util::ptr_shared<char> new_content ) {}
-    virtual void notifyAttributeChanged( Node &node, GQuark name, Util::ptr_shared<char> old_value, Util::ptr_shared<char> new_value ) {
+    virtual void notifyChildAdded( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
+    virtual void notifyChildRemoved( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
+    virtual void notifyChildOrderChanged( Node &/*node*/, Node &/*child*/, Node */*old_prev*/, Node */*new_prev*/ ) {}
+    virtual void notifyContentChanged( Node &/*node*/, Util::ptr_shared<char> /*old_content*/, Util::ptr_shared<char> /*new_content*/ ) {}
+    virtual void notifyAttributeChanged( Node &/*node*/, GQuark name, Util::ptr_shared<char> /*old_value*/, Util::ptr_shared<char> /*new_value*/ ) {
         if ( name == _lockedAttr || name == _labelAttr ) {
             if ( _mgr && _obj ) {
                 _mgr->_objectModified( _obj, 0 );
@@ -57,6 +62,65 @@ public:
     GQuark _labelAttr;
 };
 
+/*
+namespace {
+
+Util::ptr_shared<char> stringify_node(Node const &node);
+
+Util::ptr_shared<char> stringify_obj(SPObject const &obj) {
+    gchar *string;
+
+    if (obj.id) {
+        string = g_strdup_printf("SPObject(%p)=%s  repr(%p)", &obj, obj.id, obj.repr);
+    } else {
+        string = g_strdup_printf("SPObject(%p) repr(%p)", &obj, obj.repr);
+    }
+
+    Util::ptr_shared<char> result=Util::share_string(string);
+    g_free(string);
+    return result;
+
+}
+
+typedef Debug::SimpleEvent<Debug::Event::OTHER> DebugLayer;
+
+class DebugLayerNote : public DebugLayer {
+public:
+    DebugLayerNote(Util::ptr_shared<char> descr)
+        : DebugLayer(Util::share_static_string("layer-note"))
+    {
+        _addProperty("descr", descr);
+    }
+};
+
+class DebugLayerRebuild : public DebugLayer {
+public:
+    DebugLayerRebuild()
+        : DebugLayer(Util::share_static_string("rebuild-layers"))
+    {
+    }
+};
+
+class DebugLayerObj : public DebugLayer {
+public:
+    DebugLayerObj(SPObject const& obj, Util::ptr_shared<char> name)
+        : DebugLayer(name)
+    {
+        _addProperty("layer", stringify_obj(obj));
+    }
+};
+
+class DebugAddLayer : public DebugLayerObj {
+public:
+    DebugAddLayer(SPObject const &obj)
+        : DebugLayerObj(obj, Util::share_static_string("add-layer"))
+    {
+    }
+};
+
+
+} // end of namespace
+*/
 
 LayerManager::LayerManager(SPDesktop *desktop)
 : _desktop(desktop), _document(NULL)
@@ -74,6 +138,13 @@ LayerManager::LayerManager(SPDesktop *desktop)
     _setDocument(desktop->doc());
 }
 
+LayerManager::~LayerManager()
+{
+    _layer_connection.disconnect();
+    _document_connection.disconnect();
+    _resource_connection.disconnect();
+    _document = 0;
+}
 
 void LayerManager::setCurrentLayer( SPObject* obj )
 {
@@ -81,55 +152,60 @@ void LayerManager::setCurrentLayer( SPObject* obj )
     if ( _desktop->currentRoot() ) {
         _desktop->setCurrentLayer( obj );
 
-        if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+        if (prefs->getBool("/options/selection/layerdeselect", true)) {
             sp_desktop_selection( _desktop )->clear();
         }
     }
 }
 
-void LayerManager::renameLayer( SPObject* obj, gchar const *label )
+void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify )
 {
     Glib::ustring incoming( label ? label : "" );
     Glib::ustring result(incoming);
     Glib::ustring base(incoming);
     guint startNum = 1;
 
-    size_t pos = base.rfind('#');
-    if ( pos != Glib::ustring::npos ) {
-        gchar* numpart = g_strdup(base.substr(pos+1).c_str());
-        if ( numpart ) {
-            gchar* endPtr = 0;
-            guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
-            if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
-                base.erase( pos );
-                result = base;
-                startNum = static_cast<int>(val);
+    if (uniquify) {
+
+        Glib::ustring::size_type pos = base.rfind('#');
+        if ( pos != Glib::ustring::npos ) {
+            gchar* numpart = g_strdup(base.substr(pos+1).c_str());
+            if ( numpart ) {
+                gchar* endPtr = 0;
+                guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
+                if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
+                    base.erase( pos );
+                    result = base;
+                    startNum = static_cast<int>(val);
+                }
+                g_free(numpart);
             }
-            g_free(numpart);
         }
-    }
 
-    std::set<Glib::ustring> currentNames;
-    GSList const *layers=sp_document_get_resource_list(_document, "layer");
-    SPObject *root=_desktop->currentRoot();
-    if ( root ) {
-        for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
-            SPObject *layer=static_cast<SPObject *>(iter->data);
-            if ( layer != obj ) {
-                currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
+        std::set<Glib::ustring> currentNames;
+        GSList const *layers=_document->getResourceList("layer");
+        SPObject *root=_desktop->currentRoot();
+        if ( root ) {
+            for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
+                SPObject *layer=static_cast<SPObject *>(iter->data);
+                if ( layer != obj ) {
+                    currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
+                }
             }
         }
-    }
 
-    // Not sure if we need to cap it, but we'll just be paranoid for the moment
-    // Intentionally unsigned
-    guint endNum = startNum + 3000;
-    for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
-        gchar* suffix = g_strdup_printf("#%d", i);
-        result = base;
-        result += suffix;
+        // Not sure if we need to cap it, but we'll just be paranoid for the moment
+        // Intentionally unsigned
+        guint endNum = startNum + 3000;
+        for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
+            gchar* suffix = g_strdup_printf("#%d", i);
+            result = base;
+            result += suffix;
+
+            g_free(suffix);
+        }
 
-        g_free(suffix);
     }
 
     obj->setLabel( result.c_str() );
@@ -143,17 +219,19 @@ void LayerManager::_setDocument(SPDocument *document) {
     }
     _document = document;
     if (document) {
-        _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
+        _resource_connection = document->connectResourcesChanged("layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
     }
     _rebuild();
 }
 
-void LayerManager::_objectModified( SPObject* obj, guint flags )
+void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
 {
     _details_changed_signal.emit( obj );
 }
 
 void LayerManager::_rebuild() {
+//     Debug::EventTracker<DebugLayerRebuild> tracker1();
+
     while ( !_watchers.empty() ) {
         LayerWatcher* one = _watchers.back();
         _watchers.pop_back();
@@ -168,36 +246,81 @@ void LayerManager::_rebuild() {
 
     _clear();
 
-    GSList const *layers=sp_document_get_resource_list(_document, "layer");
+    if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
+        return;
+
+    GSList const *layers = _document->getResourceList("layer");
     SPObject *root=_desktop->currentRoot();
     if ( root ) {
         _addOne(root);
 
-        for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
-            SPObject *layer=static_cast<SPObject *>(iter->data);
+        std::set<SPGroup*> layersToAdd;
+
+        for ( GSList const *iter = layers; iter; iter = iter->next ) {
+            SPObject *layer = static_cast<SPObject *>(iter->data);
+//             Debug::EventTracker<DebugLayerNote> tracker(Util::format("Examining %s", layer->label()));
+            bool needsAdd = false;
+            std::set<SPGroup*> additional;
+
+            if ( root->isAncestorOf(layer) ) {
+                needsAdd = true;
+                for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
+                    if ( SP_IS_GROUP(curr) ) {
+                        SPGroup* group = SP_GROUP(curr);
+                        if ( group->layerMode() == SPGroup::LAYER ) {
+                            // If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
+                            needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
+                                                       // XML Tree being used here directly while it shouldn't be...
+                            if ( (!(group->getRepr())) || (!(group->getRepr()->parent())) ) {
+                                needsAdd = false;
+                            }
+                        } else {
+                            // If a non-layer group is a parent of layer groups, then show it also as a layer.
+                            // TODO add the magic Inkscape group mode?
+                                                       // XML Tree being used directly while it shouldn't be...
+                            if ( group->getRepr() && group->getRepr()->parent() ) {
+                                additional.insert(group);
+                            } else {
+                                needsAdd = false;
+                            }
+                        }
+                    }
+                }
+            }
+            if ( needsAdd ) {
+                if ( !includes(layer) ) {
+                    layersToAdd.insert(SP_GROUP(layer));
+                }
+                for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
+                    if ( !includes(*it) ) {
+                        layersToAdd.insert(*it);
+                    }
+                }
+            }
+        }
 
-            for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
-                if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
-                    // Filter out objects in the middle of being deleted
+        for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
+            SPGroup* layer = *it;
+            // Filter out objects in the middle of being deleted
 
-                    // Such may have been the cause of bug 1339397.
-                    // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
+            // Such may have been the cause of bug 1339397.
+            // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
 
-                    SPObject const *higher = curr;
-                    while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
-                        higher = SP_OBJECT_PARENT(higher);
-                    }
-                    Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
-                    if ( node && node->parent() ) {
-                        sigc::connection connection = curr->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
+            SPObject const *higher = layer;
+            while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
+                higher = SP_OBJECT_PARENT(higher);
+            }
+            Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
+            if ( node && node->parent() ) {
+//                 Debug::EventTracker<DebugAddLayer> tracker(*layer);
 
-                        LayerWatcher *eye = new LayerWatcher(this, curr, connection);
-                        _watchers.push_back( eye );
-                        SP_OBJECT_REPR(curr)->addObserver(*eye);
+                sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
 
-                        _addOne(curr);
-                    }
-                }
+                LayerWatcher *eye = new LayerWatcher(this, layer, connection);
+                _watchers.push_back( eye );
+                SP_OBJECT_REPR(layer)->addObserver(*eye);
+
+                _addOne(layer);
             }
         }
     }
@@ -221,4 +344,4 @@ void LayerManager::_selectedLayerChanged(SPObject *layer)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :