mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-04 21:27:58 +02:00
* Added OverlayButton.
This commit is contained in:
162
src/libtomahawk/widgets/OverlayButton.cpp
Normal file
162
src/libtomahawk/widgets/OverlayButton.cpp
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||||
|
*
|
||||||
|
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||||
|
*
|
||||||
|
* Tomahawk is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Tomahawk is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "OverlayButton.h"
|
||||||
|
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QPropertyAnimation>
|
||||||
|
|
||||||
|
#include "utils/logger.h"
|
||||||
|
|
||||||
|
#define CORNER_ROUNDNESS 8.0
|
||||||
|
#define FADING_DURATION 500
|
||||||
|
#define FONT_SIZE 16
|
||||||
|
#define OPACITY 0.70
|
||||||
|
|
||||||
|
|
||||||
|
OverlayButton::OverlayButton( QWidget* parent )
|
||||||
|
: QPushButton( parent ) // this is on purpose!
|
||||||
|
, m_opacity( 0.0 )
|
||||||
|
, m_parent( parent )
|
||||||
|
{
|
||||||
|
resize( 160, 26 );
|
||||||
|
setAttribute( Qt::WA_TranslucentBackground, true );
|
||||||
|
|
||||||
|
setOpacity( m_opacity );
|
||||||
|
|
||||||
|
m_timer.setSingleShot( true );
|
||||||
|
connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) );
|
||||||
|
|
||||||
|
#ifdef Q_WS_MAC
|
||||||
|
QFont f( font() );
|
||||||
|
f.setPointSize( f.pointSize() - 2 );
|
||||||
|
setFont( f );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
OverlayButton::~OverlayButton()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
OverlayButton::setOpacity( qreal opacity )
|
||||||
|
{
|
||||||
|
m_opacity = opacity;
|
||||||
|
|
||||||
|
if ( m_opacity == 0.00 && !isHidden() )
|
||||||
|
{
|
||||||
|
QPushButton::hide();
|
||||||
|
}
|
||||||
|
else if ( m_opacity > 0.00 && isHidden() )
|
||||||
|
{
|
||||||
|
QPushButton::show();
|
||||||
|
}
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
OverlayButton::setText( const QString& text )
|
||||||
|
{
|
||||||
|
m_text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
OverlayButton::show( int timeoutSecs )
|
||||||
|
{
|
||||||
|
QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
|
||||||
|
animation->setDuration( FADING_DURATION );
|
||||||
|
animation->setEndValue( 1.0 );
|
||||||
|
animation->start();
|
||||||
|
|
||||||
|
if( timeoutSecs > 0 )
|
||||||
|
m_timer.start( timeoutSecs * 1000 );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
OverlayButton::hide()
|
||||||
|
{
|
||||||
|
QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
|
||||||
|
animation->setDuration( FADING_DURATION );
|
||||||
|
animation->setEndValue( 0.00 );
|
||||||
|
animation->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
OverlayButton::shown() const
|
||||||
|
{
|
||||||
|
if ( !isEnabled() )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return m_opacity == OPACITY;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
OverlayButton::paintEvent( QPaintEvent* event )
|
||||||
|
{
|
||||||
|
Q_UNUSED( event );
|
||||||
|
QPoint corner( m_parent->contentsRect().width() - width() - 12, m_parent->height() - height() - 12 );
|
||||||
|
move( corner );
|
||||||
|
|
||||||
|
QPainter p( this );
|
||||||
|
QRect r = contentsRect();
|
||||||
|
|
||||||
|
p.setBackgroundMode( Qt::TransparentMode );
|
||||||
|
p.setRenderHint( QPainter::Antialiasing );
|
||||||
|
p.setOpacity( m_opacity );
|
||||||
|
|
||||||
|
QPen pen( palette().dark().color(), .5 );
|
||||||
|
p.setPen( pen );
|
||||||
|
p.setBrush( QColor( 30, 30, 30, 255.0 * OPACITY ) );
|
||||||
|
|
||||||
|
p.drawRoundedRect( r, CORNER_ROUNDNESS, CORNER_ROUNDNESS );
|
||||||
|
|
||||||
|
QTextOption to( Qt::AlignCenter );
|
||||||
|
to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
|
||||||
|
|
||||||
|
// shrink to fit if needed
|
||||||
|
QFont f( font() );
|
||||||
|
f.setPointSize( FONT_SIZE );
|
||||||
|
f.setBold( true );
|
||||||
|
|
||||||
|
QRectF textRect = r.adjusted( 8, 8, -8, -8 );
|
||||||
|
qreal availHeight = textRect.height();
|
||||||
|
|
||||||
|
QFontMetricsF fm( f );
|
||||||
|
qreal textHeight = fm.boundingRect( textRect, Qt::AlignCenter | Qt::TextWordWrap, text() ).height();
|
||||||
|
while ( textHeight > availHeight )
|
||||||
|
{
|
||||||
|
if ( f.pointSize() <= 4 ) // don't try harder
|
||||||
|
break;
|
||||||
|
|
||||||
|
f.setPointSize( f.pointSize() - 1 );
|
||||||
|
fm = QFontMetricsF( f );
|
||||||
|
textHeight = fm.boundingRect( textRect, Qt::AlignCenter | Qt::TextWordWrap, text() ).height();
|
||||||
|
}
|
||||||
|
|
||||||
|
p.setFont( f );
|
||||||
|
p.setPen( Qt::white );
|
||||||
|
p.drawText( r.adjusted( 8, 8, -8, -8 ), text(), to );
|
||||||
|
}
|
61
src/libtomahawk/widgets/OverlayButton.h
Normal file
61
src/libtomahawk/widgets/OverlayButton.h
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||||
|
*
|
||||||
|
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||||
|
*
|
||||||
|
* Tomahawk is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Tomahawk is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef OVERLAYBUTTON_H
|
||||||
|
#define OVERLAYBUTTON_H
|
||||||
|
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QAbstractItemView>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
#include "dllmacro.h"
|
||||||
|
|
||||||
|
class DLLEXPORT OverlayButton : public QPushButton
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY( qreal opacity READ opacity WRITE setOpacity )
|
||||||
|
|
||||||
|
public:
|
||||||
|
OverlayButton( QWidget* parent );
|
||||||
|
~OverlayButton();
|
||||||
|
|
||||||
|
qreal opacity() const { return m_opacity; }
|
||||||
|
void setOpacity( qreal opacity );
|
||||||
|
|
||||||
|
QString text() const { return m_text; }
|
||||||
|
void setText( const QString& text );
|
||||||
|
|
||||||
|
bool shown() const;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void show( int timeoutSecs = 0 );
|
||||||
|
void hide();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// void changeEvent( QEvent* e );
|
||||||
|
void paintEvent( QPaintEvent* event );
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString m_text;
|
||||||
|
qreal m_opacity;
|
||||||
|
|
||||||
|
QWidget* m_parent;
|
||||||
|
QTimer m_timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // OVERLAYBUTTON_H
|
Reference in New Issue
Block a user