Support internal drag and drop in the queue

This commit is contained in:
David Sansome 2010-07-11 18:04:35 +00:00
parent 38ea733e18
commit 89fffcc37f
3 changed files with 88 additions and 0 deletions

View File

@ -16,6 +16,12 @@
#include "queue.h"
#include <QBuffer>
#include <QMimeData>
#include <QtDebug>
const char* Queue::kRowsMimetype = "application/x-clementine-queue-rows";
Queue::Queue(QObject* parent)
: QAbstractProxyModel(parent)
{
@ -34,6 +40,9 @@ QModelIndex Queue::mapFromSource(const QModelIndex& source_index) const {
}
QModelIndex Queue::mapToSource(const QModelIndex& proxy_index) const {
if (!proxy_index.isValid())
return QModelIndex();
return source_indexes_[proxy_index.row()];
}
@ -208,3 +217,63 @@ void Queue::MoveUp(int row) {
void Queue::MoveDown(int row) {
Move(QList<int>() << row, row + 2);
}
QStringList Queue::mimeTypes() const {
return QStringList() << 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<int> rows;
foreach (const QModelIndex& index, indexes) {
if (index.column() != Column_CombinedArtistTitle)
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<int> proxy_rows;
QDataStream stream(data->data(kRowsMimetype));
stream >> proxy_rows;
qStableSort(proxy_rows); // Make sure we take them in order
Move(proxy_rows, row);
}
return true;
}
Qt::ItemFlags Queue::flags(const QModelIndex &index) const {
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.isValid())
flags |= Qt::ItemIsDragEnabled;
else
flags |= Qt::ItemIsDropEnabled;
return flags;
}

View File

@ -33,6 +33,8 @@ public:
ColumnCount
};
static const char* kRowsMimetype;
// Query the queue
bool is_empty() const;
int PositionOf(const QModelIndex& source_index) const;
@ -56,6 +58,11 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex& proxy_index, int role) const;
QStringList mimeTypes() const;
Qt::DropActions supportedDropActions() const;
QMimeData* mimeData(const QModelIndexList& indexes) const;
bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
Qt::ItemFlags flags(const QModelIndex &index) const;
private slots:
void SourceDataChanged(const QModelIndex& top_left, const QModelIndex& bottom_right);

View File

@ -22,6 +22,18 @@
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QListView" name="list">
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>