From 101f111209308c34919767743b8d7bc2f09a9a72 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Tue, 31 Dec 2024 10:57:21 +0800 Subject: [PATCH] refactor: move file open event handling to standalone file --- CMakeLists.txt | 2 ++ app/fileopeneventhandler.cpp | 22 ++++++++++++++++++++++ app/fileopeneventhandler.h | 21 +++++++++++++++++++++ app/main.cpp | 14 ++++++++++++++ app/mainwindow.cpp | 18 ------------------ app/mainwindow.h | 3 --- pineapple-pictures.pro | 6 ++++-- 7 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 app/fileopeneventhandler.cpp create mode 100644 app/fileopeneventhandler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b9736e1..9d18d76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +68,7 @@ set (PPIC_CPP_FILES app/exiv2wrapper.cpp app/playlistmanager.cpp app/shortcutedit.cpp + app/fileopeneventhandler.cpp ) set (PPIC_HEADER_FILES @@ -88,6 +89,7 @@ set (PPIC_HEADER_FILES app/exiv2wrapper.h app/playlistmanager.h app/shortcutedit.h + app/fileopeneventhandler.h ) set (PPIC_QRC_FILES diff --git a/app/fileopeneventhandler.cpp b/app/fileopeneventhandler.cpp new file mode 100644 index 0000000..5f5f09c --- /dev/null +++ b/app/fileopeneventhandler.cpp @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2024 Gary Wang +// +// SPDX-License-Identifier: MIT + +#include "fileopeneventhandler.h" + +#include + +FileOpenEventHandler::FileOpenEventHandler(QObject *parent) + : QObject(parent) +{ +} + +bool FileOpenEventHandler::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::FileOpen) { + QFileOpenEvent *fileOpenEvent = static_cast(event); + emit fileOpen(fileOpenEvent->url()); + return true; + } + return QObject::eventFilter(obj, event); +} diff --git a/app/fileopeneventhandler.h b/app/fileopeneventhandler.h new file mode 100644 index 0000000..2016f04 --- /dev/null +++ b/app/fileopeneventhandler.h @@ -0,0 +1,21 @@ +// SPDX-FileCopyrightText: 2024 Gary Wang +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include + +class FileOpenEventHandler : public QObject +{ + Q_OBJECT + +public: + explicit FileOpenEventHandler(QObject *parent = nullptr); + +protected: + bool eventFilter(QObject *obj, QEvent *event) override; + +signals: + void fileOpen(const QUrl &url); +}; diff --git a/app/main.cpp b/app/main.cpp index e55b9b5..e940126 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -7,6 +7,10 @@ #include "playlistmanager.h" #include "settings.h" +#ifdef Q_OS_MACOS +#include "fileopeneventhandler.h" +#endif // Q_OS_MACOS + #include #include #include @@ -55,6 +59,16 @@ int main(int argc, char *argv[]) MainWindow w; w.show(); +#ifdef Q_OS_MACOS + FileOpenEventHandler * fileOpenEventHandler = new FileOpenEventHandler(&a); + a.installEventFilter(fileOpenEventHandler); + a.connect(fileOpenEventHandler, &FileOpenEventHandler::fileOpen, [&w](const QUrl & url){ + if (w.isHidden()) w.showNormal(); + w.showUrls({url}); + w.initWindowSize(); + }); +#endif // Q_OS_MACOS + QStringList urlStrList = parser.positionalArguments(); QList && urlList = PlaylistManager::convertToUrlList(urlStrList); diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index f027acd..a836d70 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -160,10 +160,6 @@ MainWindow::MainWindow(QWidget *parent) installResizeCapture(m_graphicsView->viewport()); installResizeCapture(m_gv); installResizeCapture(m_gv->viewport()); - -#ifdef Q_OS_MACOS - qApp->installEventFilter(this); -#endif // Q_OS_MACOS } MainWindow::~MainWindow() @@ -661,20 +657,6 @@ QSize MainWindow::sizeHint() const return QSize(710, 530); } -#ifdef Q_OS_MACOS -bool MainWindow::eventFilter(QObject *obj, QEvent *event) -{ - Q_UNUSED(obj); - if (event->type() == QEvent::FileOpen) { - QFileOpenEvent *fileOpenEvent = static_cast(event); - showUrls({fileOpenEvent->url()}); - initWindowSize(); - return true; - } - return false; -} -#endif // Q_OS_MACOS - void MainWindow::on_actionOpen_triggered() { QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); diff --git a/app/mainwindow.h b/app/mainwindow.h index 7c34645..2850901 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -77,9 +77,6 @@ protected slots: protected: QSize sizeHint() const override; -#ifdef Q_OS_MACOS - bool eventFilter(QObject *obj, QEvent *event) override; -#endif // Q_OS_MACOS private slots: void on_actionOpen_triggered(); diff --git a/pineapple-pictures.pro b/pineapple-pictures.pro index f308ece..384139e 100644 --- a/pineapple-pictures.pro +++ b/pineapple-pictures.pro @@ -40,7 +40,8 @@ SOURCES += \ app/exiv2wrapper.cpp \ app/actionmanager.cpp \ app/playlistmanager.cpp \ - app/shortcutedit.cpp + app/shortcutedit.cpp \ + app/fileopeneventhandler.cpp HEADERS += \ app/aboutdialog.h \ @@ -59,7 +60,8 @@ HEADERS += \ app/exiv2wrapper.h \ app/actionmanager.h \ app/playlistmanager.h \ - app/shortcutedit.h + app/shortcutedit.h \ + app/fileopeneventhandler.h TRANSLATIONS = \ app/translations/PineapplePictures.ts \