From 93f66824fce212b9f30d2f5bb069da44af027d39 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 24 Mar 2014 15:27:51 +0100
Subject: [PATCH] OutputDataItem is serialized into project directory.

---
 GUI/coregui/Models/JobItem.cpp             | 21 +++++++++-
 GUI/coregui/Models/JobItem.h               |  2 +-
 GUI/coregui/Models/JobQueueModel.cpp       |  1 -
 GUI/coregui/Models/OutputDataItem.h        |  1 +
 GUI/coregui/mainwindow/projectdocument.cpp | 45 ++++++++++++++++++++++
 GUI/coregui/mainwindow/projectdocument.h   |  6 +++
 6 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/GUI/coregui/Models/JobItem.cpp b/GUI/coregui/Models/JobItem.cpp
index a49c7d1c9ca..46f6c22b71b 100644
--- a/GUI/coregui/Models/JobItem.cpp
+++ b/GUI/coregui/Models/JobItem.cpp
@@ -10,12 +10,14 @@
 
 
 JobItem::JobItem(QString name)
-    : m_name(name)
-    , m_status(Idle)
+    : m_status(Idle)
     , m_progress(0)
 {
     OutputDataItem *dataItem = new OutputDataItem();
     m_data_items.append(dataItem);
+
+    setName(name);
+
     connect(dataItem, SIGNAL(modified()), this, SLOT(onDataItemModified()));
     m_status_list << "" << "running" << "completed" << "canceled";
 }
@@ -34,6 +36,21 @@ void JobItem::clear()
 }
 
 
+void JobItem::setName(QString name)
+{
+    m_name = name;
+    // setting names for OutputDataItem's
+    int n_data(0);
+    foreach(OutputDataItem *dataItem, m_data_items) {
+        QString dataFileName = QString("data_%1_%2.txt").arg(m_name, QString::number(n_data));
+        dataItem->setName(dataFileName);
+        ++n_data;
+    }
+
+    emit modified(this);
+}
+
+
 QString JobItem::getStatusString() const
 {
     return m_status_list.at(int(m_status));
diff --git a/GUI/coregui/Models/JobItem.h b/GUI/coregui/Models/JobItem.h
index cd55b6bcdd2..8206309c480 100644
--- a/GUI/coregui/Models/JobItem.h
+++ b/GUI/coregui/Models/JobItem.h
@@ -56,7 +56,7 @@ signals:
     void modified(JobItem *);
 
 public slots:
-    void setName(QString name) { m_name = name; emit modified(this); }
+    void setName(QString name);
     void setBeginTime(QString begin_time) { m_begin_time = begin_time; emit modified(this);}
     void setEndTime(QString end_time) { m_end_time = end_time; emit modified(this);}
     void setComments(QString comments) { m_comments = comments; emit modified(this);}
diff --git a/GUI/coregui/Models/JobQueueModel.cpp b/GUI/coregui/Models/JobQueueModel.cpp
index 5bda036cd34..2a4b28ba57c 100644
--- a/GUI/coregui/Models/JobQueueModel.cpp
+++ b/GUI/coregui/Models/JobQueueModel.cpp
@@ -373,4 +373,3 @@ void JobQueueModel::removeJob(const QModelIndex &index)
     m_queue_data->removeJob(identifier);
 }
 
-
diff --git a/GUI/coregui/Models/OutputDataItem.h b/GUI/coregui/Models/OutputDataItem.h
index 2520720e1a7..724b5cb47e8 100644
--- a/GUI/coregui/Models/OutputDataItem.h
+++ b/GUI/coregui/Models/OutputDataItem.h
@@ -4,6 +4,7 @@
 #include <QObject>
 #include <QString>
 #include "OutputData.h"
+#include "OutputDataIOFactory.h"
 
 class QXmlStreamWriter;
 class QXmlStreamReader;
diff --git a/GUI/coregui/mainwindow/projectdocument.cpp b/GUI/coregui/mainwindow/projectdocument.cpp
index 1299ebb3250..36678ca41d5 100644
--- a/GUI/coregui/mainwindow/projectdocument.cpp
+++ b/GUI/coregui/mainwindow/projectdocument.cpp
@@ -1,5 +1,7 @@
 #include "projectdocument.h"
 #include "JobQueueModel.h"
+#include "JobItem.h"
+#include "OutputDataItem.h"
 #include <QFile>
 #include <QTextStream>
 #include <QFileInfo>
@@ -90,6 +92,8 @@ bool ProjectDocument::save()
     writeTo(&file);
     file.close();
 
+    saveOutputData();
+
     m_modified = false;
     emit modified();
 
@@ -114,6 +118,8 @@ bool ProjectDocument::load()
     bool success_read = readFrom(&file);
     file.close();
 
+    loadOutputData();
+
     return success_read;
 }
 
@@ -201,3 +207,42 @@ QString ProjectDocument::getProjectDir()
     return result;
 }
 
+
+//! saves OutputData into project directory
+void ProjectDocument::saveOutputData()
+{
+    Q_ASSERT(m_jobQueueModel);
+
+    for(int i=0; i<m_jobQueueModel->rowCount(); ++i) {
+        JobItem *jobItem = m_jobQueueModel->getJobItemForIndex(m_jobQueueModel->index(i,0));
+        OutputDataItem *dataItem = jobItem->getOutputDataItem();
+        if(dataItem) {
+            QString filename = getProjectDir() + "/" + dataItem->getName();
+            const OutputData<double> *data = dataItem->getOutputData();
+            if(data) {
+                OutputDataIOFactory::writeIntensityData(*data, filename.toStdString());
+            }
+        }
+
+    }
+}
+
+
+//! load OutputData from project directory
+void ProjectDocument::loadOutputData()
+{
+    for(int i=0; i<m_jobQueueModel->rowCount(); ++i) {
+        JobItem *jobItem = m_jobQueueModel->getJobItemForIndex(m_jobQueueModel->index(i,0));
+        OutputDataItem *dataItem = jobItem->getOutputDataItem();
+        if(dataItem) {
+            QString filename = getProjectDir() + "/" + dataItem->getName();
+            QFileInfo info(filename);
+            if(info.exists()) {
+                jobItem->getOutputDataItem()->setOutputData(OutputDataIOFactory::readIntensityData(filename.toStdString()));
+            }
+        }
+    }
+}
+
+
+
diff --git a/GUI/coregui/mainwindow/projectdocument.h b/GUI/coregui/mainwindow/projectdocument.h
index 450b53fc0aa..a0ef213accc 100644
--- a/GUI/coregui/mainwindow/projectdocument.h
+++ b/GUI/coregui/mainwindow/projectdocument.h
@@ -7,6 +7,7 @@
 class JobQueueModel;
 class QIODevice;
 class QModelIndex;
+class JobItem;
 
 
 namespace ProjectDocumentXML
@@ -63,6 +64,11 @@ private:
     bool writeTo(QIODevice *device);
     bool readFrom(QIODevice *device);
 
+    void saveOutputData();
+    void loadOutputData();
+    //QString getOutputDataFileName(JobItem *item);
+
+
     QString m_project_path;
     QString m_project_name;
     JobQueueModel *m_jobQueueModel;
-- 
GitLab