Skip to content

Commit

Permalink
Fix race condition when applying sorting in WTrackTableView::loadTrac…
Browse files Browse the repository at this point in the history
…kModel
  • Loading branch information
Holzhaus committed Jun 6, 2019
1 parent 0909888 commit ca3e6a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ WTrackTableView::WTrackTableView(QWidget * parent,
m_pKeyNotation->connectValueChanged(this, &WTrackTableView::keyNotationChanged);

m_pSortColumn = new ControlProxy("[Library]", "sort_column", this);
m_pSortColumn->connectValueChanged(this, &WTrackTableView::applySorting);
m_pSortColumn->connectValueChanged(this, &WTrackTableView::applySortingIfVisible);
m_pSortOrder = new ControlProxy("[Library]", "sort_order", this);
m_pSortOrder->connectValueChanged(this, &WTrackTableView::applySorting);
m_pSortOrder->connectValueChanged(this, &WTrackTableView::applySortingIfVisible);

connect(this, SIGNAL(scrollValueChanged(int)),
this, SLOT(slotScrollValueChanged(int)));
Expand Down Expand Up @@ -390,6 +390,7 @@ void WTrackTableView::loadTrackModel(QAbstractItemModel *model) {

m_pSortColumn->set(trackModel->sortColumnIdFromColumnIndex(sortColumn));
m_pSortOrder->set(sortOrder);
applySorting();
}

// Set up drag and drop behavior according to whether or not the track
Expand Down Expand Up @@ -417,12 +418,6 @@ void WTrackTableView::loadTrackModel(QAbstractItemModel *model) {

setVisible(true);

if (m_sorting) {
// Sorting has to be applied after setVisible(true), since only visible
// WTrackTableViews will be sorted
applySorting();
}

restoreVScrollBarPos(newModel);
// restoring scrollBar position using model pointer as key
// scrollbar positions with respect to different models are backed by map
Expand Down Expand Up @@ -1785,13 +1780,17 @@ void WTrackTableView::doSortByColumn(int headerSection) {
horizontalScrollBar()->setValue(savedHScrollBarPos);
}

void WTrackTableView::applySorting() {
void WTrackTableView::applySortingIfVisible() {
// There are multiple instances of WTrackTableView, but we only want to
// apply the sorting to the currently visible instance
if (!isVisible()) {
return;
}

applySorting();
}

void WTrackTableView::applySorting() {
TrackModel* trackModel = getTrackModel();
int sortColumnId = static_cast<int>(m_pSortColumn->get());
if (sortColumnId < 0 || sortColumnId >= TrackModel::SortColumnId::NUM_SORTCOLUMNIDS) {
Expand Down Expand Up @@ -2034,7 +2033,7 @@ void WTrackTableView::slotSortingChanged(int headerSection, Qt::SortOrder order)
}

if (sortingChanged) {
applySorting();
applySortingIfVisible();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class WTrackTableView : public WLibraryTableView {
void addSelectionToNewCrate();
void loadSelectionToGroup(QString group, bool play = false);
void doSortByColumn(int headerSection);
void applySortingIfVisible();
void applySorting();
void slotLockBpm();
void slotUnlockBpm();
Expand Down

0 comments on commit ca3e6a8

Please sign in to comment.