Commit b29a9f0d authored by Tobias Güntner's avatar Tobias Güntner Committed by Jean-Baptiste Kempf

Keep tooltip within screen boundaries. Adjust tip position accordingly.

Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent cb1fe28b
...@@ -262,12 +262,10 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event ) ...@@ -262,12 +262,10 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event )
chapterLabel = points.at( i_selected ).name; chapterLabel = points.at( i_selected ).name;
} }
QPoint target( event->globalX() - ( event->x() - posX ),
QWidget::mapToGlobal( pos() ).y() );
secstotimestr( psz_length, ( posX * inputLength ) / size().width() ); secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
mTimeTooltip->setText( psz_length, chapterLabel ); mTimeTooltip->setTip( target, psz_length, chapterLabel );
QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
mTimeTooltip->move( p );
} }
event->accept(); event->accept();
} }
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <QPainterPath> #include <QPainterPath>
#include <QBitmap> #include <QBitmap>
#include <QFontMetrics> #include <QFontMetrics>
#include <QDesktopWidget>
#define TIP_HEIGHT 5 #define TIP_HEIGHT 5
...@@ -51,29 +52,47 @@ TimeTooltip::TimeTooltip( QWidget *parent ) : ...@@ -51,29 +52,47 @@ TimeTooltip::TimeTooltip( QWidget *parent ) :
// Inherit from the system default font size -5 // Inherit from the system default font size -5
mFont = QFont( "Verdana", qMax( qApp->font().pointSize() - 5, 7 ) ); mFont = QFont( "Verdana", qMax( qApp->font().pointSize() - 5, 7 ) );
mPreviousMetricsWidth = 0; mTipX = -1;
// Set default text
setText( "00:00:00", "" );
} }
void TimeTooltip::buildPath() void TimeTooltip::adjustPosition()
{ {
// Get the bounding box required to print the text and add some padding
QFontMetrics metrics( mFont ); 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 // Resize the widget to fit our needs
QRect textbox = metrics.boundingRect( mDisplayedText ).adjusted( -2, -2, 2, 2 ); QSize size( textbox.width() + 1, textbox.height() + TIP_HEIGHT + 1 );
if ( mPreviousMetricsWidth == textbox.width() ) // The desired label position is just above the target
return; //same width == same path QPoint position( mTarget.x() - size.width() / 2,
else mTarget.y() - size.height() + TIP_HEIGHT / 2 );
mPreviousMetricsWidth = textbox.width();
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 move( position );
resize( mBox.width() + 1, mBox.height() + TIP_HEIGHT + 1 );
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 // Prepare the painter path for future use so
// we only have to generate the text at runtime. // we only have to generate the text at runtime.
...@@ -82,12 +101,10 @@ void TimeTooltip::buildPath() ...@@ -82,12 +101,10 @@ void TimeTooltip::buildPath()
mPainterPath.addRect( mBox ); mPainterPath.addRect( mBox );
// Draw the tip // Draw the tip
int center = mBox.width() / 2;
QPolygon polygon; QPolygon polygon;
polygon << QPoint( center - 3, mBox.height() ) polygon << QPoint( qMax( 0, mTipX - 3 ), mBox.height() )
<< QPoint( center, mBox.height() + TIP_HEIGHT ) << QPoint( mTipX, mBox.height() + TIP_HEIGHT )
<< QPoint( center + 3, mBox.height() ); << QPoint( qMin( mTipX + 3, mBox.width() ), mBox.height() );
mPainterPath.addPolygon( polygon ); mPainterPath.addPolygon( polygon );
// Store the simplified version of the path // Store the simplified version of the path
...@@ -98,25 +115,26 @@ void TimeTooltip::buildPath() ...@@ -98,25 +115,26 @@ void TimeTooltip::buildPath()
mMask = QBitmap( size() ); mMask = QBitmap( size() );
QPainter painter( &mMask ); QPainter painter( &mMask );
painter.fillRect( mMask.rect(), Qt::white ); painter.fillRect( mMask.rect(), Qt::white );
painter.setPen( QColor( 0, 0, 0 ) ); painter.setPen( Qt::black );
painter.setBrush( QColor( 0, 0, 0 ) ); painter.setBrush( Qt::black );
painter.drawPath( mPainterPath ); painter.drawPath( mPainterPath );
painter.end(); 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; mDisplayedText = time;
if ( !text.isEmpty() ) if ( !text.isEmpty() )
mDisplayedText.append( " - " ).append( text ); mDisplayedText.append( " - " ).append( text );
if ( time.length() != mTime.length() || mText != text ) if( mTarget != target || time.length() != mTime.length() || mText != text )
buildPath(); {
mTarget = target;
mTime = time;
mText = text;
adjustPosition();
}
mTime = time;
mText = text;
update(); update();
} }
...@@ -133,5 +151,3 @@ void TimeTooltip::paintEvent( QPaintEvent * ) ...@@ -133,5 +151,3 @@ void TimeTooltip::paintEvent( QPaintEvent * )
p.setPen( QPen( qApp->palette().text(), 1 ) ); p.setPen( QPen( qApp->palette().text(), 1 ) );
p.drawText( mBox, Qt::AlignCenter, mDisplayedText ); p.drawText( mBox, Qt::AlignCenter, mDisplayedText );
} }
#undef TIP_HEIGHT
...@@ -36,13 +36,15 @@ class TimeTooltip : public QWidget ...@@ -36,13 +36,15 @@ class TimeTooltip : public QWidget
Q_OBJECT Q_OBJECT
public: public:
explicit TimeTooltip( QWidget *parent = 0 ); 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: protected:
virtual void paintEvent( QPaintEvent * ); virtual void paintEvent( QPaintEvent * );
private: private:
void adjustPosition();
void buildPath(); void buildPath();
QPoint mTarget;
QString mTime; QString mTime;
QString mText; QString mText;
QString mDisplayedText; QString mDisplayedText;
...@@ -50,7 +52,7 @@ private: ...@@ -50,7 +52,7 @@ private:
QRect mBox; QRect mBox;
QPainterPath mPainterPath; QPainterPath mPainterPath;
QBitmap mMask; QBitmap mMask;
int mPreviousMetricsWidth; int mTipX;
}; };
#endif // TIMETOOLTIP_H #endif // TIMETOOLTIP_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment