Code

part of #339660; layers dialog allows non-unique inkscape:label attributes
authorspeare <speare@users.sourceforge.net>
Sun, 8 Mar 2009 20:37:15 +0000 (20:37 +0000)
committerspeare <speare@users.sourceforge.net>
Sun, 8 Mar 2009 20:37:15 +0000 (20:37 +0000)
src/layer-manager.cpp
src/layer-manager.h
src/ui/dialog/layer-properties.cpp
src/ui/dialog/layers.cpp
src/verbs.cpp

index 3e49edac5723299ab5699850b14ab31fe6c478c1..8c45c7e53642fdebabb75b930ce24efb09909ab2 100644 (file)
@@ -157,49 +157,53 @@ void LayerManager::setCurrentLayer( SPObject* obj )
     }
 }
 
-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;
 
-    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);
+    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=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;
+        // 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() );
index 835f1b3405fd0c4bbacbda279e31e821f823d390..81f75e002aef5144ef16cbc8937a5a3d27b7ccb2 100644 (file)
@@ -29,7 +29,7 @@ public:
     virtual ~LayerManager();
 
     void setCurrentLayer( SPObject* obj );
-    void renameLayer( SPObject* obj, gchar const *label );
+    void renameLayer( SPObject* obj, gchar const *label, bool uniquify );
 
     sigc::connection connectCurrentLayerChanged(const sigc::slot<void, SPObject *> & slot) {
        return _layer_changed_signal.connect(slot);
index ccd91fa2e74ee191613d0e8f9f8183e06a8a356e..6cb0906a221a61105fa4e281abdb8ee82d19bbe2 100644 (file)
@@ -183,7 +183,8 @@ void LayerPropertiesDialog::Rename::perform(LayerPropertiesDialog &dialog) {
     SPDesktop *desktop=dialog._desktop;
     Glib::ustring name(dialog._layer_name_entry.get_text());
     desktop->layer_manager->renameLayer( desktop->currentLayer(),
-                                         ( name.empty() ? NULL : (gchar *)name.c_str() )
+                                         ( name.empty() ? NULL : (gchar *)name.c_str() ),
+                                         FALSE
     );
     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
                      _("Rename layer"));
@@ -212,7 +213,7 @@ void LayerPropertiesDialog::Create::perform(LayerPropertiesDialog &dialog) {
     
     Glib::ustring name(dialog._layer_name_entry.get_text());
     if (!name.empty()) {
-        desktop->layer_manager->renameLayer( new_layer, (gchar *)name.c_str() );
+        desktop->layer_manager->renameLayer( new_layer, (gchar *)name.c_str(), TRUE );
     }
     sp_desktop_selection(desktop)->clear();
     desktop->setCurrentLayer(new_layer);
index 680a53e97f1ff831e22dfb5b7f42de10827b7caf..d79f188017296c4fba10ac2b15c544f8a279f5d8 100644 (file)
@@ -539,7 +539,7 @@ void LayersPanel::_handleRowChange( Gtk::TreeModel::Path const& /*path*/, Gtk::T
             gchar const* oldLabel = obj->label();
             Glib::ustring tmp = row[_model->_colLabel];
             if ( oldLabel && oldLabel[0] && !tmp.empty() && (tmp != oldLabel) ) {
-                _mgr->renameLayer( obj, tmp.c_str() );
+                _mgr->renameLayer( obj, tmp.c_str(), FALSE );
                 row[_model->_colLabel] = obj->label();
             }
         }
index 834e1511d4bae4382fc481bba7027ca6a362f227..5e911552cde796e3afa7fc839690bef289446762 100644 (file)
@@ -1230,7 +1230,7 @@ LayerVerb::perform(SPAction *action, void *data, void */*pdata*/)
                 SPObject *new_layer = Inkscape::create_layer(dt->currentRoot(), dt->currentLayer(), LPOS_BELOW);
                 if ( dt->currentLayer()->label() ) {
                     gchar* name = g_strdup_printf(_("%s copy"), dt->currentLayer()->label());
-                    dt->layer_manager->renameLayer( new_layer, name );
+                    dt->layer_manager->renameLayer( new_layer, name, TRUE );
                     g_free(name);
                 }