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 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 _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
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
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)