From 8d9b9cb49368cc1c1de5d3967aad8f290ad4e18c Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 7 Nov 2020 12:03:43 +0100 Subject: [PATCH] Drop file opens it, command line arguments --- PdfForQtViewer/main.cpp | 6 ++++ PdfForQtViewer/pdfviewermainwindow.cpp | 40 ++++++++++++++++++++++++++ PdfForQtViewer/pdfviewermainwindow.h | 9 +++++- README.txt | 4 +-- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/PdfForQtViewer/main.cpp b/PdfForQtViewer/main.cpp index a41c59a..b0c1cb9 100644 --- a/PdfForQtViewer/main.cpp +++ b/PdfForQtViewer/main.cpp @@ -43,5 +43,11 @@ int main(int argc, char *argv[]) pdfviewer::PDFViewerMainWindow mainWindow; mainWindow.show(); + QStringList arguments = application.arguments(); + if (arguments.size() > 1) + { + mainWindow.openDocument(arguments[1]); + } + return application.exec(); } diff --git a/PdfForQtViewer/pdfviewermainwindow.cpp b/PdfForQtViewer/pdfviewermainwindow.cpp index a80ebd6..e7928d6 100644 --- a/PdfForQtViewer/pdfviewermainwindow.cpp +++ b/PdfForQtViewer/pdfviewermainwindow.cpp @@ -102,6 +102,8 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) : { ui->setupUi(this); + setAcceptDrops(true); + // Initialize toolbar icon size QSize iconSize = pdf::PDFWidgetUtils::scaleDPI(this, QSize(24, 24)); ui->mainToolBar->setIconSize(iconSize); @@ -1503,4 +1505,42 @@ void PDFViewerMainWindow::saveDocument(const QString& fileName) } } + +void PDFViewerMainWindow::dragEnterEvent(QDragEnterEvent* event) +{ + if (event->mimeData()->hasUrls()) + { + event->setDropAction(Qt::LinkAction); + event->accept(); + } +} + +void PDFViewerMainWindow::dragMoveEvent(QDragMoveEvent* event) +{ + if (event->mimeData()->hasUrls()) + { + event->setDropAction(Qt::LinkAction); + event->accept(); + } +} + +void PDFViewerMainWindow::dragLeaveEvent(QDragLeaveEvent* event) +{ + +} + +void PDFViewerMainWindow::dropEvent(QDropEvent* event) +{ + if (event->mimeData()->hasUrls()) + { + QList urls = event->mimeData()->urls(); + if (urls.size() == 1) + { + openDocument(urls.front().toLocalFile()); + event->acceptProposedAction(); + } + } +} + + } // namespace pdfviewer diff --git a/PdfForQtViewer/pdfviewermainwindow.h b/PdfForQtViewer/pdfviewermainwindow.h index 6f1930f..5390c7a 100644 --- a/PdfForQtViewer/pdfviewermainwindow.h +++ b/PdfForQtViewer/pdfviewermainwindow.h @@ -74,9 +74,17 @@ public: virtual void closeEvent(QCloseEvent* event) override; virtual void showEvent(QShowEvent* event) override; + void openDocument(const QString& fileName); + signals: void queryPasswordRequest(QString* password, bool* ok); +protected: + virtual void dragEnterEvent(QDragEnterEvent* event) override; + virtual void dragMoveEvent(QDragMoveEvent* event) override; + virtual void dragLeaveEvent(QDragLeaveEvent* event) override; + virtual void dropEvent(QDropEvent* event) override; + private slots: void onQueryPasswordRequest(QString* password, bool* ok); @@ -137,7 +145,6 @@ private: void onViewerSettingsChanged(); void onRenderingOptionTriggered(bool checked); - void openDocument(const QString& fileName); void setDocument(pdf::PDFModifiedDocument document); void closeDocument(); void saveDocument(const QString& fileName); diff --git a/README.txt b/README.txt index 9d32406..880de0a 100644 --- a/README.txt +++ b/README.txt @@ -1,7 +1,7 @@ ========================================================================== PDFforQt library / PDF viewer - (c) Jakub Melka 2018-2019 + (c) Jakub Melka 2018-2020 ========================================================================== This software is consisting of PDF rendering library, and viewer, @@ -69,4 +69,4 @@ to compile the project. 7. DISCLAIMER -I wrote this project in my free time. I hope you will find it useful! \ No newline at end of file +I wrote this project in my free time. I hope you will find it useful!