class GraphicsPixmapWidget(QGraphicsWidget): def __init__(self, pixmap, parent=None): QGraphicsWidget.__init__(self, parent) self.setCacheMode(QGraphicsItem.ItemCoordinateCache) self._pixmap = pixmap self._pixmapSize = QSizeF() self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) def setPixmap(self, pixmap): if self._pixmap != pixmap: self._pixmap = QPixmap(pixmap) self.updateGeometry() def pixmap(self): return QPixmap(self._pixmap) def setPixmapSize(self, size): if self._pixmapSize != size: self._pixmapSize = QSizeF(size) self.updateGeometry() def pixmapSize(self): if self._pixmapSize.isValid(): return QSizeF(self._pixmapSize) else: return QSizeF(self._pixmap.size()) def sizeHint(self, which, constraint=QSizeF()): if which == Qt.PreferredSize: return self.pixmapSize() else: return QGraphicsWidget.sizeHint(self, which, constraint) def paint(self, painter, option, widget=0): if self._pixmap.isNull(): return rect = self.contentsRect() pixsize = self.pixmapSize() pixrect = QRectF(QPointF(0, 0), pixsize) pixrect.moveCenter(rect.center()) painter.save() painter.setPen(QPen(QColor(0, 0, 0, 50), 3)) painter.drawRoundedRect(pixrect, 2, 2) painter.setRenderHint(QPainter.SmoothPixmapTransform) source = QRectF(QPointF(0, 0), QSizeF(self._pixmap.size())) painter.drawPixmap(pixrect, self._pixmap, source) painter.restore()
class GraphicsThumbnailWidget(QGraphicsWidget): def __init__(self, pixmap, title="", parent=None): QGraphicsWidget.__init__(self, parent) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100) def setPixmap(self, pixmap): self.pixmapWidget.setPixmap(pixmap) self._updatePixmapSize() def pixmap(self): return self.pixmapWidget.pixmap() def setTitle(self, title): if self._title != title: self._title = title self.labelWidget.setHtml('<center>' + escape(title) + '</center>') self.layout().invalidate() def title(self): return self._title def setThumbnailSize(self, size): if self._size != size: self._size = QSizeF(size) self._updatePixmapSize() self.labelWidget.setTextWidth(max(100, size.width())) def setTitleWidth(self, width): self.labelWidget.setTextWidth(width) self.layout().invalidate() def paint(self, painter, option, widget=0): contents = self.contentsRect() if self.isSelected(): painter.save() painter.setPen(QPen(QColor(125, 162, 206, 192))) painter.setBrush(QBrush(QColor(217, 232, 252, 192))) painter.drawRoundedRect( QRectF(contents.topLeft(), self.geometry().size()), 3, 3) painter.restore() def _updatePixmapSize(self): pixmap = self.pixmap() if not pixmap.isNull() and self._size.isValid(): pixsize = QSizeF(self.pixmap().size()) pixsize.scale(self._size, Qt.KeepAspectRatio) else: pixsize = QSizeF() self.pixmapWidget.setPixmapSize(pixsize)
class GraphicsThumbnailWidget(QGraphicsWidget): def __init__(self, pixmap, title="", parent=None): QGraphicsWidget.__init__(self, parent) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100) def setPixmap(self, pixmap): self.pixmapWidget.setPixmap(pixmap) self._updatePixmapSize() def pixmap(self): return self.pixmapWidget.pixmap() def setTitle(self, title): if self._title != title: self._title = title self.labelWidget.setHtml( '<center>' + escape(title) + '</center>' ) self.layout().invalidate() def title(self): return self._title def setThumbnailSize(self, size): if self._size != size: self._size = QSizeF(size) self._updatePixmapSize() self.labelWidget.setTextWidth(max(100, size.width())) def setTitleWidth(self, width): self.labelWidget.setTextWidth(width) self.layout().invalidate() def paint(self, painter, option, widget=0): contents = self.contentsRect() if self.isSelected(): painter.save() painter.setPen(QPen(QColor(125, 162, 206, 192))) painter.setBrush(QBrush(QColor(217, 232, 252, 192))) painter.drawRoundedRect(QRectF(contents.topLeft(), self.geometry().size()), 3, 3) painter.restore() def _updatePixmapSize(self): pixmap = self.pixmap() if not pixmap.isNull() and self._size.isValid(): pixsize = QSizeF(self.pixmap().size()) pixsize.scale(self._size, Qt.KeepAspectRatio) else: pixsize = QSizeF() self.pixmapWidget.setPixmapSize(pixsize)