diff options
| author | John Smith <john.smith7545@yahoo.com> | 2012-10-12 05:39:08 +0000 |
|---|---|---|
| committer | John Smith <john.smith7545@yahoo.com> | 2012-10-12 05:39:08 +0000 |
| commit | c384a6116dd087856bd91806e9f8fb5e0eaa6dd8 (patch) | |
| tree | 6f9203b390701aa7084c3995f47de06fdf2d498a /src/layer-manager.cpp | |
| parent | Fix for 177931 : Shift click layer icons to trigger solo (diff) | |
| download | inkscape-c384a6116dd087856bd91806e9f8fb5e0eaa6dd8.tar.gz inkscape-c384a6116dd087856bd91806e9f8fb5e0eaa6dd8.zip | |
Fix for 1019974 : Default layer names are inconsistent
(bzr r11785)
Diffstat (limited to 'src/layer-manager.cpp')
| -rw-r--r-- | src/layer-manager.cpp | 81 |
1 files changed, 47 insertions, 34 deletions
diff --git a/src/layer-manager.cpp b/src/layer-manager.cpp index 894758a97..c02d75d16 100644 --- a/src/layer-manager.cpp +++ b/src/layer-manager.cpp @@ -159,53 +159,66 @@ void LayerManager::setCurrentLayer( SPObject* obj ) } } -void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify ) +/* + * Return a unique layer name similar to param label + * A unique name is made by substituting or appending the label's number suffix with + * the next unique larger number suffix not already used for any layer name + */ +Glib::ustring LayerManager::getNextLayerName( SPObject* obj, gchar const *label) { - Glib::ustring incoming( label ? label : "" ); + Glib::ustring incoming( label ? label : "Layer 1" ); Glib::ustring result(incoming); Glib::ustring base(incoming); + Glib::ustring split(" "); guint startNum = 1; - if (uniquify) { + gint pos = base.length()-1; + while (pos >= 0 && g_ascii_isdigit(base[pos])) { + pos-- ; + } - 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 = NULL; - 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); - } + gchar* numpart = g_strdup(base.substr(pos+1).c_str()); + if ( numpart ) { + gchar* endPtr = NULL; + guint64 val = g_ascii_strtoull( numpart, &endPtr, 10); + if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) { + base.erase( pos+1); + result = incoming; + startNum = static_cast<int>(val); + split = ""; } + g_free(numpart); + } - std::set<Glib::ustring> currentNames; - GSList const *layers=_document->getResourceList("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=_document->getResourceList("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++ ) { + result = Glib::ustring::format(base, split, i); + } - g_free(suffix); - } + return result; +} +void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify ) +{ + Glib::ustring incoming( label ? label : "" ); + Glib::ustring result(incoming); + + if (uniquify) { + result = getNextLayerName(obj, label); } obj->setLabel( result.c_str() ); |
