From 26be690c70e55a4a8aed7a95334dbd757de28e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Thu, 14 Apr 2022 11:20:16 +0200 Subject: [PATCH] Add drop file support --- imagescrollareagl.cpp | 25 +++++++++++++++++++++++++ imagescrollareagl.h | 4 ++++ mainwindow.cpp | 3 ++- mainwindow.h | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/imagescrollareagl.cpp b/imagescrollareagl.cpp index d76564b..a162efd 100644 --- a/imagescrollareagl.cpp +++ b/imagescrollareagl.cpp @@ -6,6 +6,7 @@ #include #include #include +#include struct RawImageType { @@ -54,6 +55,7 @@ ImageWidget::ImageWidget(QWidget *parent) : QOpenGLWidget(parent) m_range = UINT16_MAX; m_imgWidth = m_imgHeight = -1; m_superpixel = m_invert = false; + setAcceptDrops(true); } ImageWidget::~ImageWidget() @@ -256,6 +258,29 @@ void ImageWidget::initializeGL() m_transferOptions->setAlignment(1); } +void ImageWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if(event->mimeData()->hasUrls() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction)) + event->acceptProposedAction(); +} + +void ImageWidget::dropEvent(QDropEvent *event) +{ + if(event->mimeData()->hasUrls() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction)) + { + for(const QUrl &url : event->mimeData()->urls()) + { + if(url.isLocalFile()) + { + emit fileDropped(url.path()); + event->accept(); + return; + } + } + } + event->ignore(); +} + ImageScrollAreaGL::ImageScrollAreaGL(QWidget *parent) : QWidget(parent) { QGridLayout *layout = new QGridLayout(this); diff --git a/imagescrollareagl.h b/imagescrollareagl.h index 686afca..2eb7363 100644 --- a/imagescrollareagl.h +++ b/imagescrollareagl.h @@ -59,6 +59,10 @@ protected: void paintGL(); void resizeGL(int w, int h); void initializeGL(); + void dragEnterEvent(QDragEnterEvent *event); + void dropEvent(QDropEvent *event); +signals: + void fileDropped(const QString &path); }; class ImageScrollAreaGL : public QWidget diff --git a/mainwindow.cpp b/mainwindow.cpp index 7d9d9b1..c663bd0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -80,6 +80,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) connect(m_ringList, SIGNAL(currentImageChanged(int)), this, SLOT(updateWindowTitle())); connect(m_ringList, SIGNAL(infoLoaded(ImageInfoData)), m_info, SLOT(setInfo(const ImageInfoData&))); connect(m_ringList, SIGNAL(currentImageChanged(int)), m_filesystem, SLOT(selectFile(int))); + connect(m_imageGL->imageWidget(), &ImageWidget::fileDropped, this, static_cast(&MainWindow::loadFile)); QMenu *fileMenu = new QMenu(tr("File"), this); fileMenu->addAction(tr("Open"), this, SLOT(loadFile()), QKeySequence("Ctrl+O")); @@ -256,7 +257,7 @@ void MainWindow::loadFile() loadFile(file); } -void MainWindow::loadFile(const QString path) +void MainWindow::loadFile(const QString &path) { if(!path.isEmpty()) { diff --git a/mainwindow.h b/mainwindow.h index c9a83e6..1c81e3d 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -41,7 +41,7 @@ protected slots: void updateWindowTitle(); void pixmapLoaded(Image *image); void loadFile(); - void loadFile(const QString path); + void loadFile(const QString &path); void loadFile(int row); void indexDir(); void saveAs();