]> Creatis software - clitk.git/commitdiff
Open files by dropping them inside the window
authorMartino Pilia <martino.pilia@gmail.com>
Tue, 11 Sep 2018 18:47:09 +0000 (20:47 +0200)
committerMartino Pilia <martino.pilia@gmail.com>
Tue, 11 Sep 2018 18:47:09 +0000 (20:47 +0200)
Set the window to allow drag events entering it, and add an handler to
open files dropped within the window.

vv/vvMainWindow.cxx
vv/vvMainWindow.h

index 4e3ba2131de86c02ba02eccfa6a7e5cb307f2bd8..153b461380e4566fb22418e90bd388141d84e887 100644 (file)
@@ -18,6 +18,7 @@ It is distributed under dual licence
 
 #include <algorithm>
 #include <QMessageBox>
+#include <QMimeData>
 #include <QInputDialog>
 #include <QTimer>
 #include "QTreePushButton.h"
@@ -132,6 +133,8 @@ vvMainWindow::vvMainWindow():vvMainWindowBase()
 { 
   setupUi(this); // this sets up the GUI
 
+  setAcceptDrops(true); // enable to drop into the window
+
   setDicomClient();
 
   //Qt::WindowFlags flags = windowFlags();
@@ -816,6 +819,25 @@ void vvMainWindow::OpenRecentImage()
   LoadImages(images, vvImageReader::IMAGE);
 }
 //------------------------------------------------------------------------------
+void vvMainWindow::dragEnterEvent(QDragEnterEvent *event)
+{
+  if (event->mimeData()->hasUrls()) {
+    event->acceptProposedAction();
+  }
+}
+//------------------------------------------------------------------------------
+void vvMainWindow::dropEvent(QDropEvent *event)
+{
+  const QMimeData * mimeData = event->mimeData();
+  if (!mimeData->hasUrls())
+    return;
+  std::vector<std::string> images;
+  for (auto const & url : mimeData->urls()) {
+    images.push_back(url.toLocalFile().toStdString());
+  }
+  LoadImages(images, vvImageReader::IMAGE);
+}
+//------------------------------------------------------------------------------
 
 
 //------------------------------------------------------------------------------
index 8908b24004ca7fca8bae8c6f0e3d76fa5f9968a1..2d6a21dde238ef34ed446114ed907ad503cfc429 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <iostream>
 #include <vector>
+#include <QDropEvent>
 #include "ui_vvMainWindow.h"
 #include "vvMainWindowBase.h"
 #include "vvToolManager.h"
@@ -192,6 +193,10 @@ protected:
   void createRecentlyOpenedFilesMenu();
   void updateRecentlyOpenedFilesMenu(const std::list<std::string> &files);
   
+  // Drag and drop event handlers
+  virtual void dragEnterEvent(QDragEnterEvent *event);
+  virtual void dropEvent(QDropEvent *event);
+
 private:
 
   //variables