From 9784ab9fa84eaa465f8358f69632b690522da4c6 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Fri, 25 Jul 2014 12:57:02 +0200
Subject: [PATCH] Only currently selected MultiLayer is propagated into
 QuickSimulation.

---
 .../QuickSimulationWidget.cpp                 |  4 +-
 .../SimulationWidgets/QuickSimulationWidget.h |  3 +-
 .../SimulationWidgets/SampleTuningWidget.cpp  | 52 ++++++++++++++++---
 .../SimulationWidgets/SampleTuningWidget.h    | 12 +++--
 GUI/coregui/Views/SimulationView.cpp          |  4 +-
 5 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.cpp b/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.cpp
index 3b3dafca378..4c9efc1b4bd 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.cpp
+++ b/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.cpp
@@ -38,9 +38,9 @@ QuickSimulationWidget::QuickSimulationWidget(SampleModel *sampleModel, Instrumen
     setLayout(mainLayout);
 }
 
-void QuickSimulationWidget::updateViews()
+void QuickSimulationWidget::updateViews(const QString &instrument, const QString &sample)
 {
-    m_sampleTuningWidget->updateTreeView();
+    m_sampleTuningWidget->updateTreeView(instrument, sample);
 }
 
 
diff --git a/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.h b/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.h
index 5366a8ad7b5..860a3dc1a04 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.h
+++ b/GUI/coregui/Views/Components/SimulationWidgets/QuickSimulationWidget.h
@@ -2,6 +2,7 @@
 #define QUICKSIMULATIONWIDGET_H
 
 #include <QWidget>
+#include <QString>
 class SampleModel;
 class InstrumentModel;
 class SampleTuningWidget;
@@ -13,7 +14,7 @@ class QuickSimulationWidget : public QWidget
 
 public:
     QuickSimulationWidget(SampleModel *sampleModel, InstrumentModel *instrumentModel, QWidget *parent = 0);
-    void updateViews();
+    void updateViews(const QString &instrument, const QString &sample);
 
 private:
     SampleModel *m_sampleModel;
diff --git a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp
index a25a1a49db2..7869c3801bc 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp
+++ b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp
@@ -3,10 +3,13 @@
 #include "InstrumentModel.h"
 #include "PropertyAttribute.h"
 #include "SampleTuningDelegate.h"
+#include "GUIHelpers.h"
 #include "ItemLink.h"
 #include <QItemSelectionModel>
 #include <QDebug>
 
+
+
 SampleTuningWidget::SampleTuningWidget(SampleModel *sampleModel, InstrumentModel *instrumentModel, QWidget *parent)
     : QWidget(parent)
     , m_parameterModel(0)
@@ -245,24 +248,59 @@ void SampleTuningWidget::insertRowIntoItem(QStandardItem *parentItem, QString ti
 //}
 
 
-void SampleTuningWidget::updateTreeView()
+void SampleTuningWidget::updateTreeView(const QString &instrument, const QString &sample)
 {
-    m_treeView->setModel(0);
+    if(instrument != m_instrument_name || sample != m_sample_name) {
+        m_instrument_name = instrument;
+        m_sample_name = sample;
 
-    delete m_parameterModel;
-    m_parameterModel = createParameterModel();
+        m_treeView->setModel(0);
 
-    m_treeView->setModel(m_parameterModel);
-    m_treeView->expandAll();
+        delete m_parameterModel;
+        m_parameterModel = createParameterModel();
+
+        m_treeView->setModel(m_parameterModel);
+        m_treeView->expandAll();
+    }
 }
 
+
 QStandardItemModel *SampleTuningWidget::createParameterModel()
 {
     QStandardItemModel *result(0);
     result = new QStandardItemModel(this);
     result->setHorizontalHeaderItem( 0, new QStandardItem( "Property" ) );
     result->setHorizontalHeaderItem( 1, new QStandardItem( "Value" ) );
-    result->setItem(0, iterateSessionModel());
+
+    QStandardItem *multiLayerItem = new QStandardItem(m_sample_name);
+
+    QModelIndex multiLayerIndex = getMultiLayerIndex(m_sample_name);
+
+    result->setItem(0, iterateSessionModel(multiLayerIndex, multiLayerItem));
+    return result;
+}
+
+
+//! returns parent index of MultiLayer with given name
+QModelIndex SampleTuningWidget::getMultiLayerIndex(const QString &name)
+{
+    Q_ASSERT(m_sampleModel);
+
+    QModelIndex result;
+    for( int i_row = 0; i_row < m_sampleModel->rowCount( QModelIndex()); ++i_row) {
+         QModelIndex itemIndex = m_sampleModel->index( i_row, 0, QModelIndex() );
+
+         if (ParameterizedItem *item = m_sampleModel->itemForIndex(itemIndex)){
+             if(item->modelType() == Constants::MultiLayerType && item->itemName() == name) {
+                 result = itemIndex;
+             }
+         }
+    }
+
+    if(!result.isValid()) {
+        throw GUIHelpers::Error("SampleTuningWidget::getParentSampleIndex() -> Can't find the sample with given name" + name);
+    }
+
     return result;
 }
 
diff --git a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h
index 84a8b7275ea..8168751cd5c 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h
+++ b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h
@@ -22,23 +22,29 @@ class SampleTuningWidget : public QWidget
 
 public:
     SampleTuningWidget(SampleModel *sampleModel, InstrumentModel *instrumentModel, QWidget *parent = 0);
-    void updateTreeView();
+    void updateTreeView(const QString &instrument, const QString &sample);
 
 
 private:
     //QStandardItemModel *getItemModelFromSessionModel();
     // QStandardItemModel *getTestItemModel();
-    QStandardItem *iterateSessionModel(const QModelIndex &parentIndex = QModelIndex(), QStandardItem *parentItem = NULL);
-    void insertRowIntoItem(QStandardItem *parentItem, QStandardItem *childTitleItem, QStandardItem *childValueItem = NULL);
+    QStandardItem *iterateSessionModel(const QModelIndex &parentIndex = QModelIndex(), QStandardItem *parentItem = 0);
+    void insertRowIntoItem(QStandardItem *parentItem, QStandardItem *childTitleItem, QStandardItem *childValueItem = 0);
     void insertRowIntoItem(QStandardItem *parentItem, QString title, QVariant value, ParameterizedItem *parameterizedItem);
 
     QStandardItemModel *createParameterModel();
 
+    QModelIndex getMultiLayerIndex(const QString &name);
+
     QStandardItemModel *m_parameterModel;
     QTreeView *m_treeView;
     SampleTuningDelegate *m_delegate;
     SampleModel *m_sampleModel;
     InstrumentModel *m_instrumentModel;
+
+    QString m_instrument_name;
+    QString m_sample_name;
+
 };
 
 
diff --git a/GUI/coregui/Views/SimulationView.cpp b/GUI/coregui/Views/SimulationView.cpp
index fca409b7989..02b7b91d864 100644
--- a/GUI/coregui/Views/SimulationView.cpp
+++ b/GUI/coregui/Views/SimulationView.cpp
@@ -39,7 +39,7 @@ SimulationView::SimulationView(MainWindow *mainWindow)
 void SimulationView::updateSimulationViewElements()
 {
     m_simulationSetupWidget->updateViewElements();
-    m_quickSimulationWidget->updateViews();
+    m_quickSimulationWidget->updateViews(m_simulationSetupWidget->getInstrumentSelection(), m_simulationSetupWidget->getSampleSelection());
 }
 
 void SimulationView::onChangeTabWidget(int index)
@@ -50,6 +50,6 @@ void SimulationView::onChangeTabWidget(int index)
     }
     else if(index == QuickSimulationTab)
     {
-        m_quickSimulationWidget->updateViews();
+        m_quickSimulationWidget->updateViews(m_simulationSetupWidget->getInstrumentSelection(), m_simulationSetupWidget->getSampleSelection());
     }
 }
-- 
GitLab