Skip to content

Commit

Permalink
refactor: move file open event handling to standalone file
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Dec 31, 2024
1 parent c227c74 commit 101f111
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set (PPIC_CPP_FILES
app/exiv2wrapper.cpp
app/playlistmanager.cpp
app/shortcutedit.cpp
app/fileopeneventhandler.cpp
)

set (PPIC_HEADER_FILES
Expand All @@ -88,6 +89,7 @@ set (PPIC_HEADER_FILES
app/exiv2wrapper.h
app/playlistmanager.h
app/shortcutedit.h
app/fileopeneventhandler.h
)

set (PPIC_QRC_FILES
Expand Down
22 changes: 22 additions & 0 deletions app/fileopeneventhandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <[email protected]>
//
// SPDX-License-Identifier: MIT

#include "fileopeneventhandler.h"

#include <QFileOpenEvent>

FileOpenEventHandler::FileOpenEventHandler(QObject *parent)
: QObject(parent)
{
}

bool FileOpenEventHandler::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::FileOpen) {
QFileOpenEvent *fileOpenEvent = static_cast<QFileOpenEvent *>(event);
emit fileOpen(fileOpenEvent->url());
return true;
}
return QObject::eventFilter(obj, event);
}
21 changes: 21 additions & 0 deletions app/fileopeneventhandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <[email protected]>
//
// SPDX-License-Identifier: MIT

#pragma once

#include <QObject>

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);
};
14 changes: 14 additions & 0 deletions app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "playlistmanager.h"
#include "settings.h"

#ifdef Q_OS_MACOS
#include "fileopeneventhandler.h"
#endif // Q_OS_MACOS

#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
Expand Down Expand Up @@ -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<QUrl> && urlList = PlaylistManager::convertToUrlList(urlStrList);

Expand Down
18 changes: 0 additions & 18 deletions app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<QFileOpenEvent *>(event);
showUrls({fileOpenEvent->url()});
initWindowSize();
return true;
}
return false;
}
#endif // Q_OS_MACOS

void MainWindow::on_actionOpen_triggered()
{
QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
Expand Down
3 changes: 0 additions & 3 deletions app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions pineapple-pictures.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down

0 comments on commit 101f111

Please sign in to comment.