1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-01-31 11:35:24 +01:00

Cache the background pixmap, and add support for X without transparency

This commit is contained in:
David Sansome 2010-12-27 19:37:36 +00:00
parent 62e21d64f3
commit 6456665066
2 changed files with 89 additions and 52 deletions

View File

@ -1,11 +1,16 @@
#include "tracksliderpopup.h"
#include <QBitmap>
#include <QCoreApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
#include <QtDebug>
#ifdef Q_WS_X11
# include <QX11Info>
#endif
const int TrackSliderPopup::kTextMargin = 4;
const int TrackSliderPopup::kPointLength = 16;
const int TrackSliderPopup::kPointWidth = 4;
@ -16,7 +21,6 @@ void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bo
TrackSliderPopup::TrackSliderPopup(QWidget* parent)
: QWidget(parent),
text_("Test text"),
font_metrics_(fontMetrics()),
mouse_over_slider_(false),
mouse_over_popup_(false),
@ -41,8 +45,13 @@ TrackSliderPopup::TrackSliderPopup(QWidget* parent)
font_.setPointSizeF(7.5);
font_.setBold(true);
font_metrics_ = QFontMetrics(font_);
}
UpdatePixmap();
bool TrackSliderPopup::IsTransparencyAvailable() {
#ifdef Q_WS_X11
return QX11Info::isCompositingManagerRunning();
#endif
return true;
}
void TrackSliderPopup::SetText(const QString& text) {
@ -70,19 +79,81 @@ void TrackSliderPopup::UpdatePixmap() {
total_rect.width() - kBlurRadius * 2,
bubble_bottom - kBlurRadius);
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));
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 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);
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(IsTransparencyAvailable() ?
Qt::transparent : bg_color_2);
QPainter p(&background_cache_);
p.setRenderHint(QPainter::Antialiasing);
p.setRenderHint(QPainter::HighQualityAntialiasing);
if (IsTransparencyAvailable()) {
// Draw the shadow to a different image
QImage blur_source(total_rect.size(), QImage::Format_ARGB32_Premultiplied);
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();
} else {
QBitmap mask(total_rect.size());
mask.clear();
QPainter mask_painter(&mask);
mask_painter.setBrush(Qt::color1);
mask_painter.drawRoundedRect(bubble_rect, kBorderRadius, kBorderRadius);
mask_painter.drawPolygon(pointy);
mask_painter.end();
setMask(mask);
}
// 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);
@ -90,45 +161,8 @@ void TrackSliderPopup::UpdatePixmap() {
p.setRenderHint(QPainter::Antialiasing);
p.setRenderHint(QPainter::HighQualityAntialiasing);
// Draw the shadow to a different image
QImage blur_source(total_rect.size(), QImage::Format_ARGB32_Premultiplied);
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);
// Background
p.drawPixmap(total_rect.topLeft(), background_cache_);
// Text
p.setPen(palette().color(QPalette::Text));

View File

@ -10,6 +10,8 @@ public:
TrackSliderPopup(QWidget* parent);
public slots:
static bool IsTransparencyAvailable();
void SetText(const QString& text);
void SetPopupPosition(const QPoint& pos);
@ -44,6 +46,7 @@ private:
QFont font_;
QFontMetrics font_metrics_;
QPixmap pixmap_;
QPixmap background_cache_;
bool mouse_over_slider_;
bool mouse_over_popup_;