|
|
#include "folderlistwidget.h"
|
|
|
#include <QDragEnterEvent>
|
|
|
#include <QMimeData>
|
|
|
#include <QDebug>
|
|
|
#include <QDrag>
|
|
|
#include <QApplication>
|
|
|
#include <QMap>
|
|
|
#include <QVariant>
|
|
|
#include <QDir>
|
|
|
|
|
|
FolderListWidget::FolderListWidget(QWidget *parent) : QListWidget(parent)
|
|
|
{
|
|
|
this->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
this->setAcceptDrops(true);
|
|
|
this->setDragEnabled(true);
|
|
|
this->setDragDropMode(QAbstractItemView::DragDrop);
|
|
|
this->setDefaultDropAction(Qt::MoveAction);
|
|
|
this->p_mainWin=NULL;
|
|
|
this->p_path="";
|
|
|
}
|
|
|
|
|
|
FolderListWidget::~FolderListWidget()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
bool FolderListWidget::contains(const QString &name)
|
|
|
{
|
|
|
for(int i=0;i<this->count();i++)
|
|
|
{
|
|
|
if(this->item(i)->text()==name)
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool FolderListWidget::isDraging(const QString &name)
|
|
|
{
|
|
|
for(int i=0;i<this->lastDragItems.count();i++)
|
|
|
{
|
|
|
if(lastDragItems.at(i)==name)
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::setMainWindow(MainWindow *mw)
|
|
|
{
|
|
|
this->p_mainWin = mw;
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::setPath(const QString &path)
|
|
|
{
|
|
|
this->p_path = path;
|
|
|
this->lastDragItems.clear();
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::dragEnterEvent(QDragEnterEvent *event)
|
|
|
{
|
|
|
const QMimeData *mimeData = event->mimeData();
|
|
|
QStringList mimeFormats = mimeData->formats();
|
|
|
bool containsFile=false;
|
|
|
if(mimeFormats.count())
|
|
|
{
|
|
|
QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist");
|
|
|
QDataStream stream(&encoded, QIODevice::ReadOnly);
|
|
|
while (!stream.atEnd())
|
|
|
{
|
|
|
int row, col;
|
|
|
QMap<int, QVariant> roleDataMap;
|
|
|
stream >> row >> col >> roleDataMap;
|
|
|
if(this->contains(roleDataMap[0].toString()))
|
|
|
containsFile = true;
|
|
|
}
|
|
|
}
|
|
|
if(!containsFile)
|
|
|
{
|
|
|
QListWidget::dragEnterEvent(event);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this->lastDragItems.clear();
|
|
|
QList<QListWidgetItem*> selItems = this->selectedItems();
|
|
|
for(int i=0;i<selItems.count();i++)
|
|
|
{
|
|
|
lastDragItems.append(selItems.at(i)->text());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::dragMoveEvent(QDragMoveEvent *event)
|
|
|
{
|
|
|
QListWidget::dragMoveEvent(event);
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::dragLeaveEvent(QDragLeaveEvent *event)
|
|
|
{
|
|
|
QListWidget::dragLeaveEvent(event);
|
|
|
this->lastDragItems.clear();
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::dropEvent(QDropEvent *event)
|
|
|
{
|
|
|
QByteArray encoded = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
|
|
|
QDataStream stream(&encoded, QIODevice::ReadOnly);
|
|
|
while (!stream.atEnd())
|
|
|
{
|
|
|
int row, col;
|
|
|
QMap<int, QVariant> roleDataMap;
|
|
|
stream >> row >> col >> roleDataMap;
|
|
|
QString name=roleDataMap[0].toString();
|
|
|
QString oldPath = p_mainWin->getFilePath(name);
|
|
|
if((oldPath!="") && (p_path!=""))
|
|
|
{
|
|
|
if(QFile::rename(oldPath+"/"+name,this->p_path+"/"+name))
|
|
|
QListWidget::dropEvent(event);
|
|
|
emit askGlobalRescan();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::mousePressEvent(QMouseEvent *event)
|
|
|
{
|
|
|
QListWidget::mousePressEvent(event);
|
|
|
}
|
|
|
|
|
|
void FolderListWidget::mouseMoveEvent(QMouseEvent *event)
|
|
|
{
|
|
|
QListWidget::mouseMoveEvent(event);
|
|
|
}
|
|
|
|