From 9184a8e966625f0d5548af5fe10511d4822d8eb8 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Fri, 10 Dec 2021 11:51:59 +0100
Subject: [PATCH] reduce SessionModel usage

---
 GUI/Model/Data/RealDataModel.cpp         |  8 ++++++++
 GUI/Model/Data/RealDataModel.h           |  2 ++
 GUI/Model/From/SampleListModel.cpp       |  4 ++--
 GUI/Model/Instrument/InstrumentModel.cpp | 11 +++++++++--
 GUI/Model/Instrument/InstrumentModel.h   |  2 ++
 GUI/Model/Sample/SampleModel.cpp         |  8 ++++++++
 GUI/Model/Sample/SampleModel.h           |  2 ++
 GUI/View/Toplevel/SimulationView.cpp     |  6 +++---
 8 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/GUI/Model/Data/RealDataModel.cpp b/GUI/Model/Data/RealDataModel.cpp
index d5d4ffd694e..54554179a72 100644
--- a/GUI/Model/Data/RealDataModel.cpp
+++ b/GUI/Model/Data/RealDataModel.cpp
@@ -98,6 +98,14 @@ void RealDataModel::remove(RealDataItem* item)
     removeItem(item);
 }
 
+QStringList RealDataModel::realDataNames() const
+{
+    QStringList result;
+    for (auto item : realDataItems())
+        result.append(item->itemName());
+    return result;
+}
+
 void RealDataModel::onRowsChange(const QModelIndex& parent, int, int)
 {
     // valid parent means not a data (which is top level item) but something below
diff --git a/GUI/Model/Data/RealDataModel.h b/GUI/Model/Data/RealDataModel.h
index 9356dbc065d..c8df8eaa248 100644
--- a/GUI/Model/Data/RealDataModel.h
+++ b/GUI/Model/Data/RealDataModel.h
@@ -41,6 +41,8 @@ public:
 
     void remove(RealDataItem* item);
 
+    QStringList realDataNames() const;
+
 signals:
     void realDataAddedOrRemoved();
 
diff --git a/GUI/Model/From/SampleListModel.cpp b/GUI/Model/From/SampleListModel.cpp
index 2be7e27509e..49ea321ab7f 100644
--- a/GUI/Model/From/SampleListModel.cpp
+++ b/GUI/Model/From/SampleListModel.cpp
@@ -126,12 +126,12 @@ void SampleListModel::removeSample(MultiLayerItem* item)
 
 QModelIndex SampleListModel::createSample()
 {
-    using namespace GUI::Model::ItemUtils;
+    const QStringList existingNames = m_sampleModel->multiLayerNames();
 
     const int row = m_sampleModel->multiLayerItems().size();
     beginInsertRows(QModelIndex(), row, row);
     auto* multilayer_item = m_sampleModel->addMultiLayer();
-    multilayer_item->setSampleName(suggestName(topItemNames(m_sampleModel), "Sample"));
+    multilayer_item->setSampleName(GUI::Model::ItemUtils::suggestName(existingNames, "Sample"));
     endInsertRows();
     return indexForItem(multilayer_item);
 }
diff --git a/GUI/Model/Instrument/InstrumentModel.cpp b/GUI/Model/Instrument/InstrumentModel.cpp
index 3ccfd10f486..cac494dda77 100644
--- a/GUI/Model/Instrument/InstrumentModel.cpp
+++ b/GUI/Model/Instrument/InstrumentModel.cpp
@@ -100,10 +100,17 @@ QVector<InstrumentItem*> InstrumentModel::instrumentItems() const
     return topItems<InstrumentItem>();
 }
 
+QStringList InstrumentModel::instrumentNames() const
+{
+    QStringList existingNames;
+    for (const auto* item : instrumentItems())
+        existingNames << item->instrumentName();
+    return existingNames;
+}
+
 QString InstrumentModel::suggestInstrumentName(const QString& baseName) const
 {
-    using namespace GUI::Model::ItemUtils;
-    return suggestName(topItemNames(this), baseName);
+    return GUI::Model::ItemUtils::suggestName(instrumentNames(), baseName);
 }
 
 QVector<InstrumentItem*>
diff --git a/GUI/Model/Instrument/InstrumentModel.h b/GUI/Model/Instrument/InstrumentModel.h
index 66e71cd7fdd..7dd9a8cdd80 100644
--- a/GUI/Model/Instrument/InstrumentModel.h
+++ b/GUI/Model/Instrument/InstrumentModel.h
@@ -49,6 +49,8 @@ public:
     QVector<InstrumentItem*>
     instrumentItems(std::function<bool(const InstrumentItem*)> accept) const;
 
+    QStringList instrumentNames() const;
+
 signals:
     void instrumentAddedOrRemoved();
     void instrumentNameChanged(const InstrumentItem* instrument);
diff --git a/GUI/Model/Sample/SampleModel.cpp b/GUI/Model/Sample/SampleModel.cpp
index 001bc665d0f..57c09de0089 100644
--- a/GUI/Model/Sample/SampleModel.cpp
+++ b/GUI/Model/Sample/SampleModel.cpp
@@ -60,6 +60,14 @@ void SampleModel::removeMultiLayer(MultiLayerItem* item)
     emit sampleAddedOrRemoved();
 }
 
+QStringList SampleModel::multiLayerNames() const
+{
+    QStringList existingNames;
+    for (const auto* item : multiLayerItems())
+        existingNames << item->sampleName();
+    return existingNames;
+}
+
 void SampleModel::onDataChanged(const QModelIndex& index, const QModelIndex&)
 {
     // check whether it's an item, then emit the relevant signal
diff --git a/GUI/Model/Sample/SampleModel.h b/GUI/Model/Sample/SampleModel.h
index d310ed74fcf..335e8cfe96e 100644
--- a/GUI/Model/Sample/SampleModel.h
+++ b/GUI/Model/Sample/SampleModel.h
@@ -38,6 +38,8 @@ public:
     MultiLayerItem* addMultiLayer();
     void removeMultiLayer(MultiLayerItem* item);
 
+    QStringList multiLayerNames() const;
+
 signals:
     //! This signal is emitted if any item data changed (compare to
     //! QAbstractItemModel::dataChanged).
diff --git a/GUI/View/Toplevel/SimulationView.cpp b/GUI/View/Toplevel/SimulationView.cpp
index d9e39265eed..01ee666ee71 100644
--- a/GUI/View/Toplevel/SimulationView.cpp
+++ b/GUI/View/Toplevel/SimulationView.cpp
@@ -120,9 +120,9 @@ void SimulationView::writeOptionsToUI()
 
     // -- selection group
     using GUI::Model::ItemUtils::itemNames;
-    updateSelection(m_ui->instrumentCombo, itemNames(instrumentItems()));
-    updateSelection(m_ui->sampleCombo, sampleNames(multiLayerItems()));
-    updateSelection(m_ui->realDataCombo, itemNames(realDataItems()), true);
+    updateSelection(m_ui->instrumentCombo, m_document->instrumentModel()->instrumentNames());
+    updateSelection(m_ui->sampleCombo, m_document->sampleModel()->multiLayerNames());
+    updateSelection(m_ui->realDataCombo, m_document->realDataModel()->realDataNames(), true);
 
     // -- options group
     optionsItem()->runImmediately() ? m_ui->runPolicyImmediatelyRadio->setChecked(true)
-- 
GitLab