BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
IOService.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Project/IOService.cpp
6 //! @brief Implements class DatafieldIOService
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 
16 #include "GUI/Model/Job/JobItem.h"
22 #include <QFile>
23 #include <QSet>
24 
25 namespace {
26 
27 JobItem* parentJobItem(SaveLoadInterface* item)
28 {
29  auto* session_item = dynamic_cast<SessionItem*>(item); // sidecast
30  return dynamic_cast<JobItem*>(GUI::Model::Path::ancestor(session_item, JobItem::M_TYPE));
31 }
32 
33 } // namespace
34 
36  : QObject(parent)
37  , m_applicationModels(nullptr)
38 {
39  setObjectName("DatafieldIOService");
40 }
41 
43  : QObject(parent)
44  , m_applicationModels(nullptr)
45 {
46  setObjectName("DatafieldIOService");
47  setApplicationModels(models);
48 }
49 
51 {
52  m_applicationModels = models;
53 }
54 
56 {
59 
60  DatafieldDirHistory newHistory;
61 
62  for (SaveLoadInterface* item : dataInterfaces()) {
64  item->saveData(projectDir);
65  newHistory.markAsSaved(item);
66  }
67 
68  // dealing with files
69  QStringList oldFiles = m_history.savedFileNames(projectDir);
70  QStringList newFiles = newHistory.savedFileNames();
71  cleanOldFiles(projectDir, oldFiles, newFiles);
72 
73  // if oldHistory contained some deleted items, that info will be dropped here
74  m_history.setHistory(projectDir, newHistory);
75 }
76 
77 void DatafieldIOService::loadDataFiles(const QString& projectDir, MessageService* messageService)
78 {
79  DatafieldDirHistory newHistory;
80 
81  for (SaveLoadInterface* item : dataInterfaces()) {
82  try {
83  item->loadData(projectDir);
84  newHistory.markAsSaved(item);
85  // handling crash of GUI during job run and non-existing file
86  if (auto* jobItem = parentJobItem(item)) {
87  if (jobItem->isRunning()) {
88  jobItem->setComments("Possible GUI crash while job was running");
89  jobItem->setStatus(JobStatus::Failed);
90  }
91  }
92  } catch (const std::exception& ex) {
93  if (auto* jobItem = parentJobItem(item)) {
94  // Handling corrupted file on disk
95  jobItem->setComments(
96  QString("Load of the data from disk failed with '%1'").arg(QString(ex.what())));
97  jobItem->setStatus(JobStatus::Failed);
98  }
99  if (!messageService)
100  throw ex;
101  messageService->addWarning(this, QString(ex.what()));
102  }
103  }
104  m_history.setHistory(projectDir, newHistory);
105 }
106 
107 //! Returns all non-XML items available for save/load.
108 
109 QVector<SaveLoadInterface*> DatafieldIOService::dataInterfaces() const
110 {
111  QVector<SaveLoadInterface*> result;
112 
113  if (!m_applicationModels)
114  return result;
115 
116  for (auto* item : m_applicationModels->nonXMLItems())
117  if (auto* non_xml_item = dynamic_cast<SaveLoadInterface*>(item))
118  result.push_back(non_xml_item);
119 
120  return result;
121 }
122 
123 //! Clean old saved files.
124 //! All files in oldSaves list, which are not in newSaves list, will be removed.
125 
126 void DatafieldIOService::cleanOldFiles(const QString& projectDir, const QStringList& oldSaves,
127  const QStringList& newSaves)
128 {
129  const auto toRemove = oldSaves.toSet().subtract(newSaves.toSet());
130  for (const auto& name : toRemove)
131  QFile::remove(projectDir + "/" + name);
132 }
Defines class holding all application models.
Defines class DatafieldIOService.
Defines class JobItem.
@ Failed
the job aborted because it hit an error
Defines MessageService class.
Defines namespace GUI::Model::Path.
Defines namespace GUI::Project::Utils.
Defines save/load interface.
QVector< SessionItem * > nonXMLItems() const
Returns all non-XML items.
Save history information for collection of items with non-XML data.
Definition: IOHistory.h:51
QStringList savedFileNames() const
Returns list of file names used to save all items in a history.
Definition: IOHistory.cpp:75
void markAsSaved(const SaveLoadInterface *item)
Definition: IOHistory.cpp:48
void setHistory(const QString &dirname, const DatafieldDirHistory &history)
Sets history for given directory. Previous history will be rewritten.
Definition: IOHistory.cpp:114
bool wasModifiedSinceLastSave(const QString &dirname, const SaveLoadInterface *item)
Definition: IOHistory.cpp:102
QStringList savedFileNames(const QString &dirname) const
Definition: IOHistory.cpp:121
bool hasHistory(const QString &dirname) const
Definition: IOHistory.cpp:97
ApplicationModels * m_applicationModels
Definition: IOService.h:49
void setApplicationModels(ApplicationModels *models)
Definition: IOService.cpp:50
QVector< SaveLoadInterface * > dataInterfaces() const
Returns all non-XML items available for save/load.
Definition: IOService.cpp:109
DatafieldIOHistory m_history
Definition: IOService.h:48
void save(const QString &projectDir)
Definition: IOService.cpp:55
DatafieldIOService(QObject *parent=nullptr)
Definition: IOService.cpp:35
void cleanOldFiles(const QString &projectDir, const QStringList &oldSaves, const QStringList &newSaves)
Clean old saved files. All files in oldSaves list, which are not in newSaves list,...
Definition: IOService.cpp:126
void loadDataFiles(const QString &projectDir, MessageService *messageService=nullptr)
Definition: IOService.cpp:77
static constexpr auto M_TYPE
Definition: JobItem.h:62
The service to collect messages from different senders.
void addWarning(QObject *sender, const QString &description)
Abstract base class to handle non-XML data save and load.
Base class for a GUI data item.
Definition: SessionItem.h:204
SessionItem * ancestor(SessionItem *item, const QString &requiredModelType)
Returns ancestor of given modelType for given item.
Definition: ModelPath.cpp:32
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString const & name(EShape k)
Definition: particles.cpp:20