From 1c335f691ff3d27a53df4c60848829c1fd4fe804 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 20 Nov 2017 17:36:48 +0100
Subject: [PATCH] ComponentFlatView now regenerates all widgets on group
 property change

---
 .../PropertyEditor/ComponentFlatView.cpp      | 37 +++++++++++++++++++
 .../Views/PropertyEditor/ComponentFlatView.h  |  9 +++++
 .../PropertyEditor/PropertyWidgetItem.cpp     |  6 ++-
 .../PropertyEditor/TestComponentView.cpp      |  4 +-
 Tests/UnitTests/GUI/TestComponentUtils.h      | 23 ++++++++++++
 5 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
index fd477d43a8d..8737c1b7d22 100644
--- a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
+++ b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
@@ -30,6 +30,8 @@ ComponentFlatView::ComponentFlatView(QWidget* parent)
     : QWidget(parent)
     , m_mainLayout(new QVBoxLayout)
     , m_gridLayout(nullptr)
+    , m_currentItem(nullptr)
+    , m_model(nullptr)
 {
     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
@@ -45,6 +47,40 @@ void ComponentFlatView::addItemProperties(SessionItem* item)
 {
     Q_ASSERT(item);
 
+    m_currentItem = item;
+    setModel(m_currentItem->model());
+
+    updateItemProperties(m_currentItem);
+}
+
+void ComponentFlatView::setModel(SessionModel* model)
+{
+    if (m_model) {
+        disconnect(m_model, &SessionModel::dataChanged, this, &ComponentFlatView::onDataChanged);
+
+    }
+
+    m_model = model;
+
+    if (m_model) {
+        connect(m_model, &SessionModel::dataChanged, this, &ComponentFlatView::onDataChanged);
+    }
+
+}
+
+void ComponentFlatView::onDataChanged(const QModelIndex& topLeft, const QModelIndex&,
+                                      const QVector<int>& )
+{
+    SessionItem *item = m_model->itemForIndex(topLeft);
+    Q_ASSERT(item);
+    if (item->modelType() == Constants::GroupItemType)
+        updateItemProperties(m_currentItem);
+}
+
+void ComponentFlatView::updateItemProperties(SessionItem* item)
+{
+    Q_ASSERT(item);
+
     clearLayout();
 
     int nrow(0);
@@ -57,6 +93,7 @@ void ComponentFlatView::addItemProperties(SessionItem* item)
         widget->addToGrid(m_gridLayout, ++nrow);
         m_widgetItems.push_back(widget);
     }
+
 }
 
 void ComponentFlatView::clearLayout()
diff --git a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h
index ab83ed6c818..6468081d248 100644
--- a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h
+++ b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h
@@ -21,6 +21,7 @@
 #include <QWidget>
 
 class SessionItem;
+class SessionModel;
 class QGridLayout;
 class QBoxLayout;
 class PropertyWidgetItem;
@@ -37,7 +38,13 @@ public:
 
     void addItemProperties(SessionItem* item);
 
+    void setModel(SessionModel* model);
+
+public slots:
+    void onDataChanged(const QModelIndex &topLeft, const QModelIndex &, const QVector<int> &);
+
 private:
+    void updateItemProperties(SessionItem* item);
     void clearLayout();
     void initGridLayout();
     PropertyWidgetItem* createWidget(SessionItem* item);
@@ -45,6 +52,8 @@ private:
     QBoxLayout* m_mainLayout;
     QGridLayout* m_gridLayout;
     QVector<PropertyWidgetItem*> m_widgetItems;
+    SessionItem* m_currentItem;
+    SessionModel* m_model;
 };
 
 #endif
diff --git a/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp b/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
index 71fd0570e58..575ee63f778 100644
--- a/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
+++ b/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
@@ -37,12 +37,14 @@ PropertyWidgetItem::PropertyWidgetItem(QWidget* parent)
 PropertyWidgetItem::~PropertyWidgetItem()
 {
     delete m_label;
-    delete m_editor;
+    // if editor's action leads to deletion of the editor itself, we have to give him chance to
+    // send all signals
+    m_editor->deleteLater();
 }
 
 void PropertyWidgetItem::setItemEditor(SessionItem* item, QWidget* editor)
 {
-    delete m_editor;
+    Q_ASSERT(m_editor == nullptr);
     m_editor = editor;
 
     m_label->setText(item->displayName());
diff --git a/GUI/coregui/Views/PropertyEditor/TestComponentView.cpp b/GUI/coregui/Views/PropertyEditor/TestComponentView.cpp
index 49891d1ebc4..733067e1242 100644
--- a/GUI/coregui/Views/PropertyEditor/TestComponentView.cpp
+++ b/GUI/coregui/Views/PropertyEditor/TestComponentView.cpp
@@ -147,8 +147,8 @@ void TestComponentView::onSelectionChanged(const QItemSelection& selected, const
 
         auto item = m_sourceModel->itemForIndex(indices.front());
         m_obsoleteEditor->setItem(item, item->modelType());
-//        m_obsoleteBoxEditor->clearEditor();
-//        m_obsoleteBoxEditor->addPropertyItems(item);
+        m_obsoleteBoxEditor->clearEditor();
+        m_obsoleteBoxEditor->addPropertyItems(item);
 
         m_componentFlat->addItemProperties(item);
     }
diff --git a/Tests/UnitTests/GUI/TestComponentUtils.h b/Tests/UnitTests/GUI/TestComponentUtils.h
index b2b075f897e..eb913a25c26 100644
--- a/Tests/UnitTests/GUI/TestComponentUtils.h
+++ b/Tests/UnitTests/GUI/TestComponentUtils.h
@@ -14,6 +14,7 @@ public:
 
 private slots:
     void test_componentItems();
+    void test_componentItemsFFChange();
 };
 
 //! Testing component items of particle item.
@@ -38,3 +39,25 @@ inline void TestComponentUtils::test_componentItems()
     QCOMPARE(itemList.size(), 6);
     QCOMPARE(itemList, expectedList);
 }
+
+inline void TestComponentUtils::test_componentItemsFFChange()
+{
+    SessionModel model("TestModel");
+
+    SessionItem* particle = model.insertNewItem(Constants::ParticleType);
+    SessionItem* group = particle->getItem(ParticleItem::P_FORM_FACTOR);
+
+    particle->setGroupProperty(ParticleItem::P_FORM_FACTOR, Constants::FullSphereType);
+    SessionItem* sphereItem = particle->getGroupItem(ParticleItem::P_FORM_FACTOR);
+
+    QList<SessionItem*> expectedList = QList<SessionItem*> ()
+            << group
+            << sphereItem->getItem(FullSphereItem::P_RADIUS)
+            << particle->getItem(ParticleItem::P_MATERIAL)
+            << particle->getItem(ParticleItem::P_ABUNDANCE)
+            << particle->getItem(ParticleItem::P_POSITION);
+
+    auto itemList = ComponentUtils::componentItems(*particle);
+    QCOMPARE(itemList.size(), 5);
+    QCOMPARE(itemList, expectedList);
+}
-- 
GitLab