From: joncruz Date: Sat, 27 May 2006 04:26:51 +0000 (+0000) Subject: Prevent layer name duplication X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a556f5c62c0487ed9fb81094b1c6bba62b8f2803;p=inkscape.git Prevent layer name duplication --- diff --git a/ChangeLog b/ChangeLog index ac0fdfb0f..3edb6e86b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-05-26 Jon A. Cruz + + * 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 * src/widgets/desktop-widget.cpp: diff --git a/src/dialogs/layer-properties.cpp b/src/dialogs/layer-properties.cpp index 6a88597be..e407c7082 100644 --- a/src/dialogs/layer-properties.cpp +++ b/src/dialogs/layer-properties.cpp @@ -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); diff --git a/src/dialogs/layers-panel.cpp b/src/dialogs/layers-panel.cpp index a7ad817cf..31e9823c6 100644 --- a/src/dialogs/layers-panel.cpp +++ b/src/dialogs/layers-panel.cpp @@ -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(); } } } diff --git a/src/layer-manager.cpp b/src/layer-manager.cpp index 840d12380..81e8698b0 100644 --- a/src/layer-manager.cpp +++ b/src/layer-manager.cpp @@ -7,6 +7,7 @@ * Released under GNU GPL, read the file 'COPYING' for more information */ +#include #include #include #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(val); + } + g_free(numpart); + } + } + + std::set 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(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(); diff --git a/src/layer-manager.h b/src/layer-manager.h index d92946a4c..461134baa 100644 --- a/src/layer-manager.h +++ b/src/layer-manager.h @@ -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 & slot) { return _layer_changed_signal.connect(slot); }