From 313c219fb58b0ce250ba65b6295b199944afc300 Mon Sep 17 00:00:00 2001
From: Gemma Guest <gemma.guest@stfc.ac.uk>
Date: Fri, 16 Aug 2019 18:15:20 +0100
Subject: [PATCH] Implement function to show slit calculator

Also includes some minor refactoring of tests which require an
instrument to be set to construct the instrument directly in the test
rather than call a notification function which causes the instrument to
be loaded from the name.

Re #26533
---
 .../GUI/MainWindow/MainWindowPresenter.cpp    | 20 ++++++--
 .../GUI/MainWindow/MainWindowPresenter.h      |  3 +-
 .../GUI/MainWindow/QtMainWindowView.cpp       |  3 ++
 .../MainWindow/MainWindowPresenterTest.h      | 50 +++++++++++++++----
 .../MantidQtWidgets/Common/ISlitCalculator.h  |  1 +
 .../Common/MockSlitCalculator.h               |  1 +
 .../MantidQtWidgets/Common/SlitCalculator.h   |  1 +
 qt/widgets/common/src/SlitCalculator.cpp      |  2 +
 8 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
index 63d76498a5e..1d54229f2f7 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
@@ -46,9 +46,8 @@ MainWindowPresenter::MainWindowPresenter(
     std::unique_ptr<ISlitCalculator> slitCalculator,
     std::unique_ptr<IBatchPresenterFactory> batchPresenterFactory)
     : m_view(view), m_messageHandler(messageHandler),
-      m_slitCalculator(std::move(slitCalculator)),
-      m_batchPresenterFactory(std::move(batchPresenterFactory)),
-      m_instrument() {
+      m_instrument(), m_slitCalculator(std::move(slitCalculator)),
+      m_batchPresenterFactory(std::move(batchPresenterFactory)) {
   view->subscribe(this);
   for (auto *batchView : m_view->batches())
     addNewBatch(batchView);
@@ -81,9 +80,15 @@ void MainWindowPresenter::notifyCloseBatchRequested(int batchIndex) {
   }
 }
 
-void MainWindowPresenter::notifyShowOptionsRequested() {}
+void MainWindowPresenter::notifyShowOptionsRequested() {
+  // TODO Show the options dialog when it is implemented
+}
 
-void MainWindowPresenter::notifyShowSlitCalculatorRequested() {}
+void MainWindowPresenter::notifyShowSlitCalculatorRequested() {
+  m_slitCalculator->setCurrentInstrumentName(instrumentName());
+  m_slitCalculator->processInstrumentHasBeenChanged();
+  m_slitCalculator->show();
+}
 
 void MainWindowPresenter::notifyAnyBatchAutoreductionResumed() {
   for (const auto &batchPresenter : m_batchPresenters) {
@@ -232,8 +237,13 @@ void MainWindowPresenter::updateInstrument(const std::string &instrumentName) {
   MatrixWorkspace_sptr instWorkspace = loadAlg->getProperty("OutputWorkspace");
   m_instrument = instWorkspace->getInstrument();
 
+  // Notify child presenters
   for (auto &batchPresenter : m_batchPresenters)
     batchPresenter->notifyInstrumentChanged(instrumentName);
+
+  // Notify the slit calculator
+  m_slitCalculator->setCurrentInstrumentName(instrumentName);
+  m_slitCalculator->processInstrumentHasBeenChanged();
 }
 } // namespace ISISReflectometry
 } // namespace CustomInterfaces
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
index 9c7ea3e110e..02d7e205ac3 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
@@ -71,13 +71,12 @@ protected:
   IMainWindowView *m_view;
   IMessageHandler *m_messageHandler;
   std::vector<std::unique_ptr<IBatchPresenter>> m_batchPresenters;
+  Mantid::Geometry::Instrument_const_sptr m_instrument;
 
 private:
   std::unique_ptr<MantidWidgets::ISlitCalculator> m_slitCalculator;
   std::unique_ptr<IBatchPresenterFactory> m_batchPresenterFactory;
 
-  Mantid::Geometry::Instrument_const_sptr m_instrument;
-
   void showHelp();
   void addNewBatch(IBatchView *batchView);
   void initNewBatch(IBatchPresenter *batchPresenter,
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/QtMainWindowView.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/QtMainWindowView.cpp
index 581070e0ebe..71794d9eeb2 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/QtMainWindowView.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/QtMainWindowView.cpp
@@ -16,6 +16,9 @@
 #include <QToolButton>
 
 namespace MantidQt {
+
+using MantidWidgets::SlitCalculator;
+
 namespace CustomInterfaces {
 namespace ISISReflectometry {
 
diff --git a/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h b/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
index 9850c08b197..b31333d7751 100644
--- a/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
+++ b/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
@@ -12,6 +12,7 @@
 #include "../Batch/MockBatchView.h"
 #include "../ReflMockObjects.h"
 #include "MantidAPI/FrameworkManager.h"
+#include "MantidGeometry/Instrument_fwd.h"
 #include "MantidQtWidgets/Common/MockSlitCalculator.h"
 #include "MockMainWindowView.h"
 
@@ -154,14 +155,25 @@ public:
     verifyAndClear();
   }
 
-  void testShowOptionsRequested() {
+  void testShowOptionsOpensDialog() {
     auto presenter = makePresenter();
+    // TODO Add test when options dialog is impelemented
+    //EXPECT_CALL(*m_optionsDialog, show()).Times(1);
     presenter.notifyShowOptionsRequested();
     verifyAndClear();
   }
 
-  void testShowSlitCalculatorRequested() {
+  void testShowSlitCalculatorSetsInstrument() {
     auto presenter = makePresenter();
+    auto const instrument = setupInstrument(presenter, "TEST_INSTRUMENT");
+    expectSlitCalculatorInstrumentUpdated();
+    presenter.notifyShowSlitCalculatorRequested();
+    verifyAndClear();
+  }
+
+  void testShowSlitCalculatorOpensDialog() {
+    auto presenter = makePresenter();
+    EXPECT_CALL(*m_slitCalculator, show()).Times(1);
     presenter.notifyShowSlitCalculatorRequested();
     verifyAndClear();
   }
@@ -237,11 +249,9 @@ public:
     verifyAndClear();
   }
 
-  void testUpdateInstrumentRequestedUpdatesInstrumentInChildPresenters() {
+  void testUpdateInstrumentUpdatesInstrumentInChildPresenters() {
     auto presenter = makePresenter();
-    // must set the instrument to something valid first
-    presenter.notifyChangeInstrumentRequested("POLREF");
-    auto const instrument = presenter.instrumentName();
+    auto const instrument = setupInstrument(presenter, "POLREF");
     EXPECT_CALL(*m_batchPresenters[0], notifyInstrumentChanged(instrument))
         .Times(1);
     EXPECT_CALL(*m_batchPresenters[1], notifyInstrumentChanged(instrument))
@@ -250,17 +260,23 @@ public:
     verifyAndClear();
   }
 
-  void testUpdateInstrumentRequestedDoesNotChangeInstrumentName() {
+  void testUpdateInstrumentUpdatesInstrumentInSlitCalculator() {
+    auto presenter = makePresenter();
+    auto const instrument = setupInstrument(presenter, "POLREF");
+    expectSlitCalculatorInstrumentUpdated();
+    presenter.notifyUpdateInstrumentRequested();
+    verifyAndClear();
+  }
+
+  void testUpdateInstrumentDoesNotChangeInstrumentName() {
     auto presenter = makePresenter();
-    // must set the instrument to something valid first
-    presenter.notifyChangeInstrumentRequested("POLREF");
-    auto const instrument = presenter.instrumentName();
+    auto const instrument = setupInstrument(presenter, "POLREF");
     presenter.notifyUpdateInstrumentRequested();
     TS_ASSERT_EQUALS(presenter.instrumentName(), instrument);
     verifyAndClear();
   }
 
-  void testUpdateInstrumentRequestedThrowsIfInstrumentNotSet() {
+  void testUpdateInstrumentThrowsIfInstrumentNotSet() {
     auto presenter = makePresenter();
     TS_ASSERT_THROWS_ANYTHING(presenter.notifyUpdateInstrumentRequested());
     verifyAndClear();
@@ -315,6 +331,13 @@ private:
     m_batchPresenters.clear();
   }
 
+  std::string setupInstrument(MainWindowPresenterFriend &presenter,
+    std::string const& instrumentName) {
+    presenter.m_instrument =
+        boost::make_shared<Mantid::Geometry::Instrument>(instrumentName);
+    return presenter.instrumentName();
+  }
+
   void expectBatchAdded(MockBatchPresenter *batchPresenter) {
     EXPECT_CALL(*batchPresenter, acceptMainPresenter(_)).Times(1);
     EXPECT_CALL(*batchPresenter, initInstrumentList()).Times(1);
@@ -381,6 +404,11 @@ private:
         .Times(1);
   }
 
+  void expectSlitCalculatorInstrumentUpdated() {
+    EXPECT_CALL(*m_slitCalculator, setCurrentInstrumentName(instrument)).Times(1);
+    EXPECT_CALL(*m_slitCalculator, processInstrumentHasBeenChanged()).Times(1);
+  }
+
   void assertFirstBatchWasRemovedFromModel(
       MainWindowPresenterFriend const &presenter) {
     TS_ASSERT_EQUALS(presenter.m_batchPresenters.size(), 1);
diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/ISlitCalculator.h b/qt/widgets/common/inc/MantidQtWidgets/Common/ISlitCalculator.h
index f6a8f3cee1d..a56b6caf67f 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/ISlitCalculator.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/ISlitCalculator.h
@@ -17,6 +17,7 @@ public:
   virtual ~ISlitCalculator() = default;
   virtual void setCurrentInstrumentName(std::string instrumentName) = 0;
   virtual void processInstrumentHasBeenChanged() = 0;
+  virtual void show() = 0;
 };
 } // namespace MantidWidgets
 } // namespace MantidQt
diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/MockSlitCalculator.h b/qt/widgets/common/inc/MantidQtWidgets/Common/MockSlitCalculator.h
index 3947c099757..2a0dc116050 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/MockSlitCalculator.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/MockSlitCalculator.h
@@ -16,6 +16,7 @@ public:
   GNU_DIAG_OFF_SUGGEST_OVERRIDE
   MOCK_METHOD1(setCurrentInstrumentName, void(std::string));
   MOCK_METHOD0(processInstrumentHasBeenChanged, void());
+  MOCK_METHOD0(show, void());
   GNU_DIAG_ON_SUGGEST_OVERRIDE
 };
 
diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/SlitCalculator.h b/qt/widgets/common/inc/MantidQtWidgets/Common/SlitCalculator.h
index f8af6cdcf07..7f6d5243903 100644
--- a/qt/widgets/common/inc/MantidQtWidgets/Common/SlitCalculator.h
+++ b/qt/widgets/common/inc/MantidQtWidgets/Common/SlitCalculator.h
@@ -29,6 +29,7 @@ public:
   ~SlitCalculator() override;
   void setCurrentInstrumentName(std::string instrumentName) override;
   void processInstrumentHasBeenChanged() override;
+  void show() override;
 
 protected:
   Ui::SlitCalculator ui;
diff --git a/qt/widgets/common/src/SlitCalculator.cpp b/qt/widgets/common/src/SlitCalculator.cpp
index 57a0b4e2382..c863c64cf30 100644
--- a/qt/widgets/common/src/SlitCalculator.cpp
+++ b/qt/widgets/common/src/SlitCalculator.cpp
@@ -61,6 +61,8 @@ void SlitCalculator::setInstrument(std::string instrumentName) {
   setupSlitCalculatorWithInstrumentValues(instrument);
 }
 
+void SlitCalculator::show() { QDialog::show(); }
+
 void SlitCalculator::setupSlitCalculatorWithInstrumentValues(
     Mantid::Geometry::Instrument_const_sptr instrument) {
   // fetch the components that we need for values from IDF
-- 
GitLab