From fcc74d4a87a42f4d708308ac0e21845fc7980c83 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Tue, 2 Nov 2021 17:34:24 +0100
Subject: [PATCH] new file GUI/Session/SessionData to decouple instrument
 library from QApplication settings

---
 App/main.cpp                                  | 11 ++++---
 GUI/Application/GlobalSettings.cpp            |  5 ---
 GUI/Application/GlobalSettings.h              |  4 ---
 GUI/CMakeLists.txt                            |  1 +
 GUI/Models/Instrument/InstrumentLibrary.cpp   |  4 +--
 .../Instrument/InstrumentsTreeModel.cpp       |  1 +
 GUI/Session/SessionData.cpp                   | 28 ++++++++++++++++
 GUI/Session/SessionData.h                     | 33 +++++++++++++++++++
 .../InstrumentLibraryEditor.cpp               |  7 ++--
 .../InstrumentWidgets/InstrumentListView.cpp  |  4 +--
 .../JobWidgets/JobPropertiesTableModel.h      |  2 +-
 11 files changed, 79 insertions(+), 21 deletions(-)
 create mode 100644 GUI/Session/SessionData.cpp
 create mode 100644 GUI/Session/SessionData.h

diff --git a/App/main.cpp b/App/main.cpp
index d109f008395..4601b073a8f 100644
--- a/App/main.cpp
+++ b/App/main.cpp
@@ -16,6 +16,7 @@
 #include "App/appoptions.h"
 #include "GUI/Application/GlobalSettings.h"
 #include "GUI/DataLoaders/DataLoaderUtil.h"
+#include "GUI/Session/SessionData.h"
 #include "GUI/mainwindow/mainwindow.h"
 #include "GUI/utils/hostosinfo.h"
 #include "GUI/utils/Helpers.h"
@@ -49,6 +50,7 @@ int main(int argc, char* argv[])
         QApplication::setWindowIcon(QIcon(":/images/BornAgain.ico"));
 
     GlobalSettings GSettings;
+    SessionData GSession;
     int ret;
 
 #if !defined(BUILD_DEBUG)
@@ -61,10 +63,11 @@ int main(int argc, char* argv[])
         auto style = baApp->settings().styleToUse();
         baApp->loadStyle(style);
 
-        if (!QDir().exists(baApp->appDataFolder()))
-            QDir().mkpath(baApp->appDataFolder());
+        QString dir = SessionData::appDataFolder();
+        if (!QDir().exists(dir))
+            QDir().mkpath(dir);
 
-        baApp->instrumentLibrary().load();
+        gSessionData->instrumentLibrary.load();
 
         MainWindow win;
         if (options.find("geometry"))
@@ -74,7 +77,7 @@ int main(int argc, char* argv[])
 
         ret = QApplication::exec();
 
-        baApp->instrumentLibrary().saveIfModified();
+        gSessionData->instrumentLibrary.saveIfModified();
 #if !defined(BUILD_DEBUG)
     } catch (...) {
         QMessageBox box;
diff --git a/GUI/Application/GlobalSettings.cpp b/GUI/Application/GlobalSettings.cpp
index 87dc13f4939..1d8013100f8 100644
--- a/GUI/Application/GlobalSettings.cpp
+++ b/GUI/Application/GlobalSettings.cpp
@@ -92,11 +92,6 @@ const GlobalSettings::Palette& GlobalSettings::styleSheetPalette() const
     return m_styleSheetPalette;
 }
 
-InstrumentLibrary& GlobalSettings::instrumentLibrary()
-{
-    return m_instrumentLibrary;
-}
-
 QString GlobalSettings::appDataFolder() const
 {
     return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
diff --git a/GUI/Application/GlobalSettings.h b/GUI/Application/GlobalSettings.h
index 97b6fa9e4b1..542927c29de 100644
--- a/GUI/Application/GlobalSettings.h
+++ b/GUI/Application/GlobalSettings.h
@@ -16,7 +16,6 @@
 #define BORNAGAIN_GUI_APPLICATION_GLOBALSETTINGS_H
 
 #include "GUI/Application/ApplicationSettings.h"
-#include "GUI/Models/Instrument/InstrumentLibrary.h"
 #include <QColor>
 
 //! Provides access to application wide stuff like settings.
@@ -37,8 +36,6 @@ public:
 
     const Palette& styleSheetPalette() const;
 
-    InstrumentLibrary& instrumentLibrary();
-
     //! The folder where persistent application data shall be stored. E.g. in Windows this is the
     //! AppData/Roaming location.
     //! Used e.g. for storing the instrument library.
@@ -46,7 +43,6 @@ public:
 
 private:
     ApplicationSettings m_settings;
-    InstrumentLibrary m_instrumentLibrary;
     ApplicationSettings::Style m_currentStyle;
     Palette m_styleSheetPalette;
 };
diff --git a/GUI/CMakeLists.txt b/GUI/CMakeLists.txt
index c6acc90d528..a1c06ac03cb 100644
--- a/GUI/CMakeLists.txt
+++ b/GUI/CMakeLists.txt
@@ -21,6 +21,7 @@ set(include_dirs
     ${CMAKE_CURRENT_SOURCE_DIR}/Items
     ${CMAKE_CURRENT_SOURCE_DIR}/Mapper
     ${CMAKE_CURRENT_SOURCE_DIR}/Project
+    ${CMAKE_CURRENT_SOURCE_DIR}/Session
     ${CMAKE_CURRENT_SOURCE_DIR}/mainwindow
     ${CMAKE_CURRENT_SOURCE_DIR}/utils
     ${CMAKE_CURRENT_SOURCE_DIR}/Views/FitWidgets
diff --git a/GUI/Models/Instrument/InstrumentLibrary.cpp b/GUI/Models/Instrument/InstrumentLibrary.cpp
index 0d7569f5a4f..2e630314759 100644
--- a/GUI/Models/Instrument/InstrumentLibrary.cpp
+++ b/GUI/Models/Instrument/InstrumentLibrary.cpp
@@ -15,7 +15,7 @@
 #include "GUI/Models/Instrument/InstrumentLibrary.h"
 #include "GUI/Items/InstrumentItems.h"
 #include "GUI/Models/Instrument/InstrumentsTreeModel.h"
-#include "GUI/Application/GlobalSettings.h"
+#include "GUI/Session/SessionData.h"
 #include <QFile>
 #include <QXmlStreamWriter>
 
@@ -24,7 +24,7 @@ const QString XML_ROOT_TAG = "BornAgainInstrumentLibrary";
 const QString XML_VERSION_TAG = "Version";
 
 QString instrumentLibraryFilePath() {
-    return baApp->appDataFolder() + "/BornAgainInstrumentLibrary.balib";
+    return SessionData::appDataFolder() + "/BornAgainInstrumentLibrary.balib";
 }
 
 } // namespace
diff --git a/GUI/Models/Instrument/InstrumentsTreeModel.cpp b/GUI/Models/Instrument/InstrumentsTreeModel.cpp
index 1bbcb1311fd..17ac1e1edfb 100644
--- a/GUI/Models/Instrument/InstrumentsTreeModel.cpp
+++ b/GUI/Models/Instrument/InstrumentsTreeModel.cpp
@@ -13,6 +13,7 @@
 //  ************************************************************************************************
 
 #include "GUI/Models/Instrument/InstrumentsTreeModel.h"
+#include "GUI/Models/Instrument/InstrumentModel.h"
 #include "GUI/Application/GlobalSettings.h"
 #include "GUI/Items/InstrumentItems.h"
 #include <QApplication>
diff --git a/GUI/Session/SessionData.cpp b/GUI/Session/SessionData.cpp
new file mode 100644
index 00000000000..4b3d7e4afe5
--- /dev/null
+++ b/GUI/Session/SessionData.cpp
@@ -0,0 +1,28 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Session/SessionData.cpp
+//! @brief     Implements struct SessionData
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2021
+//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
+//
+//  ************************************************************************************************
+
+#include "GUI/Session/SessionData.h"
+#include <QStandardPaths>
+
+SessionData* gSessionData; //!< global pointer to the single instance
+
+SessionData::SessionData()
+{
+    gSessionData = this;
+}
+
+QString SessionData::appDataFolder()
+{
+    return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
+}
diff --git a/GUI/Session/SessionData.h b/GUI/Session/SessionData.h
new file mode 100644
index 00000000000..b2499e87098
--- /dev/null
+++ b/GUI/Session/SessionData.h
@@ -0,0 +1,33 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Session/SessionData.h
+//! @brief     Defines struct SessionData
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2021
+//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
+//
+//  ************************************************************************************************
+
+#ifndef BORNAGAIN_GUI_SESSION_SESSIONDATA_H
+#define BORNAGAIN_GUI_SESSION_SESSIONDATA_H
+
+#include "GUI/Models/Instrument/InstrumentLibrary.h"
+
+struct SessionData {
+    SessionData();
+
+    InstrumentLibrary instrumentLibrary;
+
+    //! The folder where persistent application data shall be stored. E.g. in Windows this is the
+    //! AppData/Roaming location.
+    //! Used e.g. for storing the instrument library.
+    static QString appDataFolder();
+};
+
+extern SessionData* gSessionData; //!< global pointer to the single instance
+
+#endif // BORNAGAIN_GUI_SESSION_SESSIONDATA_H
diff --git a/GUI/Views/InstrumentWidgets/InstrumentLibraryEditor.cpp b/GUI/Views/InstrumentWidgets/InstrumentLibraryEditor.cpp
index 8b79ca538bc..318a2f6a6d8 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentLibraryEditor.cpp
+++ b/GUI/Views/InstrumentWidgets/InstrumentLibraryEditor.cpp
@@ -17,6 +17,7 @@
 #include "GUI/CommonWidgets/ItemViewOverlayButtons.h"
 #include "GUI/CommonWidgets/StyleUtils.h"
 #include "GUI/Items/InstrumentItems.h"
+#include "GUI/Session/SessionData.h"
 #include "GUI/mainwindow/mainwindow.h"
 #include "GUI/utils/ItemDelegateForHTML.h"
 #include "ui_InstrumentLibraryEditor.h"
@@ -29,7 +30,7 @@
 InstrumentLibraryEditor::InstrumentLibraryEditor(QWidget* parent)
     : QDialog(parent)
     , m_ui(new Ui::InstrumentLibraryEditor)
-    , m_treeModel(new TreeModel(this, baApp->instrumentLibrary().model()))
+    , m_treeModel(new TreeModel(this, gSessionData->instrumentLibrary.model()))
     , m_chosenItem(nullptr)
 {
     m_ui->setupUi(this);
@@ -111,8 +112,8 @@ InstrumentItem* InstrumentLibraryEditor::execChoose()
 void InstrumentLibraryEditor::execAdd(const InstrumentItem& instrumentToAdd)
 {
     const QString& newName =
-        baApp->instrumentLibrary().suggestName(instrumentToAdd.instrumentName());
-    auto addedInstrument = baApp->instrumentLibrary().add(newName, instrumentToAdd);
+        gSessionData->instrumentLibrary.suggestName(instrumentToAdd.instrumentName());
+    auto addedInstrument = gSessionData->instrumentLibrary.add(newName, instrumentToAdd);
 
     setWindowTitle("Instrument Library - Add instrument");
 
diff --git a/GUI/Views/InstrumentWidgets/InstrumentListView.cpp b/GUI/Views/InstrumentWidgets/InstrumentListView.cpp
index 652e087b93c..525bf21f5ba 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentListView.cpp
+++ b/GUI/Views/InstrumentWidgets/InstrumentListView.cpp
@@ -13,7 +13,7 @@
 //  ************************************************************************************************
 
 #include "GUI/Views/InstrumentWidgets/InstrumentListView.h"
-#include "GUI/Application/GlobalSettings.h"
+#include "GUI/Session/SessionData.h"
 #include "GUI/Items/InstrumentItems.h"
 #include "GUI/Models/Instrument/InstrumentModel.h"
 #include "GUI/Project/projectdocument.h"
@@ -220,7 +220,7 @@ void InstrumentListView::onStoreInLibrary()
 
 void InstrumentListView::onLoadFromLibrary()
 {
-    if (baApp->instrumentLibrary().isEmpty()) {
+    if (gSessionData->instrumentLibrary.isEmpty()) {
         QMessageBox::information(baWin, "Select from library",
                                  "The library does not contain instruments so far.");
         return;
diff --git a/GUI/Views/JobWidgets/JobPropertiesTableModel.h b/GUI/Views/JobWidgets/JobPropertiesTableModel.h
index e42e897aeca..29a056a9e1e 100644
--- a/GUI/Views/JobWidgets/JobPropertiesTableModel.h
+++ b/GUI/Views/JobWidgets/JobPropertiesTableModel.h
@@ -2,7 +2,7 @@
 //
 //  BornAgain: simulate and fit reflection and scattering
 //
-//! @file      GUI/Views/JobWidgets/JobPropertiesWidget.h
+//! @file      GUI/Views/JobWidgets/JobPropertiesTableModel.h
 //! @brief     Defines class JobPropertiesWidget
 //!
 //! @homepage  http://www.bornagainproject.org
-- 
GitLab