From: hama Date: Tue, 3 Nov 2015 23:50:41 +0000 (+0100) Subject: drag-drop basics X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=d0fabad7990b9231d535e9504c17d57b58e06111;p=reqt drag-drop basics --- diff --git a/guiwidget/ReFileTable.cpp b/guiwidget/ReFileTable.cpp index c1fe2e1..2fcd940 100644 --- a/guiwidget/ReFileTable.cpp +++ b/guiwidget/ReFileTable.cpp @@ -49,6 +49,10 @@ ReFileTable::ReFileTable(QWidget *parent) : tableWidget->setColumnWidth(TYPE, 60); tableWidget->setColumnWidth(SIZE, 125); tableWidget->setColumnWidth(MODIFIED, 175); + tableWidget->setDragDropMode(QAbstractItemView::DragDrop); + tableWidget->setDragEnabled(true); + tableWidget->setDragDropOverwriteMode(true); + tableWidget->setAcceptDrops(true); connect(pushButtonUp, SIGNAL(clicked()), SLOT(pushButtonUpClicked())); connect(pushButtonRoot, SIGNAL(clicked()), SLOT(pushButtonRootClicked())); connect(tableWidget, SIGNAL(cellDoubleClicked(int, int)), this, @@ -106,6 +110,53 @@ QString ReFileTable::cellAsText(int row, int col){ return rc; } +/** + * Processes the dragging operation of the selected files in the table widget. + */ +void ReFileTable::fileDragging(){ + QDrag *drag = new QDrag(this); + QMimeData *mimeData = new QMimeData; + QList < QUrl > urls; + QList < QTableWidgetSelectionRange > ranges = + tableWidget->selectedRanges(); + QList ::iterator it; + int files = 0; + int dirs = 0; + bool isDir = false; + for (it = ranges.begin(); it != ranges.end(); ++it){ + for (int row = (*it).topRow(); row <= (*it).bottomRow(); row++){ + isDir = cellAsText(row, SIZE).isEmpty(); + QUrl url(buildAbsPath(row, true, true)); + urls.append(url); + if (isDir) + dirs++; + else + files++; + } + } + if (urls.size() > 0){ + mimeData->setUrls(urls); + drag->setMimeData(mimeData); + QPixmap image(200, 30); + QPainter painter(&image); + QString msg; + if (urls.size() == 1) + msg = tr("copy ") + ReFileUtils::nodeOf(urls.at(0).toString()); + else if (files > 0 && dirs > 0) + msg = tr("copy %1 file(s) and %2 dir(s)").arg(files).arg(dirs); + else if (files > 0) + msg = tr("copy %1 file(s)").arg(files); + else + msg = tr("copy %1 dirs(s)").arg(dirs); + + painter.fillRect(image.rect(), Qt::white); + painter.drawText(10, 20, msg); + drag->setPixmap(image); + + Qt::DropAction dropAction = drag->exec(Qt::CopyAction); + } +} + /** * Copies the selected files into the clipboard. * @@ -143,6 +194,25 @@ void ReFileTable::copyToClipboard(int currentRow, const QString& full){ say(LOG_INFO, tr("%1 entry/entries copied to clipboard").arg(urls.length())); } +/** + * Handles the event "drag enter". + * + * @param event the event data + */ +void ReFileTable::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasFormat("text/uri-list")) + event->acceptProposedAction(); +} + +void ReFileTable::dropEvent(QDropEvent *event) +{ + QList urls = event->mimeData()->urls(); + if (! urls.isEmpty()){ + QString fileName = urls.first().toLocalFile(); + say(LOG_INFO, "copy " + fileName); + } +} /** * Fills the table with the file data of the filesystem. diff --git a/guiwidget/ReFileTable.hpp b/guiwidget/ReFileTable.hpp index 379b163..b634643 100644 --- a/guiwidget/ReFileTable.hpp +++ b/guiwidget/ReFileTable.hpp @@ -29,6 +29,9 @@ public: void changePatterns(const QString& patterns); bool changeDirectory(QString directory); public: + virtual void dragEnterEvent(QDragEnterEvent* event); + virtual void dropEvent(QDropEvent* event); + virtual void fileDragging(); virtual void keyPressEvent(QKeyEvent* event); public slots: void tableDoubleClicked(int row, int column); diff --git a/guiwidget/reguiwidget.hpp b/guiwidget/reguiwidget.hpp index 0325f8f..c43e5fd 100644 --- a/guiwidget/reguiwidget.hpp +++ b/guiwidget/reguiwidget.hpp @@ -30,6 +30,7 @@ #include "QClipboard" #include "QMimeData" #include "QUrl" +#include "QDrag" #include "guiwidget/ReFileTable.hpp" #endif // REGUIWIDGET_HPP