From 33bf03994e78a649042a18567013f7b3bdd4d9fb Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 19 Nov 2015 17:17:25 +0200 Subject: [PATCH] Handling "Open With" from Finder in Mac OS X. --HG-- branch : develop --- src/app/tape/mapplication.cpp | 71 ++++++++++++++++--------- src/app/tape/mapplication.h | 5 +- src/app/valentina/core/vapplication.cpp | 41 ++++++++++++++ src/app/valentina/core/vapplication.h | 1 + src/app/valentina/mainwindow.h | 3 +- 5 files changed, 90 insertions(+), 31 deletions(-) diff --git a/src/app/tape/mapplication.cpp b/src/app/tape/mapplication.cpp index 4ff0eef36..c06a68090 100644 --- a/src/app/tape/mapplication.cpp +++ b/src/app/tape/mapplication.cpp @@ -391,6 +391,51 @@ void MApplication::InitTrVars() } } +//--------------------------------------------------------------------------------------------------------------------- +bool MApplication::event(QEvent *e) +{ + switch(e->type()) + { + // In Mac OS X the QFileOpenEvent event is generated when user perform "Open With" from Finder (this event is + // Mac specific). + case QEvent::FileOpen: + { + QFileOpenEvent *fileOpenEvent = static_cast(e); + if(fileOpenEvent) + { + const QString macFileOpen = fileOpenEvent->file(); + if(not macFileOpen.isEmpty()) + { + TMainWindow *mw = MainWindow(); + if (mw) + { + mw->LoadFile(macFileOpen); // open file in existing window + } + return true; + } + } + } +#if defined(Q_OS_MAC) + case QEvent::ApplicationActivate: + { + Clean(); + if (!mainWindows.isEmpty()) + { + TMainWindow *mw = MainWindow(); + if (mw && not mw->isMinimized()) + { + mw->show(); + } + return true; + } + } +#endif //defined(Q_OS_MAC) + default: + return VAbstractApplication::event(e); + } + return VAbstractApplication::event(e); +} + //--------------------------------------------------------------------------------------------------------------------- void MApplication::OpenSettings() { @@ -668,32 +713,6 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS } } -//--------------------------------------------------------------------------------------------------------------------- -#if defined(Q_WS_MAC) -bool MApplication::event(QEvent* event) -{ - switch (event->type()) - { - case QEvent::ApplicationActivate: - { - Clean(); - if (!mainWindows.isEmpty()) - { - TMainWindow *mw = MainWindow(); - if (mw && !mw->isMinimized()) - { - MainWindow()->show(); - } - return true; - } - } - default: - break; - } - return QApplication::event(event); -} -#endif - //--------------------------------------------------------------------------------------------------------------------- TMainWindow *MApplication::NewMainWindow() { diff --git a/src/app/tape/mapplication.h b/src/app/tape/mapplication.h index a257e27c7..9569e8d9a 100644 --- a/src/app/tape/mapplication.h +++ b/src/app/tape/mapplication.h @@ -60,10 +60,6 @@ public: TMainWindow *MainWindow(); QList MainWindows(); -#if defined(Q_WS_MAC) - bool event(QEvent *event); -#endif - void InitOptions(); virtual const VTranslateVars *TrVars() Q_DECL_OVERRIDE; @@ -85,6 +81,7 @@ public slots: protected: virtual void InitTrVars() Q_DECL_OVERRIDE; + virtual bool event(QEvent *e) Q_DECL_OVERRIDE; private slots: void OpenFile(const QString &path); diff --git a/src/app/valentina/core/vapplication.cpp b/src/app/valentina/core/vapplication.cpp index f53a638d9..04cff2046 100644 --- a/src/app/valentina/core/vapplication.cpp +++ b/src/app/valentina/core/vapplication.cpp @@ -36,6 +36,7 @@ #include "../version.h" #include "../vmisc/logging.h" #include "../qmuparser/qmuparsererror.h" +#include "../mainwindow.h" #include #include @@ -601,6 +602,46 @@ void VApplication::InitTrVars() trVars = new VTranslateVars(ValentinaSettings()->GetOsSeparator()); } +//--------------------------------------------------------------------------------------------------------------------- +bool VApplication::event(QEvent *e) +{ + switch(e->type()) + { + // In Mac OS X the QFileOpenEvent event is generated when user perform "Open With" from Finder (this event is + // Mac specific). + case QEvent::FileOpen: + { + QFileOpenEvent *fileOpenEvent = static_cast(e); + if(fileOpenEvent) + { + const QString macFileOpen = fileOpenEvent->file(); + if(not macFileOpen.isEmpty()) + { + MainWindow *window = qobject_cast(mainWindow); + if (window) + { + window->LoadPattern(macFileOpen); // open file in existing window + } + return true; + } + } + } +#if defined(Q_OS_MAC) + case QEvent::ApplicationActivate: + { + if (mainWindow && not mainWindow->isMinimized()) + { + mainWindow->show(); + } + return true; + } +#endif //defined(Q_OS_MAC) + default: + return VAbstractApplication::event(e); + } + return VAbstractApplication::event(e); +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief OpenSettings get acsses to application settings. diff --git a/src/app/valentina/core/vapplication.h b/src/app/valentina/core/vapplication.h index 6be7f6c29..b87b27a18 100644 --- a/src/app/valentina/core/vapplication.h +++ b/src/app/valentina/core/vapplication.h @@ -84,6 +84,7 @@ public: protected: virtual void InitTrVars() Q_DECL_OVERRIDE; + virtual bool event(QEvent *e) Q_DECL_OVERRIDE; private slots: #if defined(Q_OS_WIN) && defined(Q_CC_GNU) diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 1eb7f6149..4efbeba65 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -60,6 +60,8 @@ public: explicit MainWindow(QWidget *parent = nullptr); virtual ~MainWindow() Q_DECL_OVERRIDE; + bool LoadPattern(const QString &curFile, const QString &customMeasureFile = QString()); + public slots: void ProcessCMD(); @@ -305,7 +307,6 @@ private: void ToggleMSync(bool toggle); - bool LoadPattern(const QString &curFile, const QString &customMeasureFile = QString()); void ReopenFilesAfterCrash(QStringList &args); void DoExport(const VCommandLinePtr& expParams);