Playlist manager for handling multiple playlists. Storing/restoring is enabled.

This commit is contained in:
Bartek Taczała 2010-03-07 20:06:00 +00:00
parent 1aebf19077
commit 8ae713802d
7 changed files with 283 additions and 108 deletions

View File

@ -54,6 +54,7 @@ set(CLEMENTINE-SOURCES
albumcovermanager.cpp
albumcoverloader.cpp
m3uparser.cpp
playlistmanager.cpp
)
# Header files that have Q_OBJECT in
@ -101,6 +102,7 @@ set(CLEMENTINE-MOC-HEADERS
albumcovermanager.h
albumcoverloader.h
m3uparser.h
playlistmanager.h
)
# UI files

View File

@ -21,6 +21,7 @@
#include "stylesheetloader.h"
#include "albumcovermanager.h"
#include "m3uparser.h"
#include "playlistmanager.h"
#include "qxtglobalshortcut.h"
@ -38,6 +39,7 @@
#include <cmath>
const int MainWindow::kStateVersion = 1;
const char* MainWindow::kSettingsGroup = "MainWindow";
const char* MainWindow::kMediaFilterSpec =
@ -64,7 +66,8 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
playlist_menu_(new QMenu(this)),
library_sort_model_(new QSortFilterProxyModel(this)),
track_position_timer_(new QTimer(this)),
next_playlist_number_(1)
next_playlist_number_(1),
playlistManager_( new PlaylistManager(this) )
{
ui_.setupUi(this);
tray_icon_->setIcon(windowIcon());
@ -146,6 +149,9 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
connect(player_, SIGNAL(Paused()), osd_, SLOT(Paused()));
connect(player_, SIGNAL(Stopped()), osd_, SLOT(Stopped()));
connect(player_, SIGNAL(VolumeChanged(int)), osd_, SLOT(VolumeChanged(int)));
// PlaylistManager connections
connect (playlistManager_, SIGNAL(CurrentPlaylistChanged(Playlist*)), this, SLOT(CurrentPlaylistChanged(Playlist*)));
connect(track_slider_, SIGNAL(ValueChanged(int)), player_, SLOT(Seek(int)));
@ -211,6 +217,7 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
connect(radio_model_, SIGNAL(StreamError(QString)), SLOT(ReportError(QString)));
connect(radio_model_, SIGNAL(StreamFinished()), player_, SLOT(NextItem()));
connect(radio_model_, SIGNAL(StreamReady(QUrl,QUrl)), player_, SLOT(StreamReady(QUrl,QUrl)));
connect(radio_model_, SIGNAL(StreamMetadataFound(QUrl,Song)), current_playlist_, SLOT(SetStreamMetadata(QUrl,Song)));
connect(radio_model_, SIGNAL(AddItemToPlaylist(RadioItem*)), SLOT(InsertRadioItem(RadioItem*)));
connect(radio_model_->GetLastFMService(), SIGNAL(ScrobblingEnabledChanged(bool)), SLOT(ScrobblingEnabledChanged(bool)));
connect(ui_.radio_view, SIGNAL(doubleClicked(QModelIndex)), SLOT(RadioDoubleClick(QModelIndex)));
@ -295,25 +302,17 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
}
library_->StartThreads();
settings_.endGroup();
settings_.beginGroup(Playlist::kSettingsGroup);
foreach (const QString& group, settings_.childGroups()) {
QRegExp re(" \\d+");
if (re.indexIn(group) != -1) {
next_playlist_number_ = qMax(re.cap(0).toInt() + 1, next_playlist_number_);
}
NewPlaylist(group);
}
if (settings_.childGroups().isEmpty()) {
NewPlaylist();
}
settings_.endGroup();
settings_.beginGroup(kSettingsGroup);
playlistManager_->SetTabWidget(ui_.tabWidget);
qDebug() << "restoring";
playlistManager_->Restore() ;
}
MainWindow::~MainWindow() {
SaveGeometry();
playlistManager_->Save() ;
delete player_ ;
}
void MainWindow::HideShowTrayIcon() {
@ -657,48 +656,53 @@ void MainWindow::AddStreamAccepted() {
current_playlist_->InsertStreamUrls(urls);
}
void MainWindow::NewPlaylist(const QString& title) {
PlaylistView* playlist_view = new PlaylistView(ui_.tabWidget);
playlist_view->setObjectName(QString::fromUtf8("playlist"));
playlist_view->setAcceptDrops(true);
playlist_view->setDragEnabled(true);
playlist_view->setDragDropMode(QAbstractItemView::DragDrop);
playlist_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
playlist_view->setRootIsDecorated(false);
playlist_view->setUniformRowHeights(true);
playlist_view->setItemsExpandable(false);
playlist_view->setSortingEnabled(true);
playlist_view->setAllColumnsShowFocus(true);
Playlist* playlist = new Playlist(playlist_view) ;
playlist->IgnoreSorting(true);
playlist_view->setModel(playlist);
playlist->IgnoreSorting(false);
QString actual_title = title;
if (title.isEmpty()) {
actual_title = tr("New playlist %1").arg(next_playlist_number_++);
playlist->SetTitle(actual_title);
} else {
playlist->SetTitle(title);
playlist->Restore();
}
ui_.tabWidget->addTab(playlist_view, actual_title);
SetCurrentPlaylist(playlist_view);
void MainWindow::NewPlaylist()
{
playlistManager_->addPlaylist();
// PlaylistView * playListView = new PlaylistView(ui_.tabWidget);
// playListView->setObjectName(QString::fromUtf8("playlist"));
// playListView->setAcceptDrops(true);
// playListView->setDragEnabled(true);
// playListView->setDragDropMode(QAbstractItemView::DragDrop);
// playListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
// playListView->setRootIsDecorated(false);
// playListView->setUniformRowHeights(true);
// playListView->setItemsExpandable(false);
// playListView->setSortingEnabled(true);
// playListView->setAllColumnsShowFocus(true);
// Playlist * playList = new Playlist(playListView) ;
//
//
//
// playList->IgnoreSorting(true);
// playListView->setModel(playList);
// playList->IgnoreSorting(false);
//
// QString title = playList->Title();
// if ( title.isEmpty() )
// title = tr("New playlist") ;
// ui_.tabWidget->addTab(playListView, tr("New playlist"));
//
// SetCurrentPlaylist(playListView);
// if (title.isEmpty()) {
// actual_title = tr("New playlist %1").arg(next_playlist_number_++);
// playlist->SetTitle(actual_title);
// } else {
// playlist->SetTitle(title);
// playlist->Restore();
// }
// ui_.tabWidget->addTab(playlist_view, actual_title);
}
void MainWindow::SetCurrentPlaylist(PlaylistView* current) {
// Disconnect current playlist and tab.
if (current_playlist_) {
void MainWindow::SetCurrentPlaylist(PlaylistView* pCurrent)
{
// tab widget ;
//disconnects!!
disconnect(current_playlist_, SIGNAL(CurrentSongChanged(Song)), osd_, SLOT(SongChanged(Song)));
disconnect(current_playlist_view_, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(PlayIndex(QModelIndex)));
disconnect(current_playlist_view_, SIGNAL(PlayPauseItem(QModelIndex)),
this, SLOT(PlayIndex(QModelIndex)));
disconnect(current_playlist_view_, SIGNAL(RightClicked(QPoint,QModelIndex)),
this, SLOT(PlaylistRightClick(QPoint,QModelIndex)));
disconnect(current_playlist_view_, SIGNAL(doubleClicked(QModelIndex)),this, SLOT(PlayIndex(QModelIndex)));
disconnect(current_playlist_view_, SIGNAL(PlayPauseItem(QModelIndex)),this, SLOT(PlayIndex(QModelIndex)));
disconnect(current_playlist_view_, SIGNAL(RightClicked(QPoint,QModelIndex)),this, SLOT(PlaylistRightClick(QPoint,QModelIndex)));
disconnect(ui_.action_clear_playlist, SIGNAL(triggered()), current_playlist_, SLOT(Clear()));
disconnect(ui_.action_shuffle, SIGNAL(triggered()), current_playlist_, SLOT(Shuffle()));
disconnect(player_, SIGNAL(Paused()), current_playlist_, SLOT(Paused()));
@ -708,35 +712,42 @@ void MainWindow::SetCurrentPlaylist(PlaylistView* current) {
disconnect(player_, SIGNAL(Paused()), current_playlist_view_, SLOT(StopGlowing()));
disconnect(player_, SIGNAL(Playing()), current_playlist_view_, SLOT(StartGlowing()));
disconnect(player_, SIGNAL(Stopped()), current_playlist_view_, SLOT(StopGlowing()));
}
disconnect(radio_model_, SIGNAL(StreamMetadataFound(QUrl,Song)), current_playlist_, SLOT(SetStreamMetadata(QUrl,Song)));
// repin pointers
ui_.tabWidget->setCurrentWidget(pCurrent);
current_playlist_view_ = pCurrent ;
current_playlist_ = qobject_cast< Playlist* >( pCurrent->model() );
player_->SetCurrentPlaylist(current_playlist_);
// connects !! :)
connect(current_playlist_, SIGNAL(CurrentSongChanged(Song)), osd_, SLOT(SongChanged(Song)));
connect(current_playlist_view_, SIGNAL(doubleClicked(QModelIndex)), SLOT(PlayIndex(QModelIndex)));
connect(current_playlist_view_, SIGNAL(PlayPauseItem(QModelIndex)), SLOT(PlayIndex(QModelIndex)));
connect(current_playlist_view_, SIGNAL(RightClicked(QPoint,QModelIndex)), SLOT(PlaylistRightClick(QPoint,QModelIndex)));
connect(ui_.action_clear_playlist, SIGNAL(triggered()), current_playlist_, SLOT(Clear()));
connect(ui_.action_shuffle, SIGNAL(triggered()), current_playlist_, SLOT(Shuffle()));
connect(player_, SIGNAL(Paused()), current_playlist_, SLOT(Paused()));
connect(player_, SIGNAL(Playing()), current_playlist_, SLOT(Playing()));
connect(player_, SIGNAL(Stopped()), current_playlist_, SLOT(Stopped()));
// Repin pointers.
ui_.tabWidget->setCurrentWidget(current);
current_playlist_view_ = current;
current_playlist_ = qobject_cast<Playlist*>(current->model());
player_->SetCurrentPlaylist(current_playlist_);
// Connect new playlist and tab.
connect(current_playlist_, SIGNAL(CurrentSongChanged(Song)), osd_, SLOT(SongChanged(Song)));
connect(current_playlist_view_, SIGNAL(doubleClicked(QModelIndex)), SLOT(PlayIndex(QModelIndex)));
connect(current_playlist_view_, SIGNAL(PlayPauseItem(QModelIndex)), SLOT(PlayIndex(QModelIndex)));
connect(current_playlist_view_, SIGNAL(RightClicked(QPoint,QModelIndex)),
SLOT(PlaylistRightClick(QPoint,QModelIndex)));
connect(ui_.action_clear_playlist, SIGNAL(triggered()), current_playlist_, SLOT(Clear()));
connect(ui_.action_shuffle, SIGNAL(triggered()), current_playlist_, SLOT(Shuffle()));
connect(player_, SIGNAL(Paused()), current_playlist_, SLOT(Paused()));
connect(player_, SIGNAL(Playing()), current_playlist_, SLOT(Playing()));
connect(player_, SIGNAL(Stopped()), current_playlist_, SLOT(Stopped()));
connect(player_, SIGNAL(Paused()), current_playlist_view_, SLOT(StopGlowing()));
connect(player_, SIGNAL(Playing()), current_playlist_view_, SLOT(StartGlowing()));
connect(player_, SIGNAL(Stopped()), current_playlist_view_, SLOT(StopGlowing()));
connect(radio_model_, SIGNAL(StreamMetadataFound(QUrl,Song)),
current_playlist_, SLOT(SetStreamMetadata(QUrl,Song)));
connect(player_, SIGNAL(Paused()), current_playlist_view_, SLOT(StopGlowing()));
connect(player_, SIGNAL(Playing()), current_playlist_view_, SLOT(StartGlowing()));
connect(player_, SIGNAL(Stopped()), current_playlist_view_, SLOT(StopGlowing()));
connect(radio_model_, SIGNAL(StreamMetadataFound(QUrl,Song)), current_playlist_, SLOT(SetStreamMetadata(QUrl,Song)));
}
void MainWindow::CurrentTabChanged(int index) {
PlaylistView* current = qobject_cast<PlaylistView*>(ui_.tabWidget->currentWidget());
SetCurrentPlaylist(current);
void MainWindow::CurrentTabChanged(int index )
{
PlaylistView *pCurrent = qobject_cast< PlaylistView* >( ui_.tabWidget->currentWidget() );
SetCurrentPlaylist(pCurrent);
}
void MainWindow::CurrentPlaylistChanged(Playlist* pPlaylist)
{
}

View File

@ -7,6 +7,7 @@
#include "ui_mainwindow.h"
class PlaylistManager;
class Playlist;
class PlaylistView;
class Player;
@ -83,10 +84,12 @@ class MainWindow : public QMainWindow {
void AddMedia();
void AddStream();
void AddStreamAccepted();
void NewPlaylist(const QString& title = QString());
void CurrentTabChanged(int);
void NewPlaylist() ;
void CurrentTabChanged ( int ) ;
void CurrentPlaylistChanged( Playlist * pPlaylist ) ;
private:
void SaveGeometry();
@ -129,7 +132,7 @@ class MainWindow : public QMainWindow {
QTimer* track_position_timer_;
QSettings settings_;
PlaylistManager * playlistManager_ ;
int next_playlist_number_;
};

View File

@ -25,7 +25,9 @@ Playlist::Playlist(QObject *parent) :
current_is_paused_(false),
scrobble_point_(-1),
has_scrobbled_(false),
ignore_sorting_(false)
ignore_sorting_(false),
title_(""),
index_(-1)
{
}
@ -206,7 +208,7 @@ bool Playlist::dropMimeData(const QMimeData* data, Qt::DropAction action, int ro
}
layoutChanged();
Save();
// Save();
} else if (data->hasUrls()) {
// URL list dragged from the file list or some other app
@ -270,7 +272,7 @@ QModelIndex Playlist::InsertItems(const QList<PlaylistItem*>& items, int after)
endInsertRows();
Save();
// Save();
return index(start, 0);
}
@ -370,7 +372,7 @@ void Playlist::sort(int column, Qt::SortOrder order) {
layoutChanged();
Save();
// Save();
}
void Playlist::Playing() {
@ -396,11 +398,11 @@ void Playlist::SetCurrentIsPaused(bool paused) {
index(current_item_.row(), ColumnCount));
}
void Playlist::Save() const {
void Playlist::SaveR() const {
QSettings s;
s.beginGroup(kSettingsGroup);
s.beginGroup(title_);
Q_ASSERT(index_ != -1 ) ;
QString setGrp = kSettingsGroup + QString::number(index_) ;
s.beginGroup(setGrp);
s.beginWriteArray("items", items_.count());
for (int i=0 ; i<items_.count() ; ++i) {
s.setArrayIndex(i);
@ -408,15 +410,16 @@ void Playlist::Save() const {
items_.at(i)->Save(s);
}
s.endArray();
s.setValue("title",title_);
}
void Playlist::Restore() {
void Playlist::RestoreR() {
qDeleteAll(items_);
items_.clear();
QSettings s;
s.beginGroup(kSettingsGroup);
s.beginGroup(title_);
QString setGrp = kSettingsGroup + QString::number(index_) ;
s.beginGroup(setGrp);
int count = s.beginReadArray("items");
for (int i=0 ; i<count ; ++i) {
@ -431,7 +434,7 @@ void Playlist::Restore() {
items_ << item;
}
s.endArray();
title_ = s.value("title").toString();
reset();
}
@ -444,7 +447,7 @@ bool Playlist::removeRows(int row, int count, const QModelIndex& parent) {
endRemoveRows();
Save();
// Save();
return true;
}
@ -535,7 +538,7 @@ void Playlist::Clear() {
items_.clear();
reset();
Save();
// Save();
}
void Playlist::ReloadItems(const QList<int>& rows) {
@ -564,5 +567,5 @@ void Playlist::Shuffle() {
layoutChanged();
Save();
// Save();
}

View File

@ -13,6 +13,7 @@ class RadioService;
class Playlist : public QAbstractListModel {
Q_OBJECT
friend class PlaylistManager ;
public:
Playlist(QObject* parent = 0);
~Playlist();
@ -48,9 +49,7 @@ class Playlist : public QAbstractListModel {
static bool CompareItems(int column, Qt::SortOrder order,
const PlaylistItem* a, const PlaylistItem* b);
// Persistence
void Save() const;
void Restore();
// Accessors
int current_index() const;
@ -64,8 +63,11 @@ class Playlist : public QAbstractListModel {
PlaylistItem::Options current_item_options() const;
Song current_item_metadata() const;
const QString& Title() const { return title_; }
const QString & GetTitle() const { return title_ ; }
void SetTitle(const QString& title) { title_ = title; }
void SetPlaylistIndex( int ipos ) { index_ = ipos ; }
int GetPlaylistIndex() const { return index_ ; }
// Scrobbling
int scrobble_point() const { return scrobble_point_; }
@ -113,6 +115,10 @@ class Playlist : public QAbstractListModel {
private:
void SetCurrentIsPaused(bool paused);
void UpdateScrobblePoint();
// Persistence
void SaveR() const;
void RestoreR();
private:
QList<PlaylistItem*> items_;
@ -127,6 +133,7 @@ class Playlist : public QAbstractListModel {
// Hack to stop QTreeView::setModel sorting the playlist
bool ignore_sorting_;
QString title_;
int index_ ;
};
#endif // PLAYLIST_H

112
src/playlistmanager.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "playlistmanager.h"
#include "playlistview.h"
#include "playlist.h"
#include <QAbstractItemView>
#include <QSettings>
#include <QDebug>
PlaylistManager::PlaylistManager(QObject* parent): QObject(parent),
pTabWidget_( NULL ),
pCurrentPlaylist_(NULL),
pCurrentPlaylistView_(NULL),
playlistCount_(0)
{
}
void PlaylistManager::addPlaylist(const QString & playlistName /*= QString()*/)
{
Q_ASSERT ( pTabWidget_ != NULL ) ;
PlaylistView * playListView = new PlaylistView(pTabWidget_);
playListView->setObjectName(QString::fromUtf8("playlist"));
playListView->setAcceptDrops(true);
playListView->setDragEnabled(true);
playListView->setDragDropMode(QAbstractItemView::DragDrop);
playListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
playListView->setRootIsDecorated(false);
playListView->setUniformRowHeights(true);
playListView->setItemsExpandable(false);
playListView->setSortingEnabled(true);
playListView->setAllColumnsShowFocus(true);
Playlist * playList = new Playlist(playListView) ;
playList->IgnoreSorting(true);
playListView->setModel(playList);
playList->IgnoreSorting(false);
QString title = playList->GetTitle();
playlistCount_++ ;
if ( title.isEmpty() )
title = tr("New playlist") + QString::number(playlistCount_) ;
playList->SetPlaylistIndex( playlistCount_) ;
playList->SetTitle(title);
pTabWidget_->addTab(playListView, title);
SetCurrentPlaylist(playList);
pCurrentPlaylistView_ = playListView ;
playlists_ << playList ;
}
void PlaylistManager::SetCurrentPlaylist(Playlist* pPlaylist)
{
pCurrentPlaylist_ = pPlaylist ;
emit CurrentPlaylistChanged( pCurrentPlaylist_ ) ;
}
void PlaylistManager::Save() const
{
QSettings s ;
Q_FOREACH ( Playlist *p, playlists_ ) {
qDebug() << "Saving" << p->GetTitle() ;
p->SaveR() ;
}
s.setValue("numberofplaylists", playlistCount_ ) ;
}
void PlaylistManager::Restore()
{
Q_ASSERT ( pTabWidget_ != NULL ) ;
QSettings s ;
bool bOk ;
int nOfPlaylist = s.value("numberofplaylists").toInt(&bOk) ;
playlistCount_ = nOfPlaylist ;
qDebug() << nOfPlaylist ;
if ( bOk == false || nOfPlaylist == 0 ) {
qDebug()<< "No reading from settings";
return ;
}
int nCurrentIndex = -1 ;
for ( int i=0;i<nOfPlaylist;++i){
PlaylistView * playListView = new PlaylistView(pTabWidget_);
playListView->setObjectName(QString::fromUtf8("playlist"));
playListView->setAcceptDrops(true);
playListView->setDragEnabled(true);
playListView->setDragDropMode(QAbstractItemView::DragDrop);
playListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
playListView->setRootIsDecorated(false);
playListView->setUniformRowHeights(true);
playListView->setItemsExpandable(false);
playListView->setSortingEnabled(true);
playListView->setAllColumnsShowFocus(true);
Playlist * playList = new Playlist(playListView) ;
playList->IgnoreSorting(true);
playListView->setModel(playList);
playList->IgnoreSorting(false);
playList->SetPlaylistIndex( i+1 ) ;
playList->RestoreR() ;
pTabWidget_->addTab( playListView, playList->GetTitle() );
SetCurrentPlaylist(playList);
pCurrentPlaylistView_ = playListView ;
playlists_ << playList ;
}
}

37
src/playlistmanager.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef PLAYLISTMANAGER_H
#define PLAYLISTMANAGER_H
#include <QtCore/QObject>
class PlaylistView;
class QTabWidget;
class Playlist;
class PlaylistManager : public QObject{
Q_OBJECT
public:
PlaylistManager(QObject* parent = 0);
void addPlaylist( const QString& playlistName = QString() ) ;
// accessors
void SetTabWidget( QTabWidget * pWidget ) { pTabWidget_ = pWidget ; }
QTabWidget * GetTabWidget() const {return pTabWidget_; }
void SetCurrentPlaylist ( Playlist * pPlaylist ) ;
void Save() const ;
void Restore() ;
private:
QList<Playlist*> playlists_ ;
QTabWidget * pTabWidget_ ;
QList<QString> playlistNames_ ;
Playlist * pCurrentPlaylist_ ;
PlaylistView * pCurrentPlaylistView_ ;
int playlistCount_ ;
signals:
void CurrentPlaylistChanged( Playlist * pPlaylist ) ;
};
#endif // PLAYLISTMANAGER_H