summaryrefslogtreecommitdiffstats
path: root/src/layer-manager.cpp
diff options
context:
space:
mode:
authorJon A. Cruz <jon@joncruz.org>2006-05-27 04:26:51 +0000
committerjoncruz <joncruz@users.sourceforge.net>2006-05-27 04:26:51 +0000
commit265d3bfecbec14917ebb262ce5ea58bbaf1d48a2 (patch)
tree660fa2ff6d4e3fb4cc5fe822380fcf5bf25cdd08 /src/layer-manager.cpp
parentpatch from bug 1495755 (diff)
downloadinkscape-265d3bfecbec14917ebb262ce5ea58bbaf1d48a2.tar.gz
inkscape-265d3bfecbec14917ebb262ce5ea58bbaf1d48a2.zip
Prevent layer name duplication
(bzr r1024)
Diffstat (limited to 'src/layer-manager.cpp')
-rw-r--r--src/layer-manager.cpp52
1 files changed, 52 insertions, 0 deletions
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 <set>
#include <sigc++/functors/mem_fun.h>
#include <sigc++/adaptors/hide.h>
#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<int>(val);
+ }
+ 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() );
+ }
+ }
+ }
+
+ // 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();