From 8310d055689bb7bf9bc06f45eaf185e302ea49e9 Mon Sep 17 00:00:00 2001 From: ronso0 Date: Mon, 22 Nov 2021 00:56:44 +0100 Subject: [PATCH] Library: keep selection when search is less specific --- src/widget/wtracktableview.cpp | 69 +++++++++++++++++++++++++++++++++- src/widget/wtracktableview.h | 2 + 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/widget/wtracktableview.cpp b/src/widget/wtracktableview.cpp index b9f558f483b..d60e8acb194 100644 --- a/src/widget/wtracktableview.cpp +++ b/src/widget/wtracktableview.cpp @@ -11,6 +11,7 @@ #include "library/library.h" #include "library/library_prefs.h" #include "library/librarytablemodel.h" +#include "library/searchqueryparser.h" #include "library/trackcollection.h" #include "library/trackcollectionmanager.h" #include "mixer/playermanager.h" @@ -475,8 +476,30 @@ void WTrackTableView::onSearch(const QString& text) { TrackModel* trackModel = getTrackModel(); if (trackModel) { saveCurrentViewState(); + bool queryIsLessSpecific = SearchQueryParser::queryIsLessSpecific( + trackModel->currentSearch(), text); + QList selectedTracks = getSelectedTrackIds(); + TrackId prevTrack = getCurrentTrackId(); + int prevColumn = 0; + if (currentIndex().isValid()) { + prevColumn = currentIndex().column(); + } trackModel->search(text); - restoreCurrentViewState(); + if (queryIsLessSpecific) { + // If the user removed query terms, we try to select the same + // tracks as before + setCurrentTrackId(prevTrack, prevColumn); + setSelectedTracks(selectedTracks); + } else { + // The user created a more specific search query, try to restore a + // previous state + if (!restoreCurrentViewState()) { + // We found no saved state for this query, try to select the + // tracks last active, if they are part of the result set + setCurrentTrackId(prevTrack, prevColumn); + setSelectedTracks(selectedTracks); + } + } } } @@ -869,6 +892,26 @@ QList WTrackTableView::getSelectedTrackIds() const { return trackIds; } +TrackId WTrackTableView::getCurrentTrackId() const { + QItemSelectionModel* pSelectionModel = selectionModel(); + VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) { + qWarning() << "No selected tracks available"; + return TrackId(); + } + + TrackModel* pTrackModel = getTrackModel(); + VERIFY_OR_DEBUG_ASSERT(pTrackModel != nullptr) { + qWarning() << "No selected tracks available"; + return TrackId(); + } + + const QModelIndex current = selectionModel()->currentIndex(); + if (current.isValid()) { + return pTrackModel->getTrackId(current); + } + return TrackId(); +} + void WTrackTableView::setSelectedTracks(const QList& trackIds) { QItemSelectionModel* pSelectionModel = selectionModel(); VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) { @@ -892,6 +935,30 @@ void WTrackTableView::setSelectedTracks(const QList& trackIds) { } } +bool WTrackTableView::setCurrentTrackId(const TrackId& trackId, const int column = 0) { + QItemSelectionModel* pSelectionModel = selectionModel(); + VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) { + qWarning() << "No selected tracks available"; + return false; + } + + TrackModel* pTrackModel = getTrackModel(); + VERIFY_OR_DEBUG_ASSERT(pTrackModel != nullptr) { + qWarning() << "No selected tracks available"; + return false; + } + const QVector gts = pTrackModel->getTrackRows(trackId); + if (gts.empty()) { + return false; + } + + const QModelIndex idx = model()->index(gts[0], column); + selectRow(idx.row()); + pSelectionModel->setCurrentIndex(idx, + QItemSelectionModel::SelectCurrent | QItemSelectionModel::Select); + return true; +} + void WTrackTableView::addToAutoDJ(PlaylistDAO::AutoDJSendLoc loc) { auto* trackModel = getTrackModel(); if (!trackModel->hasCapabilities(TrackModel::Capability::AddToAutoDJ)) { diff --git a/src/widget/wtracktableview.h b/src/widget/wtracktableview.h index 9cb18bbc5b0..473dd038510 100644 --- a/src/widget/wtracktableview.h +++ b/src/widget/wtracktableview.h @@ -42,6 +42,8 @@ class WTrackTableView : public WLibraryTableView { TrackModel::SortColumnId getColumnIdFromCurrentIndex() override; QList getSelectedTrackIds() const; void setSelectedTracks(const QList& tracks); + TrackId getCurrentTrackId() const; + bool setCurrentTrackId(const TrackId& trackId, const int column); double getBackgroundColorOpacity() const { return m_backgroundColorOpacity;