void HudWindow::resizeEvent(QResizeEvent* event) { Q_UNUSED(event); if (desktopCompositingEnabled) { // Pre-draw the window drop shadow. It only needs to be drawn upon // resize. We do this because applying a blur effect to the drop // shadow is computationally expensive. // First, draw the shadow, using a pleasant gradient. QLinearGradient shadowGradient; shadowGradient.setStart(rect().width() / 2, 0.0); shadowGradient.setFinalStop(rect().width() / 2, rect().height()); shadowGradient.setColorAt(0.0, QColor(0, 0, 0, 20)); shadowGradient.setColorAt(1.0, QColor(0, 0, 0, 200)); QImage unblurredImage ( rect().width(), rect().height(), QImage::Format_ARGB32_Premultiplied ); unblurredImage.fill(Qt::transparent); QPainter painter(&unblurredImage); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(QPen(Qt::NoPen)); painter.setBrush(QBrush(shadowGradient)); painter.drawRoundedRect(rect().adjusted(10, 10, -10, -8), 5, 5); painter.end(); // Now we need to blur the shadow onto its final destination image, // dropShadowImg, which will be drawn on the next paintEvent(). // dropShadowImg = QImage ( rect().width(), rect().height(), QImage::Format_ARGB32_Premultiplied ); dropShadowImg.fill(Qt::transparent); painter.begin(&dropShadowImg); painter.setRenderHint(QPainter::Antialiasing); // Note that the blur only applies to the alpha channel. qt_blurImage(&painter, unblurredImage, 20, true, true); painter.end(); } }
QT_END_NAMESPACE void tst_QPixmapFilter::blurIndexed8() { QImage img(16, 32, QImage::Format_Indexed8); img.setColorCount(256); for (int i = 0; i < 256; ++i) img.setColor(i, qRgb(i, i, i)); img.fill(255); QImage original = img; qt_blurImage(img, 10, true, false); QCOMPARE(original.size(), img.size()); original = img; qt_blurImage(img, 10, true, true); QCOMPARE(original.size(), QSize(img.height(), img.width())); }
void Shadow::draw(QPainter* painter) { // if nothing to show outside the item, just draw source if ((blurRadius() + distance()) <= 0) { drawSource(painter); return; } PixmapPadMode mode = QGraphicsEffect::PadToEffectiveBoundingRect; QPoint offset; const QPixmap px = sourcePixmap(Qt::DeviceCoordinates, &offset, mode); // return if no source if (px.isNull()) return; // save world transform QTransform restoreTransform = painter->worldTransform(); painter->setWorldTransform(QTransform()); // Calculate size for the background image QSize szi(px.size().width() + 2 * distance(), px.size().height() + 2 * distance()); QImage tmp(szi, QImage::Format_ARGB32_Premultiplied); QPixmap scaled = px.scaled(szi); tmp.fill(0); QPainter tmpPainter(&tmp); tmpPainter.setCompositionMode(QPainter::CompositionMode_Source); tmpPainter.drawPixmap(QPointF(-distance(), -distance()), scaled); tmpPainter.end(); // blur the alpha channel QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(0); QPainter blurPainter(&blurred); qt_blurImage(&blurPainter, tmp, blurRadius(), false, true); blurPainter.end(); tmp = blurred; // blacken the image... tmpPainter.begin(&tmp); tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); tmpPainter.fillRect(tmp.rect(), color()); tmpPainter.end(); // draw the blurred shadow... painter->drawImage(offset, tmp); // draw the actual pixmap... painter->drawPixmap(offset, px, QRectF()); // restore world transform painter->setWorldTransform(restoreTransform); }
QPixmap NodeInstance::blurredRenderPixmap() const { if (d->blurredRenderPixmap.isNull()) { d->blurredRenderPixmap = QPixmap(d->renderPixmap.size()); QPainter blurPainter(&d->blurredRenderPixmap); QImage renderImage = d->renderPixmap.toImage(); qt_blurImage(&blurPainter, renderImage, 8.0, false, false); } return d->blurredRenderPixmap; }
// addDropShadow is inspired by QPixmapDropShadowFilter::draw in // qt/src/gui/effects/qpixmapfilter.cpp QPixmap addDropShadow( const QPixmap& source, const QSize& targetSize ) { const QPoint offset( 2, 4 ); const int radius = 4; const QColor shadowColor( 100, 100, 100, 100 ); // If there is no targetSize, then return a larger pixmap with the shadow added on // otherwise, return a bounded pixmap and shrink the source const QSize sizeToUse = targetSize.isEmpty() ? QSize( source.width() + offset.x() + radius, source.height() + offset.y() + radius ) : targetSize; const QSize shrunkToFit( sizeToUse.width() - offset.x() - radius, sizeToUse.height() - offset.y() - radius ); const QPixmap shrunk = source.scaled( shrunkToFit, Qt::KeepAspectRatio, Qt::SmoothTransformation ); QImage tmp( sizeToUse, QImage::Format_ARGB32_Premultiplied ); tmp.fill( 0 ); QPainter tmpPainter( &tmp ); tmpPainter.setCompositionMode( QPainter::CompositionMode_Source ); tmpPainter.drawPixmap( offset, shrunk ); tmpPainter.end(); // blur the alpha channel QImage blurred( sizeToUse, QImage::Format_ARGB32_Premultiplied ); blurred.fill( 0 ); QPainter blurPainter( &blurred ); qt_blurImage( &blurPainter, tmp, radius, false, true ); blurPainter.end(); // blacken the image... QPainter blackenPainter( &blurred ); blackenPainter.setCompositionMode( QPainter::CompositionMode_SourceIn ); blackenPainter.fillRect( blurred.rect(), shadowColor ); blackenPainter.end(); const QRect resultRect( shrunk.rect().united( shrunk.rect().translated( offset ).adjusted( -radius, -radius, radius, radius ) ) ); QPixmap result( resultRect.size() ); result.fill( Qt::transparent ); QPainter resultPainter( &result ); // draw the blurred drop shadow... resultPainter.drawImage( 0, 0, blurred ); // Draw the actual pixmap... resultPainter.drawPixmap( 0, 0, shrunk ); return result; }
void WidgetFadeHelper::CaptureParent() { // Take a "screenshot" of the window original_pixmap_ = QPixmap::grabWidget(parent_); QImage original_image = original_pixmap_.toImage(); // Blur it QImage blurred(original_image.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(Qt::transparent); QPainter blur_painter(&blurred); blur_painter.save(); qt_blurImage(&blur_painter, original_image, 10.0, true, false); blur_painter.restore(); // Draw some loading text over the top QFont loading_font(font()); loading_font.setBold(true); QFontMetrics loading_font_metrics(loading_font); const QString loading_text = tr("Loading..."); const QSize loading_size( kLoadingPadding * 2 + loading_font_metrics.width(loading_text), kLoadingPadding * 2 + loading_font_metrics.height()); const QRect loading_rect((blurred.width() - loading_size.width()) / 2, 100, loading_size.width(), loading_size.height()); blur_painter.setRenderHint(QPainter::Antialiasing); blur_painter.setRenderHint(QPainter::HighQualityAntialiasing); blur_painter.translate(0.5, 0.5); blur_painter.setPen(QColor(200, 200, 200, 255)); blur_painter.setBrush(QColor(200, 200, 200, 192)); blur_painter.drawRoundedRect(loading_rect, kLoadingBorderRadius, kLoadingBorderRadius); blur_painter.setPen(palette().brush(QPalette::Text).color()); blur_painter.setFont(loading_font); blur_painter.drawText(loading_rect.translated(-1, -1), Qt::AlignCenter, loading_text); blur_painter.translate(-0.5, -0.5); blur_painter.end(); blurred_pixmap_ = QPixmap::fromImage(blurred); resize(parent_->size()); }
QT_END_NAMESPACE void ShadowEffect::draw(QPainter* painter){ if((blurRadius() + distance()) <= 0){ drawSource(painter); return; } PixmapPadMode mode = QGraphicsEffect::PadToEffectiveBoundingRect; QPoint offset; const QPixmap px = sourcePixmap(Qt::DeviceCoordinates, &offset, mode); if(px.isNull()) return; QTransform restoreTransform = painter->worldTransform(); painter->setWorldTransform(QTransform()); QSize szi(px.size().width() + 2 * distance(), px.size().height() + 2 * distance()); QImage tmp(szi, QImage::Format_ARGB32_Premultiplied); QPixmap scaled = px.scaled(szi); tmp.fill(0); QPainter tmpPainter(&tmp); tmpPainter.setCompositionMode(QPainter::CompositionMode_Source); tmpPainter.drawPixmap(QPointF(-distance(), -distance()), scaled); tmpPainter.end(); QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(0); QPainter blurPainter(&blurred); qt_blurImage(&blurPainter, tmp, blurRadius(), false, true); blurPainter.end(); tmp = blurred; tmpPainter.begin(&tmp); tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); tmpPainter.fillRect(tmp.rect(), color()); tmpPainter.end(); painter->drawImage(offset, tmp); painter->drawPixmap(offset, px, QRectF()); painter->setWorldTransform(restoreTransform); }
void ContextWidget::updateImage(QImage img, bool created) { DBUG << img.isNull() << currentBackdrop.isNull(); oldBackdrop=currentBackdrop; oldIsAlbumCoverBackdrop=albumCoverBackdrop; currentBackdrop=QPixmap(); animator.stop(); if (img.isNull() && oldBackdrop.isNull()) { return; } if (img.isNull()) { currentImage=img; } else { if (backdropOpacity<100) { img=TreeView::setOpacity(img, (backdropOpacity*1.0)/100.0); } if (backdropBlur>0) { QImage blurred(img.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(Qt::transparent); QPainter painter(&blurred); qt_blurImage(&painter, img, backdropBlur, true, false); painter.end(); img = blurred; } #ifdef SCALE_CONTEXT_BGND currentImage=img; #else currentBackdrop=QPixmap::fromImage(img); #endif } albumCoverBackdrop=created; resizeBackdrop(); animator.stop(); if (PlayQueueView::BI_Custom==backdropType || !isVisible()) { setFade(1.0); } else { fadeValue=0.0; animator.setDuration(250); animator.setEndValue(1.0); animator.start(); } }
void Context2D::endPainting() { if (m_state.shadowBlur > 0) { QImage alphaChannel = m_shadowbuffer.alphaChannel(); qt_blurImage(alphaChannel, m_state.shadowBlur, false, 1); QRect imageRect = m_shadowbuffer.rect(); if (m_shadowColorIndexBuffer.isEmpty() || m_shadowColorBuffer != m_state.shadowColor) { m_shadowColorIndexBuffer.clear(); m_shadowColorBuffer = m_state.shadowColor; for (int i = 0; i < 256; ++i) { m_shadowColorIndexBuffer << qRgba(qRound(255 * m_state.shadowColor.redF()), qRound(255 * m_state.shadowColor.greenF()), qRound(255 * m_state.shadowColor.blueF()), i); } } alphaChannel.setColorTable(m_shadowColorIndexBuffer); if (m_painter.isActive()) m_painter.end(); m_painter.begin(&m_pixmap); // draw the blurred drop shadow... m_painter.save(); QTransform tf = m_painter.transform(); m_painter.translate(0, imageRect.height()); m_painter.rotate(-90); m_painter.drawImage(0, 0, alphaChannel); m_painter.setTransform(tf); m_painter.restore(); // draw source m_painter.drawImage(-m_state.shadowOffsetX, -m_state.shadowOffsetY, m_shadowbuffer.copy()); m_painter.end(); } }
void SearchTermWidget::Overlay::Grab() { hide(); // Take a "screenshot" of the window QPixmap pixmap = QPixmap::grabWidget(parent_); QImage image = pixmap.toImage(); // Blur it QImage blurred(image.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(Qt::transparent); QPainter blur_painter(&blurred); qt_blurImage(&blur_painter, image, 10.0, true, false); blur_painter.end(); pixmap_ = QPixmap::fromImage(blurred); resize(parent_->size()); show(); update(); }
void PlaylistView::set_background_image(const QImage& image) { // Save previous image, for fading previous_background_image_ = cached_scaled_background_image_; if (image.isNull() || image.format() == QImage::Format_ARGB32) { background_image_ = image; } else { background_image_ = image.convertToFormat(QImage::Format_ARGB32); } if (!background_image_.isNull()) { // Apply opacity filter uchar* bits = background_image_.bits(); for (int i = 0; i < background_image_.height() * background_image_.bytesPerLine(); i += 4) { bits[i + 3] = (opacity_level_ / 100.0) * 255; } if (blur_radius_ != 0) { QImage blurred(background_image_.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(Qt::transparent); QPainter blur_painter(&blurred); qt_blurImage(&blur_painter, background_image_, blur_radius_, true, false); blur_painter.end(); background_image_ = blurred; } } if (isVisible()) { previous_background_image_opacity_ = 1.0; fade_animation_->start(); } }
// Draws a cached pixmap with shadow void StyleHelper::drawIconWithShadow(const QIcon &icon, const QRect &rect, QPainter *p, QIcon::Mode iconMode, int radius, const QColor &color, const QPoint &offset) { QPixmap cache; QString pixmapName = QString("icon %0 %1 %2").arg(icon.cacheKey()).arg(iconMode).arg(rect.height()); if (!QPixmapCache::find(pixmapName, cache)) { QPixmap px = icon.pixmap(rect.size()); cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)); cache.fill(Qt::transparent); QPainter cachePainter(&cache); if (iconMode == QIcon::Disabled) { QImage im = px.toImage().convertToFormat(QImage::Format_ARGB32); for (int y=0; y<im.height(); ++y) { QRgb *scanLine = (QRgb*)im.scanLine(y); for (int x=0; x<im.width(); ++x) { QRgb pixel = *scanLine; char intensity = qGray(pixel); *scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel)); ++scanLine; } } px = QPixmap::fromImage(im); } // Draw shadow QImage tmp(px.size() + QSize(radius * 2, radius * 2 + 1), QImage::Format_ARGB32_Premultiplied); tmp.fill(Qt::transparent); QPainter tmpPainter(&tmp); tmpPainter.setCompositionMode(QPainter::CompositionMode_Source); tmpPainter.drawPixmap(QPoint(radius, radius), px); tmpPainter.end(); // blur the alpha channel QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(Qt::transparent); QPainter blurPainter(&blurred); qt_blurImage(&blurPainter, tmp, radius, false, true); 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); } QRect targetRect = cache.rect(); targetRect.moveCenter(rect.center()); p->drawPixmap(targetRect.topLeft() - offset, cache); }
void TrackSliderPopup::UpdatePixmap() { const int text_width = qMax(font_metrics_.width(text_), small_font_metrics_.width(small_text_)); const QRect text_rect1(kBlurRadius + kTextMargin, kBlurRadius + kTextMargin, text_width + 2, font_metrics_.height()); const QRect text_rect2(kBlurRadius + kTextMargin, text_rect1.bottom(), text_width, small_font_metrics_.height()); const int bubble_bottom = text_rect2.bottom() + kTextMargin; const QRect total_rect(0, 0, text_rect1.right() + kBlurRadius + kTextMargin, kBlurRadius + bubble_bottom + kPointLength); const QRect bubble_rect(kBlurRadius, kBlurRadius, total_rect.width() - kBlurRadius * 2, bubble_bottom - kBlurRadius); if (background_cache_.size() != total_rect.size()) { const QColor highlight( palette().color(QPalette::Active, QPalette::Highlight)); const QColor bg_color_1(highlight.lighter(110)); const QColor bg_color_2(highlight.darker(120)); QPolygon pointy; pointy << QPoint(total_rect.width() / 2 - kPointWidth, bubble_bottom) << QPoint(total_rect.width() / 2, total_rect.bottom() - kBlurRadius) << QPoint(total_rect.width() / 2 + kPointWidth, bubble_bottom); QPolygon inner_pointy; inner_pointy << QPoint(pointy[0].x() + 1, pointy[0].y() - 1) << QPoint(pointy[1].x(), pointy[1].y() - 1) << QPoint(pointy[2].x() - 1, pointy[2].y() - 1); background_cache_ = QPixmap(total_rect.size()); background_cache_.fill(Qt::transparent); QPainter p(&background_cache_); p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::HighQualityAntialiasing); // Draw the shadow to a different image QImage blur_source(total_rect.size(), QImage::Format_ARGB32); blur_source.fill(Qt::transparent); QPainter blur_painter(&blur_source); blur_painter.setRenderHint(QPainter::Antialiasing); blur_painter.setRenderHint(QPainter::HighQualityAntialiasing); blur_painter.setBrush(bg_color_2); blur_painter.drawRoundedRect(bubble_rect, kBorderRadius, kBorderRadius); blur_painter.drawPolygon(pointy); // Fade the shadow out towards the bottom QLinearGradient fade_gradient(QPoint(0, bubble_bottom), QPoint(0, bubble_bottom + kPointLength)); fade_gradient.setColorAt(0.0, QColor(255, 0, 0, 0)); fade_gradient.setColorAt(1.0, QColor(255, 0, 0, 255)); blur_painter.setCompositionMode(QPainter::CompositionMode_DestinationOut); blur_painter.fillRect(total_rect, fade_gradient); blur_painter.end(); p.save(); qt_blurImage(&p, blur_source, kBlurRadius, true, false); p.restore(); // Outer bubble p.setPen(Qt::NoPen); p.setBrush(bg_color_2); p.drawRoundedRect(bubble_rect, kBorderRadius, kBorderRadius); // Outer pointy p.drawPolygon(pointy); // Inner bubble p.setBrush(bg_color_1); p.drawRoundedRect(bubble_rect.adjusted(1, 1, -1, -1), kBorderRadius, kBorderRadius); // Inner pointy p.drawPolygon(inner_pointy); } pixmap_ = QPixmap(total_rect.size()); pixmap_.fill(Qt::transparent); QPainter p(&pixmap_); p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::HighQualityAntialiasing); // Background p.drawPixmap(total_rect.topLeft(), background_cache_); // Text p.setPen(palette().color(QPalette::HighlightedText)); p.setFont(font_); p.drawText(text_rect1, Qt::AlignHCenter, text_); p.setFont(small_font_); p.setOpacity(0.65); p.drawText(text_rect2, Qt::AlignHCenter, small_text_); p.end(); resize(pixmap_.size()); UpdatePosition(); update(); }
// Draws a cached pixmap with shadow void FancyTabBar::drawIconWithShadow(const QIcon &icon, const QRect &rect, QPainter *p, QIcon::Mode iconMode, int dipRadius, const QColor &color, const QPoint &dipOffset) { QPixmap cache; QString pixmapName = QString::fromLatin1("icon %0 %1 %2").arg(icon.cacheKey()).arg(iconMode).arg(rect.height()); if (!QPixmapCache::find(pixmapName, cache)) { // High-dpi support: The in parameters (rect, radius, offset) are in // device-independent pixels. The call to QIcon::pixmap() below might // return a high-dpi pixmap, which will in that case have a devicePixelRatio // different than 1. The shadow drawing caluculations are done in device // pixels. QPixmap px = icon.pixmap(rect.size()); //jassuncao int devicePixelRatio = qCeil(px.devicePixelRatio()); int devicePixelRatio = 1; int radius = dipRadius * devicePixelRatio; QPoint offset = dipOffset * devicePixelRatio; cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)); cache.fill(Qt::transparent); QPainter cachePainter(&cache); if (iconMode == QIcon::Disabled) { QImage im = px.toImage().convertToFormat(QImage::Format_ARGB32); for (int y=0; y<im.height(); ++y) { QRgb *scanLine = (QRgb*)im.scanLine(y); for (int x=0; x<im.width(); ++x) { QRgb pixel = *scanLine; char intensity = qGray(pixel); *scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel)); ++scanLine; } } px = QPixmap::fromImage(im); } // Draw shadow QImage tmp(px.size() + QSize(radius * 2, radius * 2 + 1), QImage::Format_ARGB32_Premultiplied); tmp.fill(Qt::transparent); QPainter tmpPainter(&tmp); tmpPainter.setCompositionMode(QPainter::CompositionMode_Source); tmpPainter.drawPixmap(QRect(radius, radius, px.width(), px.height()), px); tmpPainter.end(); // blur the alpha channel QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied); blurred.fill(Qt::transparent); QPainter blurPainter(&blurred); qt_blurImage(&blurPainter, tmp, radius, false, true); 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(QRect(QPoint(radius, radius) + offset, QSize(px.width(), px.height())), px); //jassuncao: cache.setDevicePixelRatio(devicePixelRatio); QPixmapCache::insert(pixmapName, cache); } QRect targetRect = cache.rect(); //jassuncao targetRect.setSize(targetRect.size() / cache.devicePixelRatio()); targetRect.moveCenter(rect.center() - dipOffset); p->drawPixmap(targetRect, cache); }