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 _paintImage(self, painter, option, index): """ Draw an image in the cell :parameters: painter : QtGui.QPainter painter to draw the image with option : QtGui.QStyleOption style information index : QtCore.QModelIndex index if the cell to draw """ # using old method for now imageFile = index.data() if not imageFile: return model = index.model() # only load an image where one hasn't already been loaded origImage = image = index.data(role=common.ROLE_IMAGE) origScaledImage = scaledImage = index.data(role=self.ROLE_SCALED_IMAGE) origFailedImage = failedImage = index.data(role=self.ROLE_FAILED_IMAGE) # On OSX 10.9 PyQt 4.10 replaces null QImages with QPyNullVariants when retreiving them from a model if image is not None and not isinstance(image, QtGui.QImage): image = QtGui.QImage() if scaledImage is not None and not isinstance(scaledImage, QtGui.QImage): scaledImage = QtGui.QImage() if not failedImage: if image is None: # if it's a null image or a loading image, load it. scaledImage = image = self.__imageLoader.loadImage( imageFile, None, QtCore.QPersistentModelIndex(index)) # if it's not a null image or a loading image... if not image.isNull(): if scaledImage is None: scaledImage = image scaledImageSize = scaledImage.size() targetSize = option.rect.size().boundedTo(image.size()) targetSize.setHeight( int(targetSize.width() / self.__imageCellAspectRatio)) if scaledImageSize.width() != targetSize.width( ) and scaledImageSize.height() != targetSize.height(): scaledImage = image.scaled(targetSize, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation) else: failedImage = True # if <image> is null, set scaled image to null as well # this would make null images show up as failed, we could maybe have a more neutral icon? # scaledImage = self.__imageLoader._imageMissingLrg # update model data dataUpdateMap = {} if origImage is not image: dataUpdateMap[common.ROLE_IMAGE] = image if origScaledImage is not scaledImage: dataUpdateMap[self.ROLE_SCALED_IMAGE] = scaledImage if origFailedImage != failedImage: dataUpdateMap[self.ROLE_FAILED_IMAGE] = failedImage if dataUpdateMap: model.setItemData(index, dataUpdateMap) # center the image in the cell targetRect = scaledImage.rect() targetRect.moveCenter(option.rect.center()) # draw the image painter.save() painter.drawImage(targetRect, scaledImage) # Image cycling icon ( test ) # if common.imageFormatIsAnimatable( imageFile ) and os.path.isfile( imageFile ): # animIcon = QtGui.QImage( common.ICON_CLAPBOARD_SML ) # animIconSize = animIcon.size() # brPoint = targetRect.bottomRight() - QtCore.QPoint( animIconSize.width(), animIconSize.height() ) # painter.drawImage( brPoint, animIcon ) # TODO proper icon painter.restore()