Moved object name collision detection from new_object() to ObjectCollection.append(). Solves issue #107.

This commit is contained in:
jpcaram
2015-02-13 15:04:57 -05:00
parent b89a04d1e2
commit 5ab7e04c2e
2 changed files with 32 additions and 14 deletions

View File

@@ -737,17 +737,17 @@ class App(QtCore.QObject):
t0 = time.time() # Debug
### Check for existing name
while name in self.collection.get_names():
## Create a new name
# Ends with number?
App.log.debug("new_object(): Object name (%s) exists, changing." % name)
match = re.search(r'(.*[^\d])?(\d+)$', name)
if match: # Yes: Increment the number!
base = match.group(1) or ''
num = int(match.group(2))
name = base + str(num + 1)
else: # No: add a number!
name += "_1"
# while name in self.collection.get_names():
# ## Create a new name
# # Ends with number?
# App.log.debug("new_object(): Object name (%s) exists, changing." % name)
# match = re.search(r'(.*[^\d])?(\d+)$', name)
# if match: # Yes: Increment the number!
# base = match.group(1) or ''
# num = int(match.group(2))
# name = base + str(num + 1)
# else: # No: add a number!
# name += "_1"
## Create object
classdict = {
@@ -1237,8 +1237,11 @@ class App(QtCore.QObject):
"""
t0 = time.time() # DEBUG
self.log.debug("on_object_created()")
self.inform.emit("Object (%s) created: %s" % (obj.kind, obj.options['name']))
# The Collection might change the name if there is a collision
self.collection.append(obj)
self.inform.emit("Object (%s) created: %s" % (obj.kind, obj.options['name']))
obj.plot()
self.on_zoom_fit(None)
t1 = time.time() # DEBUG

View File

@@ -95,15 +95,30 @@ class ObjectCollection(QtCore.QAbstractListModel):
def append(self, obj, active=False):
FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.append()")
# Prevent same name
name = obj.options["name"]
while name in self.get_names():
## Create a new name
# Ends with number?
FlatCAMApp.App.log.debug("new_object(): Object name (%s) exists, changing." % name)
match = re.search(r'(.*[^\d])?(\d+)$', name)
if match: # Yes: Increment the number!
base = match.group(1) or ''
num = int(match.group(2))
name = base + str(num + 1)
else: # No: add a number!
name += "_1"
obj.options["name"] = name
obj.set_ui(obj.ui_type())
# Required before appending
# Required before appending (Qt MVC)
self.beginInsertRows(QtCore.QModelIndex(), len(self.object_list), len(self.object_list))
# Simply append to the python list
self.object_list.append(obj)
# Required after appending
# Required after appending (Qt MVC)
self.endInsertRows()
def get_names(self):