PageRenderTime 111ms CodeModel.GetById 61ms app.highlight 14ms RepoModel.GetById 26ms app.codeStats 0ms

/src/libtomahawk/widgets/newplaylistwidget.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 151 lines | 94 code | 39 blank | 18 comment | 1 complexity | 3edee665f560a93efbe150ae562f5499 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 "NewPlaylistWidget.h"
 20#include "ui_NewPlaylistWidget.h"
 21
 22#include <QPushButton>
 23#include <QDialogButtonBox>
 24
 25#include "SourceList.h"
 26#include "ViewManager.h"
 27#include "playlist/PlaylistModel.h"
 28#include "widgets/OverlayWidget.h"
 29
 30#include "utils/XspfLoader.h"
 31#include "utils/TomahawkUtils.h"
 32#include "utils/Logger.h"
 33
 34#define FILTER_TIMEOUT 280
 35
 36
 37NewPlaylistWidget::NewPlaylistWidget( QWidget* parent )
 38    : QWidget( parent )
 39    , ui( new Ui::NewPlaylistWidget )
 40{
 41    ui->setupUi( this );
 42
 43    m_saveButton = new QPushButton( tr( "&Create Playlist" ) );
 44    m_saveButton->setDefault( true );
 45    m_saveButton->setEnabled( false );
 46
 47    ui->buttonBox->addButton( m_saveButton, QDialogButtonBox::AcceptRole );
 48
 49    connect( ui->titleEdit, SIGNAL( textChanged( QString ) ), SLOT( onTitleChanged( QString ) ) );
 50    connect( ui->tagEdit, SIGNAL( textChanged( QString ) ), SLOT( onTagChanged() ) );
 51    connect( ui->buttonBox, SIGNAL( accepted() ), SLOT( savePlaylist() ) );
 52    connect( ui->buttonBox, SIGNAL( rejected() ), SLOT( cancel() ) );
 53
 54    m_suggestionsModel = new PlaylistModel( ui->suggestionsView );
 55    ui->suggestionsView->setPlaylistModel( m_suggestionsModel );
 56    ui->suggestionsView->overlay()->setEnabled( false );
 57
 58    connect( &m_filterTimer, SIGNAL( timeout() ), SLOT( updateSuggestions() ) );
 59
 60    ui->titleEdit->setFocus();
 61}
 62
 63
 64NewPlaylistWidget::~NewPlaylistWidget()
 65{
 66    delete ui;
 67}
 68
 69
 70void
 71NewPlaylistWidget::changeEvent( QEvent* e )
 72{
 73    QWidget::changeEvent( e );
 74    switch ( e->type() )
 75    {
 76        case QEvent::LanguageChange:
 77            ui->retranslateUi( this );
 78            break;
 79
 80        default:
 81            break;
 82    }
 83}
 84
 85
 86void
 87NewPlaylistWidget::onTitleChanged( const QString& title )
 88{
 89    m_saveButton->setEnabled( !title.isEmpty() );
 90}
 91
 92
 93void
 94NewPlaylistWidget::onTagChanged()
 95{
 96    m_tag = ui->tagEdit->text();
 97
 98    m_filterTimer.stop();
 99    m_filterTimer.setInterval( FILTER_TIMEOUT );
100    m_filterTimer.setSingleShot( true );
101    m_filterTimer.start();
102}
103
104
105void
106NewPlaylistWidget::updateSuggestions()
107{
108    QUrl url( QString( "http://ws.audioscrobbler.com/1.0/tag/%1/toptracks.xspf" ).arg( m_tag ) );
109
110    XSPFLoader* loader = new XSPFLoader( false );
111    connect( loader, SIGNAL( ok( Tomahawk::playlist_ptr ) ), SLOT( suggestionsFound() ) );
112
113    loader->load( url );
114}
115
116
117void
118NewPlaylistWidget::suggestionsFound()
119{
120    XSPFLoader* loader = qobject_cast<XSPFLoader*>( sender() );
121
122    m_queries = loader->entries();
123
124    delete m_suggestionsModel;
125    m_suggestionsModel = new PlaylistModel( ui->suggestionsView );
126    ui->suggestionsView->setPlaylistModel( m_suggestionsModel );
127
128    m_suggestionsModel->appendQueries( m_queries );
129
130    loader->deleteLater();
131}
132
133
134void
135NewPlaylistWidget::savePlaylist()
136{
137    Tomahawk::playlist_ptr playlist;
138
139    playlist = Tomahawk::Playlist::create( SourceList::instance()->getLocal(), uuid(), ui->titleEdit->text(), "", "", false, m_queries );
140
141    ViewManager::instance()->show( playlist );
142    cancel();
143}
144
145
146void
147NewPlaylistWidget::cancel()
148{
149    // will be deleted by viewmanager
150    emit destroyed( this );
151}