/src/libtomahawk/playlist/topbar/topbar.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 343 lines · 238 code · 79 blank · 26 comment · 13 complexity · 790ff9e93fc4c37903f5e16a3c981c60 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 "topbar.h"
  19. #include "ui_topbar.h"
  20. #include "searchbutton.h"
  21. #include <QPropertyAnimation>
  22. #include <QRadioButton>
  23. #include <QFile>
  24. #include "utils/tomahawkutils.h"
  25. #include "utils/logger.h"
  26. #define MAXDUDES 3
  27. #define DUDEWIDTH 10
  28. #define DUDEX( i ) ( DUDEWIDTH * i )
  29. using namespace Tomahawk;
  30. TopBar::TopBar( QWidget* parent )
  31. : QWidget( parent )
  32. , ui( new Ui::TopBar )
  33. , m_sources( 0 )
  34. , m_tracks( 0 )
  35. , m_artists( 0 )
  36. , m_shown( 0 )
  37. {
  38. ui->setupUi( this );
  39. ui->statsLabelNumTracks->setFormat( "%L1 " + tr( "Tracks" ) );
  40. ui->statsLabelNumArtists->setFormat( "%L1 " + tr( "Artists" ) );
  41. connect( ui->filterEdit, SIGNAL( textChanged( QString ) ), SIGNAL( filterTextChanged( QString ) ) );
  42. ui->filterEdit->setStyleSheet( "QLineEdit { border: 1px solid gray; border-radius: 6px; margin-right: 2px; }" );
  43. ui->filterEdit->setInactiveText( tr( "Filter" ) );
  44. #ifdef Q_WS_MAC
  45. ui->filterEdit->setAttribute( Qt::WA_MacShowFocusRect, 0 );
  46. #endif
  47. ui->filterEdit->searchButton()->setImage( QImage( RESPATH "images/filter.png" ) );
  48. // initialise dudes
  49. for( int i = 0; i < MAXDUDES; ++i )
  50. {
  51. QLabel* manlbl = new QLabel( ui->widgetMen );
  52. manlbl->setPixmap( QPixmap( RESPATH "images/avatar-dude.png" ) );
  53. manlbl->move( QPoint( -10,0 ) );
  54. manlbl->show();
  55. m_dudes.append( manlbl );
  56. }
  57. QFile f( RESPATH "stylesheets/topbar-radiobuttons.css" );
  58. f.open( QFile::ReadOnly );
  59. QString css = QString::fromAscii( f.readAll() );
  60. f.close();
  61. ui->widgetRadio->setStyleSheet( css );
  62. ui->radioNormal->setFocusPolicy( Qt::NoFocus );
  63. ui->radioDetailed->setFocusPolicy( Qt::NoFocus );
  64. ui->radioCloud->setFocusPolicy( Qt::NoFocus );
  65. ui->radioCloud->hide();
  66. ui->radioNormal->setToolTip( tr( "Artist View" ) );
  67. ui->radioDetailed->setToolTip( tr("Flat View" ) );
  68. connect( ui->radioNormal, SIGNAL( clicked() ), SIGNAL( artistMode() ) );
  69. connect( ui->radioDetailed, SIGNAL( clicked() ), SIGNAL( flatMode() ) );
  70. connect( ui->radioCloud, SIGNAL( clicked() ), SIGNAL( albumMode() ) );
  71. setNumSources( 0 );
  72. setNumTracks( 0 );
  73. setNumArtists( 0 );
  74. setNumShown( 0 );
  75. onArtistMode();
  76. connect( ViewManager::instance(), SIGNAL( numSourcesChanged( unsigned int ) ),
  77. SLOT( setNumSources( unsigned int ) ) );
  78. connect( ViewManager::instance(), SIGNAL( numTracksChanged( unsigned int ) ),
  79. SLOT( setNumTracks( unsigned int ) ) );
  80. connect( ViewManager::instance(), SIGNAL( numArtistsChanged( unsigned int ) ),
  81. SLOT( setNumArtists( unsigned int ) ) );
  82. connect( ViewManager::instance(), SIGNAL( numShownChanged( unsigned int ) ),
  83. SLOT( setNumShown( unsigned int ) ) );
  84. connect( ViewManager::instance(), SIGNAL( statsAvailable( bool ) ),
  85. SLOT( setStatsVisible( bool ) ) );
  86. connect( ViewManager::instance(), SIGNAL( modesAvailable( bool ) ),
  87. SLOT( setModesVisible( bool ) ) );
  88. connect( ViewManager::instance(), SIGNAL( filterAvailable( bool ) ),
  89. SLOT( setFilterVisible( bool ) ) );
  90. connect( ViewManager::instance(), SIGNAL( modeChanged( Tomahawk::PlaylistInterface::ViewMode ) ),
  91. SLOT( onModeChanged( Tomahawk::PlaylistInterface::ViewMode ) ) );
  92. }
  93. TopBar::~TopBar()
  94. {
  95. delete ui;
  96. }
  97. void
  98. TopBar::changeEvent( QEvent* e )
  99. {
  100. QWidget::changeEvent( e );
  101. switch ( e->type() )
  102. {
  103. case QEvent::LanguageChange:
  104. ui->retranslateUi( this );
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. void
  111. TopBar::resizeEvent( QResizeEvent* e )
  112. {
  113. QWidget::resizeEvent( e );
  114. }
  115. void
  116. TopBar::fadeInDude( unsigned int i )
  117. {
  118. // qDebug() << Q_FUNC_INFO << i;
  119. QLabel* dude = m_dudes.at( i );
  120. QPropertyAnimation* ani = new QPropertyAnimation( dude, "pos" );
  121. ani->setDuration( 1000 );
  122. ani->setEasingCurve( QEasingCurve::InQuad );
  123. ani->setStartValue( QPoint( -10,0 ) );
  124. ani->setEndValue( QPoint( DUDEX( i+1 ), 0 ) );
  125. qDebug() << "Animating from" << ani->startValue() << "to" << ani->endValue();
  126. connect( ani, SIGNAL( finished() ), ani, SLOT( deleteLater() ) );
  127. ani->start();
  128. }
  129. void
  130. TopBar::fadeOutDude( unsigned int i )
  131. {
  132. // qDebug() << Q_FUNC_INFO << i;
  133. QLabel* dude = m_dudes.at( i );
  134. QPropertyAnimation* ani = new QPropertyAnimation( dude, "pos" );
  135. ani->setDuration( 1000 );
  136. ani->setEasingCurve( QEasingCurve::OutQuad );
  137. ani->setStartValue( dude->pos() );
  138. ani->setEndValue( QPoint( -10, 0 ) );
  139. qDebug() << "Animating from" << ani->startValue() << "to" << ani->endValue();
  140. connect( ani, SIGNAL( finished() ), ani, SLOT( deleteLater() ) );
  141. ani->start();
  142. }
  143. void
  144. TopBar::setNumSources( unsigned int i )
  145. {
  146. // qDebug() << Q_FUNC_INFO << i;
  147. // Dude0 Dude1 Dude2
  148. ui->statsLabelNumSources->setText( QString( "%L1 %2" ).arg( i ).arg( tr( "Sources" ) ) );
  149. if( ( m_sources >= MAXDUDES && i >= MAXDUDES ) || m_sources == i )
  150. {
  151. m_sources = i;
  152. return;
  153. }
  154. if( i > m_sources )
  155. {
  156. for( unsigned int k = m_sources; k < MAXDUDES && k < i; ++k )
  157. {
  158. fadeInDude( k );
  159. }
  160. m_sources = i;
  161. }
  162. else
  163. {
  164. int k = qMin( (unsigned int)MAXDUDES - 1, m_sources - 1 );
  165. do
  166. {
  167. fadeOutDude( k );
  168. m_sources--;
  169. } while( (unsigned int)k-- > i );
  170. m_sources = i;
  171. }
  172. }
  173. void
  174. TopBar::setNumTracks( unsigned int i )
  175. {
  176. m_tracks = i;
  177. ui->statsLabelNumTracks->setVal( i );
  178. }
  179. void
  180. TopBar::setNumArtists( unsigned int i )
  181. {
  182. m_artists = i;
  183. ui->statsLabelNumArtists->setVisible( m_artists > 0 );
  184. ui->statsLabelNumArtists->setVal( i );
  185. }
  186. void
  187. TopBar::setNumShown( unsigned int i )
  188. {
  189. m_shown = i;
  190. ui->statsLabelNumShown->setVisible( m_shown != m_tracks && ui->statsLabelNumTracks->isVisible() );
  191. ui->statsLabelNumShown->setText( QString( "%L1 %2" ).arg( i ).arg( tr( "Shown" ) ) );
  192. }
  193. void
  194. TopBar::addSource()
  195. {
  196. // qDebug() << Q_FUNC_INFO;
  197. setNumSources( m_sources + 1 );
  198. }
  199. void
  200. TopBar::removeSource()
  201. {
  202. // qDebug() << Q_FUNC_INFO;
  203. Q_ASSERT( m_sources > 0 );
  204. setNumSources( m_sources - 1 );
  205. }
  206. void
  207. TopBar::setStatsVisible( bool b )
  208. {
  209. foreach( QLabel* dude, m_dudes )
  210. dude->setVisible( b );
  211. // ui->statsLabelNumArtists->setVisible( b );
  212. // ui->statsLabelNumShown->setVisible( b );
  213. ui->statsLabelNumSources->setVisible( b );
  214. ui->statsLabelNumTracks->setVisible( b );
  215. }
  216. void
  217. TopBar::setModesVisible( bool b )
  218. {
  219. ui->widgetRadio->setVisible( b );
  220. }
  221. void
  222. TopBar::setFilterVisible( bool b )
  223. {
  224. ui->filterEdit->setVisible( b );
  225. }
  226. void
  227. TopBar::setFilter( const QString& filter )
  228. {
  229. ui->filterEdit->setText( filter );
  230. }
  231. void
  232. TopBar::onModeChanged( Tomahawk::PlaylistInterface::ViewMode mode )
  233. {
  234. qDebug() << Q_FUNC_INFO << mode;
  235. switch ( mode )
  236. {
  237. case Tomahawk::PlaylistInterface::Flat:
  238. onFlatMode();
  239. break;
  240. case Tomahawk::PlaylistInterface::Tree:
  241. onArtistMode();
  242. break;
  243. case Tomahawk::PlaylistInterface::Album:
  244. onAlbumMode();
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. void
  251. TopBar::onFlatMode()
  252. {
  253. ui->radioDetailed->setChecked( true );
  254. }
  255. void
  256. TopBar::onArtistMode()
  257. {
  258. ui->radioNormal->setChecked( true );
  259. }
  260. void
  261. TopBar::onAlbumMode()
  262. {
  263. ui->radioCloud->setChecked( true );
  264. }