Skip to content

Commit

Permalink
Library: keep selection when search is less specific
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Nov 22, 2021
1 parent a5a01ce commit 8310d05
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 68 additions & 1 deletion src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -475,8 +476,30 @@ void WTrackTableView::onSearch(const QString& text) {
TrackModel* trackModel = getTrackModel();
if (trackModel) {
saveCurrentViewState();
bool queryIsLessSpecific = SearchQueryParser::queryIsLessSpecific(
trackModel->currentSearch(), text);
QList<TrackId> 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);
}
}
}
}

Expand Down Expand Up @@ -869,6 +892,26 @@ QList<TrackId> 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<TrackId>& trackIds) {
QItemSelectionModel* pSelectionModel = selectionModel();
VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) {
Expand All @@ -892,6 +935,30 @@ void WTrackTableView::setSelectedTracks(const QList<TrackId>& 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<int> 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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class WTrackTableView : public WLibraryTableView {
TrackModel::SortColumnId getColumnIdFromCurrentIndex() override;
QList<TrackId> getSelectedTrackIds() const;
void setSelectedTracks(const QList<TrackId>& tracks);
TrackId getCurrentTrackId() const;
bool setCurrentTrackId(const TrackId& trackId, const int column);

double getBackgroundColorOpacity() const {
return m_backgroundColorOpacity;
Expand Down

0 comments on commit 8310d05

Please sign in to comment.