PageRenderTime 61ms CodeModel.GetById 29ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/widgets/OverlayButton.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 156 lines | 101 code | 37 blank | 18 comment | 11 complexity | 1a12d5c3599a2746546484f9980bf88c MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "OverlayButton.h"
 20
 21#include <QPainter>
 22#include <QPropertyAnimation>
 23#include <QAbstractScrollArea>
 24#include <QScrollBar>
 25
 26#include "utils/TomahawkUtilsGui.h"
 27#include "utils/Logger.h"
 28
 29#define CORNER_ROUNDNESS 8.0
 30#define FADING_DURATION 500
 31#define OPACITY 0.70
 32
 33
 34OverlayButton::OverlayButton( QWidget* parent )
 35    : QPushButton( parent ) // this is on purpose!
 36    , m_opacity( 0.0 )
 37    , m_parent( parent )
 38{
 39    resize( 0, 28 );
 40    setAttribute( Qt::WA_TranslucentBackground, true );
 41
 42    setOpacity( m_opacity );
 43
 44    m_timer.setSingleShot( true );
 45    connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) );
 46}
 47
 48
 49OverlayButton::~OverlayButton()
 50{
 51}
 52
 53
 54void
 55OverlayButton::setOpacity( qreal opacity )
 56{
 57    m_opacity = opacity;
 58
 59    if ( m_opacity == 0.00 && !isHidden() )
 60    {
 61        QPushButton::hide();
 62    }
 63    else if ( m_opacity > 0.00 && isHidden() )
 64    {
 65        QPushButton::show();
 66    }
 67
 68    repaint();
 69}
 70
 71
 72void
 73OverlayButton::setText( const QString& text )
 74{
 75    m_text = text;
 76
 77    QFont f( font() );
 78    f.setPointSize( TomahawkUtils::defaultFontSize() + 3 );
 79    f.setBold( true );
 80
 81    QFontMetrics fm( f );
 82    resize( fm.width( text ) + 24, height() );
 83}
 84
 85
 86void
 87OverlayButton::show( int timeoutSecs )
 88{
 89    QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
 90    animation->setDuration( FADING_DURATION );
 91    animation->setEndValue( 1.0 );
 92    animation->start();
 93
 94    if( timeoutSecs > 0 )
 95        m_timer.start( timeoutSecs * 1000 );
 96}
 97
 98
 99void
100OverlayButton::hide()
101{
102    QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
103    animation->setDuration( FADING_DURATION );
104    animation->setEndValue( 0.00 );
105    animation->start();
106}
107
108
109bool
110OverlayButton::shown() const
111{
112    if ( !isEnabled() )
113        return false;
114
115    return m_opacity == OPACITY;
116}
117
118
119void
120OverlayButton::paintEvent( QPaintEvent* event )
121{
122    Q_UNUSED( event );
123
124    int scrollBarWidth = 0;
125    QAbstractScrollArea* scrollArea = qobject_cast<QAbstractScrollArea*>( m_parent );
126    if ( scrollArea && scrollArea->verticalScrollBar()->isVisible() )
127        scrollBarWidth = scrollArea->verticalScrollBar()->width();
128
129    QPoint corner( m_parent->contentsRect().width() - width() - scrollBarWidth - 12, m_parent->height() - height() - 12 );
130    move( corner );
131
132    QPainter p( this );
133    QRect r = contentsRect();
134
135    p.setBackgroundMode( Qt::TransparentMode );
136    p.setRenderHint( QPainter::Antialiasing );
137    p.setOpacity( m_opacity );
138
139    QPen pen( palette().dark().color(), .5 );
140    p.setPen( pen );
141    //FIXME const color
142    p.setBrush( QColor( 30, 30, 30, 255.0 * OPACITY ) );
143
144    p.drawRoundedRect( r, CORNER_ROUNDNESS, CORNER_ROUNDNESS );
145
146    QTextOption to( Qt::AlignCenter );
147    to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
148
149    QFont f( font() );
150    f.setPointSize( TomahawkUtils::defaultFontSize() + 3 );
151    f.setBold( true );
152
153    p.setFont( f );
154    p.setPen( Qt::white );
155    p.drawText( r, text(), to );
156}