Skip to content

Commit

Permalink
Crates: restore sidebar selection position after crate deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Aug 13, 2021
1 parent 42d925a commit 185e373
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/library/trackset/crate/cratefeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,11 @@ void CrateFeature::slotDeleteCrate() {
qWarning() << "Refusing to delete locked crate" << crate;
return;
}
// TODO Store sibling index to restore selection after crate was deleted
// to avoid scroll position reset (to Crate root item)
if (m_pTrackCollection->deleteCrate(crate.getId())) {
CrateId crateId = crate.getId();
// Store sibling id to restore selection after crate was deleted
// to avoid the scroll position being reset to Crate root item.
storePrevSiblingCrateId(crateId);
if (m_pTrackCollection->deleteCrate(crateId)) {
qDebug() << "Deleted crate" << crate;
return;
}
Expand Down Expand Up @@ -768,16 +770,34 @@ void CrateFeature::slotExportTrackFiles() {
track_export.exportTracks();
}

void CrateFeature::storePrevSiblingCrateId(CrateId crateId) {
QModelIndex actIndex = indexFromCrateId(crateId);
for (int i = (actIndex.row() + 1); i >= (actIndex.row() - 1); i -= 2) {
QModelIndex newIndex = actIndex.sibling(i, actIndex.column());
if (newIndex.isValid()) {
TreeItem* pTreeItem = m_pSidebarModel->getItem(newIndex);
DEBUG_ASSERT(pTreeItem != nullptr);
if (!pTreeItem->hasChildren()) {
m_prevSiblingCrate = crateIdFromIndex(newIndex);
}
}
}
}

void CrateFeature::slotCrateTableChanged(CrateId crateId) {
if (m_lastRightClickedIndex.isValid() &&
(crateIdFromIndex(m_lastRightClickedIndex) == crateId)) {
// Preserve crate selection
// Try to restore previous selection
m_lastRightClickedIndex = rebuildChildModel(crateId);
if (m_lastRightClickedIndex.isValid()) {
// Select last active crate
activateCrate(crateId);
} else if (m_prevSiblingCrate.isValid()) {
// Select neighbour of deleted crate
activateCrate(m_prevSiblingCrate);
}
} else {
// Discard crate selection
// No valid selection to restore
rebuildChildModel();
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/library/trackset/crate/cratefeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class CrateFeature : public BaseTrackSetFeature {

CrateTableModel m_crateTableModel;

// Stores the id of a crate in the sidebar that is adjacent to the crate(crateId).
void storePrevSiblingCrateId(CrateId crateId);
// Can be used to restore a similiar selection after the sidebar model was rebuilt.
CrateId m_prevSiblingCrate;

QModelIndex m_lastRightClickedIndex;
TrackPointer m_pSelectedTrack;

Expand Down

0 comments on commit 185e373

Please sign in to comment.