From 8b11741cdbfe7a8e3896970614f45cab88e9f8f2 Mon Sep 17 00:00:00 2001
From: Tobias Knopff <t.knopff@fz-juelich.de>
Date: Wed, 6 Oct 2021 10:49:16 +0200
Subject: [PATCH] More elegant implementation of the change communication
 regarding instrument view

---
 .../InstrumentWidgets/InstrumentEditor.cpp    |  4 +-
 .../InstrumentWidgets/InstrumentEditor.h      |  3 --
 .../InstrumentWidgets/InstrumentListModel.cpp | 38 ++++++++++++++++++-
 .../InstrumentWidgets/InstrumentListModel.h   |  5 +++
 .../InstrumentWidgets/InstrumentListView.cpp  |  5 ---
 .../InstrumentWidgets/InstrumentListView.h    |  3 --
 .../InstrumentWidgets/InstrumentView.cpp      |  3 --
 7 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp b/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp
index ff621958d11..4bd008392d9 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp
+++ b/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp
@@ -131,8 +131,6 @@ void InstrumentEditor::setItem(InstrumentItem* item)
 
 void InstrumentEditor::onInstrumentNameChanged()
 {
-    if (m_item) {
+    if (m_item)
         m_item->setItemName(m_nameLineEdit->text());
-        emit instrumentNameChanged();
-    }
 }
diff --git a/GUI/Views/InstrumentWidgets/InstrumentEditor.h b/GUI/Views/InstrumentWidgets/InstrumentEditor.h
index 291eb5dd1dd..c60a0857cbd 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentEditor.h
+++ b/GUI/Views/InstrumentWidgets/InstrumentEditor.h
@@ -40,9 +40,6 @@ public:
 
     void setItem(InstrumentItem* instrument);
 
-signals:
-    void instrumentNameChanged();
-
 private slots:
     void onInstrumentNameChanged();
 
diff --git a/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp b/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp
index df58d282c71..c152878cba0 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp
+++ b/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp
@@ -66,6 +66,15 @@ InstrumentListModel::InstrumentListModel(InstrumentModel* instruments, QObject*
     m_specularIcon.addPixmap(QPixmap(":/images/specular_instrument_shaded.svg"), QIcon::Normal);
     m_depthProbeIcon.addPixmap(QPixmap(":/images/depth_instrument.svg"), QIcon::Selected);
     m_depthProbeIcon.addPixmap(QPixmap(":/images/depth_instrument_shaded.svg"), QIcon::Normal);
+
+    for (InstrumentItem* instrument : m_instruments->instrumentItems())
+        attachToInstrument(instrument);
+}
+
+InstrumentListModel::~InstrumentListModel()
+{
+    for (InstrumentItem* instrument : m_instruments->instrumentItems())
+        detachFromInstrument(instrument);
 }
 
 int InstrumentListModel::rowCount(const QModelIndex&) const
@@ -129,7 +138,9 @@ QModelIndex InstrumentListModel::addNewDepthProbeInstrument()
 void InstrumentListModel::removeInstrument(const QModelIndex& index)
 {
     beginRemoveRows(QModelIndex(), index.row(), index.row());
-    m_instruments->removeInstrument(instrumentForIndex(index));
+    InstrumentItem* instrument = instrumentForIndex(index);
+    detachFromInstrument(instrument);
+    m_instruments->removeInstrument(instrument);
     endRemoveRows();
 }
 
@@ -149,6 +160,7 @@ QModelIndex InstrumentListModel::copyInstrument(const InstrumentItem* source)
     beginInsertRows(QModelIndex(), row, row);
     InstrumentItem* copy = m_instruments->insertCopy(*source);
     copy->setItemName(copyName);
+    attachToInstrument(copy);
     endInsertRows();
 
     return m_instruments->indexOfItem(copy);
@@ -166,7 +178,31 @@ template <class Instrument> QModelIndex InstrumentListModel::addNewInstrument()
     beginInsertRows(QModelIndex(), row, row);
     Instrument* instrument = m_instruments->insertItem<Instrument>();
     instrument->setItemName(name);
+    attachToInstrument(instrument);
     endInsertRows();
 
     return m_instruments->indexOfItem(instrument);
 }
+
+void InstrumentListModel::attachToInstrument(InstrumentItem* instrument)
+{
+    instrument->mapper()->setOnPropertyChange
+        (std::bind(&InstrumentListModel::notifyInstrumentPropertyChange, this, instrument,
+                   std::placeholders::_1), this);
+}
+
+void InstrumentListModel::detachFromInstrument(InstrumentItem* instrument)
+{
+    instrument->mapper()->unsubscribe(this);
+}
+
+void InstrumentListModel::notifyInstrumentPropertyChange(InstrumentItem* instrument,
+                                                         const QString& property)
+{
+    if (SessionItem::isItemNamePropertyName(property)) {
+        QVector<InstrumentItem*> instruments = m_instruments->instrumentItems();
+        int i = instruments.indexOf(instrument);
+        if (i != -1)
+            emit dataChanged(index(i,0),index(i,0));
+    }
+}
diff --git a/GUI/Views/InstrumentWidgets/InstrumentListModel.h b/GUI/Views/InstrumentWidgets/InstrumentListModel.h
index de0b10658b8..182f381bdf5 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentListModel.h
+++ b/GUI/Views/InstrumentWidgets/InstrumentListModel.h
@@ -26,6 +26,7 @@ class InstrumentListModel : public QAbstractListModel {
 
 public:
     InstrumentListModel(InstrumentModel* instruments, QObject* parent = nullptr);
+    ~InstrumentListModel();
 
     virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
 
@@ -50,6 +51,10 @@ public:
 private:
     template <class Instrument> QModelIndex addNewInstrument();
 
+    void attachToInstrument(InstrumentItem* item);
+    void detachFromInstrument(InstrumentItem* item);
+    void notifyInstrumentPropertyChange(InstrumentItem* instrument, const QString& property);
+
 private:
     InstrumentModel* m_instruments;
 
diff --git a/GUI/Views/InstrumentWidgets/InstrumentListView.cpp b/GUI/Views/InstrumentWidgets/InstrumentListView.cpp
index 04545dc1c6c..e9c64295323 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentListView.cpp
+++ b/GUI/Views/InstrumentWidgets/InstrumentListView.cpp
@@ -146,11 +146,6 @@ QList<QAction*> InstrumentListView::toolbarActions() const
             m_loadFromLibraryAction};
 }
 
-void InstrumentListView::updateSelectedInstrument()
-{
-    update();
-}
-
 void InstrumentListView::onItemSelectionChanged()
 {
     updateActions();
diff --git a/GUI/Views/InstrumentWidgets/InstrumentListView.h b/GUI/Views/InstrumentWidgets/InstrumentListView.h
index 3619741f41a..917c07163e6 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentListView.h
+++ b/GUI/Views/InstrumentWidgets/InstrumentListView.h
@@ -41,9 +41,6 @@ public:
 signals:
     void instrumentSelected(InstrumentItem* instrument);
 
-public slots:
-    void updateSelectedInstrument();
-
 private slots:
     void onItemSelectionChanged();
     void onNewGisas();
diff --git a/GUI/Views/InstrumentWidgets/InstrumentView.cpp b/GUI/Views/InstrumentWidgets/InstrumentView.cpp
index f2827c8dbcd..ece40599aa1 100644
--- a/GUI/Views/InstrumentWidgets/InstrumentView.cpp
+++ b/GUI/Views/InstrumentWidgets/InstrumentView.cpp
@@ -44,9 +44,6 @@ InstrumentView::InstrumentView(QWidget* parent, ProjectDocument* document)
     connect(m_instrumentListView, &InstrumentListView::instrumentSelected, this,
             &InstrumentView::onInstrumentSelected);
 
-    connect(m_instrumentEditor, &InstrumentEditor::instrumentNameChanged, m_instrumentListView,
-            &InstrumentListView::updateSelectedInstrument);
-
     onInstrumentSelected(nullptr);
 }
 
-- 
GitLab