From b4b5c40cc5f3ed33d1922d9ff76d084e4dde981e Mon Sep 17 00:00:00 2001
From: Robert Applin <robert.applin@stfc.ac.uk>
Date: Wed, 17 Mar 2021 16:13:56 +0000
Subject: [PATCH] Refs #30686. Use template when updating x ranges.

---
 .../Common/FitScriptGeneratorDataTable.h      |  1 +
 .../Common/FitScriptGeneratorPresenter.h      | 15 +++--
 .../Common/FitScriptGeneratorView.h           |  1 +
 .../Common/IFitScriptGeneratorView.h          |  1 +
 .../src/FitScriptGeneratorDataTable.cpp       |  8 +++
 .../src/FitScriptGeneratorPresenter.cpp       | 65 ++++++++++---------
 .../common/src/FitScriptGeneratorView.cpp     |  4 ++
 7 files changed, 58 insertions(+), 37 deletions(-)

diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorDataTable.h b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorDataTable.h
index 1f74616a2f4..5106be006cf 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorDataTable.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorDataTable.h
@@ -57,6 +57,7 @@ public:
 
   [[nodiscard]] std::vector<FitDomainIndex> allRows() const;
   [[nodiscard]] std::vector<FitDomainIndex> selectedRows() const;
+  [[nodiscard]] FitDomainIndex currentRow() const;
 
   bool hasLoadedData() const;
 
diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h
index c5cfca0d43e..3cea1f795a9 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h
@@ -82,23 +82,28 @@ private:
                     WorkspaceIndex workspaceIndex, double startX, double endX);
 
   void updateStartX(std::string const &workspaceName,
-                    WorkspaceIndex workspaceIndex, double startX);
+                    WorkspaceIndex workspaceIndex, FitDomainIndex domainIndex);
   void updateEndX(std::string const &workspaceName,
-                  WorkspaceIndex workspaceIndex, double endX);
+                  WorkspaceIndex workspaceIndex, FitDomainIndex domainIndex);
 
   void updateParameterTie(std::string const &workspaceName,
                           WorkspaceIndex workspaceIndex,
                           std::string const &parameter, std::string const &tie);
 
-  [[nodiscard]] std::vector<FitDomainIndex> getRowIndices() const;
-
-  void checkForWarningMessages();
+  template <void (FitScriptGeneratorPresenter::*func)(
+      std::string const &workspaceName, WorkspaceIndex workspaceIndex,
+      FitDomainIndex domainIndex)>
+  void updateDomainXRange();
 
   template <void (IFitScriptGeneratorModel::*func)(
       std::string const &workspaceName, WorkspaceIndex workspaceIndex,
       std::string const &function)>
   void updateDomainFunctions(std::string const &function);
 
+  [[nodiscard]] std::vector<FitDomainIndex> getRowIndices() const;
+
+  void checkForWarningMessages();
+
   std::vector<std::string> m_warnings;
 
   IFitScriptGeneratorView *m_view;
diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorView.h b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorView.h
index 017bcac2981..47b4307987a 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorView.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorView.h
@@ -56,6 +56,7 @@ public:
 
   [[nodiscard]] std::vector<FitDomainIndex> allRows() const override;
   [[nodiscard]] std::vector<FitDomainIndex> selectedRows() const override;
+  [[nodiscard]] FitDomainIndex currentRow() const override;
 
   [[nodiscard]] bool hasLoadedData() const override;
 
diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/IFitScriptGeneratorView.h b/qt/widgets/common/inc/MantidQtWidgets/Common/IFitScriptGeneratorView.h
index 1755972e3d6..45e8774ca8d 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/IFitScriptGeneratorView.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/IFitScriptGeneratorView.h
@@ -68,6 +68,7 @@ public:
 
   [[nodiscard]] virtual std::vector<FitDomainIndex> allRows() const = 0;
   [[nodiscard]] virtual std::vector<FitDomainIndex> selectedRows() const = 0;
+  [[nodiscard]] virtual FitDomainIndex currentRow() const = 0;
 
   [[nodiscard]] virtual bool hasLoadedData() const = 0;
 
diff --git a/qt/widgets/common/src/FitScriptGeneratorDataTable.cpp b/qt/widgets/common/src/FitScriptGeneratorDataTable.cpp
index 2a45726e820..cee0a8db69b 100644
--- a/qt/widgets/common/src/FitScriptGeneratorDataTable.cpp
+++ b/qt/widgets/common/src/FitScriptGeneratorDataTable.cpp
@@ -229,6 +229,14 @@ std::vector<FitDomainIndex> FitScriptGeneratorDataTable::selectedRows() const {
   return rowIndices;
 }
 
+FitDomainIndex FitScriptGeneratorDataTable::currentRow() const {
+  if (hasLoadedData())
+    return selectedRows()[0];
+
+  throw std::runtime_error(
+      "There is no currentRow as data has not been loaded yet.");
+}
+
 bool FitScriptGeneratorDataTable::hasLoadedData() const {
   return this->rowCount() > 0;
 }
diff --git a/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp b/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp
index bbaead62b87..7fde71cb998 100644
--- a/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp
+++ b/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp
@@ -137,25 +137,11 @@ void FitScriptGeneratorPresenter::handleAddWorkspaceClicked() {
 }
 
 void FitScriptGeneratorPresenter::handleStartXChanged() {
-  auto const selectedRows = m_view->selectedRows();
-  if (!selectedRows.empty()) {
-    auto const workspaceName = m_view->workspaceName(selectedRows[0]);
-    auto const workspaceIndex = m_view->workspaceIndex(selectedRows[0]);
-    auto const startX = m_view->startX(selectedRows[0]);
-
-    updateStartX(workspaceName, workspaceIndex, startX);
-  }
+  updateDomainXRange<&FitScriptGeneratorPresenter::updateStartX>();
 }
 
 void FitScriptGeneratorPresenter::handleEndXChanged() {
-  auto const selectedRows = m_view->selectedRows();
-  if (!selectedRows.empty()) {
-    auto const workspaceName = m_view->workspaceName(selectedRows[0]);
-    auto const workspaceIndex = m_view->workspaceIndex(selectedRows[0]);
-    auto const endX = m_view->endX(selectedRows[0]);
-
-    updateEndX(workspaceName, workspaceIndex, endX);
-  }
+  updateDomainXRange<&FitScriptGeneratorPresenter::updateEndX>();
 }
 
 void FitScriptGeneratorPresenter::handleSelectionChanged() {
@@ -350,8 +336,9 @@ void FitScriptGeneratorPresenter::addWorkspace(std::string const &workspaceName,
 
 void FitScriptGeneratorPresenter::updateStartX(std::string const &workspaceName,
                                                WorkspaceIndex workspaceIndex,
-                                               double startX) {
-  if (!m_model->updateStartX(workspaceName, workspaceIndex, startX)) {
+                                               FitDomainIndex domainIndex) {
+  if (!m_model->updateStartX(workspaceName, workspaceIndex,
+                             m_view->startX(domainIndex))) {
     m_view->resetSelection();
     m_view->displayWarning("The StartX provided must be within the x limits of "
                            "its workspace, and less than the EndX.");
@@ -360,26 +347,15 @@ void FitScriptGeneratorPresenter::updateStartX(std::string const &workspaceName,
 
 void FitScriptGeneratorPresenter::updateEndX(std::string const &workspaceName,
                                              WorkspaceIndex workspaceIndex,
-                                             double endX) {
-  if (!m_model->updateEndX(workspaceName, workspaceIndex, endX)) {
+                                             FitDomainIndex domainIndex) {
+  if (!m_model->updateEndX(workspaceName, workspaceIndex,
+                           m_view->endX(domainIndex))) {
     m_view->resetSelection();
     m_view->displayWarning("The EndX provided must be within the x limits of "
                            "its workspace, and greater than the StartX.");
   }
 }
 
-template <void (IFitScriptGeneratorModel::*func)(
-    std::string const &workspaceName, WorkspaceIndex workspaceIndex,
-    std::string const &function)>
-void FitScriptGeneratorPresenter::updateDomainFunctions(
-    std::string const &function) {
-  for (auto const &domainIndex : getRowIndices()) {
-    auto const workspaceName = m_view->workspaceName(domainIndex);
-    auto const workspaceIndex = m_view->workspaceIndex(domainIndex);
-    (m_model->*func)(workspaceName, workspaceIndex, function);
-  }
-}
-
 void FitScriptGeneratorPresenter::updateParameterTie(
     std::string const &workspaceName, WorkspaceIndex workspaceIndex,
     std::string const &parameter, std::string const &tie) {
@@ -396,6 +372,31 @@ void FitScriptGeneratorPresenter::updateParameterTie(
   }
 }
 
+template <void (FitScriptGeneratorPresenter::*func)(
+    std::string const &workspaceName, WorkspaceIndex workspaceIndex,
+    FitDomainIndex domainIndex)>
+void FitScriptGeneratorPresenter::updateDomainXRange() {
+  if (m_view->hasLoadedData()) {
+    auto const domainIndex = m_view->currentRow();
+    auto const workspaceName = m_view->workspaceName(domainIndex);
+    auto const workspaceIndex = m_view->workspaceIndex(domainIndex);
+
+    (this->*func)(workspaceName, workspaceIndex, domainIndex);
+  }
+}
+
+template <void (IFitScriptGeneratorModel::*func)(
+    std::string const &workspaceName, WorkspaceIndex workspaceIndex,
+    std::string const &function)>
+void FitScriptGeneratorPresenter::updateDomainFunctions(
+    std::string const &function) {
+  for (auto const &domainIndex : getRowIndices()) {
+    auto const workspaceName = m_view->workspaceName(domainIndex);
+    auto const workspaceIndex = m_view->workspaceIndex(domainIndex);
+    (m_model->*func)(workspaceName, workspaceIndex, function);
+  }
+}
+
 std::vector<FitDomainIndex> FitScriptGeneratorPresenter::getRowIndices() const {
   return m_view->applyFunctionChangesToAll() ? m_view->allRows()
                                              : m_view->selectedRows();
diff --git a/qt/widgets/common/src/FitScriptGeneratorView.cpp b/qt/widgets/common/src/FitScriptGeneratorView.cpp
index ab7ef6d8119..fa9c0db4cb6 100644
--- a/qt/widgets/common/src/FitScriptGeneratorView.cpp
+++ b/qt/widgets/common/src/FitScriptGeneratorView.cpp
@@ -293,6 +293,10 @@ std::vector<FitDomainIndex> FitScriptGeneratorView::selectedRows() const {
   return m_dataTable->selectedRows();
 }
 
+FitDomainIndex FitScriptGeneratorView::currentRow() const {
+  return m_dataTable->currentRow();
+}
+
 bool FitScriptGeneratorView::hasLoadedData() const {
   return m_dataTable->hasLoadedData();
 }
-- 
GitLab