From e8114b124253196f613d84f9242be47c1f2afa64 Mon Sep 17 00:00:00 2001
From: Joachim Wuttke <j.wuttke@fz-juelich.de>
Date: Sun, 18 Feb 2024 09:13:15 +0100
Subject: [PATCH] InstrumentsQModel::copyInstrument() now directly operating on
 current instrument

---
 GUI/View/List/InstrumentsQListView.cpp | 34 +++++---------------------
 GUI/View/List/InstrumentsQListView.h   |  3 ---
 GUI/View/List/InstrumentsQModel.cpp    | 30 ++++++++++-------------
 GUI/View/List/InstrumentsQModel.h      |  8 +++---
 4 files changed, 22 insertions(+), 53 deletions(-)

diff --git a/GUI/View/List/InstrumentsQListView.cpp b/GUI/View/List/InstrumentsQListView.cpp
index 2f5918339e6..583c73fbc49 100644
--- a/GUI/View/List/InstrumentsQListView.cpp
+++ b/GUI/View/List/InstrumentsQListView.cpp
@@ -49,10 +49,11 @@ InstrumentsQListView::InstrumentsQListView()
     connect(gActions->new_depthprobe_instrument, &QAction::triggered, this,
             &InstrumentsQListView::onNewDepthprobe);
 
-    connect(gActions->remove_instrument, &QAction::triggered, this,
-            &InstrumentsQListView::onRemove);
+    connect(gActions->remove_instrument, &QAction::triggered, m_model,
+            &InstrumentsQModel::removeInstrument);
 
-    connect(gActions->copy_instrument, &QAction::triggered, this, &InstrumentsQListView::onCopy);
+    connect(gActions->copy_instrument, &QAction::triggered, m_model,
+            &InstrumentsQModel::copyInstrument);
 
     connect(gActions->store_in_library_instrument, &QAction::triggered, this,
             &InstrumentsQListView::onStoreInLibrary);
@@ -95,27 +96,6 @@ void InstrumentsQListView::onNewDepthprobe()
     m_model->addNewDepthprobeInstrument();
 }
 
-//! Removes currently selected instrument.
-void InstrumentsQListView::onRemove()
-{
-    QModelIndexList indexes = selectionModel()->selectedIndexes();
-    if (!indexes.empty()) {
-        m_model->removeInstrument(indexes.front());
-
-        ensureItemSelected();
-    }
-}
-
-//! Makes a copy of the currently selected instrument.
-void InstrumentsQListView::onCopy()
-{
-    QModelIndexList indexes = selectionModel()->selectedIndexes();
-    if (!indexes.empty()) {
-        QModelIndex idx = m_model->copyInstrument(indexes.front());
-        selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
-    }
-}
-
 void InstrumentsQListView::onStoreInLibrary() const
 {
     const InstrumentItem* t = gDoc->instruments()->currentItem();
@@ -144,10 +124,8 @@ void InstrumentsQListView::onLoadFromLibrary()
         return;
 
     try {
-        if (InstrumentItem* ii = InstrumentXML::load(fname)) {
-            QModelIndex idx = m_model->copyInstrument(ii);
-            selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
-        }
+        if (InstrumentItem* t = InstrumentXML::load(fname))
+            m_model->pushInstrument(t);
     } catch (const std::exception& ex) {
         QMessageBox(QMessageBox::Warning, "BornAgain: failed loading", QString(ex.what()),
                     QMessageBox::Ok, nullptr)
diff --git a/GUI/View/List/InstrumentsQListView.h b/GUI/View/List/InstrumentsQListView.h
index 064b50b3d28..0274ca1f537 100644
--- a/GUI/View/List/InstrumentsQListView.h
+++ b/GUI/View/List/InstrumentsQListView.h
@@ -39,9 +39,6 @@ private slots:
     void onNewSpecular();
     void onNewDepthprobe();
 
-    void onRemove();
-    void onCopy();
-
     void onStoreInLibrary() const;
     void onLoadFromLibrary();
 
diff --git a/GUI/View/List/InstrumentsQModel.cpp b/GUI/View/List/InstrumentsQModel.cpp
index 3b6079c1a16..0ff2c55d03b 100644
--- a/GUI/View/List/InstrumentsQModel.cpp
+++ b/GUI/View/List/InstrumentsQModel.cpp
@@ -82,31 +82,27 @@ QModelIndex InstrumentsQModel::addNewDepthprobeInstrument()
     return pushInstrument(t);
 }
 
-void InstrumentsQModel::removeInstrument(const QModelIndex& index)
+void InstrumentsQModel::removeInstrument()
 {
-    beginRemoveRows(QModelIndex(), index.row(), index.row());
-    const InstrumentItem* instrument = instrumentItemForIndex(index);
-    gDoc->instrumentsModifier()->removeInstrument(instrument);
-    endRemoveRows();
-}
-
-QModelIndex InstrumentsQModel::copyInstrument(const QModelIndex& source)
-{
-    const InstrumentItem* srcInstr = instrumentItemForIndex(source);
-    ASSERT(srcInstr);
+    InstrumentsSet* set = gDoc->instrumentsModifier();
+    const InstrumentItem* t = set->currentItem();
 
-    return copyInstrument(srcInstr);
+    const int row = set->index_of(t);
+    beginInsertRows({}, row, row);
+    set->removeInstrument(t);
+    endRemoveRows();
 }
 
-QModelIndex InstrumentsQModel::copyInstrument(const InstrumentItem* source)
+QModelIndex InstrumentsQModel::copyInstrument() // TODO implement using pushInstrument
 {
     InstrumentsSet* set = gDoc->instrumentsModifier();
-    const QString copyName = set->suggestInstrumentName(source->instrumentName());
-    const int row = set->instrumentItems().size();
+    const InstrumentItem* t = set->currentItem();
+    const QString copyName = set->suggestInstrumentName(t->instrumentName());
 
+    const int row = set->instrumentItems().size();
     beginInsertRows({}, row, row);
-    InstrumentItem* copy = set->insertItemCopy(*source);
-    copy->setInstrumentName(copyName);
+    InstrumentItem* t2 = set->insertItemCopy(*t);
+    t2->setInstrumentName(copyName);
     emit set->instrumentAddedOrRemoved();
     endInsertRows();
 
diff --git a/GUI/View/List/InstrumentsQModel.h b/GUI/View/List/InstrumentsQModel.h
index fd8e4acbebe..f54c4767ad5 100644
--- a/GUI/View/List/InstrumentsQModel.h
+++ b/GUI/View/List/InstrumentsQModel.h
@@ -36,13 +36,11 @@ public:
     QModelIndex addNewSpecularInstrument();
     QModelIndex addNewDepthprobeInstrument();
 
-    void removeInstrument(const QModelIndex& index);
-
-    QModelIndex copyInstrument(const QModelIndex& source);
-    QModelIndex copyInstrument(const InstrumentItem* source);
+    void removeInstrument();
+    QModelIndex copyInstrument();
+    QModelIndex pushInstrument(InstrumentItem*);
 
 private:
-    QModelIndex pushInstrument(InstrumentItem*);
     void onInstrumentNameChanged(const InstrumentItem* instrument);
 };
 
-- 
GitLab