2010-07-11 17:37:40 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-07-11 17:37:40 +02:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "queue.h"
|
|
|
|
|
2010-07-11 20:04:35 +02:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
const char* Queue::kRowsMimetype = "application/x-clementine-queue-rows";
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Queue::Queue(QObject* parent) : QAbstractProxyModel(parent) {}
|
2010-07-11 17:37:40 +02:00
|
|
|
|
|
|
|
QModelIndex Queue::mapFromSource(const QModelIndex& source_index) const {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!source_index.isValid()) return QModelIndex();
|
2010-07-11 17:37:40 +02:00
|
|
|
|
|
|
|
const int source_row = source_index.row();
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < source_indexes_.count(); ++i) {
|
2010-07-11 17:37:40 +02:00
|
|
|
if (source_indexes_[i].row() == source_row)
|
|
|
|
return index(i, source_index.column());
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2010-11-21 18:48:58 +01:00
|
|
|
bool Queue::ContainsSourceRow(int source_row) const {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < source_indexes_.count(); ++i) {
|
|
|
|
if (source_indexes_[i].row() == source_row) return true;
|
2010-11-21 18:48:58 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-11 17:37:40 +02:00
|
|
|
QModelIndex Queue::mapToSource(const QModelIndex& proxy_index) const {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!proxy_index.isValid()) return QModelIndex();
|
2010-07-11 20:04:35 +02:00
|
|
|
|
2010-07-11 17:37:40 +02:00
|
|
|
return source_indexes_[proxy_index.row()];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Queue::setSourceModel(QAbstractItemModel* source_model) {
|
|
|
|
if (sourceModel()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
disconnect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
|
|
|
|
this, SLOT(SourceDataChanged(QModelIndex, QModelIndex)));
|
|
|
|
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(SourceLayoutChanged()));
|
|
|
|
disconnect(sourceModel(), SIGNAL(layoutChanged()), this,
|
|
|
|
SLOT(SourceLayoutChanged()));
|
2010-07-11 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractProxyModel::setSourceModel(source_model);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
|
|
|
|
SLOT(SourceDataChanged(QModelIndex, QModelIndex)));
|
|
|
|
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
|
|
|
|
SLOT(SourceLayoutChanged()));
|
|
|
|
connect(sourceModel(), SIGNAL(layoutChanged()), this,
|
|
|
|
SLOT(SourceLayoutChanged()));
|
2010-07-11 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Queue::SourceDataChanged(const QModelIndex& top_left,
|
|
|
|
const QModelIndex& bottom_right) {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int row = top_left.row(); row <= bottom_right.row(); ++row) {
|
2010-07-11 17:37:40 +02:00
|
|
|
QModelIndex proxy_index = mapFromSource(sourceModel()->index(row, 0));
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!proxy_index.isValid()) continue;
|
2010-07-11 17:37:40 +02:00
|
|
|
|
2010-07-11 23:27:36 +02:00
|
|
|
emit dataChanged(proxy_index, proxy_index);
|
2010-07-11 17:37:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Queue::SourceLayoutChanged() {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < source_indexes_.count(); ++i) {
|
2010-07-11 17:37:40 +02:00
|
|
|
if (!source_indexes_[i].isValid()) {
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
source_indexes_.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex Queue::index(int row, int column, const QModelIndex& parent) const {
|
|
|
|
return createIndex(row, column);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QModelIndex Queue::parent(const QModelIndex& child) const {
|
2010-07-11 17:37:40 +02:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int Queue::rowCount(const QModelIndex& parent) const {
|
|
|
|
if (parent.isValid()) return 0;
|
2010-07-11 17:37:40 +02:00
|
|
|
return source_indexes_.count();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int Queue::columnCount(const QModelIndex&) const { return 1; }
|
2010-07-11 17:37:40 +02:00
|
|
|
|
|
|
|
QVariant Queue::data(const QModelIndex& proxy_index, int role) const {
|
2010-07-11 18:58:22 +02:00
|
|
|
QModelIndex source_index = source_indexes_[proxy_index.row()];
|
|
|
|
|
2010-07-11 18:17:38 +02:00
|
|
|
switch (role) {
|
|
|
|
case Playlist::Role_QueuePosition:
|
|
|
|
return proxy_index.row();
|
2010-07-11 17:37:40 +02:00
|
|
|
|
2010-07-11 23:27:36 +02:00
|
|
|
case Qt::DisplayRole: {
|
2014-02-07 16:34:20 +01:00
|
|
|
const QString artist =
|
|
|
|
source_index.sibling(source_index.row(), Playlist::Column_Artist)
|
|
|
|
.data()
|
|
|
|
.toString();
|
|
|
|
const QString title =
|
|
|
|
source_index.sibling(source_index.row(), Playlist::Column_Title)
|
|
|
|
.data()
|
|
|
|
.toString();
|
|
|
|
|
|
|
|
if (artist.isEmpty()) return title;
|
2012-02-19 14:44:33 +01:00
|
|
|
return QString(artist + " - " + title);
|
2010-07-11 23:27:36 +02:00
|
|
|
}
|
2010-07-11 18:17:38 +02:00
|
|
|
|
2010-07-11 18:58:22 +02:00
|
|
|
default:
|
2010-07-11 23:27:36 +02:00
|
|
|
return QVariant();
|
2010-07-11 17:37:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Queue::ToggleTracks(const QModelIndexList& source_indexes) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& source_index : source_indexes) {
|
2010-07-11 17:37:40 +02:00
|
|
|
QModelIndex proxy_index = mapFromSource(source_index);
|
|
|
|
if (proxy_index.isValid()) {
|
|
|
|
// Dequeue the track
|
|
|
|
const int row = proxy_index.row();
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
source_indexes_.removeAt(row);
|
|
|
|
endRemoveRows();
|
|
|
|
} else {
|
|
|
|
// Enqueue the track
|
|
|
|
const int row = source_indexes_.count();
|
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
|
|
source_indexes_ << QPersistentModelIndex(source_index);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-11 18:17:38 +02:00
|
|
|
|
|
|
|
int Queue::PositionOf(const QModelIndex& source_index) const {
|
|
|
|
return mapFromSource(source_index).row();
|
|
|
|
}
|
2010-07-11 18:58:22 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool Queue::is_empty() const { return source_indexes_.isEmpty(); }
|
2010-07-11 18:58:22 +02:00
|
|
|
|
|
|
|
void Queue::Clear() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (source_indexes_.isEmpty()) return;
|
2010-07-11 18:58:22 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
beginRemoveRows(QModelIndex(), 0, source_indexes_.count() - 1);
|
2010-07-11 18:58:22 +02:00
|
|
|
source_indexes_.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2010-07-11 19:39:11 +02:00
|
|
|
|
|
|
|
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;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (int row : proxy_rows) {
|
2014-02-07 16:34:20 +01:00
|
|
|
moved_items << source_indexes_.takeAt(row - offset);
|
|
|
|
if (pos != -1 && pos >= row) pos--;
|
2010-07-11 19:39:11 +02:00
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the items back in
|
|
|
|
const int start = pos == -1 ? source_indexes_.count() : pos;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = start; i < start + moved_items.count(); ++i) {
|
2010-07-11 19:39:11 +02:00
|
|
|
source_indexes_.insert(i, moved_items[i - start]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update persistent indexes
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& pidx : persistentIndexList()) {
|
2010-07-11 19:39:11 +02:00
|
|
|
const int dest_offset = proxy_rows.indexOf(pidx.row());
|
|
|
|
if (dest_offset != -1) {
|
|
|
|
// This index was moved
|
2014-02-07 16:34:20 +01:00
|
|
|
changePersistentIndex(
|
|
|
|
pidx, index(start + dest_offset, pidx.column(), QModelIndex()));
|
2010-07-11 19:39:11 +02:00
|
|
|
} else {
|
|
|
|
int d = 0;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (int row : proxy_rows) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (pidx.row() > row) d--;
|
2010-07-11 19:39:11 +02:00
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
if (pidx.row() + d >= start) d += proxy_rows.count();
|
2010-07-11 19:39:11 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
changePersistentIndex(
|
|
|
|
pidx, index(pidx.row() + d, pidx.column(), QModelIndex()));
|
2010-07-11 19:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layoutChanged();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Queue::MoveUp(int row) { Move(QList<int>() << row, row - 1); }
|
2010-07-11 19:39:11 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Queue::MoveDown(int row) { Move(QList<int>() << row, row + 2); }
|
2010-07-11 20:04:35 +02:00
|
|
|
|
|
|
|
QStringList Queue::mimeTypes() const {
|
2010-07-11 20:11:47 +02:00
|
|
|
return QStringList() << kRowsMimetype << Playlist::kRowsMimetype;
|
2010-07-11 20:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::DropActions Queue::supportedDropActions() const {
|
|
|
|
return Qt::MoveAction | Qt::CopyAction | Qt::LinkAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* Queue::mimeData(const QModelIndexList& indexes) const {
|
|
|
|
QMimeData* data = new QMimeData;
|
|
|
|
|
|
|
|
QList<int> rows;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& index : indexes) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (index.column() != 0) continue;
|
2010-07-11 20:04:35 +02:00
|
|
|
|
|
|
|
rows << index.row();
|
|
|
|
}
|
|
|
|
|
|
|
|
QBuffer buf;
|
|
|
|
buf.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&buf);
|
|
|
|
stream << rows;
|
|
|
|
buf.close();
|
|
|
|
|
|
|
|
data->setData(kRowsMimetype, buf.data());
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool Queue::dropMimeData(const QMimeData* data, Qt::DropAction action, int row,
|
|
|
|
int, const QModelIndex&) {
|
|
|
|
if (action == Qt::IgnoreAction) return false;
|
2010-07-11 20:04:35 +02:00
|
|
|
|
|
|
|
if (data->hasFormat(kRowsMimetype)) {
|
|
|
|
// Dragged from the queue
|
|
|
|
|
|
|
|
QList<int> proxy_rows;
|
|
|
|
QDataStream stream(data->data(kRowsMimetype));
|
|
|
|
stream >> proxy_rows;
|
2014-02-07 16:34:20 +01:00
|
|
|
qStableSort(proxy_rows); // Make sure we take them in order
|
2010-07-11 20:04:35 +02:00
|
|
|
|
|
|
|
Move(proxy_rows, row);
|
2010-07-11 20:11:47 +02:00
|
|
|
} else if (data->hasFormat(Playlist::kRowsMimetype)) {
|
|
|
|
// Dragged from the playlist
|
|
|
|
|
2014-02-06 16:49:49 +01:00
|
|
|
Playlist* playlist = nullptr;
|
2010-07-11 20:11:47 +02:00
|
|
|
QList<int> source_rows;
|
|
|
|
QDataStream stream(data->data(Playlist::kRowsMimetype));
|
2011-05-29 14:55:18 +02:00
|
|
|
stream.readRawData(reinterpret_cast<char*>(&playlist), sizeof(playlist));
|
2010-07-11 20:11:47 +02:00
|
|
|
stream >> source_rows;
|
|
|
|
|
|
|
|
QModelIndexList source_indexes;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (int source_row : source_rows) {
|
2010-07-11 20:11:47 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-11 23:27:36 +02:00
|
|
|
if (!source_indexes.isEmpty()) {
|
|
|
|
const int insert_point = row == -1 ? source_indexes_.count() : row;
|
2014-02-07 16:34:20 +01:00
|
|
|
beginInsertRows(QModelIndex(), insert_point,
|
|
|
|
insert_point + source_indexes.count() - 1);
|
|
|
|
for (int i = 0; i < source_indexes.count(); ++i) {
|
2010-07-11 23:27:36 +02:00
|
|
|
source_indexes_.insert(insert_point + i, source_indexes[i]);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
2010-07-11 20:11:47 +02:00
|
|
|
}
|
2010-07-11 20:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Qt::ItemFlags Queue::flags(const QModelIndex& index) const {
|
2010-07-11 20:04:35 +02:00
|
|
|
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
|
|
|
|
if (index.isValid())
|
|
|
|
flags |= Qt::ItemIsDragEnabled;
|
|
|
|
else
|
|
|
|
flags |= Qt::ItemIsDropEnabled;
|
|
|
|
|
|
|
|
return flags;
|
2010-07-11 21:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Queue::PeekNext() const {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (source_indexes_.isEmpty()) return -1;
|
2010-07-11 21:02:31 +02:00
|
|
|
return source_indexes_.first().row();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Queue::TakeNext() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (source_indexes_.isEmpty()) return -1;
|
2010-07-11 21:02:31 +02:00
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), 0, 0);
|
|
|
|
int ret = source_indexes_.takeFirst().row();
|
|
|
|
endRemoveRows();
|
2010-07-11 20:04:35 +02:00
|
|
|
|
2010-07-11 21:02:31 +02:00
|
|
|
return ret;
|
2010-07-11 20:04:35 +02:00
|
|
|
}
|
2010-07-11 23:27:36 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QVariant Queue::headerData(int section, Qt::Orientation orientation,
|
|
|
|
int role) const {
|
2010-07-11 23:27:36 +02:00
|
|
|
return QVariant();
|
|
|
|
}
|
2011-03-30 20:41:34 +02:00
|
|
|
|
|
|
|
void Queue::Remove(QList<int>& proxy_rows) {
|
2014-02-07 16:34:20 +01:00
|
|
|
// order the rows
|
2011-03-30 20:41:34 +02:00
|
|
|
qStableSort(proxy_rows);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// reflects immediately changes in the playlist
|
2011-03-30 20:41:34 +02:00
|
|
|
layoutAboutToBeChanged();
|
|
|
|
|
|
|
|
int removed_rows = 0;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (int row : proxy_rows) {
|
2014-02-07 16:34:20 +01:00
|
|
|
// after the first row, the row number needs to be updated
|
|
|
|
const int real_row = row - removed_rows;
|
2011-03-30 20:41:34 +02:00
|
|
|
beginRemoveRows(QModelIndex(), real_row, real_row);
|
|
|
|
source_indexes_.removeAt(real_row);
|
|
|
|
endRemoveRows();
|
|
|
|
removed_rows++;
|
|
|
|
}
|
|
|
|
|
|
|
|
layoutChanged();
|
|
|
|
}
|