From c6cec6df6f3960e5ed21819e7cbf1c73f5baeaef Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Tue, 19 May 2015 10:30:26 +0200
Subject: [PATCH] Fixed logic while saving projectdocument.

---
 GUI/coregui/mainwindow/projectdocument.cpp | 181 +++++++++++----------
 GUI/coregui/utils/GUIHelpers.cpp           |   4 +-
 2 files changed, 96 insertions(+), 89 deletions(-)

diff --git a/GUI/coregui/mainwindow/projectdocument.cpp b/GUI/coregui/mainwindow/projectdocument.cpp
index 7fdabdc9b7a..c4a840be3de 100644
--- a/GUI/coregui/mainwindow/projectdocument.cpp
+++ b/GUI/coregui/mainwindow/projectdocument.cpp
@@ -34,19 +34,12 @@
 #include <QDebug>
 
 ProjectDocument::ProjectDocument()
-    : m_materialModel(0)
-    , m_instrumentModel(0)
-    , m_sampleModel(0)
-    , m_jobModel(0)
-    , m_modified(false)
+    : m_materialModel(0), m_instrumentModel(0), m_sampleModel(0), m_jobModel(0), m_modified(false)
 {
-
 }
 
 ProjectDocument::ProjectDocument(const QString &projectFileName)
-    : m_sampleModel(0)
-    , m_jobModel(0)
-    , m_modified(false)
+    : m_sampleModel(0), m_jobModel(0), m_modified(false)
 {
     setProjectFileName(projectFileName);
     qDebug() << "ProjectDocument::ProjectDocument(const QString &projectFileName)"
@@ -63,14 +56,9 @@ void ProjectDocument::setProjectFileName(const QString &projectFileName)
 }
 
 ProjectDocument::ProjectDocument(const QString &path, const QString &name)
-    : m_project_path(path)
-    , m_project_name(name)
-    , m_materialModel(0)
-    , m_sampleModel(0)
-    , m_jobModel(0)
-    , m_modified(false)
+    : m_project_path(path), m_project_name(name), m_materialModel(0), m_sampleModel(0),
+      m_jobModel(0), m_modified(false)
 {
-
 }
 
 void ProjectDocument::onDataChanged(const QModelIndex &, const QModelIndex &)
@@ -88,62 +76,70 @@ void ProjectDocument::onJobModelChanged(const QString &)
 
 void ProjectDocument::setMaterialModel(MaterialModel *materialModel)
 {
-    if(materialModel != m_materialModel) {
-        if(m_materialModel) disconnect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+    if (materialModel != m_materialModel) {
+        if (m_materialModel)
+            disconnect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+                       SLOT(onDataChanged(QModelIndex, QModelIndex)));
         m_materialModel = materialModel;
-        connect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+        connect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+                SLOT(onDataChanged(QModelIndex, QModelIndex)));
     }
 }
 
 void ProjectDocument::setInstrumentModel(InstrumentModel *model)
 {
-    if(model != m_instrumentModel) {
-        if(m_instrumentModel) disconnect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+    if (model != m_instrumentModel) {
+        if (m_instrumentModel)
+            disconnect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+                       SLOT(onDataChanged(QModelIndex, QModelIndex)));
         m_instrumentModel = model;
-        connect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+        connect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+                SLOT(onDataChanged(QModelIndex, QModelIndex)));
     }
 }
 
-
 void ProjectDocument::setSampleModel(SampleModel *model)
 {
-    if(model != m_sampleModel) {
-        if(m_sampleModel) disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+    if (model != m_sampleModel) {
+        if (m_sampleModel)
+            disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+                       SLOT(onDataChanged(QModelIndex, QModelIndex)));
         m_sampleModel = model;
-        connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+        connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+                SLOT(onDataChanged(QModelIndex, QModelIndex)));
     }
 }
 
-
 void ProjectDocument::setJobModel(JobModel *model)
 {
-    if(model != m_jobModel) {
-        if(m_jobModel) {
-            disconnect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this, SLOT(onJobModelChanged(QString)));
+    if (model != m_jobModel) {
+        if (m_jobModel) {
+            disconnect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this,
+                       SLOT(onJobModelChanged(QString)));
         }
         m_jobModel = model;
 
-        connect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this, SLOT(onJobModelChanged(QString)));
+        connect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this,
+                SLOT(onJobModelChanged(QString)));
     }
 }
 
-
 bool ProjectDocument::hasValidNameAndPath()
 {
     return (!m_project_name.isEmpty() && !m_project_path.isEmpty());
 }
 
-
 bool ProjectDocument::save()
 {
-    qDebug() << "ProjectDocument::save() -> " << getProjectName() << getProjectPath() << getProjectFileName();
+    qDebug() << "ProjectDocument::save() -> " << getProjectName() << getProjectPath()
+             << getProjectFileName();
 
     reviseOutputData();
 
     QString filename = getProjectFileName();
 
     QFile file(filename);
-    //if (!file.open(QFile::WriteOnly | QFile::Text)) {
+    // if (!file.open(QFile::WriteOnly | QFile::Text)) {
     if (!file.open(QFile::ReadWrite | QIODevice::Truncate | QFile::Text)) {
         qDebug() << "ProjectDocument::save() -> Error! Can't save file";
         return false;
@@ -160,7 +156,6 @@ bool ProjectDocument::save()
     return true;
 }
 
-
 bool ProjectDocument::load(const QString &project_file_name)
 {
     bool success_read(false);
@@ -169,41 +164,44 @@ bool ProjectDocument::load(const QString &project_file_name)
 
     QFile file(getProjectFileName());
     if (!file.open(QFile::ReadOnly | QFile::Text)) {
-        m_error_message = QString("Can't open '%1'. Wrong permissions or binary file.")
-                .arg(project_file_name);
+        m_error_message
+            = QString("Can't open '%1'. Wrong permissions or binary file.").arg(project_file_name);
         return false;
     }
 
-    try{
+    try {
         // loading project file
         success_read = readFrom(&file);
         file.close();
 
         // loading accompanying non-xml data
         loadOutputData();
-    } catch(const std::exception &ex) {
-        m_error_message.append(QString("Exception was thrown with the error message '%1'")
-                .arg(QString(ex.what())));
+    } catch (const std::exception &ex) {
+        m_error_message.append(
+            QString("Exception was thrown with the error message '%1'").arg(QString(ex.what())));
         success_read = false;
     }
 
     return success_read;
 }
 
-
 bool ProjectDocument::readFrom(QIODevice *device)
 {
     Q_ASSERT(m_materialModel);
-    disconnect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+    disconnect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+               SLOT(onDataChanged(QModelIndex, QModelIndex)));
 
     Q_ASSERT(m_instrumentModel);
-    disconnect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+    disconnect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+               SLOT(onDataChanged(QModelIndex, QModelIndex)));
 
     Q_ASSERT(m_sampleModel);
-    disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
+    disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+               SLOT(onDataChanged(QModelIndex, QModelIndex)));
 
     Q_ASSERT(m_jobModel);
-    disconnect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this, SLOT(onJobModelChanged(QString)));
+    disconnect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this,
+               SLOT(onJobModelChanged(QString)));
 
     QXmlStreamReader reader(device);
 
@@ -212,46 +210,48 @@ bool ProjectDocument::readFrom(QIODevice *device)
         if (reader.isStartElement()) {
             if (reader.name() == ProjectDocumentXML::BornAgainTag) {
                 const QString version = reader.attributes()
-                        .value(ProjectDocumentXML::BornAgainVersionAttribute).toString();
-                if(version != GUIHelpers::getBornAgainVersionString()) {
+                                            .value(ProjectDocumentXML::BornAgainVersionAttribute)
+                                            .toString();
+                if (version != GUIHelpers::getBornAgainVersionString()) {
                     m_error_message.append(
                         QString("Given project was created using BornAgain ver. %1").arg(version));
                     m_error_message.append(
                         QString(" which is different from the version %1 you are currently using.")
-                                .arg(GUIHelpers::getBornAgainVersionString()));
-                    m_error_message.append(QString(" At the moment we do not support import from older versions.\n\n"));
+                            .arg(GUIHelpers::getBornAgainVersionString()));
+                    m_error_message.append(QString(
+                        " At the moment we do not support import from older versions.\n\n"));
                 }
-            }
-            else if (reader.name() == ProjectDocumentXML::InfoTag) {
+            } else if (reader.name() == ProjectDocumentXML::InfoTag) {
                 //
-            }
-            else if(reader.name() == SessionXML::MaterialModelTag) {
+            } else if (reader.name() == SessionXML::MaterialModelTag) {
                 m_materialModel->readFrom(&reader);
 
-            }
-            else if(reader.name() == SessionXML::InstrumentModelTag) {
+            } else if (reader.name() == SessionXML::InstrumentModelTag) {
                 m_instrumentModel->readFrom(&reader);
 
-            }
-            else if(reader.name() == SessionXML::SampleModelTag) {
+            } else if (reader.name() == SessionXML::SampleModelTag) {
                 m_sampleModel->readFrom(&reader);
 
-            }
-            else if(reader.name() == SessionXML::JobModelTag) {
+            } else if (reader.name() == SessionXML::JobModelTag) {
                 m_jobModel->readFrom(&reader);
             }
         }
     }
 
     if (reader.hasError()) {
-        m_error_message.append(QString("File parse error with error message '%1").arg(reader.errorString()));
+        m_error_message.append(
+            QString("File parse error with error message '%1").arg(reader.errorString()));
         return false;
     }
 
-    connect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
-    connect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
-    connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
-    connect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this, SLOT(onJobModelChanged(QString)));
+    connect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+            SLOT(onDataChanged(QModelIndex, QModelIndex)));
+    connect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+            SLOT(onDataChanged(QModelIndex, QModelIndex)));
+    connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
+            SLOT(onDataChanged(QModelIndex, QModelIndex)));
+    connect(m_jobModel->getJobQueueData(), SIGNAL(jobIsFinished(QString)), this,
+            SLOT(onJobModelChanged(QString)));
     return true;
 }
 
@@ -286,14 +286,13 @@ bool ProjectDocument::writeTo(QIODevice *device)
     return true;
 }
 
-
 //! returns project file name
 //!
 //! if ProjectPath=/home/username and ProjectName=MultiLayer then project file
 //! will be /home/username/MultiLayer/MultiLayer.pro
 QString ProjectDocument::getProjectFileName()
 {
-    QString result = getProjectPath() + "/" + getProjectName() + "/"+getProjectName()+".pro";
+    QString result = getProjectPath() + "/" + getProjectName() + "/" + getProjectName() + ".pro";
     return result;
 }
 
@@ -305,23 +304,34 @@ QString ProjectDocument::getProjectDir()
 }
 
 //! Adjusts name of IntensityData item to possibly changed name of JobItem. Take care of old
-//! *.int files in project directory
+//! *.int files in project directory by removing them.
 void ProjectDocument::reviseOutputData()
 {
-    for(int i=0; i<m_jobModel->rowCount(QModelIndex()); ++i) {
-        JobItem *jobItem = m_jobModel->getJobItemForIndex(m_jobModel->index(i,0, QModelIndex()));
+    //    QMap<QString, int> name_to_copy_number;
+
+    for (int i = 0; i < m_jobModel->rowCount(QModelIndex()); ++i) {
+        JobItem *jobItem = m_jobModel->getJobItemForIndex(m_jobModel->index(i, 0, QModelIndex()));
         IntensityDataItem *dataItem = jobItem->getIntensityDataItem();
-        if(dataItem) {
+        if (dataItem) {
             // handling case when user has renamed jobItem and we have to clean previous
             // *.int file
             QString filename = getProjectDir() + "/" + dataItem->itemName();
             QFile fin(filename);
-            if(fin.exists()) {
+            if (fin.exists()) {
                 fin.remove();
             }
 
             // making new name of *.int file from jobItem name
             dataItem->setNameFromProposed(jobItem->itemName());
+
+            // handling name repetition
+            //            int ncopy = name_to_copy_number[dataItem->itemName()];
+            //            if(ncopy > 0) {
+            //                QString new_name =
+            //                QString("%1(%2)").arg(dataItem->itemName()).arg(ncopy);
+            //                dataItem->setItemName(new_name);
+            //            }
+            //            name_to_copy_number[dataItem->itemName()] = ncopy+1;
         }
     }
 }
@@ -330,39 +340,34 @@ void ProjectDocument::reviseOutputData()
 void ProjectDocument::saveOutputData()
 {
     Q_ASSERT(m_jobModel);
-    for(int i=0; i<m_jobModel->rowCount(QModelIndex()); ++i) {
-        JobItem *jobItem = m_jobModel->getJobItemForIndex(m_jobModel->index(i,0, QModelIndex()));
+    for (int i = 0; i < m_jobModel->rowCount(QModelIndex()); ++i) {
+        JobItem *jobItem = m_jobModel->getJobItemForIndex(m_jobModel->index(i, 0, QModelIndex()));
         IntensityDataItem *dataItem = jobItem->getIntensityDataItem();
-        if(dataItem) {
+        if (dataItem) {
             QString filename = getProjectDir() + "/" + dataItem->itemName();
             const OutputData<double> *data = dataItem->getOutputData();
-            if(data) {
+            if (data) {
                 IntensityDataIOFactory::writeIntensityData(*data, filename.toStdString());
             }
         }
     }
 }
 
-
 //! load OutputData from project directory
 void ProjectDocument::loadOutputData()
 {
     Q_ASSERT(m_jobModel);
 
-    for(int i=0; i<m_jobModel->rowCount(QModelIndex()); ++i) {
-        JobItem *jobItem = m_jobModel->getJobItemForIndex(m_jobModel->index(i,0, QModelIndex()));
+    for (int i = 0; i < m_jobModel->rowCount(QModelIndex()); ++i) {
+        JobItem *jobItem = m_jobModel->getJobItemForIndex(m_jobModel->index(i, 0, QModelIndex()));
         IntensityDataItem *dataItem = jobItem->getIntensityDataItem();
-        if(dataItem) {
+        if (dataItem) {
             QString filename = getProjectDir() + "/" + dataItem->itemName();
             QFileInfo info(filename);
-            if(info.exists()) {
-                jobItem->getIntensityDataItem()->setOutputData(IntensityDataIOFactory::readIntensityData(filename.toStdString()));
+            if (info.exists()) {
+                jobItem->getIntensityDataItem()->setOutputData(
+                    IntensityDataIOFactory::readIntensityData(filename.toStdString()));
             }
         }
     }
-
-
 }
-
-
-
diff --git a/GUI/coregui/utils/GUIHelpers.cpp b/GUI/coregui/utils/GUIHelpers.cpp
index d709952a9a7..3cb00749948 100644
--- a/GUI/coregui/utils/GUIHelpers.cpp
+++ b/GUI/coregui/utils/GUIHelpers.cpp
@@ -16,8 +16,11 @@
 #include "GUIHelpers.h"
 #include "BAVersion.h"
 #include <QApplication>
+#include <QFile>
+#include <QRegExp>
 #include <QMessageBox>
 #include <QPushButton>
+#include <QDebug>
 
 namespace GUIHelpers {
 
@@ -147,5 +150,4 @@ QString getValidFileName(const QString &proposed_name)
 }
 
 
-
 } // namespace GUIHelpers
-- 
GitLab