PageRenderTime 115ms CodeModel.GetById 30ms app.highlight 45ms RepoModel.GetById 35ms app.codeStats 0ms

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

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