Add an extra line to the track slider popup to show the difference between the current time and the destination time.

This commit is contained in:
David Sansome 2011-02-21 20:59:30 +00:00
parent 4ecd63f137
commit 107b7b54f9
5 changed files with 35 additions and 6 deletions

View File

@ -45,6 +45,10 @@ static QString tr(const char* str) {
return QCoreApplication::translate("", str);
}
QString PrettyTimeDelta(int seconds) {
return (seconds >= 0 ? "+" : "-") + PrettyTime(seconds);
}
QString PrettyTime(int seconds) {
// last.fm sometimes gets the track length wrong, so you end up with
// negative times.

View File

@ -28,6 +28,7 @@ class QIODevice;
namespace Utilities {
QString PrettyTime(int seconds);
QString PrettyTimeDelta(int seconds);
QString PrettyTimeNanosec(qint64 nanoseconds);
QString PrettySize(quint64 bytes);
QString WordyTime(quint64 seconds);

View File

@ -18,14 +18,17 @@ void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bo
TrackSliderPopup::TrackSliderPopup(QWidget* parent)
: QWidget(parent),
font_metrics_(fontMetrics())
font_metrics_(fontMetrics()),
small_font_metrics_(fontMetrics())
{
setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
font_.setPointSizeF(7.5);
font_.setBold(true);
small_font_.setPointSizeF(6.5);
font_metrics_ = QFontMetrics(font_);
small_font_metrics_ = QFontMetrics(small_font_);
}
void TrackSliderPopup::SetText(const QString& text) {
@ -33,6 +36,11 @@ void TrackSliderPopup::SetText(const QString& text) {
UpdatePixmap();
}
void TrackSliderPopup::SetSmallText(const QString& text) {
small_text_ = text;
UpdatePixmap();
}
void TrackSliderPopup::SetPopupPosition(const QPoint& pos) {
pos_ = pos;
UpdatePosition();
@ -44,10 +52,15 @@ void TrackSliderPopup::paintEvent(QPaintEvent*) {
}
void TrackSliderPopup::UpdatePixmap() {
const QRect text_rect(kBlurRadius + kTextMargin, kBlurRadius + kTextMargin,
font_metrics_.width(text_) + 2, font_metrics_.height());
const int bubble_bottom = text_rect.bottom() + kTextMargin;
const QRect total_rect(0, 0, text_rect.right() + kBlurRadius + kTextMargin,
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,
@ -127,7 +140,12 @@ void TrackSliderPopup::UpdatePixmap() {
// Text
p.setPen(palette().color(QPalette::Text));
p.setFont(font_);
p.drawText(text_rect, Qt::AlignHCenter, text_);
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());

View File

@ -11,6 +11,7 @@ public:
public slots:
void SetText(const QString& text);
void SetSmallText(const QString& small_text);
void SetPopupPosition(const QPoint& pos);
protected:
@ -29,10 +30,13 @@ private:
private:
QString text_;
QString small_text_;
QPoint pos_;
QFont font_;
QFont small_font_;
QFontMetrics font_metrics_;
QFontMetrics small_font_metrics_;
QPixmap pixmap_;
QPixmap background_cache_;
};

View File

@ -74,8 +74,10 @@ void TrackSliderSlider::mouseMoveEvent(QMouseEvent* e) {
int seconds = QStyle::sliderValueFromPosition(
minimum(), maximum(), e->x() - slider_length/2 - slider_min + 1,
slider_max - slider_min);
int delta_seconds = seconds - value();
popup_->SetText(Utilities::PrettyTime(seconds));
popup_->SetSmallText(Utilities::PrettyTimeDelta(delta_seconds));
popup_->SetPopupPosition(mapTo(window(), QPoint(
e->x(), rect().center().y())));
}