#include "folderlistwidget.h" #include #include #include #include #include #include #include #include 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;icount();i++) { if(this->item(i)->text()==name) return true; } return false; } bool FolderListWidget::isDraging(const QString &name) { for(int i=0;ilastDragItems.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 roleDataMap; stream >> row >> col >> roleDataMap; if(this->contains(roleDataMap[0].toString())) containsFile = true; } } if(!containsFile) { QListWidget::dragEnterEvent(event); } else { this->lastDragItems.clear(); QList selItems = this->selectedItems(); for(int i=0;itext()); } } } 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 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); }