def selectAll(self): """ """ if not isinstance(self.selectionModel(), TreeSelectionModel): return QtGui.QTreeView.selectAll(self) # store parameters selectionModel = self.selectionModel() model = self.model() getIndex = model.index columnCount = model.columnCount(QtCore.QModelIndex()) selection = QtGui.QItemSelection() propagate = selectionModel.propagateSelection() oldSelection = selectionModel.selection() def selectChildRange(parentIndex): rowCount = model.rowCount(parentIndex) firstIndex = getIndex(0, 0, parentIndex) lastIndex = getIndex(rowCount - 1, columnCount - 1, parentIndex) selection.select(firstIndex, lastIndex) def recursiveSelect(parentIndex): selectChildRange(parentIndex) if propagate is True: # if we skip this check it will always select child rows. for row in range(model.rowCount(parentIndex)): index = getIndex(row, 0, parentIndex) if index.isValid(): recursiveSelect(index) # prepare block = selectionModel.blockSignals(True) self.setUpdatesEnabled(False) if propagate is True: selectionModel.setPropagateSelection(False) # do selection if self.selectionMode() == QtGui.QAbstractItemView.SingleSelection: selection.select( model.index(0, 0, QtCore.QModelIndex()), model.index(0, columnCount - 1, QtCore.QModelIndex())) else: recursiveSelect(QtCore.QModelIndex()) selectionModel.select(selection, QtGui.QItemSelectionModel.Select) # restore previous settings self.setUpdatesEnabled(True) selectionModel.setPropagateSelection(propagate) selectionModel.blockSignals(block) # refresh view QtGui.QApplication.processEvents() selectionModel.selectionChanged.emit(selection, oldSelection)
def __imageLoaded(self, filePath, targetSize, image, extraArgs): """ Repaint the rect containing the image :parameters: filePath : str path to image file targetSize : QtCore.QSize intended size of the image image : QtGui.QImage the image that has been loaded extraArgs : list Index of model item containing the image [ QtCore.QPersistandModelIndex ] """ index = QtCore.QModelIndex(extraArgs[0]) model = index.model() if model is None: return # On OSX 10.9 PyQt 4.10 replaces null QImages with QPyNullVariants when retreiving them from a model if not isinstance(image, QtGui.QImage): image = QtGui.QImage() model.setItemData(index, { common.ROLE_IMAGE: image, self.ROLE_SCALED_IMAGE: image }) if image.isNull(): return if index.isValid(): rect = self._view.visualRect(index) if rect.isValid(): self._view.viewport().repaint(rect)
def applyExpandOnLoad(self): """ """ if self.__expandOnLoad: model = self.model() root = QtCore.QModelIndex() getIndex = lambda row: model.index(row, 0, root) rowCount = model.rowCount(root) for row in xrange(0, rowCount + 1): self._expandOneBranch(getIndex(row), self.__expandOnLoad)
def stopImageCycling(self): """ Stop frame cycling on a cell at <index> :parameters: index : QtCore.QModelIndex the model index to stop frame cycling on """ cycleIndex = QtCore.QModelIndex(self.__cycleIndex) indexWidget = self._view.indexWidget(cycleIndex) if indexWidget is not None: # index widget could be set to something different in subclasses, so we can't be sure movie = indexWidget.movie() if hasattr(indexWidget, 'movie') else None if movie is not None: movie.stop() indexWidget.close() self._view.setIndexWidget(cycleIndex, None) self.__cycleIndex = QtCore.QPersistentModelIndex( QtCore.QModelIndex())
def __init__(self, view): """ Initialiser :parameters: view : QtGui.QAbstractItemView The view that this delegate acts upon """ super(Delegate, self).__init__(view) self._view = view self.__viewStyle = None self.__shadedSortColumn = False self.__imageCellAspectRatio = 16.0 / 9 self.__imageLoader = ImageLoader() self.__imageLoader.imageLoaded.connect(self.__imageLoaded) # image cycling self.__cycleIndex = QtCore.QPersistentModelIndex(QtCore.QModelIndex()) self.__imageCyclingEnabled = True
def expandToSelected(self): """ """ model = self.model() propagateSelection = self.selectionModel().propagateSelection() # remove duplicates def removeIndexDuplicates(indexList): return filter( QtCore.QModelIndex.isValid, map(model.indexFromItem, set(map(model.itemFromIndex, indexList)))) # index has no parent def isTopLevel(index): return not index.parent().isValid() # get selected indexes indexes = removeIndexDuplicates( filter(QtCore.QModelIndex.isValid, self.selectionModel().selectedIndexes())) # if no selection, expand one level if not indexes: self._expandOneBranch(model.index(0, 0, QtCore.QModelIndex()), 1) return block = self.blockSignals(True) # if the top level is selected, it's quicker to just expand all if propagateSelection is True and filter(isTopLevel, indexes): self.__expandAllIsExpanded = False self.expandAll() self.blockSignals(block) return # expand back up the hierarchy to reveal the selected index for index in indexes: while index.isValid() and not self.isExpanded(index): self.setExpanded(index, True) index = index.parent() self.blockSignals(block)