Code

Prevent layer name duplication
authorjoncruz <joncruz@users.sourceforge.net>
Sat, 27 May 2006 04:26:51 +0000 (04:26 +0000)
committerjoncruz <joncruz@users.sourceforge.net>
Sat, 27 May 2006 04:26:51 +0000 (04:26 +0000)
ChangeLog
src/dialogs/layer-properties.cpp
src/dialogs/layers-panel.cpp
src/layer-manager.cpp
src/layer-manager.h

index ac0fdfb0fe1be830dac251ac58a1206734cb2133..3edb6e86b414ba4e0f6b38527c88645297d7f885 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2006-05-26  Jon A. Cruz  <jon@joncruz.org>
+
+       * src/layer-manager.cpp, src/layer-manager.h,
+         src/dialogs/layer-properties.cpp, src/dialogs/layers-panel.cpp:
+
+         Prevent layer name duplication.
+         Fixes bug #1115407.
+
 2006-05-26  Jon A. Cruz  <jon@joncruz.org>
 
        * src/widgets/desktop-widget.cpp:
index 6a88597bed50b8e06e7aa54ccf3c30b85fa53e60..e407c7082299e03a7a814a205b8355c8f4cfbcde 100644 (file)
@@ -22,6 +22,7 @@
 #include "inkscape.h"
 #include "desktop.h"
 #include "document.h"
+#include "layer-manager.h"
 #include "message-stack.h"
 #include "desktop-handles.h"
 #include "sp-object.h"
@@ -186,8 +187,8 @@ void LayerPropertiesDialog::Rename::setup(LayerPropertiesDialog &dialog) {
 void LayerPropertiesDialog::Rename::perform(LayerPropertiesDialog &dialog) {
     SPDesktop *desktop=dialog._desktop;
     Glib::ustring name(dialog._layer_name_entry.get_text());
-    desktop->currentLayer()->setLabel(
-        ( name.empty() ? NULL : (gchar *)name.c_str() )
+    desktop->layer_manager->renameLayer( desktop->currentLayer(),
+                                         ( name.empty() ? NULL : (gchar *)name.c_str() )
     );
     sp_document_done(sp_desktop_document(desktop));
     // TRANSLATORS: This means "The layer has been renamed"
@@ -215,7 +216,7 @@ void LayerPropertiesDialog::Create::perform(LayerPropertiesDialog &dialog) {
     
     Glib::ustring name(dialog._layer_name_entry.get_text());
     if (!name.empty()) {
-        new_layer->setLabel((gchar *)name.c_str());
+        desktop->layer_manager->renameLayer( new_layer, (gchar *)name.c_str() );
     }
     sp_desktop_selection(desktop)->clear();
     desktop->setCurrentLayer(new_layer);
index a7ad817cf987f2eedb21733567e69ee76271ec9d..31e9823c6a4797952061a8ae9ad9ecfe94cccd7e 100644 (file)
@@ -576,8 +576,8 @@ void LayersPanel::_handleRowChange( Gtk::TreeModel::Path const& path, Gtk::TreeM
             gchar const* oldLabel = obj->label();
             Glib::ustring tmp = row[_model->_colLabel];
             if ( oldLabel && oldLabel[0] && !tmp.empty() && (tmp != oldLabel) ) {
-                // TODO fix name collision bug
-                obj->setLabel(tmp.c_str());
+                _mgr->renameLayer( obj, tmp.c_str() );
+                row[_model->_colLabel] = obj->label();
             }
         }
     }
index 840d123800cf98c3453582121a290863a229e321..81e8698b05f083a537a4e95f7c7fd4d9e236247c 100644 (file)
@@ -7,6 +7,7 @@
  * 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"
@@ -68,6 +69,57 @@ LayerManager::LayerManager(SPDesktop *desktop)
     _setDocument(desktop->doc());
 }
 
+
+void LayerManager::renameLayer( SPObject* obj, gchar const *label )
+{
+    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);
+            }
+            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();
index d92946a4c3af0660a1db5b002d71571255de83cc..461134baab4d735e688afb3ef6d2736d880ce16c 100644 (file)
@@ -26,6 +26,8 @@ class LayerManager : public DocumentSubset,
 public:
     LayerManager(SPDesktop *desktop);
 
+    void renameLayer( SPObject* obj, gchar const *label );
+
     sigc::connection connectCurrentLayerChanged(const sigc::slot<void, SPObject *> & slot) {
        return _layer_changed_signal.connect(slot);
     }