Code

a bunch of small changes to provide a user readable explanation of filters
[inkscape.git] / src / layer-manager.cpp
index 840d123800cf98c3453582121a290863a229e321..1ecdef30056c283d1a6fb7f9554aeec3950db344 100644 (file)
@@ -7,14 +7,18 @@
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
+#include <set>
 #include <sigc++/functors/mem_fun.h>
 #include <sigc++/adaptors/hide.h>
 #include "gc-managed.h"
 #include "gc-finalized.h"
 #include "document.h"
 #include "desktop.h"
+#include "desktop-handles.h"
 #include "layer-manager.h"
+#include "prefs-utils.h"
 #include "ui/view/view.h"
+#include "selection.h"
 #include "sp-object.h"
 #include "xml/node.h"
 #include "xml/node-observer.h"
@@ -26,18 +30,19 @@ using Inkscape::XML::Node;
 
 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
 public:
-    LayerWatcher(LayerManager* mgr, SPObject* obj) :
+    LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
         _mgr(mgr),
         _obj(obj),
+        _connection(c),
         _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
         _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 );
@@ -47,6 +52,7 @@ public:
 
     LayerManager* _mgr;
     SPObject* _obj;
+    sigc::connection _connection;
     GQuark _lockedAttr;
     GQuark _labelAttr;
 };
@@ -68,6 +74,75 @@ LayerManager::LayerManager(SPDesktop *desktop)
     _setDocument(desktop->doc());
 }
 
+LayerManager::~LayerManager()
+{
+    _layer_connection.disconnect();
+    _document_connection.disconnect();
+    _resource_connection.disconnect();
+}
+
+void LayerManager::setCurrentLayer( SPObject* obj )
+{
+    //g_return_if_fail( _desktop->currentRoot() );
+    if ( _desktop->currentRoot() ) {
+        _desktop->setCurrentLayer( obj );
+
+        if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
+            sp_desktop_selection( _desktop )->clear();
+        }
+    }
+}
+
+void LayerManager::renameLayer( SPObject* obj, gchar const *label )
+{
+    Glib::ustring incoming( label ? label : "" );
+    Glib::ustring result(incoming);
+    Glib::ustring base(incoming);
+    guint startNum = 1;
+
+    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);
+        }
+    }
+
+    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() );
+            }
+        }
+    }
+
+    // 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);
+    }
+
+    obj->setLabel( result.c_str() );
+}
+
+
+
 void LayerManager::_setDocument(SPDocument *document) {
     if (_document) {
         _resource_connection.disconnect();
@@ -79,13 +154,7 @@ void LayerManager::_setDocument(SPDocument *document) {
     _rebuild();
 }
 
-
-void LayerManager::_objectModifiedCB( SPObject* obj, guint flags, LayerManager* mgr )
-{
-    mgr->_objectModified( obj, flags );
-}
-
-void LayerManager::_objectModified( SPObject* obj, guint flags )
+void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
 {
     _details_changed_signal.emit( obj );
 }
@@ -99,11 +168,15 @@ void LayerManager::_rebuild() {
             if ( node ) {
                 node->removeObserver(*one);
             }
+            one->_connection.disconnect();
         }
     }
 
     _clear();
 
+    if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
+        return;
+
     GSList const *layers=sp_document_get_resource_list(_document, "layer");
     SPObject *root=_desktop->currentRoot();
     if ( root ) {
@@ -118,15 +191,16 @@ void LayerManager::_rebuild() {
 
                     // 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() ) {
-                        g_signal_connect( G_OBJECT(curr), "modified", G_CALLBACK( _objectModifiedCB ), this );
+                        sigc::connection connection = curr->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
 
-                        LayerWatcher* eye = new LayerWatcher(this, curr);
+                        LayerWatcher *eye = new LayerWatcher(this, curr, connection);
                         _watchers.push_back( eye );
                         SP_OBJECT_REPR(curr)->addObserver(*eye);