bool MusicItemModel
::dropMimeData(const QMimeData *data, Qt
::DropAction action,
int row,
int column,
const QModelIndex &parent
) {
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("text/uri-list"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1) {
beginRow = row;
} else if (parent.isValid()) {
beginRow = parent.row();
} else {
}
QList<QUrl> urls = data->urls();
bool internalMove = false;
if (data->text().startsWith("Row:")) {
rowsToMove
= data
->text
().
split(",",
QString::SkipEmptyParts);
internalMove = true;
}
QList<MusicItem> itemsInserted;
int insertionRow = beginRow;
for (int i = 0; i < urls.count(); i++) {
if (internalMove) {
QString rowEntry
= rowsToMove.
at(i
);
int rowToMove = rowEntry.remove("Row:").toInt();
MusicItem item = itemAt(rowToMove);
itemsInserted << item;
QList<QStandardItem *> rowItems = getRowData(item);
insertRow(insertionRow, rowItems);
insertionRow = insertionRow + 1;
} else {
QString url
= urls.
at(i
).
toString();
if (QFile::exists(url
)) { MusicItem item = Convenience::itemFromUrl(url);
itemsInserted << item;
QList<QStandardItem *> rowItems = getRowData(item);
insertRow(insertionRow, rowItems);
insertionRow = insertionRow + 1;
}
}
}
insertionRow = beginRow;
foreach (MusicItem item, itemsInserted) {
int i = rowFromItemUrl(item.Url);
m_musiclist.insert(insertionRow, item);
m_urllist.insert(insertionRow, item.Url);
if (i != -1){
m_musiclist.removeAt(i);
m_urllist.removeAt(i);
}
insertionRow = insertionRow + 1;
}
return true;
}
Qt
::ItemFlags MusicItemModel
::flags(const QModelIndex &index
) const{
Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (index.isValid()) {
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
} else {
return Qt::ItemIsDropEnabled | defaultFlags;
}
}
QMimeData* MusicItemModel
::mimeData(const QModelIndexList
&indexes
) const {
QList<QUrl> urls;
if (index.isValid() && index.column() != 1) {
QUrl url
= QUrl(data
(index, MusicItem
::UrlRole).
toString());
urls << url;
indexList
+= QString("Row:%1,").
arg(index.
row());
}
}
mimeData->setUrls(urls);
mimeData->setText(indexList);
return mimeData;
}
{
types << "text/uri-list" << "text/plain";
return types;
}
Qt::DropActions MusicItemModel::supportedDropActions() const
{
return Qt::MoveAction;
}
bool MusicItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("text/uri-list"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1) {
beginRow = row;
} else if (parent.isValid()) {
beginRow = parent.row();
} else {
beginRow = rowCount(QModelIndex());
}
QList<QUrl> urls = data->urls();
bool internalMove = false;
QStringList rowsToMove;
if (data->text().startsWith("Row:")) {
rowsToMove = data->text().split(",", QString::SkipEmptyParts);
internalMove = true;
}
QList<MusicItem> itemsInserted;
int insertionRow = beginRow;
for (int i = 0; i < urls.count(); i++) {
if (internalMove) {
QString rowEntry = rowsToMove.at(i);
int rowToMove = rowEntry.remove("Row:").toInt();
MusicItem item = itemAt(rowToMove);
itemsInserted << item;
QList<QStandardItem *> rowItems = getRowData(item);
insertRow(insertionRow, rowItems);
insertionRow = insertionRow + 1;
} else {
QString url = urls.at(i).toString();
if (QFile::exists(url)) {
MusicItem item = Convenience::itemFromUrl(url);
itemsInserted << item;
QList<QStandardItem *> rowItems = getRowData(item);
insertRow(insertionRow, rowItems);
insertionRow = insertionRow + 1;
}
}
}
insertionRow = beginRow;
foreach (MusicItem item, itemsInserted) {
int i = rowFromItemUrl(item.Url);
m_musiclist.insert(insertionRow, item);
m_urllist.insert(insertionRow, item.Url);
if (i != -1){
m_musiclist.removeAt(i);
m_urllist.removeAt(i);
}
insertionRow = insertionRow + 1;
}
return true;
}
Qt::ItemFlags MusicItemModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (index.isValid()) {
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
} else {
return Qt::ItemIsDropEnabled | defaultFlags;
}
}
QMimeData* MusicItemModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QList<QUrl> urls;
QString indexList;
foreach (QModelIndex index, indexes) {
if (index.isValid() && index.column() != 1) {
QUrl url = QUrl(data(index, MusicItem::UrlRole).toString());
urls << url;
indexList += QString("Row:%1,").arg(index.row());
}
}
mimeData->setUrls(urls);
mimeData->setText(indexList);
return mimeData;
}
QStringList MusicItemModel::mimeTypes() const
{
QStringList types;
types << "text/uri-list" << "text/plain";
return types;
}
Qt::DropActions MusicItemModel::supportedDropActions() const
{
return Qt::MoveAction;
}
To copy to clipboard, switch view to plain text mode
Bookmarks