From: Levente Polyak Date: Fri, 28 May 2010 10:24:26 +0000 (+0200) Subject: fixing wrong item positions when moving more than one item down or up X-Git-Tag: 1.2~34^2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=b791a9514e15989bbb01ce4b8c1ba302953216e6;p=minitube fixing wrong item positions when moving more than one item down or up - need to sort keys before moving, else we may end up with same result then before - if moving items down respect the single static item 'search more' - after moving set selected index to something human intuitive - do not move items beyond boundary, just leave them where they are --- diff --git a/src/ListModel.cpp b/src/ListModel.cpp index e7592ec..2b0d12f 100755 --- a/src/ListModel.cpp +++ b/src/ListModel.cpp @@ -377,7 +377,6 @@ QModelIndex ListModel::indexForVideo(Video* video) { } void ListModel::move(QModelIndexList &indexes, bool up) { - QList movedVideos; foreach (QModelIndex index, indexes) { @@ -387,10 +386,11 @@ void ListModel::move(QModelIndexList &indexes, bool up) { movedVideos << video; } - int counter = 1; + int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1; foreach (Video *video, movedVideos) { int row = rowForVideo(video); + if (row+mod==end) { end=row; continue; } // qDebug() << "video row" << row; removeRows(row, 1, QModelIndex()); @@ -401,7 +401,6 @@ void ListModel::move(QModelIndexList &indexes, bool up) { videos.insert(row, video); endInsertRows(); - counter++; } emit needSelectionFor(movedVideos); diff --git a/src/MediaView.cpp b/src/MediaView.cpp index 8735f8a..a33c23d 100644 --- a/src/MediaView.cpp +++ b/src/MediaView.cpp @@ -375,14 +375,26 @@ void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QIte void MediaView::moveUpSelected() { if (!listView->selectionModel()->hasSelection()) return; + QModelIndexList indexes = listView->selectionModel()->selectedIndexes(); + qStableSort(indexes.begin(), indexes.end()); listModel->move(indexes, true); + + // set current index after row moves to something more intuitive + int row = indexes.first().row(); + listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate); } void MediaView::moveDownSelected() { if (!listView->selectionModel()->hasSelection()) return; + QModelIndexList indexes = listView->selectionModel()->selectedIndexes(); + qStableSort(indexes.begin(), indexes.end(), qGreater()); listModel->move(indexes, false); + + // set current index after row moves to something more intuitive (respect 1 static item on bottom) + int row = indexes.first().row()+1, max = listModel->rowCount() - 2; + listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate); } void MediaView::showVideoContextMenu(QPoint point) {