def _set_selection(self): selection = QItemSelection() index = self.tableview.model().index for row, col in self.selection: sel = index(row + 2, col + 2) selection.select(sel, sel) self.tableview.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
def _vizrank_select(self): model = self.vizrank.rank_table.model() if not model.rowCount(): return selection = QItemSelection() # This flag is needed because data in the model could be # filtered by a feature and therefore selection could not be found selection_in_model = False if self.selection: sel_names = sorted(name for name, _ in self.selection) for i in range(model.rowCount()): # pylint: disable=protected-access names = sorted(x.name for x in model.data( model.index(i, 0), CorrelationRank._AttrRole)) if names == sel_names: selection.select(model.index(i, 0), model.index(i, model.columnCount() - 1)) selection_in_model = True break if not selection_in_model: selection.select(model.index(0, 0), model.index(0, model.columnCount() - 1)) self.vizrank.rank_table.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect)
def selectvars(varlist, command=selmodel.ClearAndSelect): indices = [data.domain.index(var) for var in varlist] itemsel = QItemSelection() for ind in indices: midx = model.index(ind) itemsel.select(midx, midx) selmodel.select(itemsel, command)
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)
def select_correct(self): """Select the diagonal elements of the matrix""" selection = QItemSelection() n = self.tablemodel.rowCount() for i in range(2, n): index = self.tablemodel.index(i, i) selection.select(index, index) self.tableview.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
def set_selection(self, selection): if selection: sel = QItemSelection() for row in selection: index = self.conc_view.model().index(row, 0) sel.select(index, index) self.conc_view.selectionModel().select(sel, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
def select_wrong(self): """Select the off-diagonal elements of the matrix""" selection = QItemSelection() n = self.tablemodel.rowCount() for i in range(2, n): for j in range(i + 1, n): index = self.tablemodel.index(i, j) selection.select(index, index) index = self.tablemodel.index(j, i) selection.select(index, index) self.tableview.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
def selectFiltered(self): if not self.data: return itemSelection = QItemSelection() index = self.treeWidget.model().sourceModel().index mapFromSource = self.treeWidget.model().mapFromSource for i, row in enumerate(self.cells): if not self.rowFiltered(i): itemSelection.select(mapFromSource(index(i, 0)), mapFromSource(index(i, 0))) self.treeWidget.selectionModel().select(itemSelection, QItemSelectionModel.Select | QItemSelectionModel.Rows)
def _vizrank_select(self): model = self.vizrank.rank_table.model() selection = QItemSelection() names = sorted(x.name for x in self.selection) for i in range(model.rowCount()): # pylint: disable=protected-access if sorted(x.name for x in model.data( model.index(i, 0), CorrelationRank._AttrRole)) == names: selection.select(model.index(i, 0), model.index(i, 1)) self.vizrank.rank_table.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) break
def select(model, selection_model, selected_items): all_items = list(model) try: indices = [all_items.index(item) for item in selected_items] except: indices = [] selection = QItemSelection() for ind in indices: index = model.index(ind) selection.select(index, index) selection_model.select(selection, QItemSelectionModel.Select)
def move_rows(self, view, rows, offset): model = view.model() newrows = [min(max(0, row + offset), len(model) - 1) for row in rows] for row, newrow in sorted(zip(rows, newrows), reverse=offset > 0): model[row], model[newrow] = model[newrow], model[row] selection = QItemSelection() for nrow in newrows: index = model.index(nrow, 0) selection.select(index, index) view.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect)
def update_selection(self, words): nonlocal model, proxymodel selection = QItemSelection() for i, (_, word) in enumerate(model): if word in words: index = proxymodel.mapFromSource(model.index(i, 1)) selection.select(index, index) self.__nope = True self.clearSelection() self.selectionModel().select( selection, QItemSelectionModel.Select | QItemSelectionModel.Rows) self.__nope = False
def select_rows(view, row_indices, command=QItemSelectionModel.ClearAndSelect): """ Select rows in view. :param QAbstractItemView view: :param row_indices: Integer indices of rows to select. :param command: QItemSelectionModel.SelectionFlags """ selmodel = view.selectionModel() model = view.model() selection = QItemSelection() for row in row_indices: index = model.index(row, 0) selection.select(index, index) selmodel.select(selection, command | QItemSelectionModel.Rows)
def selection(self): """ Return the item selection. Returns ------- selection : QtCore.QItemSelection """ selection = QItemSelection() if self.__model is None: return selection for pind in chain(*self.__selections.values()): ind = self.__model.index(pind.row(), pind.column(), pind.parent()) if ind.isValid(): selection.select(ind, ind) return selection
def itemselection(modelindexlist): """ Return an QtCore.QItemSelection from QModelIndex list Parameters ---------- modelindexlist : list of QtCore.QModelIndex Selected model indices. Returns ------- selection : QtCore.QItemSelection """ selection = QItemSelection() for index in modelindexlist: selection.select(index, index) return selection
def test_selection(self): self.send_signal("Corpus", self.corpus) widget = self.widget widget.controls.word.setText("of") self.wait_until_finished() view = self.widget.conc_view # Select one row, two are selected, one document on the output view.selectRow(1) self.assertEqual({ind.row() for ind in view.selectedIndexes()}, {0, 1}) self.assertListEqual( self.get_output("Selected Documents").documents, [self.corpus.documents[1]]) # Select a single row view.selectRow(3) self.assertEqual({ind.row() for ind in view.selectedIndexes()}, {3}) self.assertListEqual( self.get_output("Selected Documents").documents, [self.corpus.documents[4]]) # Add a "double" row, three are selected, two documents on the output selection_model = view.selectionModel() selection = QItemSelection() ind00 = widget.model.index(0, 0) selection.select(ind00, ind00) selection_model.select(selection, selection_model.Select) self.assertEqual({ind.row() for ind in view.selectedIndexes()}, {0, 1, 3}) self.assertEqual( self.get_output("Selected Documents").documents, [self.corpus.documents[i] for i in (1, 4)]) # Clear selection by clicking outside ind_10 = widget.model.index(-1, 0) selection_model.select(ind_10, selection_model.Select) self.assertIsNone(self.get_output("Selected Documents")) # Selected rows emptied after word change view.selectRow(3) self.assertTrue(view.selectedIndexes()) widget.controls.word.setText("o") self.wait_until_finished() self.assertFalse(view.selectedIndexes())
def selection_from_rows(self, rows: Sequence[int], model=None) -> QItemSelection: """ Return selection across all columns for given row indices (as ints) Args: rows (sequence of int): row indices (in proxy model) Returns: QItemSelection """ if model is None: model = self.model() index = model.index last_col = model.columnCount() - 1 sel = QItemSelection() for row in rows: sel.select(index(row, 0), index(row, last_col)) return sel
def select(self, selection, flags): # which rows have been selected indexes = selection.indexes() if isinstance(selection, QItemSelection) \ else [selection] # indexes[0].row() == -1 indicates clicking outside of the table if len(indexes) == 1 and indexes[0].row() == -1: self.clear() return word_index = self.model().word_index selected_docs = {word_index[index.row()][0] for index in indexes} selected_rows = [ row_index for row_index, (doc_index, _) in enumerate(word_index) if doc_index in selected_docs] selection = QItemSelection() # select all rows belonging to the selected document for row in selected_rows: index = self.model().index(row, 0) selection.select(index, index) super().select(selection, flags)
def node_info_complete(self, future): assert self.thread() is QThread.currentThread() assert future.done() self._task = None self.progressBarFinished() self.setCursor(Qt.ArrowCursor) self.btn_connect.setText(self.LABEL_CONNECT) self.Information.fetching_node_info.clear() try: lst, min_time, max_time = future.result( ) # type: Tuple[List[List], pd.Timestamp, pd.Timestamp] except self.Cancelled: pass except Exception as e: log.exception("Error fetching node info") self.Error.fetching_node_info_failed(e) else: self.model.wrap(lst) CachedNodeInfoTable.dump_list(lst) self.btn_download.setEnabled(True) # Apply saved row selection if self.selection: try: selection = QItemSelection() for row in self.model.mapFromSourceRows(self.selection): selection.select(self.model.index(row, 0)) self.view.selectionModel().select(selection) except Exception: log.exception('Failed to restore selection') self.selection = [] self.date_from.setDateTime( QDateTime.fromMSecsSinceEpoch(min_time.timestamp() * 1000, Qt.UTC)) self.date_to.setDateTime( QDateTime.fromMSecsSinceEpoch(max_time.timestamp() * 1000, Qt.UTC))
def test_selection(self): self.send_signal("Corpus", self.corpus) widget = self.widget widget.controls.word.setText("of") view = self.widget.conc_view # Select one row, two are selected, one document on the output view.selectRow(1) self.assertEqual({ind.row() for ind in view.selectedIndexes()}, {0, 1}) self.assertEqual(self.get_output("Selected Documents"), self.corpus[[1]]) # Select a single row view.selectRow(3) self.assertEqual({ind.row() for ind in view.selectedIndexes()}, {3}) self.assertEqual(self.get_output("Selected Documents"), self.corpus[[4]]) # Add a "double" row, three are selected, two documents on the output selection_model = view.selectionModel() selection = QItemSelection() ind00 = widget.model.index(0, 0) selection.select(ind00, ind00) selection_model.select(selection, selection_model.Select) self.assertEqual({ind.row() for ind in view.selectedIndexes()}, {0, 1, 3}) self.assertEqual(self.get_output("Selected Documents"), self.corpus[[1, 4]]) # Clear selection by clicking outside ind_10 = widget.model.index(-1, 0) selection_model.select(ind_10, selection_model.Select) self.assertIsNone(self.get_output("Selected Documents")) # Selected rows emptied after word change view.selectRow(3) self.assertTrue(view.selectedIndexes()) widget.controls.word.setText("o") self.assertFalse(view.selectedIndexes())
def word_clicked(self, word): """Called from JavaScript""" if not word: self.selected_words.clear() return '' selection = QItemSelection() for i, row in enumerate(self.tablemodel): for j, val in enumerate(row): if val == word: index = self.tablemodel.index(i, j) selection.select(index, index) if word not in self.selected_words: self.selected_words.add(word) self.tableview.selectionModel().select( selection, QItemSelectionModel.Select | QItemSelectionModel.Rows) return 'selected' else: self.selected_words.remove(word) self.tableview.selectionModel().select( selection, QItemSelectionModel.Deselect | QItemSelectionModel.Rows) return ''
def select(self, selection, flags): # which rows have been selected indexes = selection.indexes() if isinstance(selection, QItemSelection) \ else [selection] # prevent crashing when deleting the connection if not indexes: super().select(selection, flags) return # indexes[0].row() == -1 indicates clicking outside of the table if len(indexes) == 1 and indexes[0].row() == -1: self.clear() return word_index = self.model().word_index selected_docs = {word_index[index.row()][0] for index in indexes} selected_rows = [ row_index for row_index, (doc_index, _) in enumerate(word_index) if doc_index in selected_docs] selection = QItemSelection() # select all rows belonging to the selected document for row in selected_rows: index = self.model().index(row, 0) selection.select(index, index) super().select(selection, flags)
def move_rows(self, view: QListView, offset: int, roles=(Qt.EditRole,)): rows = [idx.row() for idx in view.selectionModel().selectedRows()] model = view.model() # type: QAbstractItemModel rowcount = model.rowCount() newrows = [min(max(0, row + offset), rowcount - 1) for row in rows] def itemData(index): return {role: model.data(index, role) for role in roles} for row, newrow in sorted(zip(rows, newrows), reverse=offset > 0): d1 = itemData(model.index(row, 0)) d2 = itemData(model.index(newrow, 0)) model.setItemData(model.index(row, 0), d2) model.setItemData(model.index(newrow, 0), d1) selection = QItemSelection() for nrow in newrows: index = model.index(nrow, 0) selection.select(index, index) view.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) self.commit()
def select(self, selection, flags): """Reimplemented.""" if isinstance(selection, QModelIndex): selection = QItemSelection(selection, selection) model = self.model() indexes = self.selectedIndexes() rows = set(ind.row() for ind in indexes) cols = set(ind.column() for ind in indexes) if flags & QItemSelectionModel.Select and \ not flags & QItemSelectionModel.Clear and self.__selectBlocks: indexes = selection.indexes() sel_rows = set(ind.row() for ind in indexes).union(rows) sel_cols = set(ind.column() for ind in indexes).union(cols) selection = QItemSelection() for r_start, r_end in ranges(sorted(sel_rows)): for c_start, c_end in ranges(sorted(sel_cols)): top_left = model.index(r_start, c_start) bottom_right = model.index(r_end - 1, c_end - 1) selection.select(top_left, bottom_right) elif self.__selectBlocks and flags & QItemSelectionModel.Deselect: indexes = selection.indexes() def to_ranges(indices): return list(range(*r) for r in ranges(indices)) selected_rows = to_ranges(sorted(rows)) selected_cols = to_ranges(sorted(cols)) desel_rows = to_ranges(set(ind.row() for ind in indexes)) desel_cols = to_ranges(set(ind.column() for ind in indexes)) selection = QItemSelection() # deselection extended vertically for row_range, col_range in \ itertools.product(selected_rows, desel_cols): selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) # deselection extended horizontally for row_range, col_range in \ itertools.product(desel_rows, selected_cols): selection.select( model.index(row_range.start, col_range.start), model.index(row_range.stop - 1, col_range.stop - 1) ) QItemSelectionModel.select(self, selection, flags)
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 qitemselection_select_range(selection: QItemSelection, model: QAbstractItemModel, rows: range, columns: range) -> None: assert rows.step == 1 and columns.step == 1 selection.select(model.index(rows.start, columns.start), model.index(rows.stop - 1, columns.stop - 1))
def set_selected_items(self, inds): index = self.model().index selection = QItemSelection() for i in inds: selection.select(index(i, i), index(i, i)) self.select(selection, QItemSelectionModel.ClearAndSelect)
def itsel(self, rows): sel = QItemSelection() for row in rows: index = self.store.proxy.index(row, 0) sel.select(index, index) return sel