PageRenderTime 144ms CodeModel.GetById 40ms app.highlight 49ms RepoModel.GetById 51ms app.codeStats 1ms

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