From ce7a8d4c7d7cedaf6bf852c6868cdc85a07e05f8 Mon Sep 17 00:00:00 2001
From: Pranav Bahuguna <pranav.bahuguna@stfc.ac.uk>
Date: Tue, 16 May 2017 14:32:21 +0100
Subject: [PATCH] Re #19480 Implemented basic pause button functionality

---
 .../Reflectometry/IReflMainWindowView.h       |  3 ---
 .../Reflectometry/IReflRunsTabPresenter.h     |  3 ++-
 .../Reflectometry/QtReflMainWindowView.h      |  3 +--
 .../Reflectometry/ReflMainWindowPresenter.h   |  4 ----
 .../Reflectometry/QtReflMainWindowView.cpp    | 17 +-------------
 .../Reflectometry/ReflMainWindowPresenter.cpp | 19 ++--------------
 .../DataProcessorMainPresenter.h              |  3 +--
 .../DataProcessorUI/DataProcessorView.h       |  1 +
 .../DataProcessorUI/QDataProcessorWidget.h    |  1 +
 .../GenericDataProcessorPresenter.cpp         | 22 ++++++++++++++-----
 .../DataProcessorUI/QDataProcessorWidget.cpp  |  9 ++++++++
 11 files changed, 34 insertions(+), 51 deletions(-)

diff --git a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflMainWindowView.h b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflMainWindowView.h
index 55dac0b680e..d5315143284 100644
--- a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflMainWindowView.h
+++ b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflMainWindowView.h
@@ -52,9 +52,6 @@ public:
   virtual void giveUserInfo(const std::string &prompt,
                             const std::string &title) = 0;
   virtual std::string runPythonAlgorithm(const std::string &pythonCode) = 0;
-
-  /// Close window handler
-  virtual void confirmCloseWindow() = 0;
 };
 }
 }
diff --git a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflRunsTabPresenter.h b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflRunsTabPresenter.h
index 44337f670fc..28202fe7308 100644
--- a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflRunsTabPresenter.h
+++ b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/IReflRunsTabPresenter.h
@@ -46,7 +46,8 @@ public:
     GroupChangedFlag,
     PauseReductionFlag,
     ResumeReductionFlag,
-    ConfirmReductionPausedFlag
+    ConfirmReductionPausedFlag,
+    ConfirmReductionResumedFlag
   };
 
   // Tell the presenter something happened
diff --git a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/QtReflMainWindowView.h b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/QtReflMainWindowView.h
index d8cf3bd25bb..5d810adf83c 100644
--- a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/QtReflMainWindowView.h
+++ b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/QtReflMainWindowView.h
@@ -67,8 +67,7 @@ public:
                     const std::string &title) override;
   std::string runPythonAlgorithm(const std::string &pythonCode) override;
 
-  /// Close window handlers
-  void confirmCloseWindow() override;
+  /// Close window handler
   void closeEvent(QCloseEvent *event) override;
 
 private:
diff --git a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/ReflMainWindowPresenter.h b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/ReflMainWindowPresenter.h
index d1cad87f952..2e1d29daf01 100644
--- a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/ReflMainWindowPresenter.h
+++ b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Reflectometry/ReflMainWindowPresenter.h
@@ -93,10 +93,6 @@ private:
   void pauseReduction() const;
   /// Resumes reduction in the Runs Tab
   void resumeReduction() const;
-  /// Confirm reduction in the Runs Tab is paused
-  void confirmReductionPaused() const;
-  /// Confirm reduction in the Runs Tab is resumed
-  void confirmReductionResumed() const;
   /// The view we are handling
   IReflMainWindowView *m_view;
   /// The presenter of tab 'Runs'
diff --git a/MantidQt/CustomInterfaces/src/Reflectometry/QtReflMainWindowView.cpp b/MantidQt/CustomInterfaces/src/Reflectometry/QtReflMainWindowView.cpp
index c3e7b2e2117..f657b3e3107 100644
--- a/MantidQt/CustomInterfaces/src/Reflectometry/QtReflMainWindowView.cpp
+++ b/MantidQt/CustomInterfaces/src/Reflectometry/QtReflMainWindowView.cpp
@@ -170,31 +170,16 @@ QtReflMainWindowView::runPythonAlgorithm(const std::string &pythonCode) {
   return output.toStdString();
 }
 
-/**
-Ask the user to confirm if they want to close the main window
-*/
-void QtReflMainWindowView::confirmCloseWindow() {
-
-  if (askUserYesNo("Runs are still being processed, are you sure you want to "
-                   "close and stop processing?",
-                   "Confirm Close"))
-    parentWidget()->parentWidget()->parentWidget()->close();
-  else
-    m_presenter->notify(IReflMainWindowPresenter::ResumeReductionFlag);
-}
-
 /**
 Handles attempt to close main window
 * @param event : [input] The close event
 */
 void QtReflMainWindowView::closeEvent(QCloseEvent *event) {
 
+  // Close only if reduction has been paused
   if (!m_presenter->checkIfProcessing()) {
-    // No runs are being processed, close window
     event->accept();
   } else {
-    // Otherwise send message to pause the reduction and store the close event
-    m_presenter->notify(IReflMainWindowPresenter::PauseReductionFlag);
     event->ignore();
   }
 }
diff --git a/MantidQt/CustomInterfaces/src/Reflectometry/ReflMainWindowPresenter.cpp b/MantidQt/CustomInterfaces/src/Reflectometry/ReflMainWindowPresenter.cpp
index fd70891adc5..0a765e718cf 100644
--- a/MantidQt/CustomInterfaces/src/Reflectometry/ReflMainWindowPresenter.cpp
+++ b/MantidQt/CustomInterfaces/src/Reflectometry/ReflMainWindowPresenter.cpp
@@ -51,10 +51,10 @@ void ReflMainWindowPresenter::notify(IReflMainWindowPresenter::Flag flag) {
     resumeReduction();
     break;
   case IReflMainWindowPresenter::ConfirmReductionPausedFlag:
-    confirmReductionPaused();
+    m_isProcessing = false;
     break;
   case IReflMainWindowPresenter::ConfirmReductionResumedFlag:
-    confirmReductionResumed();
+    m_isProcessing = true;
     break;
   }
   // Not having a 'default' case is deliberate. gcc issues a warning if there's
@@ -265,20 +265,5 @@ void ReflMainWindowPresenter::resumeReduction() const {
   m_isProcessing = true;
   m_runsPresenter->notify(IReflRunsTabPresenter::ResumeReductionFlag);
 }
-
-/** Confirm that reduction in the runs tab has been paused
-*/
-void ReflMainWindowPresenter::confirmReductionPaused() const {
-
-  m_isProcessing = false;
-  m_view->confirmCloseWindow();
-}
-
-/** Confirm that reduction in the runs tab has been resumed
-*/
-void ReflMainWindowPresenter::confirmReductionResumed() const {
-
-  m_isProcessing = true;
-}
 }
 }
diff --git a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorMainPresenter.h b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorMainPresenter.h
index e68f5f72abb..526bd373df5 100644
--- a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorMainPresenter.h
+++ b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorMainPresenter.h
@@ -74,9 +74,8 @@ public:
   /// Return time-slicing type
   virtual std::string getTimeSlicingType() const = 0;
   
-  /// Handle data reduction pause confirmation
+  /// Handle data reduction paused/resumed confirmation
   virtual void confirmReductionPaused() const = 0;
-  /// Handle data reduction resume confirmation
   virtual void confirmReductionResumed() const = 0;
 };
 }
diff --git a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorView.h b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorView.h
index 7c4058bfa07..8eab04f93bb 100644
--- a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorView.h
+++ b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/DataProcessorView.h
@@ -83,6 +83,7 @@ public:
                          int column) = 0;
   virtual void setClipboard(const std::string &text) = 0;
   virtual void setModel(const std::string &name) = 0;
+  virtual void setToolbarActionEnabled(int index, bool enabled) = 0;
 
   // Accessor methods
   virtual std::map<int, std::set<int>> getSelectedChildren() const = 0;
diff --git a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/QDataProcessorWidget.h b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/QDataProcessorWidget.h
index 57c12b6132d..aab10562695 100644
--- a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/QDataProcessorWidget.h
+++ b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/DataProcessorUI/QDataProcessorWidget.h
@@ -85,6 +85,7 @@ public:
   setOptionsHintStrategy(MantidQt::MantidWidgets::HintStrategy *hintStrategy,
                          int column) override;
   void setClipboard(const std::string &text) override;
+  void setToolbarActionEnabled(int index, bool enabled) override;
 
   // Accessor methods
   std::map<int, std::set<int>> getSelectedChildren() const override;
diff --git a/MantidQt/MantidWidgets/src/DataProcessorUI/GenericDataProcessorPresenter.cpp b/MantidQt/MantidWidgets/src/DataProcessorUI/GenericDataProcessorPresenter.cpp
index 8c6a9ac03b5..b4be2b18f7e 100644
--- a/MantidQt/MantidWidgets/src/DataProcessorUI/GenericDataProcessorPresenter.cpp
+++ b/MantidQt/MantidWidgets/src/DataProcessorUI/GenericDataProcessorPresenter.cpp
@@ -187,6 +187,9 @@ void GenericDataProcessorPresenter::acceptViews(
 
   // Start with a blank table
   newTable();
+
+  // Reduction is currently paused
+  pause();
 }
 
 /**
@@ -220,9 +223,9 @@ void GenericDataProcessorPresenter::process() {
     m_gqueue.push(std::make_pair(item.first, rowQueue));
   }
 
-  m_mainPresenter->notify(
-      DataProcessorMainPresenter::ConfirmReductionResumedFlag);
-
+  m_nextActionFlag = GenericDataProcessorPresenter::ReduceGroupFlag;
+  resume();
+  
   // Start processing the first group
   nextGroup();
 
@@ -969,7 +972,7 @@ void GenericDataProcessorPresenter::notify(DataProcessorPresenter::Flag flag) {
     collapseAll();
     break;
   case DataProcessorPresenter::PauseFlag:
-    //pause();
+    pause();
     break;
   case DataProcessorPresenter::ResumeFlag:
     resume();
@@ -1392,6 +1395,9 @@ the current thread for reducing a row or group has finished
 */
 void GenericDataProcessorPresenter::pause() {
 
+  // Disable pause button
+  m_view->setToolbarActionEnabled(1, false);
+
   if (m_reductionPaused)
     m_mainPresenter->notify(
         DataProcessorMainPresenter::ConfirmReductionPausedFlag);
@@ -1403,9 +1409,11 @@ void GenericDataProcessorPresenter::pause() {
 */
 void GenericDataProcessorPresenter::resume() {
 
-  m_reductionPaused = false;
+  // Enable pause button
+  m_view->setToolbarActionEnabled(1, true);
+
   m_mainPresenter->notify(
-      DataProcessorMainPresenter::ConfirmReductionResumedFlag);
+    DataProcessorMainPresenter::ConfirmReductionPausedFlag);
 
   switch (m_nextActionFlag) {
   case GenericDataProcessorPresenter::ReduceRowFlag:
@@ -1415,6 +1423,8 @@ void GenericDataProcessorPresenter::resume() {
     nextGroup();
     break;
   }
+
+  m_reductionPaused = false;
 }
 
 /**
diff --git a/MantidQt/MantidWidgets/src/DataProcessorUI/QDataProcessorWidget.cpp b/MantidQt/MantidWidgets/src/DataProcessorUI/QDataProcessorWidget.cpp
index 24a0dfc24b3..8153226d165 100644
--- a/MantidQt/MantidWidgets/src/DataProcessorUI/QDataProcessorWidget.cpp
+++ b/MantidQt/MantidWidgets/src/DataProcessorUI/QDataProcessorWidget.cpp
@@ -322,6 +322,15 @@ void QDataProcessorWidget::setClipboard(const std::string &text) {
   QApplication::clipboard()->setText(QString::fromStdString(text));
 }
 
+/**
+Sets a specific action in the row toolbar enabled or disabled
+@param index : The index of the action in the toolbar
+@param enabled : Whether to enable or disable the action
+*/
+void QDataProcessorWidget::setToolbarActionEnabled(int index, bool enabled) {
+  ui.rowToolBar->actions()[index]->setEnabled(enabled);
+}
+
 /**
 Get the selected instrument for processing
 @returns the selected instrument to process with
-- 
GitLab