mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-05-19 20:32:28 +12:00
Merge pull request #777 from siliconserf/Issue-324-NoID-Fix
Update abstractModel.py
This commit is contained in:
commit
653357d2e9
|
@ -39,6 +39,9 @@ class abstractModel(QAbstractItemModel):
|
||||||
- XML Import / Export
|
- XML Import / Export
|
||||||
- Drag'n'drop
|
- 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):
|
def __init__(self, parent):
|
||||||
QAbstractItemModel.__init__(self, parent)
|
QAbstractItemModel.__init__(self, parent)
|
||||||
|
@ -426,21 +429,32 @@ class abstractModel(QAbstractItemModel):
|
||||||
# In case of copy actions, items might be duplicates, so we need new IDs.
|
# 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.
|
# 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.
|
# 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:
|
if action == Qt.CopyAction:
|
||||||
IDs = self.rootItem.listAllIDs()
|
IDs = self.rootItem.listAllIDs()
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
if item.ID() in IDs:
|
if item.ID() in IDs:
|
||||||
# Recursively remove ID. So will get a new one when inserted.
|
def makeNewID(item):
|
||||||
def stripID(item):
|
k = 1
|
||||||
item.setData(Outline.ID, None)
|
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():
|
for c in item.children():
|
||||||
stripID(c)
|
makeNewID(c)
|
||||||
|
break # Actual Loop Exit
|
||||||
stripID(item)
|
else:
|
||||||
|
k = k+1 # Try the next candidate ID
|
||||||
|
|
||||||
|
makeNewID(item)
|
||||||
r = self.insertItems(items, beginRow, parent)
|
r = self.insertItems(items, beginRow, parent)
|
||||||
|
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
################# ADDING AND REMOVING #################
|
################# ADDING AND REMOVING #################
|
||||||
|
@ -459,7 +473,7 @@ class abstractModel(QAbstractItemModel):
|
||||||
|
|
||||||
# Insert only if parent is folder
|
# Insert only if parent is folder
|
||||||
if parentItem.isFolder():
|
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:
|
for i in items:
|
||||||
parentItem.insertChild(row + items.index(i), i)
|
parentItem.insertChild(row + items.index(i), i)
|
||||||
|
@ -518,7 +532,8 @@ class abstractModel(QAbstractItemModel):
|
||||||
else:
|
else:
|
||||||
parentItem = parent.internalPointer()
|
parentItem = parent.internalPointer()
|
||||||
|
|
||||||
self._removingRows = True # Views that are updating can easily know
|
self._removingRows = True
|
||||||
|
# Views that are updating can easily know
|
||||||
# if this is due to row removal.
|
# if this is due to row removal.
|
||||||
self.beginRemoveRows(parent, row, row + count - 1)
|
self.beginRemoveRows(parent, row, row + count - 1)
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
|
|
Loading…
Reference in a new issue