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 _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 select_correct(self): selection = QItemSelection() n = self.tablemodel.rowCount() for i in range(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 _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): """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_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(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(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 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 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 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 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 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 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 sort_by_column(self, index): table = self.tabs.currentWidget() if index == table.oldSortingIndex and index != -1: order = (table.oldSortingOrder == QtCore.Qt.AscendingOrder and QtCore.Qt.DescendingOrder or QtCore.Qt.AscendingOrder) else: order = QtCore.Qt.AscendingOrder oldsel = self.get_current_selection() model = table.model() if index == -1: table.horizontalHeader().setSortIndicatorShown(False) model.reset_sort() else: table.horizontalHeader().setSortIndicatorShown(1) table.sortByColumn(index, order) newsort = sorted(enumerate(model.sorted_map), key=lambda x: x[1]) newsel = [ newsort[a][0] for a in oldsel ] itemsel = QItemSelection() for a in newsel: itemsel.select(model.index(a, 0), model.index(a, 0)) table.selectionModel().select(itemsel, QItemSelectionModel.Rows | \ QItemSelectionModel.Select | QItemSelectionModel.Clear) table.oldSortingIndex = index table.oldSortingOrder = order
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 __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 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 _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 __init__(self, parent=None): super(MainWindow, self).__init__(None) 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) tableView = QTableView() tableView.setModel(model) self.setCentralWidget(tableView) #获取视图的项目选择模型 selectionModel = tableView.selectionModel() # 定义左上角和右下角的索引,然后使用这两个索引创建选择 topLeft = model.index(1, 1, QModelIndex()) bottomRight = model.index(5, 2, QModelIndex()) selection = QItemSelection(topLeft, bottomRight) # 使用指定的选择模式来选择项目 selectionModel.select(selection, 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_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 set_selection(self, data): """ Set the selected data. """ block_list = [] for b in data['blocks']: cl = None rp = None if len(b) > 2: cl = NeoDataProvider.find_io_class(b[2]) if len(b) > 3: rp = b[3] loaded = NeoDataProvider.get_block( b[1], b[0], force_io=cl, read_params=rp) if loaded is None: raise IOError('One of the files contained in the ' 'selection could not be loaded!') block_list.append(loaded) block_set = set([(b[0], b[1]) for b in data['blocks']]) # Select blocks self.ensure_not_filtered(block_list, self.parent.block_names.keys(), self.parent.get_active_filters('Block')) self.populate_neo_block_list() selection = QItemSelection() for i in self.block_model.findItems( '*', Qt.MatchWrap | Qt.MatchWildcard): block = i.data(Qt.UserRole) t = (NeoDataProvider.block_indices[block], self.parent.block_files[block]) if t in block_set: selection.append(QItemSelectionRange( self.block_model.indexFromItem(i))) self.neoBlockList.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) # Select segments seg_list = [block_list[idx[1]].segments[idx[0]] for idx in data['segments']] all_segs = [] for b in self.blocks(): all_segs.extend(b.segments) self.ensure_not_filtered(seg_list, all_segs, self.parent.get_active_filters('Segment')) self.populate_neo_segment_list() selection = QItemSelection() for i in self.segment_model.findItems( '*', Qt.MatchWrap | Qt.MatchWildcard): segment = i.data(Qt.UserRole) if not segment.block in block_list: continue seg_idx = segment.block.segments.index(segment) block_idx = block_list.index(segment.block) if [seg_idx, block_idx] in data['segments']: selection.append(QItemSelectionRange( self.segment_model.indexFromItem(i))) self.neoSegmentList.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) # Select recording channel groups rcg_list = [block_list[rcg[1]].recordingchannelgroups[rcg[0]] for rcg in data['channel_groups']] all_rcgs = [] for b in self.blocks(): all_rcgs.extend(b.recordingchannelgroups) self.ensure_not_filtered( rcg_list, all_rcgs, self.parent.get_active_filters('Recording Channel Group')) self.populate_neo_channel_group_list() selection = QItemSelection() for i in self.channelgroup_model.findItems( '*', Qt.MatchWrap | Qt.MatchWildcard): rcg = i.data(Qt.UserRole) if not rcg.block in block_list: continue rcg_idx = rcg.block.recordingchannelgroups.index(rcg) block_idx = block_list.index(rcg.block) if [rcg_idx, block_idx] in data['channel_groups']: selection.append(QItemSelectionRange( self.channelgroup_model.indexFromItem(i))) self.neoChannelGroupList.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) # Select channels rc_list = [rcg_list[rc[1]].recordingchannels[rc[0]] for rc in data['channels']] all_rcs = [] for rcg in self.recording_channel_groups(): for rc in rcg.recordingchannels: if not api.config.duplicate_channels and rc in all_rcs: continue all_rcs.append(rc) self.ensure_not_filtered( rc_list, all_rcs, self.parent.get_active_filters('Recording Channel')) self.populate_neo_channel_list() selection = QItemSelection() rcg_set = set(rcg_list) for i in self.channel_model.findItems( '*', Qt.MatchWrap | Qt.MatchWildcard): channel = i.data(Qt.UserRole) if not set(channel.recordingchannelgroups).intersection(rcg_set): continue for rcg in channel.recordingchannelgroups: if [rcg.recordingchannels.index(channel), rcg_list.index(rcg)] in data['channels']: selection.append(QItemSelectionRange( self.channel_model.indexFromItem(i))) break self.neoChannelList.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) # Select units unit_list = [rcg_list[u[1]].units[u[0]] for u in data['units']] all_units = [] for rcg in self.recording_channel_groups(): all_units.extend(rcg.units) self.ensure_not_filtered( unit_list, all_units, self.parent.get_active_filters('Unit')) self.populate_neo_unit_list() selection = QItemSelection() for i in self.unit_model.findItems( '*', Qt.MatchWrap | Qt.MatchWildcard): unit = i.data(Qt.UserRole) if unit.recordingchannelgroup not in rcg_list: continue rcg_idx = rcg_list.index(unit.recordingchannelgroup) unit_idx = unit.recordingchannelgroup.units.index(unit) if [unit_idx, rcg_idx] in data['units']: selection.append(QItemSelectionRange( self.unit_model.indexFromItem(i))) self.neoUnitList.selectionModel().select( selection, QItemSelectionModel.ClearAndSelect) self.parent.refresh_filters()