/src/libtomahawk/widgets/OverlayWidget.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 241 lines · 163 code · 54 blank · 24 comment · 24 complexity · 453e6a65d668969986fcdab468969210 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5. *
  6. * Tomahawk is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Tomahawk is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "OverlayWidget.h"
  20. #include "playlist/PlayableProxyModel.h"
  21. #include "utils/TomahawkUtilsGui.h"
  22. #include "utils/Logger.h"
  23. #include <QPainter>
  24. #include <QPropertyAnimation>
  25. #define CORNER_ROUNDNESS 8.0
  26. #define FADING_DURATION 500
  27. #define OPACITY 0.70
  28. OverlayWidget::OverlayWidget( QWidget* parent )
  29. : QWidget( parent ) // this is on purpose!
  30. , m_parent( parent )
  31. , m_itemView( 0 )
  32. {
  33. init();
  34. }
  35. OverlayWidget::OverlayWidget( QAbstractItemView* parent )
  36. : QWidget( parent ) // this is on purpose!
  37. , m_parent( parent )
  38. , m_itemView( parent )
  39. {
  40. init();
  41. onViewModelChanged();
  42. connect( m_itemView, SIGNAL( modelChanged() ), SLOT( onViewModelChanged() ) );
  43. }
  44. OverlayWidget::~OverlayWidget()
  45. {
  46. }
  47. void
  48. OverlayWidget::init()
  49. {
  50. installEventFilter( m_parent );
  51. setAcceptDrops( true );
  52. setAttribute( Qt::WA_TranslucentBackground, true );
  53. m_opacity = 0.00;
  54. setOpacity( m_opacity );
  55. m_timer.setSingleShot( true );
  56. connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) );
  57. }
  58. void
  59. OverlayWidget::setOpacity( qreal opacity )
  60. {
  61. m_opacity = opacity;
  62. if ( m_opacity == 0.00 && !isHidden() )
  63. {
  64. QWidget::hide();
  65. }
  66. else if ( m_opacity > 0.00 && isHidden() )
  67. {
  68. QWidget::show();
  69. }
  70. repaint();
  71. }
  72. void
  73. OverlayWidget::setText( const QString& text )
  74. {
  75. m_text = text;
  76. onViewChanged();
  77. }
  78. void
  79. OverlayWidget::show( int timeoutSecs )
  80. {
  81. if ( !isEnabled() )
  82. return;
  83. QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
  84. animation->setDuration( FADING_DURATION );
  85. animation->setEndValue( 1.0 );
  86. animation->start();
  87. if ( timeoutSecs > 0 )
  88. m_timer.start( timeoutSecs * 1000 );
  89. }
  90. void
  91. OverlayWidget::hide()
  92. {
  93. if ( !isEnabled() )
  94. return;
  95. QPropertyAnimation* animation = new QPropertyAnimation( this, "opacity" );
  96. animation->setDuration( FADING_DURATION );
  97. animation->setEndValue( 0.00 );
  98. animation->start();
  99. }
  100. bool
  101. OverlayWidget::shown() const
  102. {
  103. if ( !isEnabled() )
  104. return false;
  105. return m_opacity == OPACITY;
  106. }
  107. void
  108. OverlayWidget::onViewChanged()
  109. {
  110. if ( !m_itemView )
  111. return;
  112. PlayableProxyModel* model = qobject_cast<PlayableProxyModel*>( m_itemView->model() );
  113. if ( !model )
  114. return;
  115. if ( m_text.isEmpty() || model->rowCount( QModelIndex() ) || model->isLoading() )
  116. {
  117. hide();
  118. }
  119. else
  120. {
  121. show();
  122. }
  123. }
  124. void
  125. OverlayWidget::onViewModelChanged()
  126. {
  127. if ( !m_itemView )
  128. return;
  129. if ( m_itemView->model() )
  130. {
  131. connect( m_itemView->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection );
  132. connect( m_itemView->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onViewChanged() ), Qt::UniqueConnection );
  133. connect( m_itemView->model(), SIGNAL( loadingStarted() ), SLOT( onViewChanged() ), Qt::UniqueConnection );
  134. connect( m_itemView->model(), SIGNAL( loadingFinished() ), SLOT( onViewChanged() ), Qt::UniqueConnection );
  135. onViewChanged();
  136. }
  137. }
  138. void
  139. OverlayWidget::paintEvent( QPaintEvent* event )
  140. {
  141. Q_UNUSED( event );
  142. {
  143. QSize maxiSize = QSize( (double)m_parent->width() * 0.70, (double)m_parent->height() * 0.70 );
  144. QSize prefSize = QSize( 380, 128 );
  145. int width = qMin( maxiSize.width(), prefSize.width() );
  146. int height = qMin( maxiSize.height(), prefSize.height() );
  147. QSize newSize = QSize( width, height );
  148. if ( newSize != size() )
  149. resize( newSize );
  150. }
  151. QPoint center( ( m_parent->width() - width() ) / 2, ( m_parent->height() - height() ) / 2 );
  152. if ( center != pos() )
  153. {
  154. move( center );
  155. }
  156. QPainter p( this );
  157. QRect r = contentsRect();
  158. p.setBackgroundMode( Qt::TransparentMode );
  159. p.setRenderHint( QPainter::Antialiasing );
  160. p.setOpacity( m_opacity );
  161. /* QPen pen( palette().dark().color(), .5 );
  162. p.setPen( pen );
  163. //FIXME const color
  164. p.setBrush( QColor( 30, 30, 30, 255.0 * OPACITY ) );
  165. p.drawRoundedRect( r, CORNER_ROUNDNESS, CORNER_ROUNDNESS );*/
  166. QTextOption to( Qt::AlignCenter );
  167. to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
  168. // shrink to fit if needed
  169. QFont f( font() );
  170. f.setPointSize( TomahawkUtils::defaultFontSize() + 7 );
  171. f.setBold( true );
  172. QRectF textRect = r.adjusted( 8, 8, -8, -8 );
  173. qreal availHeight = textRect.height();
  174. QFontMetricsF fm( f );
  175. qreal textHeight = fm.boundingRect( textRect, Qt::AlignCenter | Qt::TextWordWrap, text() ).height();
  176. while ( textHeight > availHeight )
  177. {
  178. if ( f.pointSize() <= 4 ) // don't try harder
  179. break;
  180. f.setPointSize( f.pointSize() - 1 );
  181. fm = QFontMetricsF( f );
  182. textHeight = fm.boundingRect( textRect, Qt::AlignCenter | Qt::TextWordWrap, text() ).height();
  183. }
  184. p.setFont( f );
  185. p.setPen( Qt::gray );
  186. p.drawText( r.adjusted( 8, 8, -8, -8 ), text(), to );
  187. }