#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "playerwidget.h" #include "beetview.h" #include "indexerdialog.h" #include "globals.h" PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent){ setupGui(); createActions(); } void PlayerWidget::setupGui(){ //THE view mView = new BeetView; mViewModel = new QStandardItemModel; mView->setModel(mViewModel); mSearchModel = new QStandardItemModel; mView->setSelectionMode(QAbstractItemView::ExtendedSelection); currentModel = mViewModel; //filter QGroupBox *filterGB = new QGroupBox(tr("Filter")); mFilter = new QLineEdit; connect(mFilter, SIGNAL(returnPressed()), this, SLOT(doFilter())); QPushButton *filterB = new QPushButton(tr("Go!")); connect(filterB, SIGNAL(clicked()), this, SLOT(doFilter())); QPushButton *clearFilterB = new QPushButton(tr("Clear")); connect(clearFilterB, SIGNAL(clicked()), this, SLOT(clearFilter())); QHBoxLayout *filterLayout = new QHBoxLayout; filterLayout->addWidget(mFilter); filterLayout->addWidget(filterB); filterLayout->addWidget(clearFilterB); filterGB->setLayout(filterLayout); //left widget QWidget *leftWidget = new QWidget; QVBoxLayout *leftWidgetL = new QVBoxLayout; leftWidgetL->addWidget(filterGB); leftWidgetL->addWidget(mView); leftWidget->setLayout(leftWidgetL); //now playing label mNowPlayingL = new QLabel; mNowPlayingL->setAlignment(Qt::AlignCenter); mNowPlayingL->setFont(QFont("courier new", 20, QFont::Bold)); mNowPlayingL->setText(tr("(none)")); //slider mSlider = new QSlider; mSlider->setOrientation(Qt::Horizontal); //current info QGroupBox *currentInfoGB = new QGroupBox(tr("Current")); mCurrentTE = new QTextEdit; mCurrentTE->setReadOnly(true); QVBoxLayout *currentInfoL = new QVBoxLayout; currentInfoL->addWidget(mCurrentTE); currentInfoGB->setLayout(currentInfoL); //center widget QWidget *centerWidget = new QWidget; QVBoxLayout *centerWidgetL = new QVBoxLayout; mToolBar = new QToolBar; centerWidgetL->addWidget(mToolBar); centerWidgetL->addWidget(mNowPlayingL); centerWidgetL->addWidget(mSlider); centerWidgetL->addWidget(currentInfoGB); centerWidget->setLayout(centerWidgetL); //playlist mPlayListModel = new QStandardItemModel; mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title"); mPlayListView = new BeetView; mPlayListView->setModel(mPlayListModel); mPlayListView->setRootIsDecorated(false); mPlayListView->setSelectionMode(QAbstractItemView::ExtendedSelection); QGroupBox *playListGB = new QGroupBox(tr("Playlist")); QVBoxLayout *playListL = new QVBoxLayout; playListL->addWidget(mPlayListView); playListGB->setLayout(playListL); //right widget QWidget *rightWidget = new QWidget; QVBoxLayout *rightWidgetL = new QVBoxLayout; rightWidgetL->addWidget(playListGB); rightWidget->setLayout(rightWidgetL); //put it all together QSplitter *splitter = new QSplitter; splitter->addWidget(leftWidget); splitter->addWidget(centerWidget); splitter->addWidget(rightWidget); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(splitter); setLayout(mainLayout); populate(); } void PlayerWidget::createActions(){ QAction *playA = new QAction(QIcon(":/play.png"), tr("Play"), this); QAction *pauseA = new QAction(QIcon(":/pause.png"), tr("Pause"), this); QAction *stopA = new QAction(QIcon(":/stop.png"), tr("Stop"), this); QAction *addToPlayListA = new QAction(QIcon(":/belly_right.png"), tr("Add to playlist"), this); connect(addToPlayListA, SIGNAL(triggered()), this, SLOT(addToPlayList())); QAction *removeFromPlayListA = new QAction(QIcon(":/belly_left.png"), tr("Remove from playlist"), this); connect(removeFromPlayListA, SIGNAL(triggered()), this, SLOT(removeFromPlayList())); QAction *clearPlayListA = new QAction(QIcon(":/delete.png"), tr("Clear Playlist"), this); connect(clearPlayListA, SIGNAL(triggered()), this, SLOT(clearPlayList())); QAction *refreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh..."), this); connect(refreshA, SIGNAL(triggered()), this, SLOT(reindex())); QAction *shufflePlayistA = new QAction(QIcon(":/shuffle.png"), tr("Shuffle playlist"), this); connect(shufflePlayistA, SIGNAL(triggered()), this, SLOT(shufflePlayList())); QAction *configA = Globals::instance()->action(Globals::ConfigAction); mView->addAction(addToPlayListA); mPlayListView->addAction(removeFromPlayListA); mPlayListView->addAction(shufflePlayistA); mPlayListView->addAction(clearPlayListA); QWidget* spacer1 = new QWidget(); spacer1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); mToolBar->addWidget(spacer1); mToolBar->addAction(playA); mToolBar->addAction(pauseA); mToolBar->addAction(stopA); mToolBar->addSeparator(); mToolBar->addAction(addToPlayListA); mToolBar->addAction(removeFromPlayListA); mToolBar->addAction(clearPlayListA); mToolBar->addAction(shufflePlayistA); mToolBar->addSeparator(); mToolBar->addAction(refreshA); mToolBar->addSeparator(); mToolBar->addAction(configA); QWidget* spacer2 = new QWidget(); spacer2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); mToolBar->addWidget(spacer2); } void PlayerWidget::populateByArtist(QStandardItem *parent, const QString &filter){ QSqlDatabase db = QSqlDatabase::database("beetplayerdb"); QStandardItem *root = parent; QSqlQuery artistsQ(db); if(!filter.isEmpty()){ artistsQ.prepare("SELECT iartists_id, tartists_name FROM artists WHERE tartists_name ~ :f ORDER BY tartists_name ASC"); artistsQ.bindValue(":f", filter); }else{ artistsQ.prepare("SELECT iartists_id, tartists_name FROM artists ORDER BY tartists_name ASC"); } QSqlQuery albumQ(db); albumQ.prepare("SELECT DISTINCT(songs.ialbums_id), talbum_name, siyear FROM songs, albums WHERE songs.iartists_id = :artistid AND songs.ialbums_id = albums.ialbums_id ORDER BY siyear ASC"); QSqlQuery songQ(db); songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id FROM songs WHERE ialbums_id = :alid AND iartists_id = :arid ORDER BY sipos ASC"); //read data artistsQ.exec(); while(artistsQ.next()){ QStandardItem *curArtist = new QStandardItem; curArtist->setEditable(false); curArtist->setFont(QFont("courier")); QString artistText = QString(tr("%1 %2")).arg(QString("🅐")).arg(artistsQ.value(1).toString()); //I guess this will bite me! curArtist->setText(artistText); curArtist->setData(Artist, TypeRole); curArtist->setData(artistsQ.value(0).toInt(), IdRole); root->appendRow(curArtist); albumQ.bindValue(":artistid", artistsQ.value(0)); albumQ.exec(); while(albumQ.next()){ QStandardItem *curAlbum = new QStandardItem; curAlbum->setEditable(false); curAlbum->setFont(QFont("courier")); QString albumText = QString(tr("%1 %2 - %3")).arg(QChar(0x29BE)).arg(QString::number(albumQ.value(2).toInt())).arg(albumQ.value(1).toString()); curAlbum->setText(albumText); curAlbum->setData(Album, TypeRole); curAlbum->setData(albumQ.value(0), IdRole); curArtist->appendRow(curAlbum); songQ.bindValue(":alid", albumQ.value(0)); songQ.bindValue(":arid", artistsQ.value(0)); songQ.exec(); while(songQ.next()){ QStandardItem *curSong = new QStandardItem; curSong->setEditable(false); curSong->setFont(QFont("courier")); QString songText = QString(tr("%1 %2 - %3")).arg(QChar(0x266C)).arg(songQ.value(0).toInt(), 3, 10, QChar('0')).arg(songQ.value(1).toString()); curSong->setText(songText); curSong->setData(Song, TypeRole); curSong->setData(songQ.value(0), IdRole); curSong->setData(songQ.value(2), FullPathRole); curSong->setData(songQ.value(3), GenreRole); curSong->setData(artistsQ.value(1), ArtistRole); curSong->setData(songQ.value(1), TitleRole); curSong->setData(albumQ.value(1), AlbumRole); curAlbum->appendRow(curSong); } } } } void PlayerWidget::populateBySong(QStandardItem *parent, const QString &filter){ QSqlDatabase db = QSqlDatabase::database("beetplayerdb"); QStandardItem *root = parent; QSqlQuery songQ = QSqlQuery(db); songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name FROM songs, artists, albums WHERE ttitle ~ :f AND songs.iartists_id = artists.iartists_id and songs.ialbums_id = albums.ialbums_id ORDER BY ttitle ASC"); songQ.bindValue(":f", filter); songQ.exec(); while(songQ.next()){ QStandardItem *curSong = new QStandardItem; curSong->setEditable(false); curSong->setFont(QFont("courier")); QString songText = QString(tr("%1 %2 (%3)")).arg(QChar(0x266C)).arg(songQ.value(1).toString()).arg(songQ.value(4).toString()); curSong->setText(songText); curSong->setData(Song, TypeRole); curSong->setData(songQ.value(0), IdRole); curSong->setData(songQ.value(2), FullPathRole); curSong->setData(songQ.value(3), GenreRole); curSong->setData(songQ.value(4), ArtistRole); curSong->setData(songQ.value(1), TitleRole); curSong->setData(songQ.value(5), AlbumRole); root->appendRow(curSong); } } void PlayerWidget::populateByGenre(QStandardItem *parent, const QString &filter){ QSqlDatabase db = QSqlDatabase::database("beetplayerdb"); QStandardItem *root = parent; QSqlQuery genreQ(db); genreQ.prepare("SELECT igenres_id, tgenres_name FROM genres WHERE tgenres_name ~ :f"); genreQ.bindValue(":f", filter); genreQ.exec(); while(genreQ.next()){ QStandardItem *curGenre = new QStandardItem; curGenre->setEditable(false); curGenre->setFont(QFont("courier")); QString genreText = QString(tr("%1 %2")).arg(QString("🅖")).arg(genreQ.value(1).toString()); curGenre->setText(genreText); curGenre->setData(Genre, TypeRole); curGenre->setData(genreQ.value(0), IdRole); root->appendRow(curGenre); QSqlQuery songQ = QSqlQuery(db); songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name FROM songs, artists, albums WHERE igenres_id = :id AND songs.iartists_id = artists.iartists_id AND songs.ialbums_id = albums.ialbums_id ORDER BY ttitle ASC"); songQ.bindValue(":id", genreQ.value(0)); songQ.exec(); while(songQ.next()){ QStandardItem *curSong = new QStandardItem; curSong->setEditable(false); curSong->setFont(QFont("courier")); QString songText = QString(tr("%1 %2 (%3)")).arg(QChar(0x266C)).arg(songQ.value(1).toString()).arg(songQ.value(4).toString()); curSong->setText(songText); curSong->setData(Song, TypeRole); curSong->setData(songQ.value(0), IdRole); curSong->setData(songQ.value(2), FullPathRole); curSong->setData(songQ.value(3), GenreRole); curSong->setData(songQ.value(4), ArtistRole); curSong->setData(songQ.value(1), TitleRole); curSong->setData(songQ.value(5), AlbumRole); curGenre->appendRow(curSong); } } } void PlayerWidget::recurse(const QModelIndex &parent){ for(int i = 0; i < currentModel->rowCount(parent); ++i){ QModelIndex cur = currentModel->index(i, 0, parent); int type = cur.data(TypeRole).toInt(); if(type == Song){ addSong(cur); }else{ recurse(cur); } } } void PlayerWidget::addSong(const QModelIndex &idx){ QStandardItem *root = mPlayListModel->invisibleRootItem(); QString title = idx.data(TitleRole).toString(); QString artist = idx.data(ArtistRole).toString(); QString album = idx.data(AlbumRole).toString(); QString display = QString(tr("%1 - %2 - %3 - %4")).arg(QChar(0x266C)).arg(artist).arg(title).arg(album); QStandardItem *item = new QStandardItem; item->setFont(QFont("courier")); item->setText(display); item->setData(idx.data(FullPathRole), FullPathRole); root->appendRow(item); } void PlayerWidget::populate(){ mView->setModel(mViewModel); mViewModel->clear(); mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Name")); QStandardItem *root = mViewModel->invisibleRootItem(); populateByArtist(root, QString()); } void PlayerWidget::doFilter(){ QString filter = mFilter->text(); if(filter.isEmpty()){ mView->setModel(mViewModel); } mSearchModel->clear(); mSearchModel->setHorizontalHeaderLabels(QStringList() << tr("Name")); mView->setModel(mSearchModel); currentModel = mSearchModel; QStandardItem *root = mSearchModel->invisibleRootItem(); populateByArtist(root, filter); populateByGenre(root, filter); populateBySong(root, filter); } void PlayerWidget::clearFilter(){ mFilter->clear(); mView->setModel(mViewModel); currentModel = mViewModel; } void PlayerWidget::reindex(){ IndexerDialog dlg(this); dlg.exec(); } void PlayerWidget::addToPlayList(){ QModelIndexList sel = mView->selectionModel()->selectedRows(); if(sel.isEmpty()){ return; } foreach(QModelIndex i, sel){ int type = i.data(TypeRole).toInt(); if(type == Song){ addSong(i); }else{ recurse(i); } } } void PlayerWidget::removeFromPlayList(){ QModelIndexList sel = mPlayListView->selectionModel()->selectedRows(); QList persistent; foreach(QModelIndex i, sel){ persistent << QPersistentModelIndex(i); } foreach(QPersistentModelIndex i, persistent){ mPlayListModel->removeRow(i.row()); } } void PlayerWidget::clearPlayList(){ mPlayListModel->clear(); mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title"); } void PlayerWidget::shufflePlayList(){ QVector items; for(int i = 0; i < mPlayListModel->rowCount(); ++i){ QStandardItem *cur = mPlayListModel->item(i, 0)->clone(); items << cur; } std::random_shuffle(items.begin(), items.end()); mPlayListModel->clear(); mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title"); QStandardItem *root = mPlayListModel->invisibleRootItem(); foreach(QStandardItem *i, items){ root->appendRow(i); } }