def _restoreSelection(self): newSelection = QItemSelection() for index in self.model.selected_indexes: newSelection.select(self.createIndex(index, 0), self.createIndex(index, 0)) self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect) if len(newSelection.indexes()): currentIndex = newSelection.indexes()[0] self.view.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Current) self.view.scrollTo(currentIndex)
def _updateViewSelection(self): # Takes the selection on the model's side and update the view with it. newSelection = QItemSelection() columnCount = self.columnCount(QModelIndex()) for index in self.model.selected_indexes: newSelection.select(self.createIndex(index, 0), self.createIndex(index, columnCount-1)) self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect) if len(newSelection.indexes()): currentIndex = newSelection.indexes()[0] self.view.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Current) self.view.scrollTo(currentIndex)
def select(self, selection, flags): if isinstance(selection, QModelIndex): selection = QItemSelection(selection, selection) model = self.model() indexes = selection.indexes() sel_inds = {ind.row() for ind in indexes} | \ {ind.column() for ind in indexes} if flags == QItemSelectionModel.ClearAndSelect: selected = set() else: selected = {ind.row() for ind in self.selectedIndexes()} if flags & QItemSelectionModel.Select: selected |= sel_inds elif flags & QItemSelectionModel.Deselect: selected -= sel_inds new_selection = QItemSelection() regions = list(ranges(sorted(selected))) for r_start, r_end in regions: for c_start, c_end in regions: top_left = model.index(r_start, c_start) bottom_right = model.index(r_end - 1, c_end - 1) new_selection.select(top_left, bottom_right) QItemSelectionModel.select(self, new_selection, QItemSelectionModel.ClearAndSelect)