From bd58f0ecbe787775dc2408f4005b2aa5b9aa4974 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 28 Jul 2014 09:34:14 +0200
Subject: [PATCH] Refactoring in QuickSimulation

---
 .../SampleTuningDelegate.cpp                  | 17 ++++++-
 .../SimulationWidgets/SampleTuningDelegate.h  | 10 +++--
 .../SimulationWidgets/SampleTuningWidget.cpp  | 45 ++++++++++++++++++-
 .../SimulationWidgets/SampleTuningWidget.h    |  6 +++
 4 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.cpp b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.cpp
index 3b668e8afe8..10844316e2c 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.cpp
+++ b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.cpp
@@ -7,6 +7,11 @@
 #include <QMouseEvent>
 #include <QStyleOptionSlider>
 #include <QAbstractItemModel>
+#include <QRect>
+#include <QItemSelectionModel>
+#include <QHBoxLayout>
+#include <QDoubleSpinBox>
+
 #include <cmath>
 #include "ItemLink.h"
 
@@ -75,6 +80,9 @@ QWidget *SampleTuningDelegate::createEditor(QWidget *parent,
         }
 
         double value = index.model()->data(index, Qt::EditRole).toDouble();
+
+        m_current_link = index.model()->data(index, Qt::UserRole).value<ItemLink>();
+
         double sliderValue = value * m_multiplyFactor;
 
         double minValue = 0;
@@ -134,7 +142,11 @@ void SampleTuningDelegate::sliderValueChanged(int position)
 
 void SampleTuningDelegate::editorValueChanged(double value)
 {
-    qDebug() << "XXXX: new value: " << value;
+    qDebug() << "SampleTuningDelegate::editorValueChanged() -> new value: " << value;
+    if(m_current_link.getItem()) {
+        qDebug() << "SampleTuningDelegate::editorValueChanged() -> Working on item " << m_current_link.getItem()->modelType() << m_current_link.getPropertyName();
+        m_current_link.getItem()->setRegisteredProperty(m_current_link.getPropertyName(), m_valueBox->value());
+    }
 }
 
 void SampleTuningDelegate::setEditorData(QWidget *editor,
@@ -159,7 +171,8 @@ void SampleTuningDelegate::setModelData(QWidget *editor,
 
         if(link.getItem() != NULL)
         {
-            link.getItem()->setRegisteredProperty(link.getName(), m_valueBox->value());
+            qDebug() << "SampleTuningDelegate::setModelData() -> setting property " << link.getPropertyName();
+            link.getItem()->setRegisteredProperty(link.getPropertyName(), m_valueBox->value());
         }
 
     } else {
diff --git a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.h b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.h
index 2d0a2d044ef..ca9baffcf2d 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.h
+++ b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningDelegate.h
@@ -2,10 +2,10 @@
 #define SAMPLETUNINGDELEGATE_H
 
 #include <QItemDelegate>
-#include <QRect>
-#include <QItemSelectionModel>
-#include <QHBoxLayout>
-#include <QDoubleSpinBox>
+#include "ItemLink.h"
+
+class QDoubleSpinBox;
+class QHBoxLayout;
 
 class SampleTuningDelegate : public QItemDelegate
 {
@@ -28,6 +28,7 @@ public:
     void setModelData(QWidget *editor, QAbstractItemModel *model,
                       const QModelIndex &index) const;
 
+
 private slots:
     void sliderValueChanged(int position);
     void editorValueChanged(double value);
@@ -39,6 +40,7 @@ private:
     mutable QWidget *m_contentWidget;
     mutable QHBoxLayout * m_contentLayout;
     double m_multiplyFactor;
+    mutable ItemLink m_current_link;
 };
 
 #endif //SAMPLETUNINGDELEGATE_H
diff --git a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp
index 7869c3801bc..5fca810423f 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp
+++ b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.cpp
@@ -15,10 +15,13 @@ SampleTuningWidget::SampleTuningWidget(SampleModel *sampleModel, InstrumentModel
     , m_parameterModel(0)
     , m_treeView(0)
     , m_delegate(new SampleTuningDelegate(1))
-    , m_sampleModel(sampleModel)
-    , m_instrumentModel(instrumentModel)
+    , m_sampleModel(0)
+    , m_instrumentModel(0)
 {
 
+    setSampleModel(sampleModel);
+    setInstrumentModel(instrumentModel);
+
     //generate Tree View
     m_treeView = new QTreeView(this);
     //treeView->setModel(model);
@@ -258,6 +261,8 @@ void SampleTuningWidget::updateTreeView(const QString &instrument, const QString
 
         delete m_parameterModel;
         m_parameterModel = createParameterModel();
+        //connect(m_parameterModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(onModelChanged(QModelIndex,QModelIndex)));
+
 
         m_treeView->setModel(m_parameterModel);
         m_treeView->expandAll();
@@ -265,6 +270,42 @@ void SampleTuningWidget::updateTreeView(const QString &instrument, const QString
 }
 
 
+void SampleTuningWidget::setSampleModel(SampleModel *sampleModel)
+{
+    Q_ASSERT(sampleModel);
+    if(m_sampleModel != sampleModel) {
+
+        if(m_sampleModel)
+            disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(onModelChanged(QModelIndex,QModelIndex)));
+
+        m_sampleModel = sampleModel;
+        connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(onModelChanged(QModelIndex,QModelIndex)));
+    }
+}
+
+
+void SampleTuningWidget::setInstrumentModel(InstrumentModel *instrumentModel)
+{
+    Q_ASSERT(instrumentModel);
+    if(m_instrumentModel != instrumentModel) {
+
+        if(m_instrumentModel)
+            disconnect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(onModelChanged(QModelIndex,QModelIndex)));
+
+        m_instrumentModel = instrumentModel;
+        connect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(onModelChanged(QModelIndex,QModelIndex)));
+    }
+
+}
+
+
+void SampleTuningWidget::onModelChanged(const QModelIndex & /* first */, const QModelIndex & /* second */)
+{
+    qDebug() << "SampleTuningWidget::onModelChanged()";
+
+}
+
+
 QStandardItemModel *SampleTuningWidget::createParameterModel()
 {
     QStandardItemModel *result(0);
diff --git a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h
index 8168751cd5c..e3ad6a86b84 100644
--- a/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h
+++ b/GUI/coregui/Views/Components/SimulationWidgets/SampleTuningWidget.h
@@ -24,6 +24,12 @@ public:
     SampleTuningWidget(SampleModel *sampleModel, InstrumentModel *instrumentModel, QWidget *parent = 0);
     void updateTreeView(const QString &instrument, const QString &sample);
 
+    void setSampleModel(SampleModel *sampleModel);
+
+    void setInstrumentModel(InstrumentModel *instrumentModel);
+
+public slots:
+    void onModelChanged(const QModelIndex &first, const QModelIndex &second);
 
 private:
     //QStandardItemModel *getItemModelFromSessionModel();
-- 
GitLab