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

Make the track slider popup a normal widget instead of a top-level window. Removes a lot of the complexity and fixes some bugs

This commit is contained in:
David Sansome 2010-12-29 14:49:39 +00:00
parent 5b4b4a98c7
commit 9c03677143
3 changed files with 27 additions and 130 deletions

View File

@ -8,10 +8,6 @@
#include <QWheelEvent>
#include <QtDebug>
#ifdef Q_WS_X11
# include <QX11Info>
#endif
const int TrackSliderPopup::kTextMargin = 4;
const int TrackSliderPopup::kPointLength = 16;
const int TrackSliderPopup::kPointWidth = 4;
@ -22,33 +18,16 @@ void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bo
TrackSliderPopup::TrackSliderPopup(QWidget* parent)
: QWidget(parent),
font_metrics_(fontMetrics()),
mouse_over_slider_(false),
mouse_over_popup_(false),
visibility_timer_(new QTimer(this))
font_metrics_(fontMetrics())
{
setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint |
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
visibility_timer_->setSingleShot(true);
visibility_timer_->setInterval(10);
connect(visibility_timer_, SIGNAL(timeout()), SLOT(UpdateVisibility()));
font_.setPointSizeF(7.5);
font_.setBold(true);
font_metrics_ = QFontMetrics(font_);
}
bool TrackSliderPopup::IsTransparencyAvailable() {
#ifdef Q_WS_X11
return QX11Info::isCompositingManagerRunning();
#endif
return true;
}
void TrackSliderPopup::SetText(const QString& text) {
text_ = text;
UpdatePixmap();
@ -90,48 +69,34 @@ void TrackSliderPopup::UpdatePixmap() {
<< QPoint(pointy[2].x() - 1, pointy[2].y() - 1);
background_cache_ = QPixmap(total_rect.size());
background_cache_.fill(IsTransparencyAvailable() ?
Qt::transparent : bg_color_2);
background_cache_.fill(Qt::transparent);
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);
// 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);
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();
// 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);
}
p.save();
qt_blurImage(&p, blur_source, kBlurRadius, true, false);
p.restore();
// Outer bubble
p.setPen(Qt::NoPen);
@ -174,54 +139,3 @@ void TrackSliderPopup::UpdatePosition() {
move(pos_.x() - pixmap_.width() / 2,
pos_.y() - pixmap_.height() + kBlurRadius);
}
void TrackSliderPopup::enterEvent(QEvent* e) {
mouse_over_popup_ = true;
visibility_timer_->start();
}
void TrackSliderPopup::leaveEvent(QEvent* e) {
mouse_over_popup_ = false;
visibility_timer_->start();
}
void TrackSliderPopup::SetMouseOverSlider(bool mouse_over_slider) {
mouse_over_slider_ = mouse_over_slider;
visibility_timer_->start();
}
void TrackSliderPopup::UpdateVisibility() {
setVisible(mouse_over_popup_ || mouse_over_slider_);
}
void TrackSliderPopup::SendMouseEventToParent(QMouseEvent* e) {
QMouseEvent event(
e->type(), parentWidget()->mapFromGlobal(e->globalPos()),
e->button(), e->buttons(), e->modifiers());
QCoreApplication::sendEvent(parentWidget(), &event);
}
void TrackSliderPopup::wheelEvent(QWheelEvent* e) {
QWheelEvent event(
parentWidget()->mapFromGlobal(e->globalPos()), e->delta(), e->buttons(),
e->modifiers(), e->orientation());
QCoreApplication::sendEvent(parentWidget(), &event);
}
void TrackSliderPopup::mousePressEvent(QMouseEvent* e) {
SendMouseEventToParent(e);
}
void TrackSliderPopup::mouseReleaseEvent(QMouseEvent* e) {
SendMouseEventToParent(e);
}
void TrackSliderPopup::mouseMoveEvent(QMouseEvent* e) {
if (!parentWidget()->rect().contains(
parentWidget()->mapFromGlobal(e->globalPos()))) {
// The mouse left the parent widget - close this popup
mouse_over_popup_ = false;
visibility_timer_->start();
}
SendMouseEventToParent(e);
}

View File

@ -10,21 +10,11 @@ public:
TrackSliderPopup(QWidget* parent);
public slots:
static bool IsTransparencyAvailable();
void SetText(const QString& text);
void SetPopupPosition(const QPoint& pos);
void SetMouseOverSlider(bool mouse_over_slider);
protected:
void paintEvent(QPaintEvent*);
void enterEvent(QEvent*);
void leaveEvent(QEvent*);
void mousePressEvent(QMouseEvent*);
void mouseReleaseEvent(QMouseEvent*);
void mouseMoveEvent(QMouseEvent*);
void wheelEvent(QWheelEvent*);
private:
static const int kTextMargin;
@ -37,9 +27,6 @@ private:
void UpdatePosition();
void SendMouseEventToParent(QMouseEvent* e);
private slots:
void UpdateVisibility();
private:
QString text_;
QPoint pos_;
@ -48,10 +35,6 @@ private:
QFontMetrics font_metrics_;
QPixmap pixmap_;
QPixmap background_cache_;
bool mouse_over_slider_;
bool mouse_over_popup_;
QTimer* visibility_timer_;
};
#endif // TRACKSLIDERPOPUP_H

View File

@ -26,7 +26,7 @@
TrackSliderSlider::TrackSliderSlider(QWidget* parent)
: QSlider(parent),
popup_(new TrackSliderPopup(this))
popup_(new TrackSliderPopup(window()))
{
setMouseTracking(true);
}
@ -76,18 +76,18 @@ void TrackSliderSlider::mouseMoveEvent(QMouseEvent* e) {
slider_max - slider_min);
popup_->SetText(Utilities::PrettyTime(seconds));
popup_->SetPopupPosition(mapToGlobal(QPoint(
popup_->SetPopupPosition(mapTo(window(), QPoint(
e->x(), rect().center().y())));
}
void TrackSliderSlider::enterEvent(QEvent* e) {
QSlider::enterEvent(e);
if (isEnabled()) {
popup_->SetMouseOverSlider(true);
popup_->show();
}
}
void TrackSliderSlider::leaveEvent(QEvent* e) {
QSlider::leaveEvent(e);
popup_->SetMouseOverSlider(false);
popup_->hide();
}