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 setSelectedItems(self, items): #block = self.blockSignals(True) sel = QItemSelection() for item in items: sel.merge(QItemSelection(item.index(), item.index(1)), QItemSelectionModel.SelectCurrent) if set(sel) != set(self.selectionModel().selection()): self.selectionModel().clear() self.selectionModel().select(sel, QItemSelectionModel.Select)
def redoSelection(self, selectedCoordinates): selection = QItemSelection() for i, c in enumerate(self.coordinates): if c in selectedCoordinates: selection.merge( QItemSelection(self.createIndex(i, 0), self.createIndex(i, 1)), QItemSelectionModel.Select) self.app.aw.ui.tableView.selectionModel().select( selection, QItemSelectionModel.Select)
def set_selection(self): if len(self.selected_rows) and len(self.selected_cols): view = self.tabs.currentWidget() selection = QItemSelection() temp_selection = QItemSelection() for row in self.selected_rows: for col in self.selected_cols: index = view.model().index(row, col) temp_selection.select(index, index) selection.merge(temp_selection, QItemSelectionModel.Select) view.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
def on_actionInvert_selection_triggered(self): widget = self.activ_selection model = widget.model() selection = widget.selectionModel() root = model.root selection.select(QItemSelection(model.index(0,0,root), model.index(model.rowCount(root),1,root)), QItemSelectionModel.Toggle) widget.viewport().update()
def select(self, uuids, lbox: QTreeView = None, scroll_to_show_single=True): lbox = self.current_set_listbox if lbox is None else lbox lbox.clearSelection() if not uuids: return # FUTURE: this is quick and dirty rowdict = dict( (u, i) for i, u in enumerate(self.doc.current_layer_uuid_order)) items = QItemSelection() q = None for uuid in uuids: row = rowdict.get(uuid, None) if row is None: LOG.error( 'UUID {} cannot be selected in list view'.format(uuid)) continue q = self.createIndex(row, 0) items.select(q, q) lbox.selectionModel().select(items, QItemSelectionModel.Select) # lbox.setCurrentIndex(q) if scroll_to_show_single and len(uuids) == 1 and q is not None: lbox.scrollTo(q)
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) uic.loadUi("mainwindow.ui", self) model = QStandardItemModel(7, 4, self) for row in range(7): for column in range(4): item = QStandardItem(QString("%1").arg(row * 4 + column)) model.setItem(row, column, item) self.tableView = QTableView() self.tableView.setModel(model) self.setCentralWidget(self.tableView) # 获取视图的项目选择模型 selectionModel = self.tableView.selectionModel() # 定义左上角和右下角的索引,然后使用这两个索引创建选择 topLeft = model.index(1, 1, QModelIndex()) bottomRight = model.index(5, 2, QModelIndex()) selection = QItemSelection(topLeft, bottomRight) # 使用指定的选择模式来选择项目 selectionModel.select(selection, QItemSelectionModel.Select) self.mainToolBar.addAction(_fromUtf8("当前项目"), self.getCurrentItemData) self.mainToolBar.addAction(_fromUtf8("切换选择"), self.toggleSelection)
def toggleSelection(self): topLeft = self.tableView.model().index(0, 0, QModelIndex()) bottomRight = self.tableView.model().index( self.tableView.model().rowCount(QModelIndex()) - 1, self.tableView.model().columnCount(QModelIndex()) - 1, QModelIndex()) curSelection = QItemSelection(topLeft, bottomRight) self.tableView.selectionModel().select(curSelection, QItemSelectionModel.Toggle)
def cell_clicked(self, model_index): i, j = model_index.row(), model_index.column() if not i or not j: return n = self.tablemodel.rowCount() index = self.tablemodel.index selection = None if i == j == 1 or i == j == n - 1: selection = QItemSelection(index(2, 2), index(n - 1, n - 1)) elif i in (1, n - 1): selection = QItemSelection(index(2, j), index(n - 1, j)) elif j in (1, n - 1): selection = QItemSelection(index(i, 2), index(i, n - 1)) if selection is not None: self.tableview.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect)
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 _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 select_correct(self): 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 on_actionSelect_odd_images_triggered(self): widget = self.activ_selection model = widget.model() selection = widget.selectionModel() sel = QItemSelection() for i in range(0,len(model),2): sel.select(model.index(i,0), model.index(i,1)) selection.select(sel, QItemSelectionModel.ClearAndSelect) widget.viewport().update()
def updateCoordinate(self,row,c0,c1): # respect the limits # NOSORT if False and ((row > 0 and c0 <= self.coordinates[row-1][0]) or ((row < len(self.coordinates) - 1 and c0 >= self.coordinates[row+1][0]))): return else: selectedCoordinates = self.getSelectedCoordinates() self.beginResetModel() self.coordinates[row][0] = c0 self.coordinates[row][1] = int(c1) self.endResetModel() self.computePolyfit() selection = QItemSelection() for i,c in enumerate(self.coordinates): if c in selectedCoordinates: selection.merge(QItemSelection(self.createIndex(i,0),self.createIndex(i,1)),QItemSelectionModel.Select) self.app.aw.ui.tableView.selectionModel().select(selection,QItemSelectionModel.Select) self.app.contentModified()
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 _learner_changed(self): # The selected learner has changed indices = self.tableview.selectedIndexes() self._update() selection = QItemSelection() for sel in indices: selection.select(sel, sel) self.tableview.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) self.commit()
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(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 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 select_wrong(self): 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 selectionChanged(self): X = self.X mapping = self.onehot_mapping instances = set() where = np.where def whole_subtree(node): yield node for i in range(node.childCount()): yield from whole_subtree(node.child(i)) def itemset(node): while node: yield node.data(0, self.ITEM_DATA_ROLE) node = node.parent() def selection_ranges(node): n_children = node.childCount() if n_children: yield (self.tree.indexFromItem(node.child(0)), self.tree.indexFromItem(node.child(n_children - 1))) for i in range(n_children): yield from selection_ranges(node.child(i)) nSelectedItemsets = 0 item_selection = QItemSelection() for node in self.tree.selectedItems(): nodes = (node, ) if node.isExpanded() else whole_subtree(node) if not node.isExpanded(): for srange in selection_ranges(node): item_selection.select(*srange) for node in nodes: nSelectedItemsets += 1 cols, vals = zip(*(mapping[i] for i in itemset(node))) if issparse(X): rows = (len(cols) == np.bincount( (X[:, cols] != 0).indices, minlength=X.shape[0])).nonzero()[0] else: rows = where((X[:, cols] == vals).all(axis=1))[0] instances.update(rows) self.tree.itemSelectionChanged.disconnect(self.selectionChanged) self.tree.selectionModel().select( item_selection, QItemSelectionModel.Select | QItemSelectionModel.Rows) self.tree.itemSelectionChanged.connect(self.selectionChanged) self.nSelectedExamples = len(instances) self.nSelectedItemsets = nSelectedItemsets self.output = self.data[sorted(instances)] or None self.commit()
def selectLargePathSegments(self): selection = QItemSelection() for path in self.largePathSegmentsModel.selectedGroundings(): for i in range(self.pathSegmentsModel.rowCount()): entry = self.pathSegmentsModel.get(i) if path.contains(entry.grounding.range): idx = self.pathSegmentsModel.index(i, 0) selection.select(idx, idx) #self.pathSegmentsTable.selectRow(i) #break print 'adding path', selection self.pathSegmentsTable.selectionModel().select(selection, QItemSelectionModel.Rows | QItemSelectionModel.SelectCurrent)
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 focusInEvent(self, e): currentIndex = self.model().index( self.selectionModel().currentIndex().row(), 0) currentIndex2 = self.model().index(currentIndex.row(), self.model().columnCount() - 1) #If we never entered the widget if currentIndex.row() < 0: currentIndex = self.model().index(0, 0) currentIndex2 = self.model().index(0, self.model().columnCount() - 1) self.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Rows) self.selectionModel().select( QItemSelection(currentIndex, currentIndex2), QItemSelectionModel.Select)
def _itemWidgetSelectionChanged(self, index): """ Handles changed items of the select item widget and selects corresponding privilege definition in the table. """ item = self._repositoryModel.nodeFromIndex(index) rows = self._model.determineRows(item) if len(rows) > 0: topLeft = self._model.index(rows[0], 0) bottomRight = self._model.index(rows[len(rows )- 1], 3) selection = QItemSelection(topLeft, bottomRight) self._privilegeWidget.selectionModel().clearSelection() self._privilegeWidget.selectionModel().setCurrentIndex(topLeft, QItemSelectionModel.Select) self._privilegeWidget.selectionModel().select(selection, QItemSelectionModel.Select) else: self._privilegeWidget.selectionModel().clearSelection()
def select_rows(view, row_indices, command=QItemSelectionModel.ClearAndSelect): """ Select rows in view. :param PyQt4.QtGui.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 keyPressEvent(self, event): if event.key() == Qt.Key_Delete: index = self.selectedIndexes()[0] row = index.model().itemFromIndex(index).row() childIndex=self.model().index(row+1,0) childIndex2=self.model().index(row+1,self.model().columnCount()-1) self.selectionModel().clearSelection() self.selectionModel().select(QItemSelection(childIndex,childIndex2), QItemSelectionModel.Select) self.selectionModel().setCurrentIndex(childIndex,QItemSelectionModel.Rows) if self.playingId == row: self.runAction.emit('stop') self.model().removeRow(row) elif event.key() == Qt.Key_Return: self.runAction.emit('play') QTableView.keyPressEvent(self, event)
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) uic.loadUi("mainwindow.ui", self) model = QStandardItemModel(7, 4, self) for row in range(7): for column in range(4): item = QStandardItem(QString("%1").arg(row * 4 + column)) model.setItem(row, column, item) self.tableView = QTableView() self.tableView.setModel(model) self.setCentralWidget(self.tableView) # 获取视图的项目选择模型 selectionModel = self.tableView.selectionModel() # 定义左上角和右下角的索引,然后使用这两个索引创建选择 topLeft = model.index(1, 1, QModelIndex()) bottomRight = model.index(5, 2, QModelIndex()) selection = QItemSelection(topLeft, bottomRight) # 使用指定的选择模式来选择项目 selectionModel.select(selection, QItemSelectionModel.Select) self.mainToolBar.addAction(_fromUtf8("当前项目"), self.getCurrentItemData) self.mainToolBar.addAction(_fromUtf8("切换选择"), self.toggleSelection) self.connect(selectionModel,SIGNAL("selectionChanged(QItemSelection,QItemSelection)"), self.updateSelection) self.connect(selectionModel, SIGNAL("currentChanged(QModelIndex,QModelIndex)"), self.changeCurrent) # 多个视图共享选择 self.tableView2 = QTableView() self.tableView2.setWindowTitle("tableView2") self.tableView2.resize(400, 300) self.tableView2.setModel(model) self.tableView2.setSelectionModel(selectionModel) self.tableView2.show() # 使用自定义委托 delegate = SpinBoxDelegate(self) self.tableView.setItemDelegate(delegate)
def set_selection(self): if len(self.selected_rows) and len(self.selected_cols): view = self.tabs.currentWidget() model = view.model() if model.rowCount() <= self.selected_rows[-1] or \ model.columnCount() <= self.selected_cols[-1]: return selection = QItemSelection() rowranges = list(ranges(self.selected_rows)) colranges = list(ranges(self.selected_cols)) for rowstart, rowend in rowranges: for colstart, colend in colranges: selection.append( QtGui.QItemSelectionRange( view.model().index(rowstart, colstart), view.model().index(rowend - 1, colend - 1))) view.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
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 ''