1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 11:20:22 +02:00

Add a little arrow to the accounts popup widget.

This commit is contained in:
Teo Mrnjavac
2012-10-13 19:36:53 +02:00
parent beedc71f52
commit 68b6020bdb
3 changed files with 36 additions and 12 deletions

View File

@@ -31,6 +31,7 @@
AccountsPopupWidget::AccountsPopupWidget( QWidget* parent ) AccountsPopupWidget::AccountsPopupWidget( QWidget* parent )
: QWidget( parent ) : QWidget( parent )
, m_widget( 0 ) , m_widget( 0 )
, m_arrowOffset( 0 )
{ {
setWindowFlags( Qt::FramelessWindowHint ); setWindowFlags( Qt::FramelessWindowHint );
setWindowFlags( Qt::Popup ); setWindowFlags( Qt::Popup );
@@ -45,7 +46,7 @@ AccountsPopupWidget::AccountsPopupWidget( QWidget* parent )
m_layout = new QVBoxLayout( this ); m_layout = new QVBoxLayout( this );
setLayout( m_layout ); setLayout( m_layout );
setContentsMargins( contentsMargins().left() + 2, contentsMargins().top() + 2 , setContentsMargins( contentsMargins().left() + 2, contentsMargins().top() + 2 + 6 /*arrowHeight*/ ,
contentsMargins().right(), contentsMargins().bottom() ); contentsMargins().right(), contentsMargins().bottom() );
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
@@ -65,27 +66,44 @@ AccountsPopupWidget::setWidget( QWidget* widget )
void void
AccountsPopupWidget::anchorAt( const QPoint &p ) AccountsPopupWidget::anchorAt( const QPoint &p )
{ {
#ifdef Q_OS_WIN QPoint myTopRight( p.x() - sizeHint().width(), p.y() - 2 ); //we go 2px up to point inside the button
// We do this because Windows sticks the toolbutton really close to the
// right side border of the window
QPoint myTopRight( p.x() - sizeHint().width(), p.y() );
#else
QPoint myTopRight( p.x() - sizeHint().width() + 8, p.y() );
#endif
move( myTopRight ); move( myTopRight );
if( isVisible() ) if( isVisible() )
repaint(); repaint();
} }
void
AccountsPopupWidget::setArrowOffset( int arrowOffset )
{
arrowOffset -= 2; //a pinch of magic dust to handle the internal margin
if ( arrowOffset != m_arrowOffset )
{
m_arrowOffset = arrowOffset;
if ( isVisible() )
repaint();
}
}
void AccountsPopupWidget::paintEvent( QPaintEvent* ) void AccountsPopupWidget::paintEvent( QPaintEvent* )
{ {
// Constants for painting // Constants for painting
const int cornerRadius = 8; //the rounding radius of the widget const int cornerRadius = 6; //the rounding radius of the widget
const int arrowHeight = 6;
const QRect brect = rect().adjusted( 2, 2, -2, -2 ); //m_arrowOffset is the distance between the far right boundary and the x value of the arrow head.
//It is provided by the tool button, and is expected to be the middle of the button.
//With this, we make sure that whatever happens, it will be bigger than the rounding radius plus
//half the arrow, so that the shape of the rounded rect won't be affected.
m_arrowOffset = qMax( m_arrowOffset, cornerRadius + arrowHeight ); //at least 12!
const QRect brect = rect().adjusted( 2, arrowHeight + 2, -2, -2 );
QPainterPath outline; QPainterPath outline;
outline.addRoundedRect( 3, 3, brect.width(), brect.height(), cornerRadius, cornerRadius ); outline.addRoundedRect( brect.left(), brect.top(), brect.width(), brect.height(), cornerRadius, cornerRadius );
outline.moveTo( rect().right() - m_arrowOffset + arrowHeight, brect.top() );
outline.lineTo( rect().right() - m_arrowOffset, 2 );
outline.lineTo( rect().right() - m_arrowOffset - arrowHeight, brect.top() );
QPainter p( this ); QPainter p( this );

View File

@@ -30,7 +30,8 @@ public:
explicit AccountsPopupWidget( QWidget* parent = 0 ); explicit AccountsPopupWidget( QWidget* parent = 0 );
void setWidget( QWidget* widget ); void setWidget( QWidget* widget );
void anchorAt( const QPoint &p ); void anchorAt( const QPoint& p );
void setArrowOffset( int arrowOffset );
signals: signals:
void hidden(); void hidden();
@@ -43,6 +44,7 @@ protected:
private: private:
QVBoxLayout* m_layout; QVBoxLayout* m_layout;
QWidget* m_widget; QWidget* m_widget;
int m_arrowOffset;
}; };
#endif // ACCOUNTSPOPUPWIDGET_H #endif // ACCOUNTSPOPUPWIDGET_H

View File

@@ -112,6 +112,7 @@ AccountsToolButton::mousePressEvent( QMouseEvent* event )
{ {
QPoint myPos = mapToGlobal( rect().bottomRight() ); QPoint myPos = mapToGlobal( rect().bottomRight() );
m_popup->anchorAt( myPos ); m_popup->anchorAt( myPos );
m_popup->setArrowOffset( rect().width() / 2 );
m_popup->show(); m_popup->show();
event->accept(); event->accept();
} }
@@ -224,6 +225,9 @@ AccountsToolButton::updateIcons()
resize( sizeHint() ); resize( sizeHint() );
if ( oldWidth != sizeHint().width() ) if ( oldWidth != sizeHint().width() )
emit widthChanged(); emit widthChanged();
m_popup->setArrowOffset( rect().width() / 2 );
repaint(); repaint();
} }