diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index fddf6ac..7919e40 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -89,6 +89,9 @@ MainWindow::MainWindow() setWindowIcon(QIcon(":/mapmap-logo")); setCurrentFile(""); + // Allow drag n drop + setAcceptDrops(true); + // Create and start timer. videoTimer = new QTimer(this); connect(videoTimer, SIGNAL(timeout()), this, SLOT(processFrame())); @@ -396,6 +399,70 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event) return QMainWindow::eventFilter(object, event); } +void MainWindow::dragEnterEvent(QDragEnterEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); + bool allowDrag = true; + + if (mimeData->hasUrls()) { + foreach (QUrl url, mimeData->urls()) { + QString fileName = url.toLocalFile(); + // Don't allow drag if file is not supported + if (!fileSupported(fileName, MM::FILE_EXTENSION) && + !fileSupported(fileName, MM::IMAGE_FILES_FILTER) && + !fileSupported(fileName, MM::VIDEO_FILES_FILTER)) { + allowDrag = false; + } + } + } + + if (allowDrag) + event->acceptProposedAction(); +} + +void MainWindow::dragMoveEvent(QDragMoveEvent *event) +{ + event->acceptProposedAction(); +} + +void MainWindow::dragLeaveEvent(QDragLeaveEvent *event) +{ + event->accept(); +} + +void MainWindow::dropEvent(QDropEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); + + if (mimeData->hasUrls()) { + // In case that dragged many files + foreach (QUrl url, mimeData->urls()) { + QString fileName = url.toLocalFile(); + + if (!fileName.isEmpty()) { + // Test if is mmp file and exit loop + if (fileSupported(fileName, MM::FILE_EXTENSION)) { + if (okToContinue()) { + loadFile(fileName); + } + // Exit for prevent drag to many project files + break; + } + // Allow to drag too many videos or images + else { + // Check if file is image or not + // according to file extension + if (fileSupported(fileName, MM::IMAGE_FILES_FILTER)) + importMediaFile(fileName, true); + else + importMediaFile(fileName, false); + } + } + } + } + event->acceptProposedAction(); +} + void MainWindow::setOutputWindowFullScreen(bool enable) { outputWindow->setFullScreen(enable); @@ -2812,6 +2879,16 @@ bool MainWindow::fileSupported(const QString &file, bool isImage) return false; } +bool MainWindow::fileSupported(const QString &file, const QString &extension) +{ + if (!QFileInfo(file).suffix().isEmpty() && + extension.contains(QFileInfo(file).suffix(), Qt::CaseInsensitive)) { + return true; + } else { + return false; + } +} + QString MainWindow::locateMediaFile(const QString &uri, bool isImage) { // Get more info about url diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index b5fa1c6..46a8b89 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -83,6 +83,11 @@ protected: void keyPressEvent(QKeyEvent *event); bool eventFilter(QObject *object, QEvent *event); + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dropEvent(QDropEvent *event); + // Slots //////////////////////////////////////////////////////////////////////////////////////////////////// private slots: @@ -301,6 +306,7 @@ public: bool fileExists(const QString& file); // Check if the file is supported bool fileSupported(const QString& file, bool isImage); + bool fileSupported(const QString &file, const QString &extension); // Locate the file not found QString locateMediaFile(const QString& uri, bool isImage); diff --git a/src/gui/MapperGLCanvas.cpp b/src/gui/MapperGLCanvas.cpp index a361574..c43cabd 100644 --- a/src/gui/MapperGLCanvas.cpp +++ b/src/gui/MapperGLCanvas.cpp @@ -73,6 +73,9 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, // Black background. this->scene()->setBackgroundBrush(Qt::black); + + // Enable dragndrop + setAcceptDrops(true); } MShape::ptr MapperGLCanvas::getShapeFromMapping(Mapping::ptr mapping) @@ -173,6 +176,68 @@ void MapperGLCanvas::applyZoomToView() emit zoomFactorChanged(zoomFactor); } +void MapperGLCanvas::dragEnterEvent(QDragEnterEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); + bool allowDrag = true; + + if (mimeData->hasUrls()) { + foreach (QUrl url, mimeData->urls()) { + QString fileName = url.toLocalFile(); + // Don't allow drag if file is not supported + if (!MainWindow::window()->fileSupported(fileName, MM::FILE_EXTENSION) && + !MainWindow::window()->fileSupported(fileName, MM::IMAGE_FILES_FILTER) && + !MainWindow::window()->fileSupported(fileName, MM::VIDEO_FILES_FILTER)) { + allowDrag = false; + } + } + } + + if (allowDrag) + event->acceptProposedAction(); +} + +void MapperGLCanvas::dragMoveEvent(QDragMoveEvent *event) +{ + event->acceptProposedAction(); +} + +void MapperGLCanvas::dragLeaveEvent(QDragLeaveEvent *event) +{ + event->accept(); +} + +void MapperGLCanvas::dropEvent(QDropEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); + + if (mimeData->hasUrls()) { + // In case that dragged many files + foreach (QUrl url, mimeData->urls()) { + QString fileName = url.toLocalFile(); + + if (!fileName.isEmpty()) { + // Test if is mmp file and exit loop + if (MainWindow::window()->fileSupported(fileName, MM::FILE_EXTENSION)) { + MainWindow::window()->loadFile(fileName); + // Exit for prevent drag to many project files + break; + } + // Allow to drag too many videos or images + else { + // Check if file is image or not + // according to file extension + if (MainWindow::window()->fileSupported(fileName, MM::IMAGE_FILES_FILTER)) + MainWindow::window()->importMediaFile(fileName, true); + else + MainWindow::window()->importMediaFile(fileName, false); + } + } + } + } + event->acceptProposedAction(); +} + // void MapperGLCanvas::mousePressEvent(QMouseEvent* event) diff --git a/src/gui/MapperGLCanvas.h b/src/gui/MapperGLCanvas.h index dd8b1cf..b81a136 100644 --- a/src/gui/MapperGLCanvas.h +++ b/src/gui/MapperGLCanvas.h @@ -110,6 +110,10 @@ protected: // void mouseMoveEvent(QMouseEvent* event); // void mouseReleaseEvent(QMouseEvent* event); // void paintEvent(QPaintEvent* event); + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dropEvent(QDropEvent *event); protected: // /**