/* This file is part of Clementine. Copyright 2010, David Sansome Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Clementine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Clementine. If not, see . */ #include "queue.h" #include #include #include const char* Queue::kRowsMimetype = "application/x-clementine-queue-rows"; Queue::Queue(QObject* parent) : QAbstractProxyModel(parent) { } QModelIndex Queue::mapFromSource(const QModelIndex& source_index) const { if (!source_index.isValid()) return QModelIndex(); const int source_row = source_index.row(); for (int i=0 ; iindex(row, 0)); if (!proxy_index.isValid()) continue; emit dataChanged(proxy_index, proxy_index); } } void Queue::SourceLayoutChanged() { for (int i=0 ; i& 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); } QStringList Queue::mimeTypes() const { return QStringList() << kRowsMimetype << Playlist::kRowsMimetype; } Qt::DropActions Queue::supportedDropActions() const { return Qt::MoveAction | Qt::CopyAction | Qt::LinkAction; } QMimeData* Queue::mimeData(const QModelIndexList& indexes) const { QMimeData* data = new QMimeData; QList rows; foreach (const QModelIndex& index, indexes) { if (index.column() != 0) continue; rows << index.row(); } QBuffer buf; buf.open(QIODevice::WriteOnly); QDataStream stream(&buf); stream << rows; buf.close(); data->setData(kRowsMimetype, buf.data()); return data; } bool Queue::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int, const QModelIndex&) { if (action == Qt::IgnoreAction) return false; if (data->hasFormat(kRowsMimetype)) { // Dragged from the queue QList proxy_rows; QDataStream stream(data->data(kRowsMimetype)); stream >> proxy_rows; qStableSort(proxy_rows); // Make sure we take them in order Move(proxy_rows, row); } else if (data->hasFormat(Playlist::kRowsMimetype)) { // Dragged from the playlist Playlist* playlist = NULL; QList source_rows; QDataStream stream(data->data(Playlist::kRowsMimetype)); stream.readRawData(reinterpret_cast(&playlist), sizeof(playlist)); stream >> source_rows; QModelIndexList source_indexes; foreach (int source_row, source_rows) { const QModelIndex source_index = sourceModel()->index(source_row, 0); const QModelIndex proxy_index = mapFromSource(source_index); if (proxy_index.isValid()) { // This row was already in the queue, so no need to add it again continue; } source_indexes << source_index; } if (!source_indexes.isEmpty()) { const int insert_point = row == -1 ? source_indexes_.count() : row; beginInsertRows(QModelIndex(), insert_point, insert_point + source_indexes.count() - 1); for (int i=0 ; i& proxy_rows) { //order the rows qStableSort(proxy_rows); //reflects immediately changes in the playlist layoutAboutToBeChanged(); int removed_rows = 0; foreach (int row, proxy_rows) { //after the first row, the row number needs to be updated const int real_row = row-removed_rows; beginRemoveRows(QModelIndex(), real_row, real_row); source_indexes_.removeAt(real_row); endRemoveRows(); removed_rows++; } layoutChanged(); }