PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libtomahawk/infobar/infobar.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 316 lines | 205 code | 62 blank | 49 comment | 14 complexity | a4289a2a259f310bc24559b4dd5920bf MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2012, Leo Franchi <lfranchi@kde.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 "InfoBar.h"
  20. #include "ui_InfoBar.h"
  21. #include <QLabel>
  22. #include <QPixmap>
  23. #include <QCheckBox>
  24. #include <QPaintEvent>
  25. #include <QPainter>
  26. #include "ViewManager.h"
  27. #include "thirdparty/Qocoa/qsearchfield.h"
  28. #include "utils/TomahawkUtilsGui.h"
  29. #include "utils/Logger.h"
  30. #include "widgets/QueryLabel.h"
  31. #include "Source.h"
  32. #define ANIMATION_TIME 400
  33. #define IMAGE_HEIGHT 64
  34. using namespace Tomahawk;
  35. InfoBar::InfoBar( QWidget* parent )
  36. : QWidget( parent )
  37. , ui( new Ui::InfoBar )
  38. , m_queryLabel( 0 )
  39. {
  40. ui->setupUi( this );
  41. TomahawkUtils::unmarginLayout( layout() );
  42. layout()->setContentsMargins( 8, 4, 8, 4 );
  43. QFont boldFont = ui->captionLabel->font();
  44. boldFont.setPointSize( TomahawkUtils::defaultFontSize() + 4 );
  45. boldFont.setBold( true );
  46. ui->captionLabel->setFont( boldFont );
  47. ui->captionLabel->setElideMode( Qt::ElideRight );
  48. QFontMetrics boldFontMetrics( boldFont );
  49. boldFont.setPointSize( TomahawkUtils::defaultFontSize() + 1 );
  50. boldFont.setBold( false );
  51. ui->descriptionLabel->setFont( boldFont );
  52. boldFontMetrics = QFontMetrics( boldFont );
  53. QFont regFont = ui->longDescriptionLabel->font();
  54. regFont.setPointSize( TomahawkUtils::defaultFontSize() );
  55. ui->longDescriptionLabel->setFont( regFont );
  56. m_whitePal = ui->captionLabel->palette();
  57. m_whitePal.setColor( QPalette::Foreground, Qt::white );
  58. ui->captionLabel->setPalette( m_whitePal );
  59. ui->descriptionLabel->setPalette( m_whitePal );
  60. ui->longDescriptionLabel->setPalette( m_whitePal );
  61. ui->captionLabel->setMargin( 2 );
  62. ui->descriptionLabel->setMargin( 1 );
  63. ui->longDescriptionLabel->setMargin( 4 );
  64. ui->captionLabel->setText( QString() );
  65. ui->descriptionLabel->setText( QString() );
  66. ui->longDescriptionLabel->setText( QString() );
  67. ui->imageLabel->setText( QString() );
  68. m_queryLabel = new QueryLabel( this );
  69. m_queryLabel->setType( QueryLabel::Artist );
  70. m_queryLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
  71. m_queryLabel->setTextPen( palette().brightText().color() );
  72. m_queryLabel->setFont( boldFont );
  73. m_queryLabel->hide();
  74. connect( m_queryLabel, SIGNAL( clickedArtist() ), this, SLOT( artistClicked() ) );
  75. m_searchWidget = new QSearchField( this );
  76. m_searchWidget->setPlaceholderText( tr( "Filter..." ) );
  77. m_searchWidget->setMinimumWidth( 180 );
  78. connect( m_searchWidget, SIGNAL( textChanged( QString ) ), this, SLOT( onFilterEdited() ) );
  79. ui->horizontalLayout->addWidget( m_searchWidget );
  80. setAutoFillBackground( true );
  81. setFixedHeight( 80 );
  82. m_bgTile = TomahawkUtils::createTiledPixmap( 2000, height(), QImage( RESPATH "images/playlist-header-tiled.png" ) );
  83. connect( ViewManager::instance(), SIGNAL( filterAvailable( bool ) ), SLOT( setFilterAvailable( bool ) ) );
  84. }
  85. InfoBar::~InfoBar()
  86. {
  87. delete ui;
  88. }
  89. void
  90. InfoBar::setCaption( const QString& s )
  91. {
  92. ui->captionLabel->setText( s );
  93. }
  94. void
  95. InfoBar::setDescription( const QString& s )
  96. {
  97. if ( m_queryLabel->isVisible() )
  98. {
  99. ui->verticalLayout->removeWidget( m_queryLabel );
  100. m_queryLabel->hide();
  101. ui->verticalLayout->addWidget( ui->descriptionLabel );
  102. ui->verticalLayout->setContentsMargins( 0, 0, 0, 0 );
  103. ui->descriptionLabel->show();
  104. }
  105. ui->descriptionLabel->setText( s );
  106. }
  107. void
  108. InfoBar::setDescription( const artist_ptr& artist )
  109. {
  110. m_queryLabel->setArtist( artist );
  111. m_queryLabel->setExtraContentsMargins( 4, 0, 0, 0 );
  112. if ( !m_queryLabel->isVisible() )
  113. {
  114. ui->verticalLayout->removeWidget( ui->descriptionLabel );
  115. ui->descriptionLabel->hide();
  116. m_queryLabel->show();
  117. ui->verticalLayout->addWidget( m_queryLabel );
  118. ui->verticalLayout->setContentsMargins( 0, 0, 0, 15 );
  119. }
  120. }
  121. void
  122. InfoBar::setDescription( const album_ptr& )
  123. {
  124. // TODO
  125. }
  126. void
  127. InfoBar::artistClicked()
  128. {
  129. if ( m_queryLabel && !m_queryLabel->artist().isNull() )
  130. ViewManager::instance()->show( m_queryLabel->artist() );
  131. }
  132. void
  133. InfoBar::setLongDescription( const QString& s )
  134. {
  135. ui->longDescriptionLabel->setText( s );
  136. if ( s.isEmpty() )
  137. {
  138. ui->horizontalLayout->setStretchFactor( ui->verticalLayout, 1 );
  139. ui->horizontalLayout->setStretchFactor( ui->verticalLayout_2, 0 );
  140. } else
  141. {
  142. ui->horizontalLayout->setStretchFactor( ui->verticalLayout, 0 );
  143. ui->horizontalLayout->setStretchFactor( ui->verticalLayout_2, 99 );
  144. }
  145. }
  146. void
  147. InfoBar::setPixmap( const QPixmap& p )
  148. {
  149. ui->imageLabel->setPixmap( p.scaledToHeight( IMAGE_HEIGHT, Qt::SmoothTransformation ) );
  150. }
  151. void
  152. InfoBar::setFilter( const QString& filter )
  153. {
  154. m_searchWidget->setText( filter );
  155. }
  156. void
  157. InfoBar::setFilterAvailable( bool b )
  158. {
  159. m_searchWidget->setVisible( b );
  160. }
  161. void
  162. InfoBar::setUpdaters( const QList<PlaylistUpdaterInterface*>& updaters )
  163. {
  164. QList< QWidget* > newUpdaterWidgets;
  165. foreach ( PlaylistUpdaterInterface* updater, updaters )
  166. {
  167. if ( updater->configurationWidget() )
  168. newUpdaterWidgets << updater->configurationWidget();
  169. }
  170. foreach ( QWidget* updaterWidget, m_updaterConfigurations )
  171. {
  172. updaterWidget->hide();
  173. if ( !newUpdaterWidgets.contains( updaterWidget ) )
  174. {
  175. // Old config widget no longer present, remove it
  176. ui->horizontalLayout->removeWidget( updaterWidget );
  177. }
  178. }
  179. m_updaters = updaters;
  180. m_updaterConfigurations = newUpdaterWidgets;
  181. // Display each new widget in the proper place
  182. int insertIdx = -1; // Ugh, no indexOf for QSpacerItem*
  183. for ( int i = 0; i < ui->horizontalLayout->count(); i++ )
  184. {
  185. if ( ui->horizontalLayout->itemAt( i )->spacerItem() == ui->horizontalSpacer_4 )
  186. {
  187. insertIdx = i;
  188. break;
  189. }
  190. }
  191. insertIdx++;
  192. foreach ( QWidget* updaterWidget, m_updaterConfigurations )
  193. {
  194. updaterWidget->setPalette( m_whitePal );
  195. ui->horizontalLayout->insertWidget( insertIdx, updaterWidget );
  196. updaterWidget->show();
  197. }
  198. // if ( m_updaterConfiguration )
  199. // m_updaterConfiguration->hide();
  200. //
  201. // if ( m_updaterConfiguration && ( interface ? (m_updaterConfiguration != interface->configurationWidget()) : true ) )
  202. // ui->horizontalLayout->removeWidget( m_updaterConfiguration );
  203. //
  204. // m_updaterInterface = interface;
  205. // m_updaterConfiguration = interface ? interface->configurationWidget() : 0;
  206. //
  207. // if ( !m_updaterInterface || !m_updaterConfiguration )
  208. // return;
  209. //
  210. // m_updaterConfiguration->setPalette( m_whitePal );
  211. // int insertIdx = -1; // Ugh, no indexOf for QSpacerItem*
  212. // for ( int i = 0; i < ui->horizontalLayout->count(); i++ )
  213. // {
  214. // if ( ui->horizontalLayout->itemAt( i )->spacerItem() == ui->horizontalSpacer_4 )
  215. // {
  216. // insertIdx = i;
  217. // break;
  218. // }
  219. // }
  220. // insertIdx++;
  221. // ui->horizontalLayout->insertWidget( insertIdx, m_updaterConfiguration );
  222. //
  223. // m_updaterConfiguration->show();
  224. }
  225. void
  226. InfoBar::onFilterEdited()
  227. {
  228. emit filterTextChanged( m_searchWidget->text() );
  229. }
  230. void
  231. InfoBar::paintEvent( QPaintEvent* e )
  232. {
  233. Q_UNUSED( e );
  234. if ( m_bgTile.isNull() || width() > m_bgTile.width() )
  235. m_bgTile = TomahawkUtils::createTiledPixmap( width(), height(), QImage( RESPATH "images/playlist-header-tiled.png" ) );
  236. if ( m_bgTile.isNull() )
  237. return;
  238. QPainter p( this );
  239. // Truncate bg pixmap and paint into bg
  240. p.drawPixmap( rect(), m_bgTile, rect() );
  241. }
  242. void
  243. InfoBar::changeEvent( QEvent* e )
  244. {
  245. QWidget::changeEvent( e );
  246. switch ( e->type() )
  247. {
  248. case QEvent::LanguageChange:
  249. // ui->retranslateUi( this );
  250. break;
  251. default:
  252. break;
  253. }
  254. }