Support internal drag and drop in the queue
This commit is contained in:
parent
38ea733e18
commit
89fffcc37f
@ -16,6 +16,12 @@
|
|||||||
|
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
|
#include <QBuffer>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QtDebug>
|
||||||
|
|
||||||
|
const char* Queue::kRowsMimetype = "application/x-clementine-queue-rows";
|
||||||
|
|
||||||
Queue::Queue(QObject* parent)
|
Queue::Queue(QObject* parent)
|
||||||
: QAbstractProxyModel(parent)
|
: QAbstractProxyModel(parent)
|
||||||
{
|
{
|
||||||
@ -34,6 +40,9 @@ QModelIndex Queue::mapFromSource(const QModelIndex& source_index) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex Queue::mapToSource(const QModelIndex& proxy_index) const {
|
QModelIndex Queue::mapToSource(const QModelIndex& proxy_index) const {
|
||||||
|
if (!proxy_index.isValid())
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
return source_indexes_[proxy_index.row()];
|
return source_indexes_[proxy_index.row()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,3 +217,63 @@ void Queue::MoveUp(int row) {
|
|||||||
void Queue::MoveDown(int row) {
|
void Queue::MoveDown(int row) {
|
||||||
Move(QList<int>() << row, row + 2);
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -33,6 +33,8 @@ public:
|
|||||||
ColumnCount
|
ColumnCount
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const char* kRowsMimetype;
|
||||||
|
|
||||||
// Query the queue
|
// Query the queue
|
||||||
bool is_empty() const;
|
bool is_empty() const;
|
||||||
int PositionOf(const QModelIndex& source_index) const;
|
int PositionOf(const QModelIndex& source_index) const;
|
||||||
@ -56,6 +58,11 @@ public:
|
|||||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
QVariant data(const QModelIndex& proxy_index, int role) 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:
|
private slots:
|
||||||
void SourceDataChanged(const QModelIndex& top_left, const QModelIndex& bottom_right);
|
void SourceDataChanged(const QModelIndex& top_left, const QModelIndex& bottom_right);
|
||||||
|
@ -22,6 +22,18 @@
|
|||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QListView" name="list">
|
<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">
|
<property name="alternatingRowColors">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user