From 02da3b44b8b51347169b84efd6ee48c9dbcab92a Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 11 Jun 2008 19:52:53 -0400 Subject: Made next songs queueable and fixed getSong code all around. --- collectionsorter.cpp | 8 ++-- playerwindow.cpp | 124 +++++++++++++++++++++++++-------------------------- playerwindow.h | 22 ++++----- 3 files changed, 73 insertions(+), 81 deletions(-) diff --git a/collectionsorter.cpp b/collectionsorter.cpp index 5925acc..803bc38 100755 --- a/collectionsorter.cpp +++ b/collectionsorter.cpp @@ -39,13 +39,13 @@ QModelIndex CollectionSorter::peekNext() const { if(m_selected + 1 >= rowCount() || rowCount() == 0) return QModelIndex(); - return index(m_selected + 1, 0); + return mapToSource(index(m_selected + 1, 0)); } QModelIndex CollectionSorter::moveNext() { QModelIndex next = peekNext(); - setCurrentItem(next); + setCurrentItem(mapFromSource(next)); return next; } @@ -53,13 +53,13 @@ QModelIndex CollectionSorter::peekPrevious() const { if(m_selected <= 0 || rowCount() == 0) return QModelIndex(); - return index(m_selected - 1, 0); + return mapToSource(index(m_selected - 1, 0)); } QModelIndex CollectionSorter::movePrevious() { QModelIndex previous = peekPrevious(); - setCurrentItem(previous); + setCurrentItem(mapFromSource(previous)); return previous; } QModelIndex CollectionSorter::currentItem() const diff --git a/playerwindow.cpp b/playerwindow.cpp index 3150b25..467a05e 100755 --- a/playerwindow.cpp +++ b/playerwindow.cpp @@ -24,7 +24,7 @@ Credentials *PlayerWindow::s_credentials = 0; -PlayerWindow::PlayerWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags), m_model(0), m_treeView(0), m_currentSong(0), m_downloader(0), m_sorter(0) +PlayerWindow::PlayerWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags), m_model(0), m_treeView(0), m_downloader(0), m_sorter(0) { setupUi(); setupPhonon(); @@ -40,7 +40,6 @@ void PlayerWindow::setupUi() m_treeView->setSelectionBehavior(QAbstractItemView::SelectRows); m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection); m_treeView->header()->setResizeMode(QHeaderView::Fixed); - connect(m_treeView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(songDoubleClicked(const QModelIndex&))); setCentralWidget(m_treeView); m_downloadProgress = new QProgressBar; @@ -54,11 +53,6 @@ void PlayerWindow::setupUi() m_stopAction->setDisabled(true); m_nextAction->setDisabled(true); m_previousAction->setDisabled(true); - connect(m_playAction, SIGNAL(triggered()), this, SLOT(playTriggered())); - connect(m_pauseAction, SIGNAL(triggered()), this, SLOT(pauseTriggered())); - connect(m_stopAction, SIGNAL(triggered()), this, SLOT(stopTriggered())); - connect(m_nextAction, SIGNAL(triggered()), this, SLOT(nextTriggered())); - connect(m_previousAction, SIGNAL(triggered()), this, SLOT(previousTriggered())); QToolBar *toolBar = new QToolBar(this); toolBar->setIconSize(QSize(16, 16)); @@ -102,6 +96,25 @@ void PlayerWindow::setupUi() show(); } +void PlayerWindow::setupPhonon() +{ + m_mediaObject = new Phonon::MediaObject(this); + m_mediaObject->setPrefinishMark(5000); + m_aboutToFinishTimer.setSingleShot(true); + connect(&m_aboutToFinishTimer, SIGNAL(timeout()), this, SLOT(setAlmostCurrentSong())); + connect(m_mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(playerStateChanged(Phonon::State,Phonon::State))); + connect(m_mediaObject, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(aboutToFinish(qint32))); + connect(m_mediaObject, SIGNAL(bufferStatus(int)), m_downloadProgress, SLOT(setValue(int))); + m_seekSlider->setMediaObject(m_mediaObject); + Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, m_mediaObject); + Phonon::createPath(m_mediaObject, audioOutput); + m_volumeSlider->setAudioOutput(audioOutput); + connect(m_playAction, SIGNAL(triggered()), m_mediaObject, SLOT(play())); + connect(m_pauseAction, SIGNAL(triggered()), m_mediaObject, SLOT(pause())); + connect(m_stopAction, SIGNAL(triggered()), m_mediaObject, SLOT(stop())); + connect(m_nextAction, SIGNAL(triggered()), this, SLOT(nextTriggered())); + connect(m_previousAction, SIGNAL(triggered()), this, SLOT(previousTriggered())); +} Credentials* PlayerWindow::credentials() { if(!s_credentials) @@ -124,34 +137,21 @@ void PlayerWindow::requestCollection() m_downloader->setUser(credentials()->username(), credentials()->password()); m_downloadId = m_downloader->get(credentials()->listingPath(), m_buffer); } -void PlayerWindow::setupPhonon() -{ - m_mediaObject = new Phonon::MediaObject(this); - m_mediaObject->setPrefinishMark(1000); - connect(m_mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(playerStateChanged(Phonon::State,Phonon::State))); - connect(m_mediaObject, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(aboutToFinish(qint32))); - connect(m_mediaObject, SIGNAL(bufferStatus(int)), m_downloadProgress, SLOT(setValue(int))); - connect(&m_aboutToFinishTimer, SIGNAL(timeout()), this, SLOT(nextTriggered())); - m_aboutToFinishTimer.setSingleShot(true); - m_seekSlider->setMediaObject(m_mediaObject); - Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, m_mediaObject); - Phonon::createPath(m_mediaObject, audioOutput); - m_volumeSlider->setAudioOutput(audioOutput); -} -void PlayerWindow::aboutToFinish(qint32 msecToEnd) -{ - m_aboutToFinishTimer.start(msecToEnd); -} void PlayerWindow::playerStateChanged(Phonon::State /*newState*/, Phonon::State /*oldState*/) { updateControls(); } void PlayerWindow::updateControls() { + m_mediaObject->clearQueue(); + m_aboutToFinishTimer.stop(); + statusBar()->clearMessage(); QString message; - if(m_currentSong) - message = QString("%4: %1 - %2 - %3").arg(m_currentSong->artist()).arg(m_currentSong->album()).arg(m_currentSong->title()); + + Song *currentSong = m_model->song(m_sorter->currentItem()); + if(currentSong) + message = QString("%4: %1 - %2 - %3").arg(currentSong->artist()).arg(currentSong->album()).arg(currentSong->title()); switch(m_mediaObject->state()) { case Phonon::LoadingState: @@ -159,7 +159,7 @@ void PlayerWindow::updateControls() m_pauseAction->setDisabled(true); m_stopAction->setDisabled(true); m_downloadProgress->hide(); - if(m_currentSong) + if(currentSong) statusBar()->showMessage(message.arg("Loading")); break; case Phonon::StoppedState: @@ -172,7 +172,7 @@ void PlayerWindow::updateControls() m_playAction->setDisabled(false); m_pauseAction->setDisabled(true); m_downloadProgress->hide(); - if(m_currentSong) + if(currentSong) statusBar()->showMessage(message.arg("Paused")); break; case Phonon::PlayingState: @@ -180,7 +180,7 @@ void PlayerWindow::updateControls() m_pauseAction->setDisabled(false); m_stopAction->setDisabled(false); m_downloadProgress->hide(); - if(m_currentSong) + if(currentSong) statusBar()->showMessage(message.arg("Playing")); break; case Phonon::BufferingState: @@ -204,11 +204,38 @@ void PlayerWindow::updateControls() } void PlayerWindow::songDoubleClicked(const QModelIndex &index) { - if(m_sorter) { - m_sorter->setCurrentItem(index); - startTrack(); + m_sorter->setCurrentItem(index); + playIndex(m_sorter->currentItem()); +} +void PlayerWindow::nextTriggered() +{ + playIndex(m_sorter->moveNext()); +} +void PlayerWindow::previousTriggered() +{ + playIndex(m_sorter->movePrevious()); +} +void PlayerWindow::playIndex(const QModelIndex &index) +{ + if(index.isValid()) { + m_mediaObject->setCurrentSource(credentials()->songUrl(m_model->song(index)->sha1())); + m_mediaObject->play(); + } +} +void PlayerWindow::aboutToFinish(qint32 msecToEnd) +{ + m_almostCurrentIndex = m_sorter->peekNext(); + if (m_almostCurrentIndex.isValid()) { + m_mediaObject->enqueue(credentials()->songUrl(m_model->song(m_almostCurrentIndex)->sha1())); + m_aboutToFinishTimer.setInterval(msecToEnd); + m_aboutToFinishTimer.start(); } } +void PlayerWindow::setAlmostCurrentSong() +{ + if(m_almostCurrentIndex.isValid()) + m_sorter->setCurrentItem(m_almostCurrentIndex); +} void PlayerWindow::dataReadProgress(int done, int total) { m_downloadProgress->setMaximum(total); @@ -266,6 +293,7 @@ void PlayerWindow::requestFinished(int id, bool error) m_sorter = new CollectionSorter(this, m_model); m_treeView->setModel(m_sorter); m_searchEdit->setDisabled(false); + connect(m_treeView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(songDoubleClicked(const QModelIndex&))); credentials()->hide(); updateControls(); rowChange(); @@ -280,36 +308,6 @@ void PlayerWindow::databaseDownloadFailed() credentials()->setDisabled(false); statusBar()->hide(); } -void PlayerWindow::startTrack() -{ - m_currentSong = m_model->song(m_sorter->currentItem()); - m_mediaObject->stop(); - m_mediaObject->setCurrentSource(credentials()->songUrl(m_currentSong->sha1())); - m_mediaObject->play(); - updateControls(); -} -void PlayerWindow::nextTriggered() -{ - if (m_sorter->moveNext().isValid()) - startTrack(); -} -void PlayerWindow::previousTriggered() -{ - if(m_sorter->movePrevious().isValid()) - startTrack(); -} -void PlayerWindow::pauseTriggered() -{ - m_mediaObject->pause(); -} -void PlayerWindow::playTriggered() -{ - m_mediaObject->play(); -} -void PlayerWindow::stopTriggered() -{ - m_mediaObject->stop(); -} void PlayerWindow::rowChange() { m_treeView->setUpdatesEnabled(false); diff --git a/playerwindow.h b/playerwindow.h index 70a9406..d45ba00 100755 --- a/playerwindow.h +++ b/playerwindow.h @@ -11,14 +11,9 @@ #include #include #include +#include -#include -#include -#include -#include -#include -#include -#include +#include class Song; class QBuffer; @@ -41,16 +36,15 @@ private slots: void requestFinished(int id, bool error); void dataReadProgress(int done, int total); void songDoubleClicked(const QModelIndex &index); - void playTriggered(); - void pauseTriggered(); - void stopTriggered(); + void playIndex(const QModelIndex &index); void nextTriggered(); void previousTriggered(); void aboutToFinish(qint32 msecToEnd); void playerStateChanged(Phonon::State /*newState*/, Phonon::State /*oldState*/); void requestCollection(); void searchChanged(const QString&); - void updateSearch(); + void updateSearch(); + void setAlmostCurrentSong(); private: QBuffer *m_buffer; @@ -68,20 +62,20 @@ private: Phonon::SeekSlider *m_seekSlider; Phonon::MediaObject *m_mediaObject; SearchLineEdit *m_searchEdit; - QTimer m_aboutToFinishTimer; QTimer m_searchTimer; + QTimer m_aboutToFinishTimer; QHttp *m_downloader; - Song *m_currentSong; double m_suggestedRatio1; double m_suggestedRatio2; double m_suggestedRatio3; int m_suggestedWidthTotal; static Credentials *s_credentials; + QModelIndex m_almostCurrentIndex; void setupUi(); void setupPhonon(); void updateControls(); - void startTrack(); + void playCurrentTrack(); void databaseDownloadFailed(); void fixColumnSizes(); void rowChange(); -- cgit v1.2.3-59-g8ed1b