def paint(self, painter, option, index): """ Paint the cell :parameters: painter : QtGui.QPainter painter to draw the cell with option : QtGui.QStyleOption style information index : QtCore.QModelIndex index if the cell to draw """ option4 = QtGui.QStyleOptionViewItemV4(option) i_model = index.model() # TODO: cache the data type of the model, if seen before dataType = i_model.dataType(index) self.initStyleOption(option4, index, model=i_model, dataType=dataType) style = self._view.style() if dataType == common.TYPE_IMAGE: # draw background style.drawPrimitive(QtGui.QStyle.PE_PanelItemViewItem, option4, painter, self._view) # paint image self._paintImage(painter, option, index) elif dataType == common.TYPE_BOOLEAN: # draw background style.drawPrimitive(QtGui.QStyle.PE_PanelItemViewItem, option4, painter, self._view) # paint checkbox self._paintBoolean(painter, option4, index) else: # paint everything normally style.drawControl(QtGui.QStyle.CE_ItemViewItem, option4, painter, self._view) # paint border if option4.borderColor: oldPen = painter.pen() painter.setPen(option4.borderColor) if option4.viewItemPosition == option4.OnlyOne: painter.drawRect(option4.rect.adjusted(0, 0, -1, -1)) else: topRight = option4.rect.topRight() topLeft = option4.rect.topLeft() bottomRight = option4.rect.bottomRight() bottomLeft = option4.rect.bottomLeft() drawLine = painter.drawLine # draw rect edges drawLine(topLeft, topRight) drawLine(bottomLeft, bottomRight) if option4.viewItemPosition == option4.Beginning: drawLine(topLeft, bottomLeft) elif option4.viewItemPosition == option4.End: drawLine(topRight, bottomRight) painter.setPen(oldPen)
def sizeHint(self, option, index): """ Get the sizehint for a cell :parameters: option : QtGui.QStyleOption style information index : QtCore.QModelIndex index if the cell to draw :return: The preferred size :rtype: QtCore.QSize """ # TODO: speed this up further by caching more? i_dataType = None i_model = index.model() if i_model: i_dataType = i_model.dataType(index) size = index.data(QtCore.Qt.SizeHintRole) if not size: option4 = QtGui.QStyleOptionViewItemV4(option) self.initStyleOption(option4, index, model=i_model, dataType=i_dataType) style = self._view.style() size = style.sizeFromContents(QtGui.QStyle.CT_ItemViewItem, option4, QtCore.QSize(), self._view) # if it is an image column and has data... if i_model and i_dataType == common.TYPE_IMAGE and index.data( QtCore.Qt.DisplayRole): imageHeight = int( self._view.topHeader().sectionSize(index.column()) / self.__imageCellAspectRatio ) # give the cell a 16/9 aspect ratio if imageHeight > size.height(): size.setHeight(imageHeight) return size if isinstance(size, QtCore.QSize) else size.toSize()