]> gitweb.hamatoma.de Git - reqt/commitdiff
drag-drop basics
authorhama <hama@siduction.net>
Tue, 3 Nov 2015 23:50:41 +0000 (00:50 +0100)
committerhama <hama@siduction.net>
Tue, 3 Nov 2015 23:50:41 +0000 (00:50 +0100)
guiwidget/ReFileTable.cpp
guiwidget/ReFileTable.hpp
guiwidget/reguiwidget.hpp

index c1fe2e12a3a77fae6b4b79726af805c8c0afb221..2fcd940f8a46f02580192532f090378f33c17f3c 100644 (file)
@@ -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 <QTableWidgetSelectionRange>::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<QUrl> 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.
index 379b16316da0b0f2b4bb338edc6b4a213b7bdd6e..b634643a2f38806e817b905907bbc0a725fb8bb7 100644 (file)
@@ -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);
index 0325f8f9e3237ae1b1ccec420576b022f84eeab6..c43e5fda6a869cb3e7e0beb4fa1f37c5762f5e03 100644 (file)
@@ -30,6 +30,7 @@
 #include "QClipboard"
 #include "QMimeData"
 #include "QUrl"
+#include "QDrag"
 #include "guiwidget/ReFileTable.hpp"
 #endif // REGUIWIDGET_HPP