[vlc-commits] Keep tooltip within screen boundaries. Adjust tip position accordingly.

Tobias Güntner git at videolan.org
Wed Feb 1 22:17:21 CET 2012


vlc | branch: master | Tobias Güntner <fatbull at web.de> | Fri Jan 27 00:29:37 2012 +0100| [b29a9f0d126c41594344c43fe92e51d0a15feaa6] | committer: Jean-Baptiste Kempf

Keep tooltip within screen boundaries. Adjust tip position accordingly.

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b29a9f0d126c41594344c43fe92e51d0a15feaa6
---

 modules/gui/qt4/util/input_slider.cpp |    8 +--
 modules/gui/qt4/util/timetooltip.cpp  |   76 ++++++++++++++++++++-------------
 modules/gui/qt4/util/timetooltip.hpp  |    6 ++-
 3 files changed, 53 insertions(+), 37 deletions(-)

diff --git a/modules/gui/qt4/util/input_slider.cpp b/modules/gui/qt4/util/input_slider.cpp
index a4a2c62..4577e2f 100644
--- a/modules/gui/qt4/util/input_slider.cpp
+++ b/modules/gui/qt4/util/input_slider.cpp
@@ -262,12 +262,10 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event )
                     chapterLabel = points.at( i_selected ).name;
         }
 
+        QPoint target( event->globalX() - ( event->x() - posX ),
+                  QWidget::mapToGlobal( pos() ).y() );
         secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
-        mTimeTooltip->setText( psz_length, chapterLabel );
-
-        QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
-                  QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
-        mTimeTooltip->move( p );
+        mTimeTooltip->setTip( target, psz_length, chapterLabel );
     }
     event->accept();
 }
diff --git a/modules/gui/qt4/util/timetooltip.cpp b/modules/gui/qt4/util/timetooltip.cpp
index 9b8ab14..06f98ff 100644
--- a/modules/gui/qt4/util/timetooltip.cpp
+++ b/modules/gui/qt4/util/timetooltip.cpp
@@ -26,6 +26,7 @@
 #include <QPainterPath>
 #include <QBitmap>
 #include <QFontMetrics>
+#include <QDesktopWidget>
 
 #define TIP_HEIGHT 5
 
@@ -51,29 +52,47 @@ TimeTooltip::TimeTooltip( QWidget *parent ) :
 
     // Inherit from the system default font size -5
     mFont = QFont( "Verdana", qMax( qApp->font().pointSize() - 5, 7 ) );
-    mPreviousMetricsWidth = 0;
-
-    // Set default text
-    setText( "00:00:00", "" );
+    mTipX = -1;
 }
 
-void TimeTooltip::buildPath()
+void TimeTooltip::adjustPosition()
 {
+    // Get the bounding box required to print the text and add some padding
     QFontMetrics metrics( mFont );
+    QRect textbox = metrics.boundingRect( mDisplayedText );
+    textbox.adjust( -2, -2, 2, 2 );
+    textbox.moveTo( 0, 0 );
 
-    // Get the bounding box required to print the text and add some padding
-    QRect textbox = metrics.boundingRect( mDisplayedText ).adjusted( -2, -2, 2, 2 );
+    // Resize the widget to fit our needs
+    QSize size( textbox.width() + 1, textbox.height() + TIP_HEIGHT + 1 );
 
-    if ( mPreviousMetricsWidth == textbox.width() )
-        return; //same width == same path
-    else
-        mPreviousMetricsWidth = textbox.width();
+    // The desired label position is just above the target
+    QPoint position( mTarget.x() - size.width() / 2,
+        mTarget.y() - size.height() + TIP_HEIGHT / 2 );
 
-    mBox = QRect( 0, 0, textbox.width(), textbox.height() );
+    // Keep the tooltip on the same screen if possible
+    QRect screen = QApplication::desktop()->screenGeometry( mTarget );
+    position.setX( qMax( screen.left(), qMin( position.x(),
+        screen.left() + screen.width() - size.width() ) ) );
+    position.setY( qMax( screen.top(), qMin( position.y(),
+        screen.top() + screen.height() - size.height() ) ) );
 
-    // Resize the widget to fit our needs
-    resize( mBox.width() + 1, mBox.height() + TIP_HEIGHT + 1 );
+    move( position );
+
+    int tipX = mTarget.x() - position.x();
+    if( mBox != textbox || mTipX != tipX )
+    {
+        mBox = textbox;
+        mTipX = tipX;
+
+        resize( size );
+        buildPath();
+        setMask( mMask );
+    }
+}
 
+void TimeTooltip::buildPath()
+{
     // Prepare the painter path for future use so
     // we only have to generate the text at runtime.
 
@@ -82,12 +101,10 @@ void TimeTooltip::buildPath()
     mPainterPath.addRect( mBox );
 
     // Draw the tip
-    int center = mBox.width() / 2;
     QPolygon polygon;
-    polygon << QPoint( center - 3,   mBox.height() )
-            << QPoint( center,       mBox.height() + TIP_HEIGHT )
-            << QPoint( center + 3,   mBox.height() );
-
+    polygon << QPoint( qMax( 0, mTipX - 3 ), mBox.height() )
+            << QPoint( mTipX, mBox.height() + TIP_HEIGHT )
+            << QPoint( qMin( mTipX + 3, mBox.width() ), mBox.height() );
     mPainterPath.addPolygon( polygon );
 
     // Store the simplified version of the path
@@ -98,25 +115,26 @@ void TimeTooltip::buildPath()
     mMask = QBitmap( size() );
     QPainter painter( &mMask );
     painter.fillRect( mMask.rect(), Qt::white );
-    painter.setPen( QColor( 0, 0, 0 ) );
-    painter.setBrush( QColor( 0, 0, 0 ) );
+    painter.setPen( Qt::black );
+    painter.setBrush( Qt::black );
     painter.drawPath( mPainterPath );
     painter.end();
-
-    setMask( mMask );
 }
 
-void TimeTooltip::setText( const QString& time, const QString& text )
+void TimeTooltip::setTip( const QPoint& target, const QString& time, const QString& text )
 {
     mDisplayedText = time;
     if ( !text.isEmpty() )
         mDisplayedText.append( " - " ).append( text );
 
-    if ( time.length() != mTime.length() || mText != text )
-        buildPath();
+    if( mTarget != target || time.length() != mTime.length() || mText != text )
+    {
+        mTarget = target;
+        mTime = time;
+        mText = text;
+        adjustPosition();
+    }
 
-    mTime = time;
-    mText = text;
     update();
 }
 
@@ -133,5 +151,3 @@ void TimeTooltip::paintEvent( QPaintEvent * )
     p.setPen( QPen( qApp->palette().text(), 1 ) );
     p.drawText( mBox, Qt::AlignCenter, mDisplayedText );
 }
-
-#undef TIP_HEIGHT
diff --git a/modules/gui/qt4/util/timetooltip.hpp b/modules/gui/qt4/util/timetooltip.hpp
index 6e74f64..e8923bc 100644
--- a/modules/gui/qt4/util/timetooltip.hpp
+++ b/modules/gui/qt4/util/timetooltip.hpp
@@ -36,13 +36,15 @@ class TimeTooltip : public QWidget
     Q_OBJECT
 public:
     explicit TimeTooltip( QWidget *parent = 0 );
-    void setText( const QString& time, const QString& text );
+    void setTip( const QPoint& pos, const QString& time, const QString& text );
 
 protected:
     virtual void paintEvent( QPaintEvent * );
 
 private:
+    void adjustPosition();
     void buildPath();
+    QPoint mTarget;
     QString mTime;
     QString mText;
     QString mDisplayedText;
@@ -50,7 +52,7 @@ private:
     QRect mBox;
     QPainterPath mPainterPath;
     QBitmap mMask;
-    int mPreviousMetricsWidth;
+    int mTipX;
 };
 
 #endif // TIMETOOLTIP_H



More information about the vlc-commits mailing list