Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/library/basesqltablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,59 @@ void BaseSqlTableModel::hideTracks(const QModelIndexList& indices) {

// TODO(rryan) : do not select, instead route event to BTC and notify from
// there.
select(); //Repopulate the data model.

QSet<TrackId> tracksRemovedSet = QSet<TrackId>(trackIds.begin(), trackIds.end());
removeTrackRows(tracksRemovedSet);
}

void BaseSqlTableModel::removeTrackRows(const QSet<TrackId>& trackIdsToRemove) {
// This is called after hiding, purging or removing tracks from a track set.
// The purpose is to keep the tracks view constant after after these
// operations by avoiding pointless/confusing re-sorting of the model,
// which happens when we call select(), which rebuilds the row cache.
// We assume metadata of remaining tracks hasn't changed, we can simply
// remove the respective track rows.

// However, if this is a playlist model, track removal likely also changes
// the tracks' positions in the playlist. We need to get the tracks' new
// positions from the database, so let's do a select().
// FIXME Update position without re-sorting
if (hasPositionColumn()) {
select();
return;
}

// For other models we can now remove all track rows.
QVector<RowInfo> rowInfos = m_rowInfo;
TrackId2Rows trackIdToRows;
TrackPos2Row trackPosToRows; // remains empty

QMutableListIterator<RowInfo> it(rowInfos);
while (it.hasNext()) {
const RowInfo& rowInfo = it.next();
if (trackIdsToRemove.contains(rowInfo.trackId)) {
it.remove();
}
}

// Recreate TrackId2Rows, taken from select()
trackIdToRows.reserve(rowInfos.size());
for (int i = 0; i < rowInfos.size(); ++i) {
const RowInfo& rowInfo = rowInfos[i];
if (rowInfo.row == -1) {
// We've reached the end of valid rows. Resize rowInfo to cut off
// this and all further elements.
rowInfos.resize(i);
break;
}
trackIdToRows[rowInfo.trackId].push_back(i);
}

clearRows();
replaceRows(
std::move(rowInfos),
std::move(trackIdToRows),
std::move(trackPosToRows));
}

QList<TrackRef> BaseSqlTableModel::getTrackRefs(
Expand Down
1 change: 1 addition & 0 deletions src/library/basesqltablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class BaseSqlTableModel : public BaseTrackTableModel {
int columnIndexFromSortColumnId(TrackModel::SortColumnId sortColumn) const override;

void hideTracks(const QModelIndexList& indices) override;
void removeTrackRows(const QSet<TrackId>& trackIdsToRemove) override;

void select() override;

Expand Down
8 changes: 6 additions & 2 deletions src/library/basetracktablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ BaseTrackTableModel::BaseTrackTableModel(
m_trackPlayedColor(QColor(WTrackTableView::kDefaultTrackPlayedColor)),
m_trackMissingColor(QColor(WTrackTableView::kDefaultTrackMissingColor)) {
connect(&pTrackCollectionManager->internalCollection()->getTrackDAO(),
&TrackDAO::forceModelUpdate,
&TrackDAO::tracksRemoved,
this,
&BaseTrackTableModel::slotRefreshAllRows);
&BaseTrackTableModel::slotTracksRemoved);
connect(&PlayerInfo::instance(),
&PlayerInfo::trackChanged,
this,
Expand Down Expand Up @@ -989,6 +989,10 @@ void BaseTrackTableModel::slotRefreshAllRows() {
select();
}

void BaseTrackTableModel::slotTracksRemoved(const QSet<TrackId>& trackIds) {
removeTrackRows(trackIds);
}

void BaseTrackTableModel::emitDataChangedForMultipleRowsInColumn(
const QList<int>& rows,
int column,
Expand Down
2 changes: 2 additions & 0 deletions src/library/basetracktablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ class BaseTrackTableModel : public QAbstractTableModel, public TrackModel {

void slotRefreshAllRows();

void slotTracksRemoved(const QSet<TrackId>& trackIds);

void slotCoverFound(
const QObject* pRequester,
const CoverInfo& coverInfo,
Expand Down
4 changes: 2 additions & 2 deletions src/library/dao/trackdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,9 @@ void TrackDAO::afterPurgingTracks(
#else
QSet<TrackId> tracksRemovedSet = QSet<TrackId>::fromList(trackIds);
#endif
// Notify BaseTrackCache it should remove tracks and track models
// that they should update their cache as well.
emit tracksRemoved(tracksRemovedSet);
// notify trackmodels that they should update their cache as well.
emit forceModelUpdate();
}

namespace {
Expand Down
1 change: 1 addition & 0 deletions src/library/dao/trackdao.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class TrackDAO : public QObject, public virtual DAO, public virtual GlobalTrackC
void progressVerifyTracksOutside(const QString& path);
void progressCoverArt(const QString& file);
void forceModelUpdate();
void removeTrackRows(const QSet<TrackId>& trackIds);

public slots:
// Slots to inform the TrackDAO about changes that
Expand Down
2 changes: 1 addition & 1 deletion src/library/trackcollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TrackCollection::TrackCollection(
connect(&m_trackDao,
&TrackDAO::tracksRemoved,
this,
&TrackCollection::tracksRemoved,
&TrackCollection::tracksRemoved, // unused
/*signal-to-signal*/ Qt::DirectConnection);
connect(&m_trackDao,
&TrackDAO::forceModelUpdate,
Expand Down
2 changes: 2 additions & 0 deletions src/library/trackmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class TrackModel {
virtual void select() {
}

virtual void removeTrackRows(const QSet<TrackId>&) {};

/// This is an interface to stop any potentially running
/// model population when switching models in WTrackTableView.
/// Only implemented in ProxyTrackModel.
Expand Down
4 changes: 3 additions & 1 deletion src/library/trackset/crate/cratetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ void CrateTableModel::removeTracks(const QModelIndexList& indices) {
return;
}

select();
// Now remove the track rows
QSet<TrackId> tracksRemovedSet = QSet<TrackId>(trackIds.begin(), trackIds.end());
removeTrackRows(tracksRemovedSet);
}

QString CrateTableModel::modelKey(bool noSearch) const {
Expand Down