From c7ac2012b41947f328bde86ca46bf60248824d04 Mon Sep 17 00:00:00 2001 From: Alexander Bikadorov Date: Mon, 10 Feb 2014 16:51:43 +0100 Subject: [PATCH 1/2] correct selection after removing rows in playlist --- src/playlist/playlistview.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/playlist/playlistview.cpp b/src/playlist/playlistview.cpp index dcd2b6f4b..d34f45304 100644 --- a/src/playlist/playlistview.cpp +++ b/src/playlist/playlistview.cpp @@ -615,15 +615,16 @@ void PlaylistView::RemoveSelected() { return; } + // Store the last selected row, which is the last in the list + int last_row = selection.last().top(); + // Sort the selection so we remove the items at the *bottom* first, ensuring // we don't have to mess around with changing row numbers qSort(selection.begin(), selection.end(), CompareSelectionRanges); - // Store the last selected row, which is the first in the list - int last_row = selection.first().bottom(); - for (const QItemSelectionRange& range : selection) { - rows_removed += range.height(); + if (range.top() < last_row) + rows_removed += range.height(); model()->removeRows(range.top(), range.height(), range.parent()); } @@ -634,23 +635,20 @@ void PlaylistView::RemoveSelected() { // Select the new current item, we want always the item after the last // selected if (new_index.isValid()) { - // Update visual selection with the entire row - selectionModel()->select( - QItemSelection(new_index, - model()->index(new_row, model()->columnCount() - 1)), - QItemSelectionModel::Select); - // Update keyboard selected row, if it's not the first row + // Workaround to update keyboard selected row, if it's not the first row + // (this also triggers selection) if (new_row != 0) keyPressEvent( new QKeyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier)); - } else { - // We're removing the last item, select the new last row - selectionModel()->select( - QItemSelection(model()->index(model()->rowCount() - 1, 0), - model()->index(model()->rowCount() - 1, - model()->columnCount() - 1)), - QItemSelectionModel::Select); - } + // Update visual selection with the entire row + selectionModel()->select(new_index, QItemSelectionModel::ClearAndSelect | + QItemSelectionModel::Rows); + } else { + // We're removing the last item, select the new last row + selectionModel()->select(model()->index(model()->rowCount()-1, 0), + QItemSelectionModel::ClearAndSelect | + QItemSelectionModel::Rows); + } } QList PlaylistView::GetEditableColumns() { From 3adaf5d59894ced3367bbb88f60f1c62db545f32 Mon Sep 17 00:00:00 2001 From: Alexander Bikadorov Date: Tue, 11 Feb 2014 13:18:08 +0100 Subject: [PATCH 2/2] fixed indentation / coding style --- src/playlist/playlistview.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/playlist/playlistview.cpp b/src/playlist/playlistview.cpp index d34f45304..a97c3b1ab 100644 --- a/src/playlist/playlistview.cpp +++ b/src/playlist/playlistview.cpp @@ -576,10 +576,9 @@ void PlaylistView::keyPressEvent(QKeyEvent* event) { } else if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { if (currentIndex().isValid()) emit PlayItem(currentIndex()); event->accept(); - } else if (event->modifiers() != - Qt::ControlModifier // Ctrl+Space selects the item - && - event->key() == Qt::Key_Space) { + } else if (event->modifiers() != Qt::ControlModifier // Ctrl+Space selects + // the item + && event->key() == Qt::Key_Space) { emit PlayPause(); event->accept(); } else if (event->key() == Qt::Key_Left) { @@ -623,8 +622,7 @@ void PlaylistView::RemoveSelected() { qSort(selection.begin(), selection.end(), CompareSelectionRanges); for (const QItemSelectionRange& range : selection) { - if (range.top() < last_row) - rows_removed += range.height(); + if (range.top() < last_row) rows_removed += range.height(); model()->removeRows(range.top(), range.height(), range.parent()); } @@ -640,15 +638,15 @@ void PlaylistView::RemoveSelected() { if (new_row != 0) keyPressEvent( new QKeyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier)); - // Update visual selection with the entire row - selectionModel()->select(new_index, QItemSelectionModel::ClearAndSelect | - QItemSelectionModel::Rows); - } else { - // We're removing the last item, select the new last row - selectionModel()->select(model()->index(model()->rowCount()-1, 0), - QItemSelectionModel::ClearAndSelect | - QItemSelectionModel::Rows); - } + // Update visual selection with the entire row + selectionModel()->select(new_index, QItemSelectionModel::ClearAndSelect | + QItemSelectionModel::Rows); + } else { + // We're removing the last item, select the new last row + selectionModel()->select( + model()->index(model()->rowCount() - 1, 0), + QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + } } QList PlaylistView::GetEditableColumns() {