def select_rows(self, identifiers, using_ids=True, change_current=True, scroll=True): ''' Select rows identified by identifiers. identifiers can be a set of ids, row numbers or QModelIndexes. ''' rows = set([x.row() if hasattr(x, 'row') else x for x in identifiers]) if using_ids: rows = set([]) identifiers = set(identifiers) m = self.model() for row in xrange(m.rowCount(QModelIndex())): if m.id(row) in identifiers: rows.add(row) rows = list(sorted(rows)) if rows: row = rows[0] if change_current: self.set_current_row(row, select=False) if scroll: self.scroll_to_row(row) sm = self.selectionModel() sel = QItemSelection() m = self.model() max_col = m.columnCount(QModelIndex()) - 1 # Create a range based selector for each set of contiguous rows # as supplying selectors for each individual row causes very poor # performance if a large number of rows has to be selected. for k, g in itertools.groupby(enumerate(rows), lambda (i,x):i-x): group = list(map(operator.itemgetter(1), g)) sel.merge(QItemSelection(m.index(min(group), 0), m.index(max(group), max_col)), sm.Select)
def select_rows(self, rows): sel = QItemSelection() sm = self.selectionModel() m = self.model() # Create a range based selector for each set of contiguous rows # as supplying selectors for each individual row causes very poor # performance if a large number of rows has to be selected. for k, g in itertools.groupby(enumerate(rows), lambda (i,x):i-x): group = list(map(operator.itemgetter(1), g)) sel.merge(QItemSelection(m.index(min(group), 0), m.index(max(group), 0)), sm.Select)
def on_tableview_selection_changed(self, selected: Qt.QItemSelection, deselected: Qt.QItemSelection) -> None: if len(selected.indexes()) == 0: self.delete_button.setEnabled(False) return index = selected.indexes()[0] scene = self.scening_list[index.row()] qt_silent_call(self.start_frame_control.setValue, scene.start) qt_silent_call(self. end_frame_control.setValue, scene.end) qt_silent_call(self. start_time_control.setValue, Time(scene.start)) qt_silent_call(self. end_time_control.setValue, Time(scene.end)) qt_silent_call(self. label_lineedit.setText, scene.label) self.delete_button.setEnabled(True)
def keyPressEvent(self, ev): if handle_enter_press(self, ev, self.start_view_animation, False): return k = ev.key() if ev.modifiers() & Qt.ShiftModifier and k in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down): ci = self.currentIndex() if not ci.isValid(): return c = ci.row() ncols = self.number_of_columns() or 1 delta = {Qt.Key_Left: -1, Qt.Key_Right: 1, Qt.Key_Up: -ncols, Qt.Key_Down: ncols}[k] n = max(0, min(c + delta, self.model().rowCount(None) - 1)) if n == c: return sm = self.selectionModel() rows = {i.row() for i in sm.selectedIndexes()} if rows: mi, ma = min(rows), max(rows) end = mi if c == ma else ma if c == mi else c else: end = c top = self.model().index(min(n, end), 0) bottom = self.model().index(max(n, end), 0) sm.select(QItemSelection(top, bottom), sm.ClearAndSelect) sm.setCurrentIndex(self.model().index(n, 0), sm.NoUpdate) else: return QListView.keyPressEvent(self, ev)
def handle_mouse_press_event(self, ev): if QApplication.keyboardModifiers() & Qt.ShiftModifier: # Shift-Click in QListView is broken. It selects extra items in # various circumstances, for example, click on some item in the # middle of a row then click on an item in the next row, all items # in the first row will be selected instead of only items after the # middle item. index = self.indexAt(ev.pos()) if not index.isValid(): return ci = self.currentIndex() sm = self.selectionModel() sm.setCurrentIndex(index, sm.NoUpdate) if not ci.isValid(): return if not sm.hasSelection(): sm.select(index, sm.ClearAndSelect) return cr = ci.row() tgt = index.row() top = self.model().index(min(cr, tgt), 0) bottom = self.model().index(max(cr, tgt), 0) sm.select(QItemSelection(top, bottom), sm.Select) else: return QListView.mousePressEvent(self, ev)
def _setSelected(self, count): """ Set selection. Arguments: count (int): Number of selected items. """ index = min(count, self._list.count()) selection = QItemSelection() selection_model = self._list.selectionModel() selection.select(selection_model.model().index(0, 0), selection_model.model().index(index - 1, 0)) selection_model.select(selection, QItemSelectionModel.ClearAndSelect) self._list.scrollToItem(self._list.item(index - 1)) self._list.clearFocus() self._updateComment()
def import_searches(self): path = choose_files(self, 'import_saved_searches', _('Choose file'), filters=[ (_('Saved Searches'), ['json'])], all_files=False, select_only_single_file=True) if path: with open(path[0], 'rb') as f: obj = json.loads(f.read()) needed_keys = {'name', 'find', 'replace', 'case_sensitive', 'dot_all', 'mode'} def err(): error_dialog(self, _('Invalid data'), _( 'The file %s does not contain valid saved searches') % path, show=True) if not isinstance(obj, dict) or 'version' not in obj or 'searches' not in obj or obj['version'] not in (1,): return err() searches = [] for item in obj['searches']: if not isinstance(item, dict) or not set(item.iterkeys()).issuperset(needed_keys): return err searches.append({k:item[k] for k in needed_keys}) if searches: tprefs['saved_searches'] = tprefs['saved_searches'] + searches count = len(searches) self.model.add_searches(count=count) sm = self.searches.selectionModel() top, bottom = self.model.index(self.model.rowCount() - count), self.model.index(self.model.rowCount() - 1) sm.select(QItemSelection(top, bottom), sm.ClearAndSelect) self.searches.scrollTo(bottom)
def selectAll(self): # We re-implement this to ensure that only indexes from column 0 are # selected. The base class implementation selects all columns. This # causes problems with selection syncing, see # https://bugs.launchpad.net/bugs/1236348 m = self.model() sm = self.selectionModel() sel = QItemSelection(m.index(0, 0), m.index(m.rowCount(QModelIndex())-1, 0)) sm.select(sel, sm.ClearAndSelect)
def _updateModelSelection(self, nsel, origin=None): """This is called when some other widget (origin!=self) changes the set of selected model sources""" if origin is self: return self._updating_selection = True ## this is very slow because of setSelected() # for item in self.iterator(): # item.setSelected(item._src.selected) selection = QItemSelection() for item in self.iterator(): if item._src.selected: selection.append( QItemSelectionRange( self.indexFromItem(item, 0), self.indexFromItem(item, self.columnCount() - 1))) self.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect) self.changeGroupingVisibility(None, origin=origin) self._updating_selection = False