diff --git a/SPIERSedit/src/globals.cpp b/SPIERSedit/src/globals.cpp index 820cf7e..28d4f34 100644 --- a/SPIERSedit/src/globals.cpp +++ b/SPIERSedit/src/globals.cpp @@ -67,6 +67,7 @@ QByteArray Masks, Locks; QByteArray dirty; //will hold flags for brighten mode - avoid doing same pixel twice in one stroke. QString openfile; +QString currentOpenFileName; bool MasksDirty, LocksDirty, CurvesDirty; bool MasksUndoDirty, LocksUndoDirty, CurvesUndoDirty; int CurrentMode; //0=bright, 1=masks, 2=curves, 3=lock, 4=segment, 5=recalc diff --git a/SPIERSedit/src/globals.h b/SPIERSedit/src/globals.h index 84a647d..7f835f0 100644 --- a/SPIERSedit/src/globals.h +++ b/SPIERSedit/src/globals.h @@ -58,6 +58,7 @@ extern QList FilesDirty; //for re-rendering. extern QString openfile; +extern QString currentOpenFileName; // stores the currently open file name/path extern bool bodgeflag; extern bool ThreeDmode; extern int BrushY, BrushZ; diff --git a/SPIERSedit/src/main.cpp b/SPIERSedit/src/main.cpp index 5c012a1..308f0ec 100644 --- a/SPIERSedit/src/main.cpp +++ b/SPIERSedit/src/main.cpp @@ -132,8 +132,14 @@ int main(int argc, char **argv) main::main(int &argc, char *argv[]) : QApplication(argc, argv) { - //do nothing - donthandlefileevent = false; + // Set so we do NOT ignore calls to QEvent::FileOpen + ignoreFileOpenEvent = false; + + // Set the default received file name to be a empty QString + receivedFileName = ""; + + // Set the recieved file name flag to false + hasReceivedFileName = false; } /** @@ -143,8 +149,8 @@ main::main(int &argc, char *argv[]) : QApplication(argc, argv) */ bool main::event(QEvent *event) { - //we don't do anything if we were passed and argv1 - i.e. if we are a child process of first one - if (donthandlefileevent == true) + // We don't do anything if we were passed and argv1 - i.e. if we are a child process of first one + if (ignoreFileOpenEvent == true) { qDebug() << "Don't handle file open event"; return QApplication::event(event); @@ -152,24 +158,56 @@ bool main::event(QEvent *event) switch (event->type()) { + + // Pass all events other than QEvent:FileOpen to the main application default: return QApplication::event(event); break; + // Catch the QEvent::FileOpen event case QEvent::FileOpen: - qDebug() << "File Open Event"; + qDebug() << "File Open Event Caught"; + // Getting the recevied file name from the passed event QFileOpenEvent *openEvent = static_cast(event); - fn = openEvent->file(); + receivedFileName = openEvent->file(); + + qDebug() << "File name passed is: " << receivedFileName << " Open File: " << openfile; + + // Here we spin up a new SPIERSview application and pass the received file name as an argument + // But we only do this if our current application already has a file open... otherwise we expect our + // current applciation to open the file instead. + if(currentOpenFileName != "") { + + // This should be the path to the apllication we will need to call + // plus the application working directory + QString pathToApplication = qApp->applicationFilePath(); + QString pathToWorkingDirectory = qApp->applicationDirPath(); + + // Set up arguments + QStringList argumentsToPass; + argumentsToPass << receivedFileName; + + qint64 *pid = new qint64; + QProcess process; + process.setProgram(pathToApplication); + process.setArguments(argumentsToPass); + process.setWorkingDirectory(pathToWorkingDirectory); + bool startSuccess = process.startDetached(pid); + + qDebug() << "pathToApplication == " << pathToApplication << " pathToWorkingDirectory = " << pathToWorkingDirectory << " Receieved File Name: " << receivedFileName; + if(startSuccess) { + qDebug() << "Process start == OK : " << pid; + } else { + qDebug() << "Process start == Failed"; + } - qDebug() << "File name passed is: " << fn; - - openfile = fn; + } - namereceived = true; + hasReceivedFileName = true; - return true; + return QApplication::event(event); break; } @@ -180,6 +218,7 @@ int main(int argc, char *argv[]) // Install the message handler to log to file qInstallMessageHandler(logMessageOutput); + // This is a sanity check for any passed arguments... if (argc == 2) { qDebug() << "argc == 2"; @@ -196,45 +235,60 @@ int main(int argc, char *argv[]) //Style program with our dark style QApplication::setStyle(new DarkStyleTheme); - NetModule n; - n.checkForNew(); - - app.fn = ""; - app.namereceived = false; - app.setQuitOnLastWindowClosed(true); - // Set the fname global from argument if the argc value is === 2, also check for - and x to quit hte application, anything else we ignore and set the global fname to null. + // Set the fname global from argument if the argc value is === 2, also check for -x to quit the application, anything else we ignore and set the global fname to "". if (argc != 2) { - qDebug() << "openfile = null"; + qDebug() << "fname = {blank}"; openfile = ""; } else { qDebug() << "argc is 2 so we do stuff..."; + + // This is to allow cmd line quiting of the program with: > {path to appllication} -x if ((*(argv[1]) == '-') && (*(argv[1] + 1) == 'x')) { QCoreApplication::quit(); } + + + // Else we assume that it is a valid file name passed as an argument. We then do the following... else { qDebug() << "Setting fname to argv[1]"; + + // Set the global fname to the recived file name argument openfile = argv[1]; // Make sure we don't handle file event at all - app.donthandlefileevent = true; + //app.ignoreFileOpenEvent = true; } } - // Do nothing until event is received + // Do nothing until all events are received for the appllication start + app.processEvents(); + + // We then check again, as if seems that the QEvent::FileOpen event doesn't get picked up in the first check, + // but does seem to be picked up on a second check! This is a real bodge, there must be a better way to do this... app.processEvents(); + // If we have got this far then we are trying to load a file by name that has been caught in the QEvent::FileOpen + // but we either have not had a file name already passed by arguments or got a file already open + if(app.hasReceivedFileName) { + qDebug() << "Setting openfile from recieved file name by QEvent::FileOpen (we have no arguments passed nor a file already open)"; + openfile = app.receivedFileName; + } + qDebug() << "Moving to start application mainwindow..."; + NetModule n; + n.checkForNew(); + MainWindowImpl win; win.show(); diff --git a/SPIERSedit/src/main.h b/SPIERSedit/src/main.h index b8f4236..34fbb4e 100644 --- a/SPIERSedit/src/main.h +++ b/SPIERSedit/src/main.h @@ -29,15 +29,37 @@ class main : public QApplication { Q_OBJECT public: + /** + * @brief main + * @param argc + * @param argv + */ main(int &argc, char *argv[]); - QString fn; - bool namereceived; - bool donthandlefileevent; + /** + * @brief Override of the main event function + * @note This only does stuff on macOS, all other OS just pass the QEvent to the application. + * @return + */ + bool event(QEvent *) override; + /** + * @brief Flag to ignore subsequent FileOpenEvent + * @note This is a macOS only flag + */ + bool ignoreFileOpenEvent; -private: - bool event(QEvent *); + /** + * @brief Flag stating if a file name has been recieved + * @note This is a macOS only flag + */ + bool hasReceivedFileName; + + /** + * @brief Holds the received file name to open + * @note This is a macOS only flag + */ + QString receivedFileName; }; #endif diff --git a/SPIERSedit/src/mainwindowimpl.cpp b/SPIERSedit/src/mainwindowimpl.cpp index da384cd..a32eee0 100644 --- a/SPIERSedit/src/mainwindowimpl.cpp +++ b/SPIERSedit/src/mainwindowimpl.cpp @@ -65,6 +65,7 @@ MainWindowImpl::MainWindowImpl(QWidget *parent, Qt::WindowFlags f) AppMainWindow = this; setupUi(this); setStatusBar(nullptr); + currentOpenFileName = ""; showMaximized(); @@ -551,6 +552,7 @@ void MainWindowImpl::ScreenUpdate() Start(); + currentOpenFileName = openfile; openfile = ""; } } @@ -1310,6 +1312,9 @@ void MainWindowImpl::Menu_File_Import() RecentFile(FullSettingsFileName); FullFiles = Files; + // Almostly certainly could be cleaned up here! + currentOpenFileName = FullSettingsFileName; + Start();