From c9fc18f685fd496911ee697cb65b566181cdc2e9 Mon Sep 17 00:00:00 2001 From: Olivier Keshavjee Date: Fri, 27 Oct 2017 07:30:07 +0200 Subject: [PATCH] Fixes a bug in #169: dragging an open folder creates duplicates --- manuskript/models/outlineModel.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/manuskript/models/outlineModel.py b/manuskript/models/outlineModel.py index fc6dbc84..9b4f766f 100644 --- a/manuskript/models/outlineModel.py +++ b/manuskript/models/outlineModel.py @@ -302,6 +302,37 @@ class outlineModel(QAbstractItemModel): item = outlineItem(xml=ET.tostring(child)) items.append(item) + # We remove every item whose parent is also in items, otherwise it gets + # duplicated. (https://github.com/olivierkes/manuskript/issues/169) + # For example if selecting: + # - Parent + # - Child + # And draging them, items encoded in mime data are: [Parent, Child], + # but Child is already contained in Parent, so if we do nothing we end + # up with: + # - Parent + # - Child + # - Child + + newItems = items[:] + IDs = [i.ID() for i in items] + + def checkIfChildIsPresent(item): + # Recursively check every children of item, to see if any is in + # the list of items to copy. If so, we remove it from the list. + for c in item.children(): + # We check if children is in the selection + # and if it hasn't been removed yet + if c.ID() in IDs and c.ID() in [i.ID() for i in newItems]: + # Remove item by ID + newItems.remove([i for i in newItems if i.ID() == c.ID()][0]) + checkIfChildIsPresent(c) + + for i in items: + checkIfChildIsPresent(i) + + items = newItems + return items def dropMimeData(self, data, action, row, column, parent):