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); return QCoreApplication::translate("", str);
} }
QString PrettyTimeDelta(int seconds) {
return (seconds >= 0 ? "+" : "-") + PrettyTime(seconds);
}
QString PrettyTime(int seconds) { QString PrettyTime(int seconds) {
// last.fm sometimes gets the track length wrong, so you end up with // last.fm sometimes gets the track length wrong, so you end up with
// negative times. // negative times.

View File

@ -28,6 +28,7 @@ class QIODevice;
namespace Utilities { namespace Utilities {
QString PrettyTime(int seconds); QString PrettyTime(int seconds);
QString PrettyTimeDelta(int seconds);
QString PrettyTimeNanosec(qint64 nanoseconds); QString PrettyTimeNanosec(qint64 nanoseconds);
QString PrettySize(quint64 bytes); QString PrettySize(quint64 bytes);
QString WordyTime(quint64 seconds); 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) TrackSliderPopup::TrackSliderPopup(QWidget* parent)
: QWidget(parent), : QWidget(parent),
font_metrics_(fontMetrics()) font_metrics_(fontMetrics()),
small_font_metrics_(fontMetrics())
{ {
setAttribute(Qt::WA_TransparentForMouseEvents); setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true); setMouseTracking(true);
font_.setPointSizeF(7.5); font_.setPointSizeF(7.5);
font_.setBold(true); font_.setBold(true);
small_font_.setPointSizeF(6.5);
font_metrics_ = QFontMetrics(font_); font_metrics_ = QFontMetrics(font_);
small_font_metrics_ = QFontMetrics(small_font_);
} }
void TrackSliderPopup::SetText(const QString& text) { void TrackSliderPopup::SetText(const QString& text) {
@ -33,6 +36,11 @@ void TrackSliderPopup::SetText(const QString& text) {
UpdatePixmap(); UpdatePixmap();
} }
void TrackSliderPopup::SetSmallText(const QString& text) {
small_text_ = text;
UpdatePixmap();
}
void TrackSliderPopup::SetPopupPosition(const QPoint& pos) { void TrackSliderPopup::SetPopupPosition(const QPoint& pos) {
pos_ = pos; pos_ = pos;
UpdatePosition(); UpdatePosition();
@ -44,10 +52,15 @@ void TrackSliderPopup::paintEvent(QPaintEvent*) {
} }
void TrackSliderPopup::UpdatePixmap() { void TrackSliderPopup::UpdatePixmap() {
const QRect text_rect(kBlurRadius + kTextMargin, kBlurRadius + kTextMargin, const int text_width = qMax(font_metrics_.width(text_),
font_metrics_.width(text_) + 2, font_metrics_.height()); small_font_metrics_.width(small_text_));
const int bubble_bottom = text_rect.bottom() + kTextMargin; const QRect text_rect1(kBlurRadius + kTextMargin, kBlurRadius + kTextMargin,
const QRect total_rect(0, 0, text_rect.right() + 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); kBlurRadius + bubble_bottom + kPointLength);
const QRect bubble_rect(kBlurRadius, kBlurRadius, const QRect bubble_rect(kBlurRadius, kBlurRadius,
total_rect.width() - kBlurRadius * 2, total_rect.width() - kBlurRadius * 2,
@ -127,7 +140,12 @@ void TrackSliderPopup::UpdatePixmap() {
// Text // Text
p.setPen(palette().color(QPalette::Text)); p.setPen(palette().color(QPalette::Text));
p.setFont(font_); 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(); p.end();
resize(pixmap_.size()); resize(pixmap_.size());

View File

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

View File

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