From 8532189b54f472c2b276359eaf5962203df8a42c Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 19 Oct 2011 02:16:16 +0200 Subject: [PATCH] * Added OverlayButton. --- src/libtomahawk/widgets/OverlayButton.cpp | 162 ++++++++++++++++++++++ src/libtomahawk/widgets/OverlayButton.h | 61 ++++++++ 2 files changed, 223 insertions(+) create mode 100644 src/libtomahawk/widgets/OverlayButton.cpp create mode 100644 src/libtomahawk/widgets/OverlayButton.h diff --git a/src/libtomahawk/widgets/OverlayButton.cpp b/src/libtomahawk/widgets/OverlayButton.cpp new file mode 100644 index 000000000..472521bf4 --- /dev/null +++ b/src/libtomahawk/widgets/OverlayButton.cpp @@ -0,0 +1,162 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * 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 . + */ + +#include "OverlayButton.h" + +#include +#include + +#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 ); +} diff --git a/src/libtomahawk/widgets/OverlayButton.h b/src/libtomahawk/widgets/OverlayButton.h new file mode 100644 index 000000000..675d7792f --- /dev/null +++ b/src/libtomahawk/widgets/OverlayButton.h @@ -0,0 +1,61 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * 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 . + */ + +#ifndef OVERLAYBUTTON_H +#define OVERLAYBUTTON_H + +#include +#include +#include + +#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