Positioning on next track after removing from pls
[someplayer] / src / playerform.cpp
index 09d6a44..d07efb3 100644 (file)
@@ -46,6 +46,7 @@ inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
                time.setHMS(0, meta.length()/60, meta.length() % 60);
                QString t = meta.title()+"#_#"+meta.artist()+"#_#"+meta.album()+"#_#"+time.toString("mm:ss");
                _model->setItem(i, 1, new QStandardItem(t));
+               _model->setItem(i, 0, new QStandardItem("##arrow_r.png"));
        }
 }
 
@@ -111,19 +112,22 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
        _cover->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        _cover->setPixmap(QPixmap::fromImage(_coverfinder->defaultCover()));
 
+       _pls_sort_form = new PlaylistSortForm(parent);
+       _pls_sort_form->hide();
+
        connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
-       connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
-       connect(ui->playlistView, SIGNAL(activated(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
+       connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(toggleView()));
+       connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
        connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
        connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
        connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
        connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
        connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
        connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
-       connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
-       connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
+       connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(toggleRandom()));
+       connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(toggleRepeat()));
        connect(ui->seekSlider, SIGNAL(sliderMoved(int)), _player, SLOT(seek(int)));
-       connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_menu_requested(QPoint)));
+       connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_sort_playlist()));
        connect(__clear_playlist, SIGNAL(triggered()), this, SIGNAL(clearPlaylist()));
        connect(__delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
        connect(__enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
@@ -134,7 +138,7 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
        connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
        connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
        connect(ui->dirButton, SIGNAL(clicked()), this, SLOT(_dirview()));
-       connect(ui->moreButton, SIGNAL(clicked()), this, SLOT(_tools_widget_toggle()));
+       connect(ui->moreButton, SIGNAL(clicked()), this, SLOT(toggleToolsWidget()));
        connect(_tools_widget, SIGNAL(search(QString)), this, SLOT(search(QString)));
        connect(_tools_widget, SIGNAL(nextSearch()), this, SLOT(nextItem()));
        connect(_tools_widget, SIGNAL(prevSearch()), this, SLOT(prevItem()));
@@ -148,6 +152,9 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
        connect(ui->cfavButton, SIGNAL(clicked()), this, SLOT(_c_add_to_favorites()));
        connect(ui->ctagButton, SIGNAL(clicked()), this, SLOT(_c_edit_tags()));
        connect(_cover, SIGNAL(clicked()), this, SLOT(_toggle_extra_buttons()));
+       connect(_player, SIGNAL(startPlaylist()), this, SLOT(_start_playlist()));
+       connect(_player, SIGNAL(saveLastPlayed(LastPlayed)), _lib, SLOT(saveLastPlayedForCurPlaylist(LastPlayed)));
+       connect(_pls_sort_form, SIGNAL(playlistChanged()), this, SLOT(_playlist_sorted()));
        ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playback.png"));
        _top_gradient = ui->topWidget->styleSheet();
        _bottom_gradient = ui->bottomWidget->styleSheet();
@@ -167,6 +174,9 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
 
        // dbus
        _dbusadaptor = new DBusAdaptop(_player);
+       connect(_player, SIGNAL(stateChanged(PlayerState)), _dbusadaptor, SIGNAL(stateChanged()));
+       connect(_coverfinder, SIGNAL(foundPath(QString)), _dbusadaptor, SIGNAL(albumArt(QString)));
+       connect(_coverfinder, SIGNAL(foundPath(QString)), _player, SLOT(setAlbumart(QString)));
        QDBusConnection connection = QDBusConnection::sessionBus();
        bool ret = connection.registerService(_SERVICE_NAME_);
        ret = connection.registerObject("/", _player);
@@ -191,7 +201,7 @@ void PlayerForm::reload(bool reread) {
        }
 }
 
-void PlayerForm::_toggle_view() {
+void PlayerForm::toggleView() {
        int index = ui->stackedWidget->currentIndex();
        index = (!index % 2);
        if (index) {
@@ -207,9 +217,11 @@ void PlayerForm::_toggle_view() {
 }
 
 void PlayerForm::_process_click(QModelIndex index) {
+       if (_pls_sort_form->isVisible()) {
+               return;
+       }
        if (index.column() == 1) {
                int id = index.row();
-               _player->stop();
                _player->setTrackId(id);
                _player->play();
                _track_renderer->setActiveRow(id);
@@ -228,6 +240,7 @@ void PlayerForm::_track_changed(Track track) {
        ui->playlistView->show();
        _display_track(track);
        _context_menu->actions().at(2)->setText(_lib->isFavorite(track) ? tr("Remove from favorites") : tr("Add to favorites"));
+       emit trackChanged();
 }
 
 void PlayerForm::_display_track(Track track) {
@@ -242,7 +255,7 @@ void PlayerForm::_display_track(Track track) {
        ui->seekSlider->setMaximum(track.metadata().length());
        _tick(0, track.metadata().length());
        _coverfinder->find(track);
-       ui->cfavButton->setChecked(_lib->isFavorite(track) && ui->cfavButton->isVisible());
+       ui->cfavButton->setChecked(_lib->isFavorite(track) && !ui->cfavButton->icon().isNull());
 }
 
 void PlayerForm::_tick(int done, int all) {
@@ -258,6 +271,17 @@ void PlayerForm::_slider_released() {
 }
 
 void PlayerForm::_custom_context_menu_requested(const QPoint &pos) {
+       // fix for 'favorite' state of selected track:
+       QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
+       if (idx.isEmpty())
+               return;
+       int id = idx.first().row();
+       Track cur = _current_playlist.tracks().at(id);
+       if (!cur.source().isEmpty()) {
+               bool isf = _lib->isFavorite(cur);
+               _context_menu->actions().at(2)->setText(isf ? tr("Remove from favorites") : tr("Add to favorites"));
+       }
+       // end
        _context_menu->exec(pos);
 }
 
@@ -275,6 +299,11 @@ void PlayerForm::_delete_track() {
        _current_playlist.removeTrackAt(id);
        _lib->saveCurrentPlaylist(_current_playlist);
        reload(true);
+       int rc = _model->rowCount();
+       if (id >= rc) {
+               id = rc-1;
+       }
+       ui->playlistView->scrollTo(_model->index(id, 0), QAbstractItemView::PositionAtCenter);
 }
 
 void PlayerForm::_enqueue_track() {
@@ -299,8 +328,7 @@ void PlayerForm::_add_to_favorites() {
                        _lib->removeFromFavorites(cur);
                }
                isf = _lib->isFavorite(cur);
-               ui->cfavButton->setChecked(isf && ui->cfavButton->isVisible());
-               _context_menu->actions().at(2)->setText(isf ? tr("Remove from favorites") : tr("Add to favorites"));
+               ui->cfavButton->setChecked(isf && !ui->cfavButton->icon().isNull() && id == _track_renderer->activeRow());
        }
 
 }
@@ -319,7 +347,7 @@ void PlayerForm::_state_changed(PlayerState state) {
        }
 }
 
-void PlayerForm::_toggle_random() {
+void PlayerForm::toggleRandom() {
        _player->toggleRandom();
        if (_player->random()) {
                ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_on.png"));
@@ -328,7 +356,7 @@ void PlayerForm::_toggle_random() {
        }
 }
 
-void PlayerForm::_toggle_repeat() {
+void PlayerForm::toggleRepeat() {
        _player->toggleRepeat();
        if (_player->repeat() == REPEAT_ALL) {
                ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_all.png"));
@@ -430,6 +458,7 @@ void PlayerForm::_edit_tags() {
                reload(true);
                emit refreshLibrary();
        }
+       _display_track(track);
 }
 
 void PlayerForm::stop() {
@@ -443,6 +472,9 @@ void PlayerForm::_dirview() {
 void PlayerForm::landscapeMode() {
        landscape = true;
 
+       ui->topWidget->hide();
+       ui->bottomWidget->hide();
+
        ui->widget->layout()->removeItem(ui->coverLayout);
        ui->widget->layout()->removeItem(ui->controlLayout);
        ui->controlLayout->removeItem(ui->countHLayout);
@@ -468,9 +500,6 @@ void PlayerForm::landscapeMode() {
        ((QGridLayout *)ui->widget->layout())->addItem(ui->coverLayout, 0, 0);
        ((QGridLayout *)ui->widget->layout())->addItem(ui->controlLayout, 0, 1);
 
-       ui->topWidget->hide();
-       ui->bottomWidget->hide();
-
        ui->bhorizontalLayout->removeItem(ui->chorizontalSpacer_0);
        ui->bhorizontalLayout->removeItem(ui->chorizontalSpacer_1);
        ui->bhorizontalLayout->removeItem(ui->chorizontalSpacer_2);
@@ -493,19 +522,21 @@ void PlayerForm::landscapeMode() {
        ui->bhorizontalLayout->addItem(ui->chorizontalSpacer_4);
        ui->bhorizontalLayout->addWidget(ui->dirButton);
 
+       ui->bhorWidget->show();
+
        if (_tools_widget->isVisible()) {
                ui->moreButton->setIcon(QIcon(":/icons/"+_icons_theme+"/unmore.png"));
        } else {
                ui->moreButton->setIcon(QIcon(":/icons/"+_icons_theme+"/more.png"));
        }
+       _pls_sort_form->landscapeMode();
 }
 
 void PlayerForm::portraitMode() {
-       ui->topWidget->show();
-       ui->bottomWidget->show();
-
        landscape = false;
 
+       ui->bhorWidget->hide();
+
        ui->widget->layout()->removeItem(ui->coverLayout);
        ui->widget->layout()->removeItem(ui->controlLayout);
 
@@ -566,14 +597,21 @@ void PlayerForm::portraitMode() {
        ui->bottomWidget->layout()->addItem(ui->bhorizontalSpacer_3);
        ui->bottomWidget->layout()->addWidget(ui->dirButton);
 
+       ui->topWidget->show();
+       ui->bottomWidget->show();
+
        if (_tools_widget->isVisible()) {
                ui->moreButton->setIcon(QIcon(":/icons/"+_icons_theme+"/unmore.png"));
        } else {
                ui->moreButton->setIcon(QIcon(":/icons/"+_icons_theme+"/more.png"));
        }
+       _pls_sort_form->portraitMode();
 }
 
-void PlayerForm::_tools_widget_toggle() {
+void PlayerForm::toggleToolsWidget() {
+       if (0 != ui->stackedWidget->currentIndex()) {
+               return;
+       }
        if (_tools_widget->isVisible()) {
                ui->moreButton->setIcon(QIcon(":/icons/"+_icons_theme+"/more.png"));
                _tools_widget->hide();
@@ -639,6 +677,7 @@ void PlayerForm::updateIcons() {
        } else {
                ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_off.png"));
        }
+       _pls_sort_form->updateIcons();
 }
 
 void PlayerForm::checkGradient() {
@@ -650,6 +689,7 @@ void PlayerForm::checkGradient() {
                ui->topWidget->setStyleSheet("");
                ui->bottomWidget->setStyleSheet("");
        }
+       _pls_sort_form->updateGradiend();
 }
 
 void PlayerForm::play(Track track) {
@@ -746,6 +786,7 @@ void PlayerForm::updateTranslations() {
        __enqueue_action->setText(tr("Enqueue"));
        __add_to_playlists->setText(tr("Add to playlists"));
        __edit_tags->setText(tr("Edit tags"));
+       _pls_sort_form->updateTranslations();
 }
 
 void PlayerForm::updateTrackColor() {
@@ -755,3 +796,45 @@ void PlayerForm::updateTrackColor() {
        ui->playlistView->hide();
        ui->playlistView->show();
 }
+
+void PlayerForm::next() {
+       _player->next();
+}
+
+void PlayerForm::prev() {
+       _player->prev();
+}
+
+QString PlayerForm::playerCaption() {
+       TrackMetadata meta = _player->current().metadata();
+       return QString("%1 - %2").arg(meta.artist()).arg(meta.title());
+}
+
+void PlayerForm::_start_playlist() {
+       Config config;
+       if (config.getValue("playback/autoresume_off").toBool()) {
+               _player->next();
+               return;
+       }
+       LastPlayed lp = _lib->getLastPlayedForCurPlaylist();
+       _player->setTrackId(lp.trackId);
+       _player->setAwaitingSeek(lp.position);
+}
+
+void PlayerForm::_sort_playlist() {
+       _pls_sort_form->setPlaylist(_current_playlist);
+       _pls_sort_form->show();
+}
+
+void PlayerForm::_playlist_sorted() {
+       _current_playlist = _pls_sort_form->getPlaylist();
+       _lib->saveCurrentPlaylist(_current_playlist);
+       _player->setPlaylist(_current_playlist);
+       __fill_list(_model, _current_playlist);
+       ui->playlistView->setColumnWidth(0, 50);
+       _track_renderer->setActiveRow(_current_playlist.tracks().indexOf(_player->current()));
+}
+
+void PlayerForm::toggle() {
+       _player->toggle();
+}