Skip to content

Commit 6e0463d

Browse files
committed
(fix) WTrackTableView: assert we have a selection model, add getSelectedRows(), early exit slotGuiTick50ms()
1 parent 65904de commit 6e0463d

3 files changed

Lines changed: 55 additions & 34 deletions

File tree

src/library/trackmodel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ class TrackModel {
217217

218218
/// @brief modelKey returns a unique identifier for the model
219219
/// @param noSearch don't include the current search in the key
220-
/// @param baseOnly return only a identifier for the whole subsystem
221220
virtual QString modelKey(bool noSearch) const = 0;
222221

223222
virtual bool getRequireConfirmationToHideRemoveTracks() {

src/widget/wtracktableview.cpp

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ void WTrackTableView::selectionChanged(
107107
}
108108

109109
void WTrackTableView::slotGuiTick50ms(double /*unused*/) {
110+
if (!isVisible()) {
111+
// Don't proceed if this isn't visible.
112+
return;
113+
}
114+
110115
// if the user is stopped in the same row for more than 0.1 s,
111116
// we load un-cached cover arts as well.
112117
mixxx::Duration timeDelta = mixxx::Time::elapsed() - m_lastUserAction;
@@ -116,7 +121,7 @@ void WTrackTableView::slotGuiTick50ms(double /*unused*/) {
116121
// slows down scrolling performance so we wait until the user has
117122
// stopped interacting first.
118123
if (m_selectionChangedSinceLastGuiTick) {
119-
const QModelIndexList indices = selectionModel()->selectedRows();
124+
const QModelIndexList indices = getSelectedRows();
120125
if (indices.size() == 1 && indices.first().isValid()) {
121126
// A single track has been selected
122127
TrackModel* trackModel = getTrackModel();
@@ -401,7 +406,7 @@ TrackModel::SortColumnId WTrackTableView::getColumnIdFromCurrentIndex() {
401406
}
402407

403408
void WTrackTableView::assignPreviousTrackColor() {
404-
QModelIndexList indices = selectionModel()->selectedRows();
409+
const QModelIndexList indices = getSelectedRows();
405410
if (indices.isEmpty()) {
406411
return;
407412
}
@@ -422,7 +427,7 @@ void WTrackTableView::assignPreviousTrackColor() {
422427
}
423428

424429
void WTrackTableView::assignNextTrackColor() {
425-
QModelIndexList indices = selectionModel()->selectedRows();
430+
const QModelIndexList indices = getSelectedRows();
426431
if (indices.isEmpty()) {
427432
return;
428433
}
@@ -443,7 +448,7 @@ void WTrackTableView::assignNextTrackColor() {
443448
}
444449

445450
void WTrackTableView::slotPurge() {
446-
QModelIndexList indices = selectionModel()->selectedRows();
451+
const QModelIndexList indices = getSelectedRows();
447452
if (indices.isEmpty()) {
448453
return;
449454
}
@@ -457,7 +462,7 @@ void WTrackTableView::slotPurge() {
457462
}
458463

459464
void WTrackTableView::slotDeleteTracksFromDisk() {
460-
QModelIndexList indices = selectionModel()->selectedRows();
465+
const QModelIndexList indices = getSelectedRows();
461466
if (indices.isEmpty()) {
462467
return;
463468
}
@@ -468,7 +473,7 @@ void WTrackTableView::slotDeleteTracksFromDisk() {
468473
}
469474

470475
void WTrackTableView::slotUnhide() {
471-
QModelIndexList indices = selectionModel()->selectedRows();
476+
const QModelIndexList indices = getSelectedRows();
472477
if (indices.isEmpty()) {
473478
return;
474479
}
@@ -505,7 +510,10 @@ void WTrackTableView::contextMenuEvent(QContextMenuEvent* event) {
505510
}
506511
event->accept();
507512
// Update track indices in context menu
508-
QModelIndexList indices = selectionModel()->selectedRows();
513+
const QModelIndexList indices = getSelectedRows();
514+
if (indices.isEmpty()) {
515+
return;
516+
}
509517
m_pTrackMenu->loadTrackModelIndices(indices);
510518

511519
saveCurrentIndex();
@@ -576,7 +584,7 @@ void WTrackTableView::mouseMoveEvent(QMouseEvent* pEvent) {
576584
if (DragAndDropHelper::mouseMoveInitiatesDrag(pEvent)) {
577585
// Iterate over selected rows and append each item's location url to a list.
578586
QList<QString> locations;
579-
const QModelIndexList indices = selectionModel()->selectedRows();
587+
const QModelIndexList indices = getSelectedRows();
580588

581589
for (const QModelIndex& index : indices) {
582590
if (!index.isValid()) {
@@ -635,13 +643,18 @@ void WTrackTableView::dragMoveEvent(QDragMoveEvent * event) {
635643
// Drag-and-drop "drop" event. Occurs when something is dropped onto the track table view
636644
void WTrackTableView::dropEvent(QDropEvent * event) {
637645
TrackModel* trackModel = getTrackModel();
638-
639646
// We only do things to the TrackModel in this method so if we don't have
640647
// one we should just bail.
641648
if (!trackModel) {
642649
return;
643650
}
644651

652+
QItemSelectionModel* pSelectionModel = selectionModel();
653+
VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) {
654+
qWarning() << "No selection model available";
655+
return;
656+
}
657+
645658
if (!event->mimeData()->hasUrls() || trackModel->isLocked()) {
646659
event->ignore();
647660
return;
@@ -684,7 +697,7 @@ void WTrackTableView::dropEvent(QDropEvent * event) {
684697
// Save a list of row (just plain ints) so we don't get screwed over
685698
// when the QModelIndexes all become invalid (eg. after moveTrack()
686699
// or addTrack())
687-
const QModelIndexList indices = selectionModel()->selectedRows();
700+
const QModelIndexList indices = getSelectedRows();
688701

689702
QList<int> selectedRows;
690703
for (const QModelIndex& idx : indices) {
@@ -764,13 +777,13 @@ void WTrackTableView::dropEvent(QDropEvent * event) {
764777
// Highlight the moved rows again (restoring the selection)
765778
//QModelIndex newSelectedIndex = destIndex;
766779
for (int i = 0; i < selectedRowCount; i++) {
767-
this->selectionModel()->select(model()->index(selectionRestoreStartRow + i, 0),
768-
QItemSelectionModel::Select | QItemSelectionModel::Rows);
780+
pSelectionModel->select(model()->index(selectionRestoreStartRow + i, 0),
781+
QItemSelectionModel::Select | QItemSelectionModel::Rows);
769782
}
770783
} else { // Drag and drop inside Mixxx is only for few rows, bulks happen here
771784
// Reset the selected tracks (if you had any tracks highlighted, it
772785
// clears them)
773-
this->selectionModel()->clear();
786+
pSelectionModel->clear();
774787

775788
// Have to do this here because the index is invalid after
776789
// addTrack
@@ -811,9 +824,9 @@ void WTrackTableView::dropEvent(QDropEvent * event) {
811824
// reordering. (eg. crates don't support reordering/indexes)
812825
if (trackModel->hasCapabilities(TrackModel::Capability::Reorder)) {
813826
for (int i = selectionStartRow; i < selectionStartRow + numNewRows; i++) {
814-
this->selectionModel()->select(model()->index(i, 0),
815-
QItemSelectionModel::Select |
816-
QItemSelectionModel::Rows);
827+
pSelectionModel->select(model()->index(i, 0),
828+
QItemSelectionModel::Select |
829+
QItemSelectionModel::Rows);
817830
}
818831
}
819832
}
@@ -823,6 +836,15 @@ void WTrackTableView::dropEvent(QDropEvent * event) {
823836
verticalScrollBar()->setValue(vScrollBarPos);
824837
}
825838

839+
QModelIndexList WTrackTableView::getSelectedRows() const {
840+
QItemSelectionModel* pSelectionModel = selectionModel();
841+
VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) {
842+
qWarning() << "No selection model available";
843+
return {};
844+
}
845+
return pSelectionModel->selectedRows();
846+
}
847+
826848
TrackModel* WTrackTableView::getTrackModel() const {
827849
TrackModel* trackModel = dynamic_cast<TrackModel*>(model());
828850
return trackModel;
@@ -842,7 +864,7 @@ void WTrackTableView::keyPressEvent(QKeyEvent* event) {
842864
if (event->modifiers().testFlag(Qt::NoModifier)) {
843865
slotMouseDoubleClicked(currentIndex());
844866
} else if ((event->modifiers() & kPropertiesShortcutModifier)) {
845-
QModelIndexList indices = selectionModel()->selectedRows();
867+
const QModelIndexList indices = getSelectedRows();
846868
if (indices.length() == 1) {
847869
m_pTrackMenu->loadTrackModelIndices(indices);
848870
m_pTrackMenu->slotShowDlgTrackInfo();
@@ -902,7 +924,7 @@ void WTrackTableView::resizeEvent(QResizeEvent* event) {
902924
}
903925

904926
void WTrackTableView::hideOrRemoveSelectedTracks() {
905-
QModelIndexList indices = selectionModel()->selectedRows();
927+
const QModelIndexList indices = getSelectedRows();
906928
if (indices.isEmpty()) {
907929
return;
908930
}
@@ -997,15 +1019,15 @@ void WTrackTableView::hideOrRemoveSelectedTracks() {
9971019
}
9981020

9991021
void WTrackTableView::activateSelectedTrack() {
1000-
auto indices = selectionModel()->selectedRows();
1022+
const QModelIndexList indices = getSelectedRows();
10011023
if (indices.isEmpty()) {
10021024
return;
10031025
}
10041026
slotMouseDoubleClicked(indices.at(0));
10051027
}
10061028

10071029
void WTrackTableView::loadSelectedTrackToGroup(const QString& group, bool play) {
1008-
auto indices = selectionModel()->selectedRows();
1030+
const QModelIndexList indices = getSelectedRows();
10091031
if (indices.isEmpty()) {
10101032
return;
10111033
}
@@ -1045,21 +1067,14 @@ void WTrackTableView::loadSelectedTrackToGroup(const QString& group, bool play)
10451067
}
10461068

10471069
QList<TrackId> WTrackTableView::getSelectedTrackIds() const {
1048-
QList<TrackId> trackIds;
1049-
1050-
QItemSelectionModel* pSelectionModel = selectionModel();
1051-
VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) {
1052-
qWarning() << "No selected tracks available";
1053-
return trackIds;
1054-
}
1055-
10561070
TrackModel* pTrackModel = getTrackModel();
10571071
VERIFY_OR_DEBUG_ASSERT(pTrackModel != nullptr) {
10581072
qWarning() << "No selected tracks available";
1059-
return trackIds;
1073+
return {};
10601074
}
10611075

1062-
const QModelIndexList rows = selectionModel()->selectedRows();
1076+
const QModelIndexList rows = getSelectedRows();
1077+
QList<TrackId> trackIds;
10631078
trackIds.reserve(rows.size());
10641079
for (const QModelIndex& row: rows) {
10651080
const TrackId trackId = pTrackModel->getTrackId(row);
@@ -1234,8 +1249,12 @@ void WTrackTableView::doSortByColumn(int headerSection, Qt::SortOrder sortOrder)
12341249

12351250
sortByColumn(headerSection, sortOrder);
12361251

1237-
QItemSelectionModel* currentSelection = selectionModel();
1238-
currentSelection->reset(); // remove current selection
1252+
QItemSelectionModel* pSelectionModel = selectionModel();
1253+
VERIFY_OR_DEBUG_ASSERT(pSelectionModel != nullptr) {
1254+
qWarning() << "No selection model available";
1255+
return;
1256+
}
1257+
pSelectionModel->reset(); // remove current selection
12391258

12401259
// Find previously selected tracks and store respective rows for reselection.
12411260
QMap<int, int> selectedRows;
@@ -1276,7 +1295,7 @@ void WTrackTableView::doSortByColumn(int headerSection, Qt::SortOrder sortOrder)
12761295
while (i.hasNext()) {
12771296
i.next();
12781297
QModelIndex tl = itemModel->index(i.key(), 0);
1279-
currentSelection->select(tl, QItemSelectionModel::Rows | QItemSelectionModel::Select);
1298+
pSelectionModel->select(tl, QItemSelectionModel::Rows | QItemSelectionModel::Select);
12801299
}
12811300

12821301
// This seems to be broken since at least Qt 5.12: no scrolling is issued

src/widget/wtracktableview.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class WTrackTableView : public WLibraryTableView {
111111
// when dragging.
112112
void mouseMoveEvent(QMouseEvent *pEvent) override;
113113

114+
// Returns the list of selected rows, or an empty list if none are selected.
115+
QModelIndexList getSelectedRows() const;
116+
114117
// Returns the current TrackModel, or returns NULL if none is set.
115118
TrackModel* getTrackModel() const;
116119

0 commit comments

Comments
 (0)