From e00c4ec4c0978b76a6fc349f1aa0b51cdc3f5624 Mon Sep 17 00:00:00 2001
From: Anton Piccardo-Selg <anton.piccardo-selg@tessella.com>
Date: Tue, 9 Jun 2015 15:39:27 +0100
Subject: [PATCH] Refs #6457 Finish the regular save mechanism

---
 .../MantidQtCustomInterfaces/SANSRunWindow.ui | 10 +++++-
 .../CustomInterfaces/src/SANSRunWindow.cpp    | 15 ++++----
 .../MantidQtMantidWidgets/SaveWorkspaces.h    |  5 +--
 .../MantidWidgets/src/SaveWorkspaces.cpp      | 36 +++++++++++++------
 .../scripts/SANS/ISISCommandInterface.py      |  2 +-
 5 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui
index 631aee3be34..33504563ce0 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui
+++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/SANSRunWindow.ui
@@ -93,7 +93,7 @@
        <string notr="true"/>
       </property>
       <property name="currentIndex">
-       <number>1</number>
+       <number>0</number>
       </property>
       <property name="elideMode">
        <enum>Qt::ElideNone</enum>
@@ -837,6 +837,14 @@
                </property>
               </widget>
              </item>
+             <item row="4" column="0">
+              <widget class="QCheckBox" name="zeroErrorCheckBox">
+               <property name="text">
+                <string>Remove 
+Zero Error</string>
+               </property>
+              </widget>
+             </item>
             </layout>
            </widget>
           </item>
diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
index f462dddfef3..461b9d7f44e 100644
--- a/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
+++ b/Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
@@ -422,7 +422,7 @@ void SANSRunWindow::saveWorkspacesDialog()
 {
   //Qt::WA_DeleteOnClose must be set for the dialog to aviod a memory leak
   m_saveWorkspaces =
-    new SaveWorkspaces(this, m_uiForm.outfile_edit->text(), m_savFormats);
+    new SaveWorkspaces(this, m_uiForm.outfile_edit->text(), m_savFormats, m_uiForm.zeroErrorCheckBox->isChecked());
   //this dialog sometimes needs to run Python, pass this to Mantidplot via our runAsPythonScript() signal
   connect(m_saveWorkspaces, SIGNAL(runAsPythonScript(const QString&, bool)),
     this, SIGNAL(runAsPythonScript(const QString&, bool)));
@@ -432,6 +432,9 @@ void SANSRunWindow::saveWorkspacesDialog()
   // Connect the request for a zero-error-free workspace
   connect(m_saveWorkspaces, SIGNAL(createZeroErrorFreeWorkspace(QString& , QString&)),
           this, SLOT(createZeroErrorFreeClone(QString&, QString&)));
+  // Connect the request for deleting a zero-error-free workspace
+  connect(m_saveWorkspaces, SIGNAL(deleteZeroErrorFreeWorkspace(QString&)),
+         this, SLOT(deleteZeroErrorFreeClone(QString&) ));
   m_uiForm.saveSel_btn->setEnabled(false);
   m_saveWorkspaces->show();
 }
@@ -3831,9 +3834,9 @@ void SANSRunWindow::setValidators()
 void SANSRunWindow::createZeroErrorFreeClone(QString& originalWorkspaceName, QString& clonedWorkspaceName) {
   if (workspaceExists(originalWorkspaceName)) {
     // Run the python script which creates the cloned workspace
-    QString pythonCode("print i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name=");
-    pythonCode += originalWorkspaceName + ",";
-    pythonCode += " output_workspace_name=" + clonedWorkspaceName + ")";
+    QString pythonCode("print i.CreateZeroErrorFreeClonedWorkspace(input_workspace_name='");
+    pythonCode += originalWorkspaceName + "',";
+    pythonCode += " output_workspace_name='" + clonedWorkspaceName + "')";
 
     QString result(runPythonCode(pythonCode, false));
     result.trimmed();
@@ -3851,8 +3854,8 @@ void SANSRunWindow::createZeroErrorFreeClone(QString& originalWorkspaceName, QSt
 void SANSRunWindow::deleteZeroErrorFreeClone(QString& clonedWorkspaceName) {
   if (workspaceExists(clonedWorkspaceName)) {
     // Run the python script which destroys the cloned workspace
-    QString pythonCode("print i.DeleteZeroErrorFreeClonedWorkspace(input_workspace_name=");
-    pythonCode += clonedWorkspaceName + ")";
+    QString pythonCode("print i.DeleteZeroErrorFreeClonedWorkspace(input_workspace_name='");
+    pythonCode += clonedWorkspaceName + "')";
 
     QString result(runPythonCode(pythonCode, false));
     result.trimmed();
diff --git a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h
index 9ffbd68b9d1..6217561df5d 100644
--- a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h
+++ b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SaveWorkspaces.h
@@ -48,7 +48,7 @@ namespace MantidQt
 
     public: 
       SaveWorkspaces(QWidget *parent, const QString & suggFname,
-        QHash<const QCheckBox * const, QString> & defSavs);
+        QHash<const QCheckBox * const, QString> & defSavs, bool saveAsZeroErrorFree);
       void initLayout();
       ///Returns the save extension expected the name algorithm
       static QString getSaveAlgExt(const QString & algName);
@@ -63,6 +63,7 @@ namespace MantidQt
       QListWidget *m_workspaces;
       QCheckBox *m_append;
       QString m_lastName;
+      bool m_saveAsZeroErrorFree;
 
       QHash<QCheckBox * const, QString> m_savFormats;
       typedef QHash<QCheckBox * const, QString>::const_iterator SavFormatsConstIt;
@@ -76,7 +77,7 @@ namespace MantidQt
 
       void addButtonsDisab(int row);
       void closeEvent(QCloseEvent *event);
-      QString saveList(const QList<QListWidgetItem*> & list, const QString & algorithm, QString fileBase, bool toAppend);
+      QString saveList(const QList<QListWidgetItem*> & list, const QString & algorithm, QString fileBase, bool toAppend, QHash<QString, QString> workspaceMap);
       QHash<QString, QString> provideZeroFreeWorkspaces(const QListWidget * workspaces);
       void removeZeroFreeWorkspaces(QHash<QString, QString> workspaces);
 
diff --git a/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp b/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
index cea78357eee..058a4755df9 100644
--- a/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
+++ b/Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
@@ -32,8 +32,8 @@ using namespace Mantid::API;
  *  @param suggFname :: sets the initial entry in the filename box
  *  @param defSavs :: sets which boxes are ticked
  */
-SaveWorkspaces::SaveWorkspaces(QWidget *parent, const QString & suggFname, QHash<const QCheckBox * const, QString> & defSavs) :
-  API::MantidDialog(parent)
+SaveWorkspaces::SaveWorkspaces(QWidget *parent, const QString & suggFname, QHash<const QCheckBox * const, QString> & defSavs, bool saveAsZeroErrorFree) :
+  API::MantidDialog(parent), m_saveAsZeroErrorFree(saveAsZeroErrorFree)
 {
   setAttribute(Qt::WA_DeleteOnClose);
   setWindowTitle("Save Workspaces");
@@ -213,7 +213,7 @@ void SaveWorkspaces::closeEvent(QCloseEvent* event)
   emit closing();
   event->accept();
 }
-QString SaveWorkspaces::saveList(const QList<QListWidgetItem*> & wspaces, const QString & algorithm, QString fileBase, bool toAppend)
+QString SaveWorkspaces::saveList(const QList<QListWidgetItem*> & wspaces, const QString & algorithm, QString fileBase, bool toAppend, QHash<QString, QString> workspaceMap)
 {
   if ( wspaces.count() < 1 )
   {
@@ -229,7 +229,11 @@ QString SaveWorkspaces::saveList(const QList<QListWidgetItem*> & wspaces, const
   QString saveCommands;
   for (int j =0; j < wspaces.count(); ++j)
   {
-    saveCommands += algorithm + "('"+wspaces[j]->text()+"','";
+    if (workspaceMap.count(wspaces[j]->text())) {
+      saveCommands += algorithm + "('"+ workspaceMap[wspaces[j]->text()]+"','";
+    } else {
+      saveCommands += algorithm + "('"+ wspaces[j]->text()+"','";
+    }
 
     QString outFile = fileBase;
     if (outFile.isEmpty())
@@ -307,7 +311,7 @@ QString SaveWorkspaces::getSaveAlgExt(const QString & algName)
 void SaveWorkspaces::saveSel()
 {
   // For each selected workspace, provide an zero-error free clone
-  QHash<QString, QString> workspaces = provideZeroFreeWorkspaces(m_workspaces);
+  QHash<QString, QString> workspaceMap = provideZeroFreeWorkspaces(m_workspaces);
 
   QString saveCommands;
   for(SavFormatsConstIt i = m_savFormats.begin(); i != m_savFormats.end(); ++i)
@@ -327,7 +331,7 @@ void SaveWorkspaces::saveSel()
       try
       {
         saveCommands += saveList(m_workspaces->selectedItems(), i.value(),
-          m_fNameEdit->text(), toAppend);
+          m_fNameEdit->text(), toAppend, workspaceMap);
       }
       catch(std::logic_error &)
       {
@@ -340,7 +344,9 @@ void SaveWorkspaces::saveSel()
   saveCommands += "print 'success'";
   QString status(runPythonCode(saveCommands).trimmed());
 
-  removeZeroFreeWorkspaces(workspaces);
+  if (m_saveAsZeroErrorFree) {
+    removeZeroFreeWorkspaces(workspaceMap);
+  }
 
   if ( status != "success" )
   {
@@ -383,7 +389,9 @@ void SaveWorkspaces::saveFileBrowse()
 }
 
 /**
- * Goes through all selected workspaces and maps them to a zero-error free clone
+ * Goes through all selected workspaces and maps them to a zero-error free clone,
+ * if the user has selected to do this otherwise the value of the hash is set to
+ * the same as the key
  * @param workspaces :: a QListWIdget which contains the selected workspaces
  * @returns a hash which maps the original workspace to the zero-error free workspace
  */
@@ -392,9 +400,15 @@ QHash<QString, QString> SaveWorkspaces::provideZeroFreeWorkspaces(const QListWid
   QHash<QString, QString> workspaceMap;
   for (auto it = wsList.begin(); it != wsList.end(); ++it) {
     auto wsName = (*it)->text();
-    auto cloneName = wsName + "_clone";
-    emit createZeroErrorFreeWorkspace(wsName, cloneName);
-    workspaceMap.insert(wsName, cloneName);
+    auto cloneName = wsName;
+    if (m_saveAsZeroErrorFree) {
+       cloneName += "_clone_temp";
+       emit createZeroErrorFreeWorkspace(wsName, cloneName);
+    }
+
+    if (AnalysisDataService::Instance().doesExist(cloneName.toStdString())) {
+      workspaceMap.insert(wsName, cloneName);
+    }
   }
 
   return workspaceMap;
diff --git a/Code/Mantid/scripts/SANS/ISISCommandInterface.py b/Code/Mantid/scripts/SANS/ISISCommandInterface.py
index 7368efbbddc..e5c7ba9332c 100644
--- a/Code/Mantid/scripts/SANS/ISISCommandInterface.py
+++ b/Code/Mantid/scripts/SANS/ISISCommandInterface.py
@@ -1200,7 +1200,7 @@ def DeleteZeroErrorFreeClonedWorkspace(input_workspace_name):
 
     if input_workspace_name in mtd:
         DeleteWorkspace(Workspace=input_workspace_name)
-        message = 'Sucess'
+        message = 'Success'
     else:
         message = 'Failed to delete a zero-error free workspace'
 
-- 
GitLab