From 66a3d9550fc7c9cd0c60b22e0d198b0268326b7c Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Mon, 23 Aug 2021 13:11:12 +0200
Subject: [PATCH] delete views, if project is closed. Create them new when
 project is opened

---
 GUI/mainwindow/mainwindow.cpp | 103 ++++++++++++++++++++--------------
 GUI/mainwindow/mainwindow.h   |   2 +-
 2 files changed, 62 insertions(+), 43 deletions(-)

diff --git a/GUI/mainwindow/mainwindow.cpp b/GUI/mainwindow/mainwindow.cpp
index 47db8c4711f..a154e660233 100644
--- a/GUI/mainwindow/mainwindow.cpp
+++ b/GUI/mainwindow/mainwindow.cpp
@@ -113,6 +113,9 @@ MainWindow::MainWindow()
     connect(m_projectManager, &ProjectManager::documentOpenedOrClosed, this,
             &MainWindow::onDocumentOpenedOrClosed);
 
+    connect(m_projectManager, &ProjectManager::aboutToCloseDocument, this,
+            &MainWindow::onAboutToCloseDocument);
+
     ASSERT(m_viewSelectionButtons->button(ViewId::WELCOME) != nullptr);
     m_viewSelectionButtons->button(ViewId::WELCOME)->setChecked(true);
 
@@ -120,7 +123,6 @@ MainWindow::MainWindow()
     //    m_applicationModels->createTestJob();
     //    m_applicationModels->createTestRealData();
 
-    updateViewEnabling();
     updateTitle();
 
     if (baApp->settings().createNewProjectOnStartup())
@@ -292,43 +294,46 @@ void MainWindow::initProgressBar()
 
 void MainWindow::initViews()
 {
-    m_welcomeView = new WelcomeView(this);
+    if (m_welcomeView == nullptr) {
+        m_welcomeView = new WelcomeView(this);
+        addView(ViewId::WELCOME, QIcon(":/images/main_welcomeview.svg"), "Welcome",
+                "Switch to Welcome View", m_welcomeView);
+    }
 
     auto doc = m_projectManager->document();
-    
-    m_instrumentView = new InstrumentView(this, doc);
-    m_sampleView = new SampleView(this, doc);
-    m_importDataView = new ImportDataView(this, doc);
-    m_simulationView = new SimulationView(this, doc);
-    m_jobView = new JobView(this, doc);
-    m_sessionModelView = new SessionModelView(this, doc);
+    if (doc != nullptr) {
+        m_instrumentView = new InstrumentView(this, doc);
+        m_sampleView = new SampleView(this, doc);
+        m_importDataView = new ImportDataView(this, doc);
+        m_simulationView = new SimulationView(this, doc);
+        m_jobView = new JobView(this, doc);
+        m_sessionModelView = new SessionModelView(this, doc);
 
-    addView(ViewId::WELCOME, QIcon(":/images/main_welcomeview.svg"), "Welcome",
-            "Switch to Welcome View", m_welcomeView);
 
-    addView(ViewId::INSTRUMENT, QIcon(":/images/main_instrumentview.svg"), "Instrument",
-            "Define the beam and the  detector", m_instrumentView);
+        addView(ViewId::INSTRUMENT, QIcon(":/images/main_instrumentview.svg"), "Instrument",
+                "Define the beam and the  detector", m_instrumentView);
 
-    addView(ViewId::SAMPLE, QIcon(":/images/main_sampleview.svg"), "Sample", "Build the sample",
-            m_sampleView);
+        addView(ViewId::SAMPLE, QIcon(":/images/main_sampleview.svg"), "Sample", "Build the sample",
+                m_sampleView);
 
-    addView(ViewId::IMPORT, QIcon(":/images/main_importview.svg"), "Data",
-            "Import intensity data to fit", m_importDataView);
+        addView(ViewId::IMPORT, QIcon(":/images/main_importview.svg"), "Data",
+                "Import intensity data to fit", m_importDataView);
 
-    addView(ViewId::SIMULATION, QIcon(":/images/main_simulationview.svg"), "Simulation",
-            "Run simulation", m_simulationView);
+        addView(ViewId::SIMULATION, QIcon(":/images/main_simulationview.svg"), "Simulation",
+                "Run simulation", m_simulationView);
 
-    addView(ViewId::JOB, QIcon(":/images/main_jobview.svg"), "Jobs",
-            "Switch to see job results, tune parameters real time,\nfit the data", m_jobView);
+        addView(ViewId::JOB, QIcon(":/images/main_jobview.svg"), "Jobs",
+                "Switch to see job results, tune parameters real time,\nfit the data", m_jobView);
 
-    addView(ViewId::SESSIONMODEL, QIcon(":/images/main_sessionmodel.svg"), "Models", "",
-            m_sessionModelView);
+        addView(ViewId::SESSIONMODEL, QIcon(":/images/main_sessionmodel.svg"), "Models", "",
+                m_sessionModelView);
 
-    // enabling technical view
-    QSettings settings;
-    settings.beginGroup(GUI::Constants::S_SESSIONMODELVIEW);
-    onSessionModelViewActive(settings.value(GUI::Constants::S_VIEWISACTIVE, false).toBool());
-    settings.endGroup();
+        // enabling technical view
+        QSettings settings;
+        settings.beginGroup(GUI::Constants::S_SESSIONMODELVIEW);
+        onSessionModelViewActive(settings.value(GUI::Constants::S_VIEWISACTIVE, false).toBool());
+        settings.endGroup();
+    }
 }
 
 void MainWindow::readSettings()
@@ -413,27 +418,41 @@ void MainWindow::updateViewSelectionButtonsGeometry() const
     m_progressBar->setFixedWidth(buttonExtent);
 }
 
-void MainWindow::updateViewEnabling()
+void MainWindow::onDocumentOpenedOrClosed()
 {
     const bool documentExists = m_projectManager->document() != nullptr;
+    if (documentExists)
+        initViews();
+
+    updateTitle();
+}
 
-    if (!documentExists)
-        m_viewSelectionButtons->button(WELCOME)->click();
+void MainWindow::onAboutToCloseDocument()
+{
+    setCurrentView(WELCOME);
 
-    m_viewSelectionButtons->button(INSTRUMENT)->setVisible(documentExists);
-    m_viewSelectionButtons->button(SAMPLE)->setVisible(documentExists);
-    m_viewSelectionButtons->button(IMPORT)->setVisible(documentExists);
-    m_viewSelectionButtons->button(SIMULATION)->setVisible(documentExists);
-    m_viewSelectionButtons->button(JOB)->setVisible(documentExists);
-    m_viewSelectionButtons->button(SESSIONMODEL)->setVisible(documentExists);
+    while (m_viewSelectionButtons->buttons().size() > 1)
+        delete m_viewSelectionButtons->buttons().last();
 
     updateViewSelectionButtonsGeometry();
-}
 
-void MainWindow::onDocumentOpenedOrClosed()
-{
-    updateViewEnabling();
-    updateTitle();
+    delete m_instrumentView;
+    m_instrumentView = nullptr;
+
+    delete m_sampleView;
+    m_sampleView = nullptr;
+
+    delete m_importDataView;
+    m_importDataView = nullptr;
+
+    delete m_simulationView;
+    m_simulationView = nullptr;
+
+    delete m_jobView;
+    m_jobView = nullptr;
+
+    delete m_sessionModelView;
+    m_sessionModelView = nullptr;
 }
 
 QToolButton* MainWindow::createViewSelectionButton() const
diff --git a/GUI/mainwindow/mainwindow.h b/GUI/mainwindow/mainwindow.h
index 71ff463dea3..9136da68cad 100644
--- a/GUI/mainwindow/mainwindow.h
+++ b/GUI/mainwindow/mainwindow.h
@@ -98,8 +98,8 @@ private:
 
     //! Recalculate the size of the view selection buttons to show complete button text
     void updateViewSelectionButtonsGeometry() const;
-    void updateViewEnabling();
     void onDocumentOpenedOrClosed();
+    void onAboutToCloseDocument();
 
     void raiseView(int viewId);
 
-- 
GitLab