Skip to content

Commit

Permalink
Fixed SPIERSedit opening on file double click on macOS
Browse files Browse the repository at this point in the history
Note that this now opens a new SPIERSedit application thn loads the selected file; previous behaviour was (I think) to open the slected file in the already open window. There is little downside to changing this, however it is now posible to open the same SPIERSedit settings file twice in different SPIERSedit applciation on the same machine via the double click.
  • Loading branch information
alanspencer committed Jun 13, 2020
1 parent 3e3831e commit cfb4b3f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 26 deletions.
1 change: 1 addition & 0 deletions SPIERSedit/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions SPIERSedit/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

extern QList<bool> 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;
Expand Down
96 changes: 75 additions & 21 deletions SPIERSedit/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -143,33 +149,65 @@ 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);
}

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<QFileOpenEvent *>(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;
}
Expand All @@ -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";
Expand All @@ -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();

Expand Down
32 changes: 27 additions & 5 deletions SPIERSedit/src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions SPIERSedit/src/mainwindowimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ MainWindowImpl::MainWindowImpl(QWidget *parent, Qt::WindowFlags f)
AppMainWindow = this;
setupUi(this);
setStatusBar(nullptr);
currentOpenFileName = "";

showMaximized();

Expand Down Expand Up @@ -551,6 +552,7 @@ void MainWindowImpl::ScreenUpdate()

Start();

currentOpenFileName = openfile;
openfile = "";
}
}
Expand Down Expand Up @@ -1310,6 +1312,9 @@ void MainWindowImpl::Menu_File_Import()
RecentFile(FullSettingsFileName);
FullFiles = Files;

// Almostly certainly could be cleaned up here!
currentOpenFileName = FullSettingsFileName;

Start();


Expand Down

0 comments on commit cfb4b3f

Please sign in to comment.