From fa4f40a1f8e8a731e557f51266803061dea37a72 Mon Sep 17 00:00:00 2001 From: Martchus Date: Tue, 5 May 2015 23:08:49 +0200 Subject: [PATCH] added Drag & Drop support --- gui/mainwindow.cpp | 38 +++++++++++++++++++++++++++++++++++++- gui/mainwindow.h | 1 + gui/mainwindow.ui | 14 ++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index b654476..013a7d1 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -129,7 +130,7 @@ MainWindow::MainWindow(QWidget *parent) : // other widgets updateUiStatus(); m_ui->abortButton->setVisible(false); - // connect signals and slots + // connect signals and slots, install event filter // menu: application connect(m_ui->actionSettings, &QAction::triggered, this, &MainWindow::showSettingsDlg); connect(m_ui->actionQuit, &QAction::triggered, this, &MainWindow::close); @@ -156,6 +157,9 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_ui->selectNextCommandLinkButton, &QCommandLinkButton::clicked, this, &MainWindow::selectNextFile); connect(m_ui->abortButton, &QPushButton::clicked, [this] {m_abortClicked = true; m_ui->abortButton->setEnabled(false); }); connect(m_ui->tagSelectionComboBox, static_cast(&QComboBox::activated), m_ui->stackedWidget, &QStackedWidget::setCurrentIndex); + // event filter + m_ui->fileNameLabel->installEventFilter(this); + m_ui->rightWidget->installEventFilter(this); // apply settings setCurrentDirectory(Settings::mainWindowCurrentFileBrowserDirectory()); applySettingsFromDialog(); @@ -195,6 +199,38 @@ void MainWindow::closeEvent(QCloseEvent *) Settings::mainWindowCurrentFileBrowserDirectory() = currentDirectory(); } +bool MainWindow::eventFilter(QObject *obj, QEvent *event) +{ + if(obj == m_ui->rightWidget || obj == m_ui->fileNameLabel) { + switch(event->type()) { + case QEvent::DragEnter: + case QEvent::Drop: + if(QDropEvent *dropEvent = static_cast(event)) { + QString data; + const QMimeData *mimeData = dropEvent->mimeData(); + if(mimeData->hasUrls()) { + const QUrl url = mimeData->urls().front(); + if(url.scheme() == QLatin1String("file")) { + data = url.path(); + } + } else if(mimeData->hasText()) { + data = mimeData->text(); + } + if(!data.isEmpty()) { + event->accept(); + if(event->type() == QEvent::Drop) { + showFile(data, true); + } + } + return true; + } + default: + ; + } + } + return QObject::eventFilter(obj, event); +} + /*! * \brief This private slot is called when the entered text of m_ui->pathLineEdit which represents the current directory changes. * If the text is a valid path the current index of the m_ui->filesTreeView is updated to show the path. This invokes the file diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 8ba8a40..48d3b9f 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -54,6 +54,7 @@ public: protected: virtual void closeEvent(QCloseEvent *); + virtual bool eventFilter(QObject *obj, QEvent *event); private slots: // file selection diff --git a/gui/mainwindow.ui b/gui/mainwindow.ui index 79bdc0c..3877afa 100644 --- a/gui/mainwindow.ui +++ b/gui/mainwindow.ui @@ -79,6 +79,9 @@ + + true + 3 @@ -91,6 +94,9 @@ + + true + No file selected @@ -104,7 +110,11 @@ Qt::Vertical - + + + false + + @@ -314,7 +324,7 @@ the file reverting all unsaved changings. 0 0 1225 - 23 + 29