diff options
Diffstat (limited to 'playerwidget.cpp')
-rw-r--r-- | playerwidget.cpp | 184 |
1 files changed, 100 insertions, 84 deletions
diff --git a/playerwidget.cpp b/playerwidget.cpp index 4c9af4a..f90788b 100644 --- a/playerwidget.cpp +++ b/playerwidget.cpp @@ -29,6 +29,7 @@ #include <QFontMetrics> #include <QStackedWidget> #include <QSplashScreen> +#include <QTabWidget> #include <algorithm> #include <taglib/tag.h> @@ -159,35 +160,42 @@ void PlayerWidget::setupGui(QSplashScreen *splash){ } for(int i = 0; i < mCollectionStack->count(); ++i){ CollectionWidget *curW = qobject_cast<CollectionWidget*>(mCollectionStack->widget(i)); - QAction *addToPlayListA = new QAction(QIcon(":/belly_right.png"), tr("Add to playlist"), this); - connect(addToPlayListA, &QAction::triggered, this, &PlayerWidget::addToPlayList); - curW->view()->addAction(addToPlayListA); - QAction *addToPlayListAndClearA = new QAction(QIcon(":/belly_right_and_clear.png"), tr("Clear and add"), this); - connect(addToPlayListAndClearA, &QAction::triggered, this, &PlayerWidget::addToPlayListAndClear); - curW->view()->addAction(addToPlayListAndClearA); - curW->view()->addAction(Helper::createSeparator(this)); - QStandardItemModel *model = curW->model(); - QModelIndex rootIdx = model->invisibleRootItem()->index(); - if(model->hasChildren(model->index(0, 0, rootIdx))){ - QAction *expandA = new QAction(Helper::iconFromQChar(QChar(0x2640), 90), tr("Expand"), this); - connect(expandA, &QAction::triggered, [curW] { curW->view()->expandOrCollapse(BeetView::Expand);} ); - curW->view()->addAction(expandA); - QAction *collapseA = new QAction(Helper::iconFromQChar(QChar(0x2642), 120), tr("Collapse"), this); - connect(collapseA, &QAction::triggered, [curW] { curW->view()->expandOrCollapse(BeetView::Collapse);} ); - curW->view()->addAction(collapseA); + const QString name = curW->objectName(); + if(name == "webradio"){ + QAction *playA = new QAction(QIcon(":/play.png"), tr("Play"), this); + connect(playA, &QAction::triggered, [this, curW] { playUrl(curW->view()->selectionModel()->currentIndex()); }); + curW->view()->addAction(playA); + }else{ + QAction *addToPlayListA = new QAction(QIcon(":/belly_right.png"), tr("Add to playlist"), this); + connect(addToPlayListA, &QAction::triggered, this, &PlayerWidget::addToPlayList); + curW->view()->addAction(addToPlayListA); + QAction *addToPlayListAndClearA = new QAction(QIcon(":/belly_right_and_clear.png"), tr("Clear and add"), this); + connect(addToPlayListAndClearA, &QAction::triggered, this, &PlayerWidget::addToPlayListAndClear); + curW->view()->addAction(addToPlayListAndClearA); + curW->view()->addAction(Helper::createSeparator(this)); + QStandardItemModel *model = curW->model(); + QModelIndex rootIdx = model->invisibleRootItem()->index(); + if(model->hasChildren(model->index(0, 0, rootIdx))){ + QAction *expandA = new QAction(Helper::iconFromQChar(QChar(0x2640), 90), tr("Expand"), this); + connect(expandA, &QAction::triggered, [curW] { curW->view()->expandOrCollapse(BeetView::Expand);} ); + curW->view()->addAction(expandA); + QAction *collapseA = new QAction(Helper::iconFromQChar(QChar(0x2642), 120), tr("Collapse"), this); + connect(collapseA, &QAction::triggered, [curW] { curW->view()->expandOrCollapse(BeetView::Collapse);} ); + curW->view()->addAction(collapseA); + curW->view()->addAction(Helper::createSeparator(this)); + } + QAction *refreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh view"), this); + connect(refreshA, &QAction::triggered, [curW] { + qApp->setOverrideCursor(Qt::BusyCursor); + curW->populate(); + qApp->restoreOverrideCursor(); } + ); + curW->view()->addAction(refreshA); curW->view()->addAction(Helper::createSeparator(this)); + QAction *searchMBA = new QAction(QIcon(":/bizarre_amputee.png"), tr("Search Musicbrainz"), this); + connect(searchMBA, &QAction::triggered, [this, curW] { searchMusicbrainz(curW->view()->selectionModel()->currentIndex()); }); + curW->view()->addAction(searchMBA); } - QAction *refreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh view"), this); - connect(refreshA, &QAction::triggered, [curW] { - qApp->setOverrideCursor(Qt::BusyCursor); - curW->populate(); - qApp->restoreOverrideCursor(); } - ); - curW->view()->addAction(refreshA); - curW->view()->addAction(Helper::createSeparator(this)); - QAction *searchMBA = new QAction(QIcon(":/bizarre_amputee.png"), tr("Search Musicbrainz"), this); - connect(searchMBA, &QAction::triggered, [this, curW] { searchMusicbrainz(curW->view()->selectionModel()->currentIndex()); }); - curW->view()->addAction(searchMBA); } QAction *gotoAlbumA = new QAction(QIcon(":/next.png"), tr("Goto album"), this); connect(gotoAlbumA, &QAction::triggered, this, &PlayerWidget::gotoAlbum); @@ -287,24 +295,30 @@ void PlayerWidget::setupGui(QSplashScreen *splash){ centerWidget->setLayout(centerWidgetL); //playlist - mPlayListModel = new QStandardItemModel; - mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title"); - mPlayListView = new BeetView; - mPlayListView->setAlternatingRowColors(true); - mPlayListView->setModel(mPlayListModel); - mPlayListView->setRootIsDecorated(false); - mPlayListView->setSelectionMode(QAbstractItemView::ExtendedSelection); - connect(mPlayListView, &BeetView::doubleClicked, this, &PlayerWidget::playCurrent); - connect(mPlayListView->selectionModel(), &QItemSelectionModel::currentChanged, this, &PlayerWidget::rightCurrentChanged); - QGroupBox *playListGB = new QGroupBox(tr("Playlist")); - QVBoxLayout *playListL = new QVBoxLayout; - playListL->addWidget(mPlayListView); - playListGB->setLayout(playListL); + mSongModel = new QStandardItemModel; + mSongModel->setHorizontalHeaderLabels(QStringList() << "Title"); + mSongView = new BeetView; + mSongView->setAlternatingRowColors(true); + mSongView->setModel(mSongModel); + mSongView->setRootIsDecorated(false); + mSongView->setSelectionMode(QAbstractItemView::ExtendedSelection); + connect(mSongView, &BeetView::doubleClicked, this, &PlayerWidget::playCurrent); + connect(mSongView->selectionModel(), &QItemSelectionModel::currentChanged, this, &PlayerWidget::rightCurrentChanged); + mWebRadioModel = new QStandardItemModel; + mWebRadioModel->setHorizontalHeaderLabels(QStringList() << tr("Title")); + mWebRadioView = new BeetView; + mWebRadioView->setAlternatingRowColors(true); + mWebRadioView->setModel(mWebRadioModel); + mWebRadioView->setRootIsDecorated(false); + mWebRadioView->setSelectionMode(QAbstractItemView::NoSelection); + mPlaylistTab = new QTabWidget; + mPlaylistTab->addTab(mSongView, tr("Files")); + mPlaylistTab->addTab(mWebRadioView, tr("Web")); //right widget QWidget *rightWidget = new QWidget; QVBoxLayout *rightWidgetL = new QVBoxLayout; - rightWidgetL->addWidget(playListGB); + rightWidgetL->addWidget(mPlaylistTab); rightWidget->setLayout(rightWidgetL); //stream data @@ -387,15 +401,15 @@ void PlayerWidget::createActions(){ helpM->addAction(aboutQtA); helpA->setMenu(helpM); QAction *plSearchMB = new QAction(QIcon(":/bizarre_amputee.png"), tr("Search Musicbrainz"), this); - connect(plSearchMB, &QAction::triggered, [this] { searchMusicbrainz(mPlayListView->selectionModel()->currentIndex()); }); - - mPlayListView->addAction(removeFromPlayListA); - mPlayListView->addAction(Helper::createSeparator(this)); - mPlayListView->addAction(plSearchMB); - mPlayListView->addAction(Helper::createSeparator(this)); - mPlayListView->addAction(shufflePlayistA); - mPlayListView->addAction(clearPlayListA); - mPlayListView->addAction(Helper::createSeparator(this)); + connect(plSearchMB, &QAction::triggered, [this] { searchMusicbrainz(mSongView->selectionModel()->currentIndex()); }); + + mSongView->addAction(removeFromPlayListA); + mSongView->addAction(Helper::createSeparator(this)); + mSongView->addAction(plSearchMB); + mSongView->addAction(Helper::createSeparator(this)); + mSongView->addAction(shufflePlayistA); + mSongView->addAction(clearPlayListA); + mSongView->addAction(Helper::createSeparator(this)); QWidget* spacer1 = new QWidget(); spacer1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); mToolBar->addWidget(spacer1); @@ -445,7 +459,7 @@ void PlayerWidget::rightCurrentChanged(const QModelIndex &cur, const QModelIndex } void PlayerWidget::doPlay(){ - if(mPlayer->state() == QMediaPlayer::PausedState){ + if(mPlayer->state() == QMediaPlayer::PausedState || mPlayer->state() == QMediaPlayer::StoppedState){ mPlayer->play(); mPlayA->setChecked(true); emit playModeChanged(tr("Playing")); @@ -454,16 +468,16 @@ void PlayerWidget::doPlay(){ mTrayIcon->setToolTip(mCurToolTip); return; } - int playListCount = mPlayListModel->rowCount(); + int playListCount = mSongModel->rowCount(); if(playListCount == 0){ emit message(tr("Playlist is empty! Dazed and confused, but trying to continue...")); doStop(); return; } - QModelIndexList sel = mPlayListView->selectionModel()->selectedRows(); + QModelIndexList sel = mSongView->selectionModel()->selectedRows(); if(sel.isEmpty()){ - mPlayListView->selectionModel()->select(mPlayListModel->index(0, 0), QItemSelectionModel::SelectCurrent); - sel = mPlayListView->selectionModel()->selectedRows(); + mSongView->selectionModel()->select(mSongModel->index(0, 0), QItemSelectionModel::SelectCurrent); + sel = mSongView->selectionModel()->selectedRows(); } playCurrent(sel.first()); } @@ -564,7 +578,7 @@ void PlayerWidget::recurse(const QModelIndex &parent){ } void PlayerWidget::addSong(const QModelIndex &idx){ - QStandardItem *root = mPlayListModel->invisibleRootItem(); + QStandardItem *root = mSongModel->invisibleRootItem(); QString title = idx.data(CollectionWidget::TitleRole).toString(); QString artist = idx.data(CollectionWidget::ArtistRole).toString(); QString album = idx.data(CollectionWidget::AlbumRole).toString(); @@ -610,9 +624,9 @@ void PlayerWidget::addToPlayList(){ if(type == CollectionWidget::Song){ addSong(i); }else if (type == CollectionWidget::WebRadio){ - mPlayListModel->clear(); - QStandardItem *root = mPlayListModel->invisibleRootItem(); - mPlayListModel->setHorizontalHeaderLabels(QStringList() << tr("Title")); + mSongModel->clear(); + QStandardItem *root = mSongModel->invisibleRootItem(); + mSongModel->setHorizontalHeaderLabels(QStringList() << tr("Title")); QStandardItem *wrItem = new QStandardItem; wrItem->setEditable(false); wrItem->setIcon(QIcon(":dog_hood.png")); @@ -625,7 +639,7 @@ void PlayerWidget::addToPlayList(){ recurse(i); } } - QStandardItem *playListRoot = mPlayListModel->invisibleRootItem(); + QStandardItem *playListRoot = mSongModel->invisibleRootItem(); emit numFilesChanged(playListRoot->rowCount()); emit playListLengthChanged(mPlayListLength); } @@ -636,7 +650,7 @@ void PlayerWidget::addToPlayListAndClear(){ } void PlayerWidget::removeFromPlayList(){ - QModelIndexList sel = mPlayListView->selectionModel()->selectedRows(); + QModelIndexList sel = mSongView->selectionModel()->selectedRows(); QList<QPersistentModelIndex> persistent; int subSecs = 0; for(const QModelIndex &i : sel){ @@ -644,17 +658,17 @@ void PlayerWidget::removeFromPlayList(){ persistent << QPersistentModelIndex(i); } for(const QPersistentModelIndex &i : persistent){ - mPlayListModel->removeRow(i.row()); + mSongModel->removeRow(i.row()); } - QStandardItem *root = mPlayListModel->invisibleRootItem(); + QStandardItem *root = mSongModel->invisibleRootItem(); emit numFilesChanged(root->rowCount()); mPlayListLength -= subSecs; emit playListLengthChanged(mPlayListLength); } void PlayerWidget::clearPlayList(){ - mPlayListModel->clear(); - mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title"); + mSongModel->clear(); + mSongModel->setHorizontalHeaderLabels(QStringList() << "Title"); mPlayListLength = 0; emit numFilesChanged(0); emit playListLengthChanged(0); @@ -662,14 +676,14 @@ void PlayerWidget::clearPlayList(){ void PlayerWidget::shufflePlayList(){ QVector<QStandardItem*> items; - for(int i = 0; i < mPlayListModel->rowCount(); ++i){ - QStandardItem *cur = mPlayListModel->item(i, 0)->clone(); + for(int i = 0; i < mSongModel->rowCount(); ++i){ + QStandardItem *cur = mSongModel->item(i, 0)->clone(); items << cur; } std::random_shuffle(items.begin(), items.end()); - mPlayListModel->clear(); - mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title"); - QStandardItem *root = mPlayListModel->invisibleRootItem(); + mSongModel->clear(); + mSongModel->setHorizontalHeaderLabels(QStringList() << "Title"); + QStandardItem *root = mSongModel->invisibleRootItem(); for(QStandardItem *i : items){ root->appendRow(i); } @@ -677,7 +691,7 @@ void PlayerWidget::shufflePlayList(){ void PlayerWidget::randomPlay(){ clearPlayList(); - QStandardItem *root = mPlayListModel->invisibleRootItem(); + QStandardItem *root = mSongModel->invisibleRootItem(); QIcon songIcon(":/song.png"); QSqlDatabase db = QSqlDatabase::database("beetplayerdb"); QSqlQuery randomQ(db); @@ -703,11 +717,11 @@ void PlayerWidget::randomPlay(){ } void PlayerWidget::playCurrent(const QModelIndex &index){ - int isRemote = index.data(CollectionWidget::RemoteRole).toInt(); + /*int isRemote = index.data(CollectionWidget::RemoteRole).toInt(); if(isRemote){ playUrl(index.data(CollectionWidget::UrlRole).toString()); return; - } + }*/ mPlayer->stop(); QString fullPath = index.data(CollectionWidget::FullPathRole).toString(); play(fullPath); @@ -820,7 +834,7 @@ void PlayerWidget::doMetadataChange(const QString &key, const QVariant &value){ artist = titleParts.at(0); title = titleParts.at(1); } - QStandardItem *plRoot = mPlayListModel->invisibleRootItem(); + QStandardItem *plRoot = mWebRadioModel->invisibleRootItem(); QStandardItem *nowP = new QStandardItem; nowP->setFont(QFont("courier")); nowP->setEditable(false); @@ -888,21 +902,23 @@ void PlayerWidget::play(const QString &fullPath){ emit setWinTitle(mCurWinTitle); mPlayer->play(); mPlayA->setChecked(true); - searchMusicbrainz(mPlayListView->selectionModel()->currentIndex()); + searchMusicbrainz(mSongView->selectionModel()->currentIndex()); emit playModeChanged(tr("Playing")); } -void PlayerWidget::playUrl(const QString &url){ +void PlayerWidget::playUrl(const QModelIndex &idx){ + //Honestly, I have no idea why we have to disconnect first, + //but if we don't, doMetadataChange will be called times playUrl :( disconnect(mPlayer, static_cast<void(QMediaObject::*)(const QString &, const QVariant &)>(&QMediaObject::metaDataChanged), this, &PlayerWidget::doMetadataChange); connect(mPlayer, static_cast<void(QMediaObject::*)(const QString &, const QVariant &)>(&QMediaObject::metaDataChanged), this, &PlayerWidget::doMetadataChange); mSongSlider->setValue(0); mSongSlider->setEnabled(false); - mPlayer->setMedia(QUrl(url)); + QUrl u(idx.data(CollectionWidget::UrlRole).toString()); + mPlayer->setMedia(u); mPlayer->play(); mPlayA->setChecked(true); mTrayIcon->setIcon(QIcon(":/play.png")); emit playModeChanged(tr("Playing")); - emit message(url); } void PlayerWidget::volumeChanged(int volume){ @@ -929,21 +945,21 @@ void PlayerWidget::previous(){ void PlayerWidget::advance(int numSongs){ mPlayer->stop(); - QModelIndexList sel = mPlayListView->selectionModel()->selectedRows(); + QModelIndexList sel = mSongView->selectionModel()->selectedRows(); if(sel.isEmpty()){ - QStandardItem *root = mPlayListModel->invisibleRootItem(); + QStandardItem *root = mSongModel->invisibleRootItem(); if(root->rowCount() > 0){ - QModelIndex first = mPlayListModel->index(0, 0); - mPlayListView->selectionModel()->setCurrentIndex(first, QItemSelectionModel::ClearAndSelect); + QModelIndex first = mSongModel->index(0, 0); + mSongView->selectionModel()->setCurrentIndex(first, QItemSelectionModel::ClearAndSelect); playCurrent(first); return; } return; } QModelIndex cur = sel.first(); - QModelIndex nextIdx = mPlayListModel->index(cur.row() + numSongs, 0); + QModelIndex nextIdx = mSongModel->index(cur.row() + numSongs, 0); if(nextIdx.isValid()){ - mPlayListView->selectionModel()->setCurrentIndex(nextIdx, QItemSelectionModel::ClearAndSelect); + mSongView->selectionModel()->setCurrentIndex(nextIdx, QItemSelectionModel::ClearAndSelect); playCurrent(nextIdx); } } |