def _select_rows(self): model = self.view.model() n_rows, n_columns = model.rowCount(), model.columnCount() if self.sel_method == SelectionMethods.NONE: selection = QItemSelection() elif self.sel_method == SelectionMethods.ALL: selection = QItemSelection(model.index(0, 0), model.index(n_rows - 1, n_columns - 1)) elif self.sel_method == SelectionMethods.MANUAL: selection = QItemSelection() for i in range(n_rows): word = model.data(model.index(i, 0)) if word in self.selected_words: _selection = QItemSelection(model.index(i, 0), model.index(i, n_columns - 1)) selection.merge(_selection, QItemSelectionModel.Select) elif self.sel_method == SelectionMethods.N_BEST: n_sel = min(self.n_selected, n_rows) selection = QItemSelection(model.index(0, 0), model.index(n_sel - 1, n_columns - 1)) else: raise NotImplementedError self.view.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
def _set_selected_rows(self, selected_rows: List[int]): model = self._list_view.model() n_columns = model.columnCount() selection = QItemSelection() for i in selected_rows: _selection = QItemSelection(model.index(i, 0), model.index(i, n_columns - 1)) selection.merge(_selection, QItemSelectionModel.Select) self._list_view.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect)
def select(self, selection, flags): """Reimplemented.""" if isinstance(selection, QModelIndex): selection = QItemSelection(selection, selection) if not self.__selectBlocks: super().select(selection, flags) return model = self.model() def to_ranges(spans): return list(range(*r) for r in spans) if flags & QItemSelectionModel.Current: # no current selection support flags &= ~QItemSelectionModel.Current if flags & QItemSelectionModel.Toggle: # no toggle support either flags &= ~QItemSelectionModel.Toggle flags |= QItemSelectionModel.Select if flags == QItemSelectionModel.ClearAndSelect: # extend selection ranges in `selection` to span all row/columns sel_rows = selection_rows(selection) sel_cols = selection_columns(selection) selection = QItemSelection() for row_range, col_range in \ itertools.product(to_ranges(sel_rows), to_ranges(sel_cols)): selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) elif flags & (QItemSelectionModel.Select | QItemSelectionModel.Deselect): # extend all selection ranges in `selection` with the full current # row/col spans rows, cols = selection_blocks(self.selection()) sel_rows = selection_rows(selection) sel_cols = selection_columns(selection) ext_selection = QItemSelection() for row_range, col_range in \ itertools.product(to_ranges(rows), to_ranges(sel_cols)): ext_selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) for row_range, col_range in \ itertools.product(to_ranges(sel_rows), to_ranges(cols)): ext_selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) selection.merge(ext_selection, QItemSelectionModel.Select) super().select(selection, flags)
def _select_rows(self): proxy_model = self.view.model() n_rows, n_columns = proxy_model.rowCount(), proxy_model.columnCount() if self.sel_method == SelectionMethods.NONE: selection = QItemSelection() elif self.sel_method == SelectionMethods.ALL: selection = QItemSelection( proxy_model.index(0, 0), proxy_model.index(n_rows - 1, n_columns - 1)) elif self.sel_method == SelectionMethods.MANUAL: selection = QItemSelection() new_sel = [] for row in self.selected_rows: if row < n_rows: new_sel.append(row) _selection = QItemSelection( self.model.index(row, 0), self.model.index(row, n_columns - 1)) selection.merge( proxy_model.mapSelectionFromSource(_selection), QItemSelectionModel.Select, ) # selected rows must be updated when the same dataset with less rows # appear at the input - it is not handled by selectionChanged # in cases when all selected rows missing in new table self.selected_rows = new_sel elif self.sel_method == SelectionMethods.N_BEST: n_sel = min(self.n_selected, n_rows) selection = QItemSelection( proxy_model.index(0, 0), proxy_model.index(n_sel - 1, n_columns - 1)) else: raise NotImplementedError self.view.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)