BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataIOService.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/mainwindow/OutputDataIOService.cpp
6 //! @brief Implements class OutputDataIOService
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
23 
24 namespace {
25 JobItem* parentJobItem(SaveLoadInterface* item);
26 } // namespace
27 
29  : QObject(parent), m_applicationModels(nullptr)
30 {
31  setObjectName("OutputDataIOService");
32 }
33 
35  : QObject(parent), m_applicationModels(nullptr)
36 {
37  setObjectName("OutputDataIOService");
38  setApplicationModels(models);
39 }
40 
42 {
43  m_applicationModels = models;
44 }
45 
47 {
50 
51  OutputDataDirHistory newHistory;
52 
53  for (auto item : nonXMLItems()) {
55  item->save(projectDir);
56  newHistory.markAsSaved(item);
57  }
58 
59  // dealing with files
60  QStringList oldFiles = m_history.savedFileNames(projectDir);
61  QStringList newFiles = newHistory.savedFileNames();
62  cleanOldFiles(projectDir, oldFiles, newFiles);
63 
64  // if oldHistory contained some deleted items, that info will be dropped here
65  m_history.setHistory(projectDir, newHistory);
66 }
67 
68 void OutputDataIOService::load(const QString& projectDir, MessageService* messageService)
69 {
70  OutputDataDirHistory newHistory;
71 
72  for (auto item : nonXMLItems()) {
73  try {
74  item->load(projectDir);
75  newHistory.markAsSaved(item);
76  // handling crash of GUI during job run and non-existing file
77  if (auto jobItem = parentJobItem(item)) {
78  if (jobItem->isRunning()) {
79  jobItem->setComments("Possible GUI crash while job was running");
80  jobItem->setStatus("Failed");
81  }
82  }
83 
84  } catch (const std::exception& ex) {
85  if (auto jobItem = parentJobItem(item)) {
86  // Handling corrupted file on disk
87  jobItem->setComments(
88  QString("Load of the data from disk failed with '%1'").arg(QString(ex.what())));
89  jobItem->setStatus("Failed");
90  }
91  if (messageService)
92  messageService->send_warning(this, QString(ex.what()));
93  else
94  throw ex;
95  }
96  }
97  m_history.setHistory(projectDir, newHistory);
98 }
99 
100 //! Returns all non-XML items available for save/load.
101 
102 QVector<SaveLoadInterface*> OutputDataIOService::nonXMLItems() const
103 {
104  QVector<SaveLoadInterface*> result;
105 
106  if (!m_applicationModels)
107  return result;
108 
109  for (auto item : m_applicationModels->nonXMLItems())
110  if (auto non_xml_item = dynamic_cast<SaveLoadInterface*>(item))
111  result.push_back(non_xml_item);
112 
113  return result;
114 }
115 
116 //! Clean old saved files.
117 //! All files in oldSaves list, which are not in newSaves list, will be removed.
118 
119 void OutputDataIOService::cleanOldFiles(const QString& projectDir, const QStringList& oldSaves,
120  const QStringList& newSaves)
121 {
122  QStringList to_remove = ProjectUtils::substract(oldSaves, newSaves);
124 }
125 
126 namespace {
127 JobItem* parentJobItem(SaveLoadInterface* item)
128 {
129  auto session_item = dynamic_cast<SessionItem*>(item); // sidecast
130  auto jobItem = dynamic_cast<const JobItem*>(ModelPath::ancestor(session_item, "JobItem"));
131  return const_cast<JobItem*>(jobItem);
132 }
133 } // namespace
Defines class holding all application models.
Defines class IntensityDataIOFactory.
Defines class JobItem.
Defines MessageService class.
Defines ModelPath namespace.
Defines class OutputDataIOService.
Defines ProjectUtils namespace.
Defines save/load interface.
QVector< SessionItem * > nonXMLItems() const
Returns all non-XML items.
The service to collect messages from different senders.
void send_warning(QObject *sender, const QString &description)
Save history information for collection of items with non-XML data.
void markAsSaved(const SaveLoadInterface *item)
QStringList savedFileNames() const
Returns list of file names used to save all items in a history.
bool wasModifiedSinceLastSave(const QString &dirname, const SaveLoadInterface *item)
bool hasHistory(const QString &dirname) const
QStringList savedFileNames(const QString &dirname) const
void setHistory(const QString &dirname, const OutputDataDirHistory &history)
Sets history for given directory. Previous history will be rewritten.
void load(const QString &projectDir, MessageService *messageService=nullptr)
void setApplicationModels(ApplicationModels *models)
QVector< SaveLoadInterface * > nonXMLItems() const
Returns all non-XML items available for save/load.
ApplicationModels * m_applicationModels
OutputDataIOHistory m_history
OutputDataIOService(QObject *parent=nullptr)
void save(const QString &projectDir)
void cleanOldFiles(const QString &projectDir, const QStringList &oldSaves, const QStringList &newSaves)
Clean old saved files.
Purely virtual interface to handle non-XML data save and load.
const SessionItem * ancestor(const SessionItem *item, const QString &requiredModelType)
Returns ancestor of given modelType for given item.
Definition: ModelPath.cpp:87
QStringList substract(const QStringList &lhs, const QStringList &rhs)
Returns list of string from lhs, which are not in rhs.
bool removeFiles(const QString &dirname, const QStringList &filenames)
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.