diff options
| -rw-r--r-- | src/layer-manager.cpp | 81 | ||||
| -rw-r--r-- | src/layer-manager.h | 1 | ||||
| -rw-r--r-- | src/ui/dialog/layer-properties.cpp | 7 |
3 files changed, 53 insertions, 36 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() ); diff --git a/src/layer-manager.h b/src/layer-manager.h index fbb22d405..1b69324d5 100644 --- a/src/layer-manager.h +++ b/src/layer-manager.h @@ -30,6 +30,7 @@ public: void setCurrentLayer( SPObject* obj ); void renameLayer( SPObject* obj, gchar const *label, bool uniquify ); + Glib::ustring getNextLayerName( SPObject* obj, gchar const *label); sigc::connection connectCurrentLayerChanged(const sigc::slot<void, SPObject *> & slot) { return _layer_changed_signal.connect(slot); diff --git a/src/ui/dialog/layer-properties.cpp b/src/ui/dialog/layer-properties.cpp index 35a235dbc..4f9edf774 100644 --- a/src/ui/dialog/layer-properties.cpp +++ b/src/ui/dialog/layer-properties.cpp @@ -331,8 +331,11 @@ void LayerPropertiesDialog::Rename::perform(LayerPropertiesDialog &dialog) { void LayerPropertiesDialog::Create::setup(LayerPropertiesDialog &dialog) { dialog.set_title(_("Add Layer")); - //TODO: find an unused layer number, forming name from _("Layer ") + "%d" - dialog._layer_name_entry.set_text(_("Layer")); + + // Set the initial name to the "next available" layer name + LayerManager *mgr = dialog._desktop->layer_manager; + Glib::ustring newName = mgr->getNextLayerName(NULL, dialog._desktop->currentLayer()->label()); + dialog._layer_name_entry.set_text(newName.c_str()); dialog._apply_button.set_label(_("_Add")); dialog._setup_position_controls(); } |
