1 /*
2 * Strawberry Music Player
3 * This file was part of Clementine.
4 * Copyright 2011, David Sansome <me@davidsansome.com>
5 *
6 * Strawberry is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Strawberry is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "config.h"
22
23 #include <QtGlobal>
24 #include <QWidget>
25 #include <QString>
26 #include <QImage>
27 #include <QFontMetrics>
28 #include <QSize>
29 #include <QColor>
30 #include <QPainter>
31 #include <QPalette>
32 #include <QBrush>
33 #include <QRect>
34 #include <QVector>
35 #include <QPoint>
36 #include <QPolygon>
37 #include <QPaintEvent>
38
39 #include "core/qt_blurimage.h"
40 #include "tracksliderpopup.h"
41
42 const int TrackSliderPopup::kTextMargin = 4;
43 const int TrackSliderPopup::kPointLength = 16;
44 const int TrackSliderPopup::kPointWidth = 4;
45 const int TrackSliderPopup::kBorderRadius = 4;
46 const qreal TrackSliderPopup::kBlurRadius = 20.0;
47
TrackSliderPopup(QWidget * parent)48 TrackSliderPopup::TrackSliderPopup(QWidget *parent)
49 : QWidget(parent),
50 font_metrics_(fontMetrics()),
51 small_font_metrics_(fontMetrics()) {
52
53 setAttribute(Qt::WA_TransparentForMouseEvents);
54 setMouseTracking(true);
55
56 font_.setPointSizeF(7.5);
57 font_.setBold(true);
58 small_font_.setPointSizeF(7.5);
59 font_metrics_ = QFontMetrics(font_);
60 small_font_metrics_ = QFontMetrics(small_font_);
61
62 }
63
SetText(const QString & text)64 void TrackSliderPopup::SetText(const QString &text) {
65 text_ = text;
66 UpdatePixmap();
67 }
68
SetSmallText(const QString & text)69 void TrackSliderPopup::SetSmallText(const QString &text) {
70 small_text_ = text;
71 UpdatePixmap();
72 }
73
SetPopupPosition(const QPoint pos)74 void TrackSliderPopup::SetPopupPosition(const QPoint pos) {
75 pos_ = pos;
76 UpdatePosition();
77 }
78
paintEvent(QPaintEvent *)79 void TrackSliderPopup::paintEvent(QPaintEvent*) {
80 QPainter p(this);
81 p.drawPixmap(0, 0, pixmap_);
82 }
83
UpdatePixmap()84 void TrackSliderPopup::UpdatePixmap() {
85
86 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
87 const int text_width = qMax(font_metrics_.horizontalAdvance(text_), small_font_metrics_.horizontalAdvance(small_text_));
88 #else
89 const int text_width = qMax(font_metrics_.width(text_), small_font_metrics_.width(small_text_));
90 #endif
91 const QRect text_rect1(static_cast<int>(kBlurRadius) + kTextMargin, static_cast<int>(kBlurRadius) + kTextMargin, text_width + 2, font_metrics_.height());
92 const QRect text_rect2(static_cast<int>(kBlurRadius) + kTextMargin, text_rect1.bottom(), text_width, small_font_metrics_.height());
93
94 const int bubble_bottom = text_rect2.bottom() + kTextMargin;
95 const QRect total_rect(0, 0, text_rect1.right() + static_cast<int>(kBlurRadius) + kTextMargin, static_cast<int>(kBlurRadius) + bubble_bottom + kPointLength);
96 const QRect bubble_rect(static_cast<int>(kBlurRadius), static_cast<int>(kBlurRadius), total_rect.width() - static_cast<int>(kBlurRadius) * 2, bubble_bottom - static_cast<int>(kBlurRadius));
97
98 if (background_cache_.size() != total_rect.size()) {
99 const QColor highlight(palette().color(QPalette::Active, QPalette::Highlight));
100 const QColor bg_color_1(highlight.lighter(110));
101 const QColor bg_color_2(highlight.darker(120));
102
103 QPolygon pointy;
104 pointy << QPoint(total_rect.width() / 2 - kPointWidth, bubble_bottom)
105 << QPoint(total_rect.width() / 2, total_rect.bottom() - static_cast<int>(kBlurRadius))
106 << QPoint(total_rect.width() / 2 + kPointWidth, bubble_bottom);
107
108 QPolygon inner_pointy;
109 inner_pointy << QPoint(pointy[0].x() + 1, pointy[0].y() - 1)
110 << QPoint(pointy[1].x(), pointy[1].y() - 1)
111 << QPoint(pointy[2].x() - 1, pointy[2].y() - 1);
112
113 background_cache_ = QPixmap(total_rect.size());
114 background_cache_.fill(Qt::transparent);
115 QPainter p(&background_cache_);
116 p.setRenderHint(QPainter::Antialiasing);
117
118 // Draw the shadow to a different image
119 QImage blur_source(total_rect.size(), QImage::Format_ARGB32);
120 blur_source.fill(Qt::transparent);
121
122 QPainter blur_painter(&blur_source);
123 blur_painter.setRenderHint(QPainter::Antialiasing);
124 blur_painter.setBrush(bg_color_2);
125 blur_painter.drawRoundedRect(bubble_rect, kBorderRadius, kBorderRadius);
126 blur_painter.drawPolygon(pointy);
127
128 // Fade the shadow out towards the bottom
129 QLinearGradient fade_gradient(QPoint(0, bubble_bottom), QPoint(0, bubble_bottom + kPointLength));
130 fade_gradient.setColorAt(0.0, QColor(255, 0, 0, 0));
131 fade_gradient.setColorAt(1.0, QColor(255, 0, 0, 255));
132 blur_painter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
133 blur_painter.fillRect(total_rect, fade_gradient);
134 blur_painter.end();
135
136 p.save();
137 qt_blurImage(&p, blur_source, kBlurRadius, true, false);
138 p.restore();
139
140 // Outer bubble
141 p.setPen(Qt::NoPen);
142 p.setBrush(bg_color_2);
143 p.drawRoundedRect(bubble_rect, kBorderRadius, kBorderRadius);
144
145 // Outer pointy
146 p.drawPolygon(pointy);
147
148 // Inner bubble
149 p.setBrush(bg_color_1);
150 p.drawRoundedRect(bubble_rect.adjusted(1, 1, -1, -1), kBorderRadius, kBorderRadius);
151
152 // Inner pointy
153 p.drawPolygon(inner_pointy);
154 }
155
156 pixmap_ = QPixmap(total_rect.size());
157 pixmap_.fill(Qt::transparent);
158 QPainter p(&pixmap_);
159 p.setRenderHint(QPainter::Antialiasing);
160
161 // Background
162 p.drawPixmap(total_rect.topLeft(), background_cache_);
163
164 // Text
165 p.setPen(palette().color(QPalette::HighlightedText));
166 p.setFont(font_);
167 p.drawText(text_rect1, Qt::AlignHCenter, text_);
168
169 p.setFont(small_font_);
170 p.setOpacity(0.65);
171 p.drawText(text_rect2, Qt::AlignHCenter, small_text_);
172
173 p.end();
174
175 resize(pixmap_.size());
176 UpdatePosition();
177 update();
178 }
179
UpdatePosition()180 void TrackSliderPopup::UpdatePosition() {
181 move(pos_.x() - pixmap_.width() / 2, pos_.y() - pixmap_.height() + static_cast<int>(kBlurRadius));
182 }
183