def drawIconWithShadow(icon, rect, p, iconMode, radius, color, offset): cache = QPixmap() pixmapName = "icon {0} {1} {2}".format(icon.cacheKey(), iconMode, rect.height()) if not QPixmapCache.find(pixmapName, cache): px = icon.pixmap(rect.size()) cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)) cache.fill(Qt.transparent) cachePainter = QPainter(cache) if iconMode == QIcon.Disabled: im = px.toImage().convertToFormat(QImage.Format_ARGB32) for y in range(im.height()): scanLine = im.scanLine(y) for x in range(im.width()): pixel = scanLine intensity = qGray(pixel) scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel)) scanLine += 1 px = QPixmap.fromImage(im) # Draw shadow tmp = QImage(px.size() + QSize(radius * 2, radius * 2 + 1), QImage.Format_ARGB32_Premultiplied) tmp.fill(Qt.transparent) tmpPainter = QPainter(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_Source) tmpPainter.drawPixmap(QPoint(radius, radius), px) tmpPainter.end() # blur the alpha channel blurred = QImage(tmp.size(), QImage.Format_ARGB32_Premultiplied) blurred.fill(Qt.transparent) blurPainter = QPainter(blurred) # todo : blur image blurPainter.end() tmp = blurred # blacken the image tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() # draw the blurred drop shadow... cachePainter.drawImage(QRect(0, 0, cache.rect().width(), cache.rect().height()), tmp) # Draw the actual pixmap... cachePainter.drawPixmap(QPoint(radius, radius) + offset, px) QPixmapCache.insert(pixmapName, cache) targetRect = cache.rect() targetRect.moveCenter(rect.center()) p.drawPixmap(targetRect.topLeft() - offset, cache)
def testWithKey(self): pm1 = QPixmap() ok = QPixmapCache.find(QPixmapCache.Key(), pm1) self.assertFalse(ok) self.assertEqual(QPixmapCache.find(QPixmapCache.Key()), None) pm2 = QPixmap() key = QPixmapCache.insert(pm2) pm3 = QPixmap() ok = QPixmapCache.find(key, pm3) self.assertTrue(ok) self.assertEqual(QPixmapCache.find(key).toImage().bits(), pm3.toImage().bits())
def paint(self, painter, option, widget=None): """@reimp @public virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) """ #Q_UNUSED(option) #Q_UNUSED(widget) d = self.__d key = d.hash pm = QPixmap() if not QPixmapCache.find(key, pm): # Set up a convenient path path = QPainterPath() path.setFillRule(Qt.OddEvenFill) path.addEllipse(QPointF(d.actualOuterRadius, d.actualOuterRadius), d.actualOuterRadius, d.actualOuterRadius) path.addEllipse(QPointF(d.actualOuterRadius, d.actualOuterRadius), d.actualInnerRadius, d.actualInnerRadius) nActualDiameter = 2.0 * d.actualOuterRadius pm = QPixmap(nActualDiameter, nActualDiameter) pm.fill(Qt.transparent) p = QPainter(pm) # Draw the ring background p.setPen(Qt.NoPen) p.setBrush(d.backgroundColor) p.setRenderHint(QPainter.Antialiasing) p.drawPath(path) # Draw the ring foreground # TODO: Expose this gradient as Qml Property gradient = QConicalGradient(d.actualOuterRadius, d.actualOuterRadius, 0.0) gradient.setColorAt(0.0, Qt.transparent) gradient.setColorAt(0.05, d.foregroundColor) gradient.setColorAt(0.8, Qt.transparent) p.setBrush(gradient) p.drawPath(path) p.end() QPixmapCache.insert(key, pm) # Draw pixmap at center of item w, h = self.width(), self.height() sz = min(w, h) painter.drawPixmap(0.5 * (w - sz), 0.5 * (h - sz), pm)
def verticalGradient(painter, spanRect, clipRect, lightColored): if StyleHelper.usePixmapCache(): keyColor = StyleHelper.baseColor(lightColored) key = "mh_vertical {0} {1} {2} {3} {4}".format( spanRect.width(), spanRect.height(), clipRect.width(), clipRect.height(), keyColor.rgb() ) pixmap = QPixmap() if not QPixmapCache.find(key, pixmap): pixmap = QPixmap(clipRect.size()) p = QPainter(pixmap) rect = QRect(0, 0, clipRect.width(), clipRect.height()) StyleHelper.verticalGradientHelper(p, spanRect, rect, lightColored) p.end() QPixmapCache.insert(key, pixmap) painter.drawPixmap(clipRect.topLeft(), pixmap) else: StyleHelper.verticalGradientHelper(painter, spanRect, clipRect, lightColored)
def pixmap_cache ( name ): """ Return the QPixmap corresponding to a filename. If the filename does not contain a path component then the local 'images' directory is used. """ if name[:1] == '@': image = image_for( name ) if image is not None: return image.bitmap path, _ = os.path.split( name ) if not path: name = os.path.join( os.path.dirname( __file__ ), 'images', name ) pm = QPixmap() if not QPixmapCache.find( name, pm ): pm.load( name ) QPixmapCache.insert( name, pm ) return pm
def menuGradient(painter, spanRect, clipRect): if StyleHelper.usePixmapCache(): key = "mh_menu {0} {1} {2} {3} {4}".format( spanRect.width(), spanRect.height(), clipRect.width(), clipRect.height(), StyleHelper.baseColor().rgb() ) pixmap = QPixmap() if not QPixmapCache.find(key, pixmap): pixmap = QPixmap(clipRect.size()) p = QPainter(pixmap) rect = QRect(0, 0, clipRect.width(), clipRect.height()) StyleHelper.menuGradientHelper(p, spanRect, rect) painter.drawPixmap(clipRect.topLeft(), pixmap) else: StyleHelper.menuGradientHelper(painter, spanRect, clipRect)
def testWithKey(self): pm1 = QPixmap() ok = QPixmapCache.find(QPixmapCache.Key(), pm1) self.assertFalse(ok) self.assertEqual(QPixmapCache.find(QPixmapCache.Key()), None) pm2 = QPixmap() key = QPixmapCache.insert(pm2) pm3 = QPixmap() ok = QPixmapCache.find(key, pm3) self.assertTrue(ok) self.assertEqual( QPixmapCache.find(key).toImage().bits(), pm3.toImage().bits())
def testWithString(self): pm1 = QPixmap() ok = QPixmapCache.find('img', pm1) self.assertFalse(ok) self.assertEqual(QPixmapCache.find('img'), None) pm2 = QPixmap() ok = QPixmapCache.insert('img', pm2) self.assertTrue(ok) pm3 = QPixmap() ok = QPixmapCache.find('img', pm3) self.assertTrue(ok) b1 = QPixmapCache.find('img').toImage().bits() b2 = pm3.toImage().bits() self.assertEqual(QPixmapCache.find('img').toImage().bits(), pm3.toImage().bits())
def testWithString(self): pm1 = QPixmap() ok = QPixmapCache.find('img', pm1) self.assertFalse(ok) self.assertEqual(QPixmapCache.find('img'), None) pm2 = QPixmap() ok = QPixmapCache.insert('img', pm2) self.assertTrue(ok) pm3 = QPixmap() ok = QPixmapCache.find('img', pm3) self.assertTrue(ok) b1 = QPixmapCache.find('img').toImage().bits() b2 = pm3.toImage().bits() self.assertEqual( QPixmapCache.find('img').toImage().bits(), pm3.toImage().bits())
def drawArrow(element, painter, option): # From windows style but modified to enable AA if option.rect.width() <= 1 or option.rect.height() <= 1: return r = option.rect size = min(r.height(), r.width()) pixmap = QPixmap() pixmapName = "arrow-{0}-{1}-{2}-{3}-{4}".format( "$qt_ia", int(option.state), element, size, option.palette.cacheKey() ) if not QPixmapCache.find(pixmapName, pixmap): border = size / 5 sqsize = 2 * (size / 2) image = QImage(sqsize, sqsize, QImage.Format_ARGB32) image.fill(Qt.transparent) imagePainter = QPainter(image) imagePainter.setRenderHint(QPainter.Antialiasing, True) imagePainter.translate(0.5, 0.5) a = QPolygon() if element == QStyle.PE_IndicatorArrowUp: a.setPoints(3, border, sqsize / 2, sqsize / 2, border, sqsize - border, sqsize / 2) elif element == QStyle.PE_IndicatorArrowDown: a.setPoints(3, border, sqsize / 2, sqsize / 2, sqsize - border, sqsize - border, sqsize / 2) elif element == QStyle.PE_IndicatorArrowRight: a.setPoints(3, sqsize - border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border) elif element == QStyle.PE_IndicatorArrowLeft: a.setPoints(3, border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border) else: pass bsx = 0 bsy = 0 if option.state & QStyle.State_Sunken: bsx = qApp.style().pixelMetric(QStyle.PM_ButtonShiftHorizontal) bsy = qApp.style().pixelMetric(QStyle.PM_ButtonShiftVertical) bounds = a.boundingRect() sx = sqsize / 2 - bounds.center().x() - 1 sy = sqsize / 2 - bounds.center().y() - 1 imagePainter.translate(sx + bsx, sy + bsy) if not (option.state & QStyle.State_Enabled): imagePainter.setBrush(option.palette.mid().color()) imagePainter.setPen(option.palette.mid().color()) else: shadow = QColor(0, 0, 0, 100) imagePainter.translate(0, 1) imagePainter.setPen(shadow) imagePainter.setBrush(shadow) foreGround = QColor(255, 255, 255, 210) imagePainter.drawPolygon(a) imagePainter.translate(0, -1) imagePainter.setPen(foreGround) imagePainter.setBrush(foreGround) imagePainter.drawPolygon(a) imagePainter.end() pixmap = QPixmap.fromImage(image) QPixmapCache.insert(pixmapName, pixmap) xOffset = r.x() + (r.width() - size) / 2 yOffset = r.y() + (r.height() - size) / 2 painter.drawPixmap(xOffset, yOffset, pixmap)