From e805d36114f5bac02c09331fc81d5cfdde4080aa Mon Sep 17 00:00:00 2001
From: dismine <dismine@gmail.com>
Date: Tue, 8 Jul 2014 13:59:50 +0300
Subject: [PATCH] Save pointer to main window. Use when need create modal
 window.

--HG--
branch : develop
---
 src/app/main.cpp                      |  1 +
 src/app/tools/drawTools/vdrawtool.cpp |  4 ++--
 src/app/tools/drawTools/vdrawtool.h   |  4 +---
 src/app/tools/vtooldetail.cpp         |  4 +---
 src/app/widgets/vapplication.cpp      | 29 ++++++++++++++++++++-------
 src/app/widgets/vapplication.h        |  7 +++++++
 src/app/xml/vpattern.cpp              | 14 ++++++-------
 7 files changed, 41 insertions(+), 22 deletions(-)

diff --git a/src/app/main.cpp b/src/app/main.cpp
index 7cbecae0f..823e6d238 100644
--- a/src/app/main.cpp
+++ b/src/app/main.cpp
@@ -169,6 +169,7 @@ int main(int argc, char *argv[])
     MainWindow w;
     w.setWindowState(w.windowState() ^ Qt::WindowMaximized);
     app.setWindowIcon(QIcon(":/icon/64x64/icon64x64.png"));
+    app.setMainWindow(&w);
     TableWindow table;
     QObject::connect(&w, &MainWindow::ModelChosen, &table, &TableWindow::ModelChosen);
     QObject::connect(&table, &TableWindow::closed, &w, &MainWindow::tableClosed);
diff --git a/src/app/tools/drawTools/vdrawtool.cpp b/src/app/tools/drawTools/vdrawtool.cpp
index bcfaf86c2..d21179708 100644
--- a/src/app/tools/drawTools/vdrawtool.cpp
+++ b/src/app/tools/drawTools/vdrawtool.cpp
@@ -196,14 +196,14 @@ qreal VDrawTool::CheckFormula(QString &formula, VContainer *data)
         Q_UNUSED(e)
         delete cal;
 
-        DialogUndo *dialogUndo = new DialogUndo();
+        DialogUndo *dialogUndo = new DialogUndo(qApp->getMainWindow());
         if (dialogUndo->exec() == QDialog::Accepted)
         {
             UndoButton resultUndo = dialogUndo->Result();
             delete dialogUndo;
             if (resultUndo == UndoButton::Fix)
             {
-                DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data);
+                DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, qApp->getMainWindow());
                 dialog->setFormula(formula);
                 if (dialog->exec() == QDialog::Accepted)
                 {
diff --git a/src/app/tools/drawTools/vdrawtool.h b/src/app/tools/drawTools/vdrawtool.h
index 80afda756..f707fde53 100644
--- a/src/app/tools/drawTools/vdrawtool.h
+++ b/src/app/tools/drawTools/vdrawtool.h
@@ -118,9 +118,7 @@ protected:
             QAction *selectedAction = menu.exec(event->screenPos());
             if (selectedAction == actionOption)
             {
-                QGraphicsScene *scene = tool->scene();
-                QList<QGraphicsView *> list =  scene->views();
-                dialog = new Dialog(getData(), list.first());
+                dialog = new Dialog(getData(), qApp->getMainWindow());
                 dialog->setModal(true);
 
                 connect(qobject_cast< VMainGraphicsScene * >(tool->scene()),
diff --git a/src/app/tools/vtooldetail.cpp b/src/app/tools/vtooldetail.cpp
index dc0845c97..fc9076d7a 100644
--- a/src/app/tools/vtooldetail.cpp
+++ b/src/app/tools/vtooldetail.cpp
@@ -405,9 +405,7 @@ void VToolDetail::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
     QAction *selectedAction = menu.exec(event->screenPos());
     if (selectedAction == actionOption)
     {
-        QGraphicsScene *scene = this->scene();
-        QList<QGraphicsView *> list =  scene->views();
-        dialog = new DialogDetail(getData(), list.first());
+        dialog = new DialogDetail(getData(), qApp->getMainWindow());
         dialog->setModal(true);
         connect(qobject_cast< VMainGraphicsScene * >(this->scene()), &VMainGraphicsScene::ChoosedObject,
                 dialog, &DialogTool::ChoosedObject);
diff --git a/src/app/widgets/vapplication.cpp b/src/app/widgets/vapplication.cpp
index 599928392..81d3b8bbe 100644
--- a/src/app/widgets/vapplication.cpp
+++ b/src/app/widgets/vapplication.cpp
@@ -58,7 +58,8 @@ VApplication::VApplication(int &argc, char **argv)
       _widthMainLine(DefWidth), _widthHairLine(DefWidth/3.0), measurements(QMap<QString, VTranslation>()),
       guiTexts(QMap<QString, VTranslation>()), descriptions(QMap<QString, VTranslation>()),
       variables(QMap<QString, VTranslation>()), functions(QMap<QString, VTranslation>()),
-      postfixOperators(QMap<QString, VTranslation>()), undoStack(nullptr), sceneView(nullptr), autoSaveTimer(nullptr)
+      postfixOperators(QMap<QString, VTranslation>()), undoStack(nullptr), sceneView(nullptr), autoSaveTimer(nullptr),
+      mainWindow(nullptr)
 {
     undoStack = new QUndoStack(this);
 
@@ -99,32 +100,32 @@ bool VApplication::notify(QObject *receiver, QEvent *event)
     }
     catch (const VExceptionObjectError &e)
     {
-        e.CriticalMessageBox(tr("Error parsing file. Program will be terminated."));
+        e.CriticalMessageBox(tr("Error parsing file. Program will be terminated."), mainWindow);
         abort();
     }
     catch (const VExceptionBadId &e)
     {
-        e.CriticalMessageBox(tr("Error bad id. Program will be terminated."));
+        e.CriticalMessageBox(tr("Error bad id. Program will be terminated."), mainWindow);
         abort();
     }
     catch (const VExceptionConversionError &e)
     {
-        e.CriticalMessageBox(tr("Error can't convert value. Program will be terminated."));
+        e.CriticalMessageBox(tr("Error can't convert value. Program will be terminated."), mainWindow);
         abort();
     }
     catch (const VExceptionEmptyParameter &e)
     {
-        e.CriticalMessageBox(tr("Error empty parameter. Program will be terminated."));
+        e.CriticalMessageBox(tr("Error empty parameter. Program will be terminated."), mainWindow);
         abort();
     }
     catch (const VExceptionWrongId &e)
     {
-        e.CriticalMessageBox(tr("Error wrong id. Program will be terminated."));
+        e.CriticalMessageBox(tr("Error wrong id. Program will be terminated."), mainWindow);
         abort();
     }
     catch (const VException &e)
     {
-        e.CriticalMessageBox(tr("Something's wrong!!"));
+        e.CriticalMessageBox(tr("Something's wrong!!"), mainWindow);
         return true;
     }
     catch (std::exception& e)
@@ -1952,3 +1953,17 @@ QString VApplication::FormulaToUser(const QString &formula)
 
     return newFormula;
 }
+
+//---------------------------------------------------------------------------------------------------------------------
+QWidget *VApplication::getMainWindow() const
+{
+    return mainWindow;
+}
+
+//---------------------------------------------------------------------------------------------------------------------
+void VApplication::setMainWindow(QWidget *value)
+{
+    SCASSERT(value != nullptr)
+    mainWindow = value;
+}
+
diff --git a/src/app/widgets/vapplication.h b/src/app/widgets/vapplication.h
index 1eab607b3..37fa7eac4 100644
--- a/src/app/widgets/vapplication.h
+++ b/src/app/widgets/vapplication.h
@@ -76,6 +76,8 @@ public:
     void               setSceneView(VMainGraphicsView *value);
     QTimer             *getAutoSaveTimer() const;
     void               setAutoSaveTimer(QTimer *value);
+    QWidget            *getMainWindow() const;
+    void               setMainWindow(QWidget *value);
 private:
     Q_DISABLE_COPY(VApplication)
     Unit               _patternUnit;
@@ -91,6 +93,11 @@ private:
     QUndoStack         *undoStack;
     VMainGraphicsView  *sceneView;
     QTimer             *autoSaveTimer;
+    /**
+     * @brief mainWindow pointer to main window. Usefull if need create modal dialog. Without pointer to main window
+     * modality doesn't work.
+     */
+    QWidget            *mainWindow;
     void               InitLineWidth();
     void               InitMeasurements();
     void               InitVariables();
diff --git a/src/app/xml/vpattern.cpp b/src/app/xml/vpattern.cpp
index 5787834cc..8317806ff 100644
--- a/src/app/xml/vpattern.cpp
+++ b/src/app/xml/vpattern.cpp
@@ -636,7 +636,7 @@ bool VPattern::SaveDocument(const QString &fileName)
     }
     catch (const VExceptionWrongId &e)
     {
-        e.CriticalMessageBox(tr("Error no unique id."));
+        e.CriticalMessageBox(tr("Error no unique id."), qApp->getMainWindow());
         return false;
     }
     GarbageCollector();
@@ -665,31 +665,31 @@ void VPattern::LiteParseTree()
     }
     catch (const VExceptionObjectError &e)
     {
-        e.CriticalMessageBox(tr("Error parsing file."));
+        e.CriticalMessageBox(tr("Error parsing file."), qApp->getMainWindow());
         emit SetEnabledGUI(false);
         return;
     }
     catch (const VExceptionConversionError &e)
     {
-        e.CriticalMessageBox(tr("Error can't convert value."));
+        e.CriticalMessageBox(tr("Error can't convert value."), qApp->getMainWindow());
         emit SetEnabledGUI(false);
         return;
     }
     catch (const VExceptionEmptyParameter &e)
     {
-        e.CriticalMessageBox(tr("Error empty parameter."));
+        e.CriticalMessageBox(tr("Error empty parameter."), qApp->getMainWindow());
         emit SetEnabledGUI(false);
         return;
     }
     catch (const VExceptionWrongId &e)
     {
-        e.CriticalMessageBox(tr("Error wrong id."));
+        e.CriticalMessageBox(tr("Error wrong id."), qApp->getMainWindow());
         emit SetEnabledGUI(false);
         return;
     }
     catch (VException &e)
     {
-        e.CriticalMessageBox(tr("Error parsing file."));
+        e.CriticalMessageBox(tr("Error parsing file."), qApp->getMainWindow());
         emit SetEnabledGUI(false);
         return;
     }
@@ -698,7 +698,7 @@ void VPattern::LiteParseTree()
 #ifndef QT_NO_CURSOR
         QApplication::restoreOverrideCursor();
 #endif
-        QMessageBox::critical(nullptr, tr("Critical error!"), tr("Error parsing file (std::bad_alloc)."),
+        QMessageBox::critical(qApp->getMainWindow(), tr("Critical error!"), tr("Error parsing file (std::bad_alloc)."),
                               QMessageBox::Ok, QMessageBox::Ok);
 #ifndef QT_NO_CURSOR
         QApplication::setOverrideCursor(Qt::WaitCursor);