From 7b124a87614663516e1eca833d9e45dd60269832 Mon Sep 17 00:00:00 2001
From: Anton Piccardo-Selg <anton.piccardo-selg@tessella.com>
Date: Thu, 1 Jun 2017 15:58:34 +0100
Subject: [PATCH] Refs #19775 Fix sans save issue

---
 .../MantidQtCustomInterfaces/SANSRunWindow.h  |  2 +
 .../CustomInterfaces/src/SANSRunWindow.cpp    | 65 ++++++++++++++++++
 .../MantidQtMantidWidgets/SaveWorkspaces.h    |  2 +-
 MantidQt/MantidWidgets/src/SaveWorkspaces.cpp | 67 +++++++++++++++++++
 4 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h
index 7c1bd882acd..281c9ce99f3 100644
--- a/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h
+++ b/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.h
@@ -460,6 +460,8 @@ private:
   void checkWaveLengthAndQValues(bool &isValid, QString &message,
                                  QLineEdit *min, QLineEdit *max,
                                  QComboBox *selection, QString type);
+  /// Checks if the save settings are valid for a particular workspace
+  bool areSaveSettingsValid(const QString& workspaceName);
   /// Update the beam center fields
   void updateBeamCenterCoordinates();
   /// Set the beam finder details
diff --git a/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp b/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
index e7935cf7cf3..56b87f1d975 100644
--- a/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
+++ b/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
@@ -200,6 +200,22 @@ void setTransmissionOnSaveCommand(
     }
   }
 }
+
+
+bool checkSaveOptions(QString& message, bool is1D, bool isCanSAS, bool isNistQxy) {
+  // Check we are dealing with 1D or 2D data
+  bool isValid = true;
+  if (is1D && isNistQxy) {
+      isValid = false;
+      message += "Save option issue: Cannot save in NistQxy format for 1D data.\n";
+  }
+
+  if (!is1D && isCanSAS) {
+      isValid = false;
+      message += "Save option issue: Cannot save in CanSAS format for 2D data.\n";
+  }
+  return isValid;
+}
 }
 
 //----------------------------------------------
@@ -2914,6 +2930,10 @@ void SANSRunWindow::handleDefSaveClick() {
         "A filename must be entered into the text box above to save this file");
   }
 
+  if (!areSaveSettingsValid(m_outputWS)) {
+    return;
+  }
+
   // If we save with a zero-error-free correction we need to swap the
   QString workspaceNameBuffer = m_outputWS;
   QString clonedWorkspaceName = m_outputWS + "_cloned_temp";
@@ -2995,6 +3015,31 @@ void SANSRunWindow::handleDefSaveClick() {
                           "console?");
   }
 }
+
+/**
+ * Checks if the save options are valid
+ */
+bool SANSRunWindow::areSaveSettingsValid(const QString& workspaceName) {
+  Mantid::API::MatrixWorkspace_sptr ws =
+      AnalysisDataService::Instance().retrieveWS<Mantid::API::MatrixWorkspace>(workspaceName.toStdString());
+  auto is1D = ws->getNumberHistograms() == 1;
+  auto isNistQxy = m_uiForm.saveNIST_Qxy_check->isChecked();
+  auto isCanSAS = m_uiForm.saveCan_check->isChecked();
+
+  QString message;
+
+  auto isValid = checkSaveOptions(message, is1D, isCanSAS, isNistQxy);
+
+  // Print the error message if there are any
+  if (!message.isEmpty()) {
+    QString warning = "Please correct these settings before proceeding:\n";
+    warning += message;
+    QMessageBox::warning(this, "Inconsistent input", warning);
+  }
+  return isValid;
+}
+
+
 /**
  * Set up controls based on the users selection in the combination box
  * @param new_index :: The new index that has been set
@@ -4515,6 +4560,24 @@ bool SANSRunWindow::areSettingsValid(States type) {
     message += "Sample width issue: Only values > 0 are allowed.\n";
   }
 
+
+  // Check save format consistency for batch mode reduction
+  // 1D --> cannot be Nist Qxy
+  // 2D --> cannot be CanSAS
+  auto isBatchMode = !m_uiForm.single_mode_btn->isChecked();
+  if (isBatchMode) {
+      auto is1D = type == OneD;
+      auto isCanSAS =  m_uiForm.saveCan_check->isChecked();
+      auto isNistQxy = m_uiForm.saveNIST_Qxy_check->isChecked();
+      QString saveMessage;
+      auto isValidSaveOption = checkSaveOptions(saveMessage, is1D, isCanSAS, isNistQxy);
+      if (!isValidSaveOption) {
+        isValid = false;
+        message += saveMessage;
+      }
+  }
+
+
   // Print the error message if there are any
   if (!message.isEmpty()) {
     QString warning = "Please correct these settings before proceeding:\n";
@@ -4522,6 +4585,8 @@ bool SANSRunWindow::areSettingsValid(States type) {
     QMessageBox::warning(this, "Inconsistent input", warning);
   }
 
+
+
   return isValid;
 }
 
diff --git a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h
index 973c217eaee..47f9afcb5e6 100644
--- a/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h
+++ b/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h
@@ -95,7 +95,7 @@ private:
   QHash<QString, QString>
   provideZeroFreeWorkspaces(const QListWidget *workspaces);
   void removeZeroFreeWorkspaces(QHash<QString, QString> workspaces);
-
+  bool isValid();
 private slots:
   void saveSel();
   void setFileName(int row);
diff --git a/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp b/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
index fee66cdb850..e2ef6557076 100644
--- a/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
+++ b/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
@@ -313,6 +313,11 @@ QString SaveWorkspaces::getSaveAlgExt(const QString &algName) {
 *  have been selected to be saved
 */
 void SaveWorkspaces::saveSel() {
+  // Check if the save selection is valid
+  if (!isValid()) {
+    return;
+  }
+
   // For each selected workspace, provide an zero-error free clone
   QHash<QString, QString> workspaceMap =
       provideZeroFreeWorkspaces(m_workspaces);
@@ -350,6 +355,68 @@ void SaveWorkspaces::saveSel() {
                           "the selected formats");
   }
 }
+
+/**
+ * Checks if the save option selection is compatible with the dimensionality selection
+ * @return true if the save option selection is compatible with the dimensionality selection else false
+ */
+bool SaveWorkspaces::isValid() {
+  // Get the dimensionality of the workspaces
+  auto is1D = false;
+  auto is2D = false;
+
+  auto workspacesList = m_workspaces->selectedItems();
+  for (auto it = workspacesList.begin(); it != workspacesList.end(); ++it) {
+    auto wsName = (*it)->text();
+    auto workspace = AnalysisDataService::Instance().retrieveWS<Mantid::API::MatrixWorkspace>(wsName.toStdString());
+    if (workspace->getNumberHistograms() == 1) {
+      is1D = true;    
+    } else {
+      is2D = true;
+    }
+  }
+
+  // Check if the NistQxy or CanSAS were selected
+  auto isCanSAS = false;
+  auto isNistQxy = false;
+  for (SavFormatsConstIt i = m_savFormats.begin(); i != m_savFormats.end();
+       ++i) { // the key to a pointer to the check box that the user may have
+              // clicked
+    if (i.key()->isChecked()) { // we need to save in this format
+      if (i.value() == "SaveNISTDAT") {
+            isNistQxy = true;
+      }
+
+      if (i.value() == "SaveCanSAS1D") {
+            isCanSAS = true;
+      }
+    }
+  }
+
+  // Check for errors
+  QString message;
+  auto isValidOption = true;
+  if (is1D && isNistQxy) {
+    isValidOption = false;
+    message += "Save option issue: Cannot save in NistQxy format for 1D data.\n";
+  }
+
+  if (is2D && isCanSAS) {
+    isValidOption = false;
+    message += "Save option issue: Cannot save in CanSAS format for 2D data.\n";
+  }
+
+
+  // Print the error message if there are any
+  if (!message.isEmpty()) {
+    QString warning = "Please correct these save settings before proceeding:\n";
+    warning += message;
+    QMessageBox::warning(this, "Inconsistent input", warning);
+  }
+
+  return isValidOption;
+}
+
 /** Sets the filename to the name of the selected workspace
 *  @param row number of the row that is selected
 */
-- 
GitLab