diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index c53c6980d..97fb66c23 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -1455,7 +1455,7 @@ void MainWindow::PlaylistDoubleClick(const QModelIndex &idx) { switch (doubleclick_playlist_addmode_) { case BehaviourSettingsPage::PlaylistAddBehaviour_Play: app_->playlist_manager()->SetActiveToCurrent(); - app_->player()->PlayAt(row, Engine::Manual, Playlist::AutoScroll_Never, true); + app_->player()->PlayAt(row, Engine::Manual, Playlist::AutoScroll_Never, true, true); break; case BehaviourSettingsPage::PlaylistAddBehaviour_Enqueue: diff --git a/src/core/player.cpp b/src/core/player.cpp index 2afc6b394..229e5868d 100644 --- a/src/core/player.cpp +++ b/src/core/player.cpp @@ -478,7 +478,6 @@ void Player::Stop(bool stop_after) { } void Player::StopAfterCurrent() { - app_->playlist_manager()->active()->StopAfter(app_->playlist_manager()->active()->current_row()); } @@ -499,14 +498,14 @@ void Player::PreviousItem(const Engine::TrackChangeFlags change) { QDateTime now = QDateTime::currentDateTime(); if (last_pressed_previous_.isValid() && last_pressed_previous_.secsTo(now) >= 2) { last_pressed_previous_ = now; - PlayAt(app_->playlist_manager()->active()->current_row(), change, Playlist::AutoScroll_Always, false); + PlayAt(app_->playlist_manager()->active()->current_row(), change, Playlist::AutoScroll_Always, false, true); return; } last_pressed_previous_ = now; } int i = app_->playlist_manager()->active()->previous_row(ignore_repeat_track); - app_->playlist_manager()->active()->set_current_row(i, Playlist::AutoScroll_Always); + app_->playlist_manager()->active()->set_current_row(i, Playlist::AutoScroll_Always, false, true); if (i == -1) { Stop(); PlayAt(i, change, Playlist::AutoScroll_Always, true); @@ -561,7 +560,7 @@ void Player::SetVolume(const int value) { int Player::GetVolume() const { return engine_->volume(); } -void Player::PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle) { +void Player::PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform) { if (current_item_ && change == Engine::Manual && engine_->position_nanosec() != engine_->length_nanosec()) { emit TrackSkipped(current_item_); @@ -572,7 +571,8 @@ void Player::PlayAt(const int index, Engine::TrackChangeFlags change, const Play } if (reshuffle) app_->playlist_manager()->active()->ReshuffleIndices(); - app_->playlist_manager()->active()->set_current_row(index, autoscroll); + + app_->playlist_manager()->active()->set_current_row(index, autoscroll, false, force_inform); if (app_->playlist_manager()->active()->current_row() == -1) { // Maybe index didn't exist in the playlist. return; @@ -620,6 +620,10 @@ void Player::SeekTo(const int seconds) { emit Seeked(nanosec / 1000); + if (seconds == 0) { + app_->playlist_manager()->active()->InformOfCurrentSongChange(Playlist::AutoScroll_Maybe, false); + } + } void Player::SeekForward() { diff --git a/src/core/player.h b/src/core/player.h index e5e91f0fb..d5d43875a 100644 --- a/src/core/player.h +++ b/src/core/player.h @@ -73,7 +73,7 @@ class PlayerInterface : public QObject { virtual void ReloadSettings() = 0; // Manual track change to the specified track - virtual void PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle) = 0; + virtual void PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform = false) = 0; // If there's currently a song playing, pause it, otherwise play the track that was playing last, or the first one on the playlist virtual void PlayPause(Playlist::AutoScroll autoscroll = Playlist::AutoScroll_Always) = 0; @@ -158,7 +158,7 @@ class Player : public PlayerInterface { public slots: void ReloadSettings() override; - void PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle) override; + void PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform = false) override; void PlayPause(Playlist::AutoScroll autoscroll = Playlist::AutoScroll_Always) override; void RestartOrPrevious() override; void Next() override; diff --git a/src/playlist/playlist.cpp b/src/playlist/playlist.cpp index e33185f18..8baa0e603 100644 --- a/src/playlist/playlist.cpp +++ b/src/playlist/playlist.cpp @@ -135,7 +135,11 @@ Playlist::Playlist(PlaylistBackend *backend, TaskManager *task_manager, Collecti cancel_restore_(false), scrobbled_(false), scrobble_point_(-1), - editing_(-1) { + editing_(-1), + auto_sort_(false), + sort_column_(Column_Title), + sort_order_(Qt::AscendingOrder) + { undo_stack_->setUndoLimit(kUndoStackSize); @@ -591,10 +595,11 @@ int Playlist::previous_row(const bool ignore_repeat_track) const { } -void Playlist::set_current_row(const int i, const AutoScroll autoscroll, const bool is_stopping) { +void Playlist::set_current_row(const int i, const AutoScroll autoscroll, const bool is_stopping, const bool force_inform) { QModelIndex old_current_item_index = current_item_index_; - QModelIndex new_current_item_index = QPersistentModelIndex(index(i, 0, QModelIndex())); + QModelIndex new_current_item_index; + if (i != -1) new_current_item_index = QPersistentModelIndex(index(i, 0, QModelIndex())); if (new_current_item_index != current_item_index_) ClearStreamMetadata(); @@ -610,11 +615,11 @@ void Playlist::set_current_row(const int i, const AutoScroll autoscroll, const b current_item_index_ = new_current_item_index; // if the given item is the first in the queue, remove it from the queue - if (current_item_index_.row() == queue_->PeekNext()) { + if (current_item_index_.isValid() && current_item_index_.row() == queue_->PeekNext()) { queue_->TakeNext(); } - if (current_item_index_ == old_current_item_index) { + if (current_item_index_ == old_current_item_index && !force_inform) { UpdateScrobblePoint(); return; } @@ -653,26 +658,24 @@ void Playlist::set_current_row(const int i, const AutoScroll autoscroll, const b if (dynamic_playlist_ && current_item_index_.isValid()) { // When advancing to the next track - if (i > old_current_item_index.row()) { + if (old_current_item_index.isValid() && i > old_current_item_index.row()) { // Move the new item one position ahead of the last item in the history. MoveItemWithoutUndo(current_item_index_.row(), dynamic_history_length()); - // Compute the number of new items that have to be inserted. This is not - // necessarily 1 because the user might have added or removed items - // manually. Note that the future excludes the current item. + // Compute the number of new items that have to be inserted + // This is not necessarily 1 because the user might have added or removed items manually. + // Note that the future excludes the current item. const int count = dynamic_history_length() + 1 + dynamic_playlist_->GetDynamicFuture() - items_.count(); if (count > 0) { InsertDynamicItems(count); } - // Shrink the history, again this is not necessarily by 1, because the - // user might have moved items by hand. + // Shrink the history, again this is not necessarily by 1, because the user might have moved items by hand. const int remove_count = dynamic_history_length() - dynamic_playlist_->GetDynamicHistory(); if (0 < remove_count) RemoveItemsWithoutUndo(0, remove_count); } - // the above actions make all commands on the undo stack invalid, so we - // better clear it. + // the above actions make all commands on the undo stack invalid, so we better clear it. undo_stack_->clear(); } @@ -1060,7 +1063,13 @@ void Playlist::InsertItemsWithoutUndo(const PlaylistItemList &items, const int p } Save(); - ReshuffleIndices(); + + if (auto_sort_) { + sort(sort_column_, sort_order_); + } + else { + ReshuffleIndices(); + } } @@ -1308,6 +1317,9 @@ QString Playlist::abbreviated_column_name(const Column column) { void Playlist::sort(int column, Qt::SortOrder order) { + sort_column_ = column; + sort_order_ = order; + if (ignore_sorting_) return; PlaylistItemList new_items(items_); diff --git a/src/playlist/playlist.h b/src/playlist/playlist.h index 7623f377b..a188c69ef 100644 --- a/src/playlist/playlist.h +++ b/src/playlist/playlist.h @@ -295,8 +295,10 @@ class Playlist : public QAbstractListModel { void RateSong(const QModelIndex &idx, const double rating); void RateSongs(const QModelIndexList &index_list, const double rating); + void set_auto_sort(const bool auto_sort) { auto_sort_ = auto_sort; } + public slots: - void set_current_row(const int i, const AutoScroll autoscroll = AutoScroll_Maybe, const bool is_stopping = false); + void set_current_row(const int i, const AutoScroll autoscroll = AutoScroll_Maybe, const bool is_stopping = false, const bool force_inform = false); void Paused(); void Playing(); void Stopped(); @@ -430,6 +432,10 @@ class Playlist : public QAbstractListModel { PlaylistGeneratorPtr dynamic_playlist_; + bool auto_sort_; + int sort_column_; + Qt::SortOrder sort_order_; + }; #endif // PLAYLIST_H diff --git a/src/playlist/playlistview.cpp b/src/playlist/playlistview.cpp index 750224b43..7ac2fe296 100644 --- a/src/playlist/playlistview.cpp +++ b/src/playlist/playlistview.cpp @@ -158,6 +158,7 @@ PlaylistView::PlaylistView(QWidget *parent) previous_background_image_y_(0), glow_enabled_(true), select_track_(false), + auto_sort_(false), currently_glowing_(false), glow_intensity_step_(0), inhibit_autoscroll_timer_(new QTimer(this)), @@ -309,6 +310,7 @@ void PlaylistView::SetPlaylist(Playlist *playlist) { DynamicModeChanged(playlist->is_dynamic()); setFocus(); JumpToLastPlayedTrack(); + playlist->set_auto_sort(auto_sort_); connect(playlist_, SIGNAL(RestoreFinished()), SLOT(JumpToLastPlayedTrack())); connect(playlist_, SIGNAL(MaybeAutoscroll(Playlist::AutoScroll)), SLOT(MaybeAutoscroll(Playlist::AutoScroll))); @@ -1133,6 +1135,7 @@ void PlaylistView::ReloadSettings() { glow_enabled_ = s.value("glow_effect", glow_effect).toBool(); bool editmetadatainline = s.value("editmetadatainline", false).toBool(); select_track_ = s.value("select_track", false).toBool(); + auto_sort_ = s.value("auto_sort", false).toBool(); s.endGroup(); s.beginGroup(AppearanceSettingsPage::kSettingsGroup); @@ -1221,6 +1224,8 @@ void PlaylistView::ReloadSettings() { else setEditTriggers(editTriggers() & ~QAbstractItemView::SelectedClicked); + if (playlist_) playlist_->set_auto_sort(auto_sort_); + } void PlaylistView::SaveSettings() { diff --git a/src/playlist/playlistview.h b/src/playlist/playlistview.h index 737b166ac..c685ad02a 100644 --- a/src/playlist/playlistview.h +++ b/src/playlist/playlistview.h @@ -257,6 +257,7 @@ class PlaylistView : public QTreeView { bool glow_enabled_; bool select_track_; + bool auto_sort_; bool currently_glowing_; QBasicTimer glow_timer_; diff --git a/src/settings/playlistsettingspage.cpp b/src/settings/playlistsettingspage.cpp index 241e13db4..4d38639c4 100644 --- a/src/settings/playlistsettingspage.cpp +++ b/src/settings/playlistsettingspage.cpp @@ -64,6 +64,7 @@ void PlaylistSettingsPage::Load() { ui_->checkbox_greyout_songs_play->setChecked(s.value("greyout_songs_play", true).toBool()); ui_->checkbox_select_track->setChecked(s.value("select_track", false).toBool()); ui_->checkbox_playlist_clear->setChecked(s.value("playlist_clear", true).toBool()); + ui_->checkbox_auto_sort->setChecked(s.value("auto_sort", false).toBool()); Playlist::Path path = Playlist::Path(s.value(Playlist::kPathType, Playlist::Path_Automatic).toInt()); switch (path) { @@ -126,6 +127,7 @@ void PlaylistSettingsPage::Save() { s.setValue("editmetadatainline", ui_->checkbox_editmetadatainline->isChecked()); s.setValue(Playlist::kWriteMetadata, ui_->checkbox_writemetadata->isChecked()); s.setValue("delete_files", ui_->checkbox_delete_files->isChecked()); + s.setValue("auto_sort", ui_->checkbox_auto_sort->isChecked()); s.endGroup(); } diff --git a/src/settings/playlistsettingspage.ui b/src/settings/playlistsettingspage.ui index a677d4ad4..5a1ee8bcb 100644 --- a/src/settings/playlistsettingspage.ui +++ b/src/settings/playlistsettingspage.ui @@ -70,6 +70,13 @@ + + + + Automatically sort playlist when inserting songs + + +