From 77edd7dfbe0126db8cbebe3535ace3b055f61c64 Mon Sep 17 00:00:00 2001 From: speare Date: Sun, 8 Mar 2009 20:37:15 +0000 Subject: [PATCH] part of #339660; layers dialog allows non-unique inkscape:label attributes --- src/layer-manager.cpp | 64 ++++++++++++++++-------------- src/layer-manager.h | 2 +- src/ui/dialog/layer-properties.cpp | 5 ++- src/ui/dialog/layers.cpp | 2 +- src/verbs.cpp | 2 +- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/layer-manager.cpp b/src/layer-manager.cpp index 3e49edac5..8c45c7e53 100644 --- a/src/layer-manager.cpp +++ b/src/layer-manager.cpp @@ -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(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(val); + } + g_free(numpart); } - 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() ); + 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; + // 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() ); diff --git a/src/layer-manager.h b/src/layer-manager.h index 835f1b340..81f75e002 100644 --- a/src/layer-manager.h +++ b/src/layer-manager.h @@ -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 & slot) { return _layer_changed_signal.connect(slot); diff --git a/src/ui/dialog/layer-properties.cpp b/src/ui/dialog/layer-properties.cpp index ccd91fa2e..6cb0906a2 100644 --- a/src/ui/dialog/layer-properties.cpp +++ b/src/ui/dialog/layer-properties.cpp @@ -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); diff --git a/src/ui/dialog/layers.cpp b/src/ui/dialog/layers.cpp index 680a53e97..d79f18801 100644 --- a/src/ui/dialog/layers.cpp +++ b/src/ui/dialog/layers.cpp @@ -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(); } } diff --git a/src/verbs.cpp b/src/verbs.cpp index 834e1511d..5e911552c 100644 --- a/src/verbs.cpp +++ b/src/verbs.cpp @@ -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); } -- 2.30.2