void MirrorItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * /*widget*/) { // TODO: real HQ rendering, skipping the pixmap (better result)! // generate Reflection pixmap, if needed if (m_dirty || m_pixmap.isNull() || RenderOpts::HQRendering) { // change pixmap size to match the bounding rect if (m_pixmap.size() != m_boundingRect.size().toSize()) m_pixmap = QPixmap(m_boundingRect.width(), m_boundingRect.height()); // clear pixmap m_pixmap.fill(Qt::transparent); // find out the Transform chain to mirror a rotated item QRectF sceneRectF = m_source->mapToScene(m_source->boundingRect()).boundingRect(); QTransform tFromItem = m_source->transform() * QTransform(1, 0, 0, 1, m_source->pos().x(), m_source->pos().y()); QTransform tFromPixmap = QTransform(1, 0, 0, -1.0, sceneRectF.left(), sceneRectF.bottom()); QTransform tItemToPixmap = tFromItem * tFromPixmap.inverted(); // draw the transformed item onto the pixmap QPainter p(&m_pixmap); p.setRenderHint(QPainter::Antialiasing, true); p.setTransform(tItemToPixmap, true); m_source->paint(&p, 0, 0); p.end(); // add a linear alpha channel to the image QPixmap alphaPixmap(m_pixmap.size()); QPainter alphaPainter(&alphaPixmap); QLinearGradient alphaGradient(0, 0, 0, alphaPixmap.height()); alphaGradient.setColorAt(0.0 , QColor(128, 128, 128)); alphaGradient.setColorAt(0.50, QColor( 32, 32, 32)); alphaGradient.setColorAt(1.0 , QColor( 0, 0, 0)); alphaPainter.fillRect(alphaPixmap.rect(), alphaGradient); alphaPainter.end(); m_pixmap.setAlphaChannel(alphaPixmap); // reset dirty m_dirty = false; } // draw the reflection pixmap if (!option) painter->drawPixmap(0, 0, m_pixmap); else painter->drawPixmap(option->rect, m_pixmap, option->rect); }
void Carton::paintFaceReflectionTexture(QPainter *painter, Faces face) { if (m_specularityValue <= 0) return; const Faces emittingFace = face == FrontReflection?Front :face == BackReflection?Back :face == LeftReflection?Left :/* face == RightReflection? */Right; const QSizeF faceSize(this->faceSize(face)); const qreal faceWith = faceSize.width(); const qreal faceHeight = faceSize.height(); const qreal reflectionHeight = faceHeight/100 * m_specularityValue; const qreal remainingFaceHeight = faceHeight - reflectionHeight; QImage blendImage(QSizeF(faceWith, reflectionHeight).toSize(), QImage::Format_ARGB32); QPainter blendPainter(&blendImage); blendPainter.translate(0, -remainingFaceHeight); paintFaceTexture(&blendPainter, emittingFace); blendPainter.end(); QImage alphaImage(QSizeF(faceWith, reflectionHeight).toSize(), QImage::Format_ARGB32); QPainter alphaPainter(&alphaImage); QLinearGradient alphaGradient(0, 0, 0, faceHeight); alphaGradient.setColorAt(0, Qt::black); alphaGradient.setColorAt(1, Qt::lightGray); alphaPainter.setPen(Qt::NoPen); alphaPainter.fillRect(QRectF(0, 0, faceWith, reflectionHeight), alphaGradient); blendImage.setAlphaChannel(alphaImage); alphaPainter.end(); painter->save(); painter->drawImage(0, int(remainingFaceHeight), blendImage); painter->restore(); }