From 7b9b23b1430c81cc9157336fa7fdfa65b340f387 Mon Sep 17 00:00:00 2001 From: frostasm Date: Tue, 5 Dec 2017 09:44:01 +0200 Subject: [PATCH] Implement drag&drop support in main form to open database files --- src/gui/MainWindow.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/gui/MainWindow.h | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index bc7b3a89..2715a3df 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -114,6 +114,8 @@ MainWindow::MainWindow() { m_ui->setupUi(this); + setAcceptDrops(true); + m_ui->toolBar->setContextMenuPolicy(Qt::PreventContextMenu); // Setup the search widget in the toolbar @@ -997,3 +999,39 @@ void MainWindow::handleScreenLock() lockDatabasesAfterInactivity(); } } + +QStringList MainWindow::kdbxFilesFromUrls(const QList& urls) +{ + QStringList kdbxFiles; + for (const QUrl& url: urls) { + const QFileInfo fInfo(url.toLocalFile()); + const bool isKdbxFile = fInfo.isFile() && fInfo.suffix().toLower() == "kdbx"; + if (isKdbxFile) { + kdbxFiles.append(fInfo.absoluteFilePath()); + } + } + + return kdbxFiles; +} + +void MainWindow::dragEnterEvent(QDragEnterEvent* event) +{ + const QMimeData* mimeData = event->mimeData(); + if (mimeData->hasUrls()) { + const QStringList kdbxFiles = kdbxFilesFromUrls(mimeData->urls()); + if (!kdbxFiles.isEmpty()) { + event->acceptProposedAction(); + } + } +} + +void MainWindow::dropEvent(QDropEvent* event) +{ + const QMimeData* mimeData = event->mimeData(); + if (mimeData->hasUrls()) { + const QStringList kdbxFiles = kdbxFilesFromUrls(mimeData->urls()); + for(const QString &kdbxFile: kdbxFiles) { + openDatabase(kdbxFile); + } + } +} diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index ade339c5..bb18e170 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -107,6 +107,10 @@ private: void updateTrayIcon(); bool isTrayIconEnabled() const; + static QStringList kdbxFilesFromUrls(const QList& urls); + void dragEnterEvent(QDragEnterEvent* event) override; + void dropEvent(QDropEvent* event) override; + const QScopedPointer m_ui; SignalMultiplexer m_actionMultiplexer; QAction* m_clearHistoryAction;