/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. #include "OverlayButton.h"
  19. #include <QPainter>
  20. #include <QPropertyAnimation>
  21. #include <QAbstractScrollArea>
  22. #include <QScrollBar>
  23. #include "utils/TomahawkUtilsGui.h"
  24. #include "utils/Logger.h"
  25. #define CORNER_ROUNDNESS 8.0
  26. #define FADING_DURATION 500
  27. #define OPACITY 0.70
  28. OverlayButton::OverlayButton( QWidget* parent )
  29. : QPushButton( parent ) // this is on purpose!
  30. , m_opacity( 0.0 )
  31. , m_parent( parent )
  32. {
  33. resize( 0, 28 );
  34. setAttribute( Qt::WA_TranslucentBackground, true );
  35. setOpacity( m_opacity );
  36. m_timer.setSingleShot( true );
  37. connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) );
  38. }
  39. OverlayButton::~OverlayButton()
  40. {
  41. }
  42. void
  43. OverlayButton::setOpacity( qreal opacity )
  44. {
  45. m_opacity = opacity;
  46. if ( m_opacity == 0.00 && !isHidden() )
  47. {
  48. QPushButton::hide();
  49. }
  50. else if ( m_opacity > 0.00 && isHidden() )
  51. {
  52. QPushButton::show();
  53. }
  54. repaint();
  55. }
  56. void
  57. OverlayButton::setText( const QString& text )
  58. {
  59. m_text = text;
  60. QFont f( font() );
  61. f.setPointSize( TomahawkUtils::defaultFontSize() + 3 );
  62. f.setBold( true );
  63. QFontMetrics fm( f );
  64. resize( fm.width( text ) + 24, height() );
  65. }
  66. void
  67. OverlayButton::show( int timeoutSecs )
  68. {
  69. QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
  70. animation->setDuration( FADING_DURATION );
  71. animation->setEndValue( 1.0 );
  72. animation->start();
  73. if( timeoutSecs > 0 )
  74. m_timer.start( timeoutSecs * 1000 );
  75. }
  76. void
  77. OverlayButton::hide()
  78. {
  79. QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
  80. animation->setDuration( FADING_DURATION );
  81. animation->setEndValue( 0.00 );
  82. animation->start();
  83. }
  84. bool
  85. OverlayButton::shown() const
  86. {
  87. if ( !isEnabled() )
  88. return false;
  89. return m_opacity == OPACITY;
  90. }
  91. void
  92. OverlayButton::paintEvent( QPaintEvent* event )
  93. {
  94. Q_UNUSED( event );
  95. int scrollBarWidth = 0;
  96. QAbstractScrollArea* scrollArea = qobject_cast<QAbstractScrollArea*>( m_parent );
  97. if ( scrollArea && scrollArea->verticalScrollBar()->isVisible() )
  98. scrollBarWidth = scrollArea->verticalScrollBar()->width();
  99. QPoint corner( m_parent->contentsRect().width() - width() - scrollBarWidth - 12, m_parent->height() - height() - 12 );
  100. move( corner );
  101. QPainter p( this );
  102. QRect r = contentsRect();
  103. p.setBackgroundMode( Qt::TransparentMode );
  104. p.setRenderHint( QPainter::Antialiasing );
  105. p.setOpacity( m_opacity );
  106. QPen pen( palette().dark().color(), .5 );
  107. p.setPen( pen );
  108. //FIXME const color
  109. p.setBrush( QColor( 30, 30, 30, 255.0 * OPACITY ) );
  110. p.drawRoundedRect( r, CORNER_ROUNDNESS, CORNER_ROUNDNESS );
  111. QTextOption to( Qt::AlignCenter );
  112. to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
  113. QFont f( font() );
  114. f.setPointSize( TomahawkUtils::defaultFontSize() + 3 );
  115. f.setBold( true );
  116. p.setFont( f );
  117. p.setPen( Qt::white );
  118. p.drawText( r, text(), to );
  119. }