Implement moving items in the queue

This commit is contained in:
David Sansome 2010-07-11 17:39:11 +00:00
parent 1f9e72f971
commit 4b81a86d1d
6 changed files with 85 additions and 2 deletions

View File

@ -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 ; i<queue_->rowCount() ; ++i) {
const QModelIndex index = queue_->mapToSource(queue_->index(i, Column_Title));
emit dataChanged(index, index);
}
}

View File

@ -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_;

View File

@ -158,3 +158,53 @@ void Queue::Clear() {
source_indexes_.clear();
endRemoveRows();
}
void Queue::Move(const QList<int>& proxy_rows, int pos) {
layoutAboutToBeChanged();
QList<QPersistentModelIndex> 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<start+moved_items.count() ; ++i) {
source_indexes_.insert(i, moved_items[i - start]);
}
// Update persistent indexes
foreach (const QModelIndex& pidx, persistentIndexList()) {
const int dest_offset = proxy_rows.indexOf(pidx.row());
if (dest_offset != -1) {
// This index was moved
changePersistentIndex(pidx, index(start + dest_offset, pidx.column(), QModelIndex()));
} else {
int d = 0;
foreach (int row, proxy_rows) {
if (pidx.row() > 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<int>() << row, row - 1);
}
void Queue::MoveDown(int row) {
Move(QList<int>() << row, row + 2);
}

View File

@ -41,6 +41,9 @@ public:
QModelIndex TakeNext();
void ToggleTracks(const QModelIndexList& source_indexes);
void Clear();
void Move(const QList<int>& proxy_rows, int pos);
void MoveUp(int row);
void MoveDown(int row);
// QAbstractProxyModel
void setSourceModel(QAbstractItemModel* source_model);

View File

@ -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() {

View File

@ -25,6 +25,9 @@
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>