def _moveSelectionRange(self, selectionrange, dst): if selectionrange.top() == dst: return selectionrange model = selectionrange.model() parentindex = selectionrange.parent() parentitem = self._parentitem(selectionrange) nrows_selected = selectionrange.bottom() - selectionrange.top() + 1 ncols = model.columnCount() if nrows_selected == parentitem.rowCount(): return selectionrange if dst > parentitem.rowCount() - nrows_selected or dst < 0: return selectionrange selectedrows = self._takeRowsRange(selectionrange) for index, items in enumerate(selectedrows): parentitem.insertRow(dst + index, items) topleft = model.index(dst, 0, parentindex) bottomright = model.index(dst + nrows_selected - 1, ncols - 1, parentindex) return QtCore.QItemSelectionRange(topleft, bottomright)
def _selectionmap(selection): sortedselection = sorted(selection, key=QtCore.QItemSelectionRange.parent) selectionmap = {} # collections.OrderedDict() for key, group in itertools.groupby(sortedselection, QtCore.QItemSelectionRange.parent): ranges = [] for item in sorted(group, key=QtCore.QItemSelectionRange.top): if len(ranges) == 0: ranges.append(item) continue lastitem = ranges[-1] assert lastitem.parent() == item.parent() if lastitem.bottom() + 1 >= item.top(): model = lastitem.model() topleft = model.index( min(lastitem.top(), item.top()), #min(lastitem.left(), item.left()), 0, lastitem.parent()) bottomright = model.index( max(lastitem.bottom(), item.bottom()), #max(lastitem.right(), item.right()), model.columnCount() - 1, lastitem.parent()) ranges[-1] = QtCore.QItemSelectionRange( topleft, bottomright) else: ranges.append(item) selectionmap[key] = ranges return selectionmap