Merge pull request #777 from siliconserf/Issue-324-NoID-Fix

Update abstractModel.py
This commit is contained in:
Tobias Frisch 2021-04-10 14:39:50 +02:00 committed by GitHub
commit 653357d2e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,6 +38,9 @@ class abstractModel(QAbstractItemModel):
- Interface with QModelIndex and stuff
- XML Import / Export
- Drag'n'drop
Row => item/abstractModel/etc.
Col => data sub-element. Col 1 (second counting) is ID for all model types.
"""
def __init__(self, parent):
@ -426,21 +429,32 @@ class abstractModel(QAbstractItemModel):
# In case of copy actions, items might be duplicates, so we need new IDs.
# But they might not be, if we cut, then paste. Paste is a Copy Action.
# The first paste would not need new IDs. But subsequent ones will.
# Recursively change the existing IDs to new, unique values. No need to strip out the old
# even if they are not duplicated in pasting. There is no practical need for ID conservation.
if action == Qt.CopyAction:
IDs = self.rootItem.listAllIDs()
for item in items:
if item.ID() in IDs:
# Recursively remove ID. So will get a new one when inserted.
def stripID(item):
item.setData(Outline.ID, None)
for c in item.children():
stripID(c)
stripID(item)
def makeNewID(item):
k = 1
while True: # Python doesn't have "Do...While"
ks = str(k)
if ks not in IDs:
item.setData(Outline.ID,ks)
IDs.append(ks) #Keep track of new IDs allocated.
for c in item.children():
makeNewID(c)
break # Actual Loop Exit
else:
k = k+1 # Try the next candidate ID
makeNewID(item)
r = self.insertItems(items, beginRow, parent)
return r
################# ADDING AND REMOVING #################
@ -459,13 +473,13 @@ class abstractModel(QAbstractItemModel):
# Insert only if parent is folder
if parentItem.isFolder():
self.beginInsertRows(parent, row, row + len(items) - 1)
self.beginInsertRows(parent, row, row + len(items) - 1) # Create space.
for i in items:
parentItem.insertChild(row + items.index(i), i)
self.endInsertRows()
return True
else:
@ -518,8 +532,9 @@ class abstractModel(QAbstractItemModel):
else:
parentItem = parent.internalPointer()
self._removingRows = True # Views that are updating can easily know
# if this is due to row removal.
self._removingRows = True
# Views that are updating can easily know
# if this is due to row removal.
self.beginRemoveRows(parent, row, row + count - 1)
for i in range(count):
item = parentItem.removeChild(row)