diff --git a/src/playlist/playlist.cpp b/src/playlist/playlist.cpp index f812a3746..6d7b6449c 100644 --- a/src/playlist/playlist.cpp +++ b/src/playlist/playlist.cpp @@ -78,6 +78,7 @@ Playlist::Playlist(PlaylistBackend* backend, TaskManager* task_manager, connect(queue_, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(TracksAboutToBeDequeued(QModelIndex,int,int))); connect(queue_, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(TracksDequeued())); + connect(queue_, SIGNAL(layoutChanged()), SLOT(QueueLayoutChanged())); } Playlist::~Playlist() { @@ -1071,7 +1072,6 @@ PlaylistItemList Playlist::library_items_by_id(int id) const { } void Playlist::TracksAboutToBeDequeued(const QModelIndex&, int begin, int end) { - qDebug() << begin << end; for (int i=begin ; i<=end ; ++i) { temp_queue_change_indexes_ << queue_->mapToSource(queue_->index(i, Column_Title)); } @@ -1079,8 +1079,14 @@ void Playlist::TracksAboutToBeDequeued(const QModelIndex&, int begin, int end) { void Playlist::TracksDequeued() { foreach (const QModelIndex& index, temp_queue_change_indexes_) { - qDebug() << "Changed" << index.row() << index.data(Role_QueuePosition); emit dataChanged(index, index); } temp_queue_change_indexes_.clear(); } + +void Playlist::QueueLayoutChanged() { + for (int i=0 ; irowCount() ; ++i) { + const QModelIndex index = queue_->mapToSource(queue_->index(i, Column_Title)); + emit dataChanged(index, index); + } +} diff --git a/src/playlist/playlist.h b/src/playlist/playlist.h index e1df20b93..fe55c8fc9 100644 --- a/src/playlist/playlist.h +++ b/src/playlist/playlist.h @@ -200,6 +200,7 @@ class Playlist : public QAbstractListModel { private slots: void TracksAboutToBeDequeued(const QModelIndex&, int begin, int end); void TracksDequeued(); + void QueueLayoutChanged(); private: PlaylistFilter* proxy_; diff --git a/src/playlist/queue.cpp b/src/playlist/queue.cpp index 942de11dd..529c5ef53 100644 --- a/src/playlist/queue.cpp +++ b/src/playlist/queue.cpp @@ -158,3 +158,53 @@ void Queue::Clear() { source_indexes_.clear(); endRemoveRows(); } + +void Queue::Move(const QList& proxy_rows, int pos) { + layoutAboutToBeChanged(); + QList moved_items; + + // Take the items out of the list first, keeping track of whether the + // insertion point changes + int offset = 0; + foreach (int row, proxy_rows) { + moved_items << source_indexes_.takeAt(row-offset); + if (pos != -1 && pos >= row) + pos --; + offset++; + } + + // Put the items back in + const int start = pos == -1 ? source_indexes_.count() : pos; + for (int i=start ; i row) + d --; + } + if (pidx.row() + d >= start) + d += proxy_rows.count(); + + changePersistentIndex(pidx, index(pidx.row() + d, pidx.column(), QModelIndex())); + } + } + + layoutChanged(); +} + +void Queue::MoveUp(int row) { + Move(QList() << row, row - 1); +} + +void Queue::MoveDown(int row) { + Move(QList() << row, row + 2); +} diff --git a/src/playlist/queue.h b/src/playlist/queue.h index 3d8f2e8fb..bc97aa6c5 100644 --- a/src/playlist/queue.h +++ b/src/playlist/queue.h @@ -41,6 +41,9 @@ public: QModelIndex TakeNext(); void ToggleTracks(const QModelIndexList& source_indexes); void Clear(); + void Move(const QList& proxy_rows, int pos); + void MoveUp(int row); + void MoveDown(int row); // QAbstractProxyModel void setSourceModel(QAbstractItemModel* source_model); diff --git a/src/playlist/queuemanager.cpp b/src/playlist/queuemanager.cpp index 6d4ce87a8..57e15ac9c 100644 --- a/src/playlist/queuemanager.cpp +++ b/src/playlist/queuemanager.cpp @@ -58,6 +58,8 @@ void QueueManager::CurrentPlaylistChanged(Playlist* playlist) { this, SLOT(UpdateButtonState())); disconnect(current_playlist_->queue(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(UpdateButtonState())); + disconnect(current_playlist_->queue(), SIGNAL(layoutChanged()), + this, SLOT(UpdateButtonState())); } current_playlist_ = playlist; @@ -66,6 +68,8 @@ void QueueManager::CurrentPlaylistChanged(Playlist* playlist) { this, SLOT(UpdateButtonState())); connect(current_playlist_->queue(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(UpdateButtonState())); + connect(current_playlist_->queue(), SIGNAL(layoutChanged()), + this, SLOT(UpdateButtonState())); ui_->list->setModel(current_playlist_->queue()); ui_->list->setModelColumn(Queue::Column_CombinedArtistTitle); @@ -75,11 +79,27 @@ void QueueManager::CurrentPlaylistChanged(Playlist* playlist) { } void QueueManager::MoveUp() { + QModelIndexList indexes = ui_->list->selectionModel()->selectedRows(); + qStableSort(indexes); + if (indexes.isEmpty() || indexes.first().row() == 0) + return; + + foreach (const QModelIndex& index, indexes) { + current_playlist_->queue()->MoveUp(index.row()); + } } void QueueManager::MoveDown() { + QModelIndexList indexes = ui_->list->selectionModel()->selectedRows(); + qStableSort(indexes); + if (indexes.isEmpty() || indexes.last().row() == current_playlist_->queue()->rowCount()-1) + return; + + for (int i=indexes.count()-1 ; i>=0 ; --i) { + current_playlist_->queue()->MoveDown(indexes[i].row()); + } } void QueueManager::Clear() { diff --git a/src/playlist/queuemanager.ui b/src/playlist/queuemanager.ui index ec9b60bfb..78e10fb1f 100644 --- a/src/playlist/queuemanager.ui +++ b/src/playlist/queuemanager.ui @@ -25,6 +25,9 @@ true + + QAbstractItemView::ExtendedSelection + QAbstractItemView::SelectRows