From 0a0b9c13766d18a77756f662d435e44e186719e7 Mon Sep 17 00:00:00 2001
From: Gemma Guest <gemma.guest@stfc.ac.uk>
Date: Mon, 4 Nov 2019 12:16:03 +0000
Subject: [PATCH] Set the correct facility for the reflectometry GUI

The reflectometry GUI sets the default instrument in the config service
when it starts up to one of the ISIS reflectometers. This commit adds a
check that the facility is ISIS and if not it sets the facility to ISIS.
This avoids confusion where you essentially have invalid config. It also
avoids confusion where e.g. loading runs from the default archive might
not work because the archive may be a different facility.

I have also added code to save the new config settings to file. Again
this is to avoid confusion between what users see in mantid and what is
in the saved settings. It is particularly confusing otherwise because
Workbench will write settings to file when it closes but MantidPlot will not.

Re #27241
---
 .../GUI/MainWindow/MainWindowPresenter.cpp    | 22 +++++++++--
 .../GUI/MainWindow/MainWindowPresenter.h      |  1 +
 .../GUI/RunsTable/QtRunsTableView.cpp         |  1 -
 .../MainWindow/MainWindowPresenterTest.h      | 37 +++++++++++++++++++
 4 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
index 38e8863befe..f9e0bf63d2f 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
@@ -222,9 +222,7 @@ std::string MainWindowPresenter::instrumentName() const {
 }
 
 void MainWindowPresenter::updateInstrument(const std::string &instrumentName) {
-  Mantid::Kernel::ConfigService::Instance().setString("default.instrument",
-                                                      instrumentName);
-  g_log.information() << "Instrument changed to " << instrumentName;
+  setDefaultInstrument(instrumentName);
 
   // Load a workspace for this instrument so we can get the actual instrument
   auto loadAlg =
@@ -246,6 +244,24 @@ void MainWindowPresenter::updateInstrument(const std::string &instrumentName) {
   m_slitCalculator->setCurrentInstrumentName(instrumentName);
   m_slitCalculator->processInstrumentHasBeenChanged();
 }
+
+void MainWindowPresenter::setDefaultInstrument(
+    const std::string &requiredInstrument) {
+  auto &config = Mantid::Kernel::ConfigService::Instance();
+
+  auto currentFacility = config.getString("default.facility");
+  auto requiredFacility = "ISIS";
+  if (currentFacility != requiredFacility) {
+    config.setString("default.facility", requiredFacility);
+    g_log.notice() << "Facility changed to " << requiredFacility;
+  }
+
+  auto currentInstrument = config.getString("default.instrument");
+  if (currentInstrument != requiredInstrument) {
+    config.setString("default.instrument", requiredInstrument);
+    g_log.notice() << "Instrument changed to " << requiredInstrument;
+  }
+}
 } // namespace ISISReflectometry
 } // namespace CustomInterfaces
 } // namespace MantidQt
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
index 02d7e205ac3..c1070a554ba 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
@@ -83,6 +83,7 @@ private:
                     std::string const &instrument);
   void changeInstrument(std::string const &instrumentName);
   void updateInstrument(const std::string &instrumentName);
+  void setDefaultInstrument(const std::string &newInstrument);
 
   void disableSaveAndLoadBatch();
   void enableSaveAndLoadBatch();
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/RunsTable/QtRunsTableView.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/RunsTable/QtRunsTableView.cpp
index bf646821d4f..607f5be498d 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/RunsTable/QtRunsTableView.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/RunsTable/QtRunsTableView.cpp
@@ -6,7 +6,6 @@
 // SPDX - License - Identifier: GPL - 3.0 +
 #include "QtRunsTableView.h"
 #include "Common/IndexOf.h"
-#include "MantidKernel/ConfigService.h"
 #include "MantidKernel/UsageService.h"
 #include "MantidQtIcons/Icon.h"
 #include "MantidQtWidgets/Common/AlgorithmHintStrategy.h"
diff --git a/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h b/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
index d0bf7862c3c..a21fd3409a0 100644
--- a/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
+++ b/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
@@ -13,6 +13,7 @@
 #include "../ReflMockObjects.h"
 #include "MantidAPI/FrameworkManager.h"
 #include "MantidGeometry/Instrument_fwd.h"
+#include "MantidKernel/ConfigService.h"
 #include "MantidQtWidgets/Common/MockSlitCalculator.h"
 #include "MockMainWindowView.h"
 
@@ -49,6 +50,18 @@ public:
     ON_CALL(m_view, batches()).WillByDefault(Return(m_batchViews));
   }
 
+  void setUp() override {
+    auto &config = Mantid::Kernel::ConfigService::Instance();
+    backup_facility = config.getString("default.facility");
+    backup_instrument = config.getString("default.instrument");
+  }
+
+  void tearDown() override {
+    auto &config = Mantid::Kernel::ConfigService::Instance();
+    config.setString("default.facility", backup_facility);
+    config.setString("default.instrument", backup_instrument);
+  }
+
   void testPresenterSubscribesToView() {
     EXPECT_CALL(m_view, subscribe(_)).Times(1);
     auto presenter = makePresenter();
@@ -282,6 +295,26 @@ public:
     verifyAndClear();
   }
 
+  void testUpdateInstrumentSetsFacilityInConfig() {
+    auto presenter = makePresenter();
+    auto const instrument = setupInstrument(presenter, "POLREF");
+    auto &config = Mantid::Kernel::ConfigService::Instance();
+    config.setString("default.facility", "OLD_FACILITY");
+    presenter.notifyUpdateInstrumentRequested();
+    TS_ASSERT_EQUALS(config.getString("default.facility"), "ISIS");
+    verifyAndClear();
+  }
+
+  void testUpdateInstrumentSetsInstrumentInConfig() {
+    auto presenter = makePresenter();
+    auto const instrument = setupInstrument(presenter, "POLREF");
+    auto &config = Mantid::Kernel::ConfigService::Instance();
+    config.setString("default.instrument", "OLD_INSTRUMENT");
+    presenter.notifyUpdateInstrumentRequested();
+    TS_ASSERT_EQUALS(config.getString("default.instrument"), instrument);
+    verifyAndClear();
+  }
+
 private:
   NiceMock<MockMainWindowView> m_view;
   NiceMock<MockMessageHandler> m_messageHandler;
@@ -428,6 +461,10 @@ private:
       TS_ASSERT_EQUALS(presenter.m_batchPresenters[index].get(),
                        m_batchPresenters[index]);
   }
+
+private:
+  std::string backup_facility;
+  std::string backup_instrument;
 };
 
 #endif // MANTID_CUSTOMINTERFACES_MAINWINDOWPRESENTERTEST_H_
-- 
GitLab