Properly delete MainWindow

This commit is contained in:
Florian Märkl 2018-02-27 14:06:04 +01:00
parent a0a3f9278d
commit 8c3d8d77cb
2 changed files with 24 additions and 21 deletions

View File

@ -64,9 +64,7 @@ CutterApplication::CutterApplication(int &argc, char **argv) : QApplication(argc
} }
} }
MainWindow *main = new MainWindow(); mainWindow = new MainWindow();
setMainWindow(main);
if (args.empty()) if (args.empty())
{ {
@ -76,19 +74,28 @@ CutterApplication::CutterApplication(int &argc, char **argv) : QApplication(argc
exit(1); exit(1);
} }
main->displayNewFileDialog(); mainWindow->displayNewFileDialog();
} }
else // filename specified as positional argument else // filename specified as positional argument
{ {
main->openNewFile(args[0], analLevelSpecified ? analLevel : -1); mainWindow->openNewFile(args[0], analLevelSpecified ? analLevel : -1);
} }
} }
bool CutterApplication::event(QEvent *e){ CutterApplication::~CutterApplication()
if (e->type() == QEvent::FileOpen) { {
delete mainWindow;
}
bool CutterApplication::event(QEvent *e)
{
if (e->type() == QEvent::FileOpen)
{
QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(e); QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(e);
if (openEvent) { if (openEvent)
if (m_FileAlreadyDropped) { {
if (m_FileAlreadyDropped)
{
// we already dropped a file in macOS, let's spawn another instance // we already dropped a file in macOS, let's spawn another instance
// (Like the File -> Open) // (Like the File -> Open)
QString fileName = openEvent->file(); QString fileName = openEvent->file();
@ -96,11 +103,13 @@ bool CutterApplication::event(QEvent *e){
process.setEnvironment(QProcess::systemEnvironment()); process.setEnvironment(QProcess::systemEnvironment());
QStringList args = QStringList(fileName); QStringList args = QStringList(fileName);
process.startDetached(qApp->applicationFilePath(), args); process.startDetached(qApp->applicationFilePath(), args);
} else { }
else
{
QString fileName = openEvent->file(); QString fileName = openEvent->file();
m_FileAlreadyDropped = true; m_FileAlreadyDropped = true;
m_MainWindow->closeNewFileDialog(); mainWindow->closeNewFileDialog();
m_MainWindow->openNewFile(fileName, -1); mainWindow->openNewFile(fileName, -1);
} }
} }
} }

View File

@ -10,25 +10,19 @@
class CutterApplication : public QApplication class CutterApplication : public QApplication
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(MainWindow* mainWindow READ mainWindow WRITE setMainWindow)
public: public:
CutterApplication(int &argc, char **argv); CutterApplication(int &argc, char **argv);
~CutterApplication();
MainWindow * mainWindow() { MainWindow *getMainWindow() { return mainWindow; }
return m_MainWindow;
}
void setMainWindow(MainWindow * mw) {
m_MainWindow = mw;
}
protected: protected:
bool event(QEvent *e); bool event(QEvent *e);
private: private:
bool m_FileAlreadyDropped; bool m_FileAlreadyDropped;
MainWindow *m_MainWindow; MainWindow *mainWindow;
}; };
#endif // CUTTERAPPLICATION_H #endif // CUTTERAPPLICATION_H