]> Some of my projects - aniplayer-old.git/commitdiff
- Display the seek time with the marker shadow on the seek slider.
authorAPTX <APTX@.(none)>
Wed, 26 Aug 2009 22:16:01 +0000 (00:16 +0200)
committerAPTX <APTX@.(none)>
Wed, 26 Aug 2009 22:16:01 +0000 (00:16 +0200)
src/seekslider.cpp
src/seekslider.h

index 5c8770aefb3f7a1006e9eb4f453faf09a766c308..0692e905095da2bd770ce6cde7df0dc5ebcf3c99 100644 (file)
@@ -3,8 +3,10 @@
 #include <QPainter>
 #include <QEvent>
 #include <QMouseEvent>
-#include "aniplayer.h"
+#include <QTime>
+#include <QFontMetrics>
 
+#include "aniplayer.h"
 #include <QDebug>
 
 SeekSlider::SeekSlider(QWidget *parent) : QWidget(parent)
@@ -106,12 +108,15 @@ void SeekSlider::paintEvent(QPaintEvent *event)
        seekerWidth = width() - 1;
 
        const int markerWidth = 2;
-       const int markerPos = seekerLeft + 1 + markerWidth / 2 + qRound(double(m_value) / double(m_length) * double(seekerWidth - markerWidth / 2 - 1));
+       const int markerPos = time2pos(m_value);
        const QColor markerColor(255, 0, 0);
        const QColor markerShadowColor(255, 0, 0, 100);
        const QColor previousMarkerColor(255, 255, 255);
        const QColor watchedPartColor(0, 0, 255, 150);
        const QColor unwatchedPartColor(0, 255, 0, 100);
+       const QColor toolTipBackgroundColor(0, 0, 0, 150);
+       const QColor toolTipForegroundColor(255, 255, 255);
+
 
        // border
        p.drawRect(seekerLeft, seekerTop, seekerWidth, seekerHeight);
@@ -121,12 +126,12 @@ void SeekSlider::paintEvent(QPaintEvent *event)
        // unwatched part
        p.fillRect(markerPos + markerWidth / 2, seekerTop + 1, seekerWidth - markerPos, seekerHeight - 1, unwatchedPartColor);
 
-       int i = 0;
+       int i = 1;
        for (QQueue<qint64>::const_iterator it = previuousPos.constBegin(); it != previuousPos.constEnd(); ++it)
        {
                int markerPos = seekerLeft + 1 + markerWidth / 2 + qRound(double(*it) / double(m_length) * double(seekerWidth - markerWidth / 2 - 1));
                QColor c = previousMarkerColor;
-               c.setAlpha(255 / previuousPos.count() * (i + 1));
+               c.setAlpha(255 / previuousPos.count() * i);
                p.fillRect(markerPos - markerWidth / 2, seekerTop + 1, markerWidth, seekerHeight - 1, c);
                ++i;
        }
@@ -135,10 +140,23 @@ void SeekSlider::paintEvent(QPaintEvent *event)
        p.fillRect(markerPos - markerWidth / 2, seekerTop + 1, markerWidth, seekerHeight - 1, markerColor);
 
        // marker shadow (where the marker would move when mouse is clicked)
-       if (drawMarkerShadow)
+       if (drawMarkerShadow && isEnabled())
        {
                markerShadowPos = qBound(seekerLeft + 1 + markerWidth / 2, markerShadowPos, seekerLeft + seekerWidth - markerWidth / 2);
                p.fillRect(markerShadowPos - markerWidth / 2, seekerTop + 1, markerWidth, seekerHeight - 1, markerShadowColor);
+
+               QString time = QTime().addMSecs(pos2time(markerShadowPos)).toString("hh:mm:ss");
+               QRect r = p.fontMetrics().boundingRect(time);
+
+               int xdelta = markerShadowPos + markerWidth / 2 + 1;
+               if (xdelta + r.width() < width())
+                       r.translate(xdelta, r.height());
+               else
+                       r.translate(markerShadowPos - (markerWidth / 2 + 1) - r.width(), r.height());
+
+               p.fillRect(r, toolTipBackgroundColor);
+               p.setPen(QPen(toolTipForegroundColor));
+               p.drawText(r, time);
        }
 
 }
@@ -280,7 +298,7 @@ void SeekSlider::setLength(qint64 length)
 void SeekSlider::seek(int x)
 {
        int newMarkerPos = qBound(seekerLeft + 1, x, seekerLeft + seekerWidth);
-       qint64 newSeekPos = double(newMarkerPos) / double(seekerLeft + seekerWidth) * double(m_length);
+       qint64 newSeekPos = pos2time(newMarkerPos);
 
        while (!previuousPos.isEmpty() && previuousPos.count() + 1 > maxPreviousPos) previuousPos.dequeue();
        previuousPos.enqueue(m_value);
@@ -290,3 +308,15 @@ void SeekSlider::seek(int x)
        ticking = false;
        seek(newSeekPos);
 }
+
+qint64 SeekSlider::pos2time(int pos) const
+{
+       const int halfMarkerWidth = markerWidth / 2;
+       return qint64(double(pos - (seekerLeft + 1 + halfMarkerWidth)) / double(seekerWidth - halfMarkerWidth - 1) * double(m_length));
+}
+
+int SeekSlider::time2pos(qint64 msec) const
+{
+       const int halfMarkerWidth = markerWidth / 2;
+       return seekerLeft + 1 + halfMarkerWidth + qRound(double(msec) / double(m_length) * double(seekerWidth - halfMarkerWidth - 1));
+}
index 45e7f4c82bd6771b97bfce255235db6cdb101389..b28943cd00a340a7e3f644c3b6bf035c0929811d 100644 (file)
@@ -79,6 +79,9 @@ private:
 
        void seek(int x);
 
+       qint64 pos2time(int pos) const;
+       int time2pos(qint64 msec) const;
+
        QPointer<Phonon::MediaObject> m_media;
        bool ticking;
        bool markerShadowEnabled;