BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DatafieldIOService Class Reference

Description

Provide read/write of heavy data files in a separate thread.

Listens all models and keep tracks of changes in items. Provides logic to not to re-save already saved data.

Definition at line 30 of file IOService.h.

Inheritance diagram for DatafieldIOService:
[legend]
Collaboration diagram for DatafieldIOService:
[legend]

Public Member Functions

 DatafieldIOService (ApplicationModels *models, QObject *parent=nullptr)
 
 DatafieldIOService (QObject *parent=nullptr)
 
QVector< SaveLoadInterface * > dataInterfaces () const
 Returns all non-XML items available for save/load. More...
 
void loadDataFiles (const QString &projectDir, MessageService *messageService=nullptr)
 
void save (const QString &projectDir)
 
void setApplicationModels (ApplicationModels *models)
 

Private Member Functions

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, will be removed. More...
 

Private Attributes

ApplicationModelsm_applicationModels
 
DatafieldIOHistory m_history
 

Constructor & Destructor Documentation

◆ DatafieldIOService() [1/2]

DatafieldIOService::DatafieldIOService ( QObject *  parent = nullptr)
explicit

Definition at line 35 of file IOService.cpp.

36  : QObject(parent)
37  , m_applicationModels(nullptr)
38 {
39  setObjectName("DatafieldIOService");
40 }
ApplicationModels * m_applicationModels
Definition: IOService.h:49

◆ DatafieldIOService() [2/2]

DatafieldIOService::DatafieldIOService ( ApplicationModels models,
QObject *  parent = nullptr 
)
explicit

Definition at line 42 of file IOService.cpp.

43  : QObject(parent)
44  , m_applicationModels(nullptr)
45 {
46  setObjectName("DatafieldIOService");
47  setApplicationModels(models);
48 }
void setApplicationModels(ApplicationModels *models)
Definition: IOService.cpp:50

References setApplicationModels().

Here is the call graph for this function:

Member Function Documentation

◆ cleanOldFiles()

void DatafieldIOService::cleanOldFiles ( const QString &  projectDir,
const QStringList &  oldSaves,
const QStringList &  newSaves 
)
private

Clean old saved files. All files in oldSaves list, which are not in newSaves list, will be removed.

Definition at line 126 of file IOService.cpp.

128 {
129  const auto toRemove = oldSaves.toSet().subtract(newSaves.toSet());
130  for (const auto& name : toRemove)
131  QFile::remove(projectDir + "/" + name);
132 }
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString const & name(EShape k)
Definition: particles.cpp:20

References GUI::RealSpace::Particles::name(), and GUI::Project::Utils::projectDir().

Referenced by save().

Here is the call graph for this function:

◆ dataInterfaces()

QVector< SaveLoadInterface * > DatafieldIOService::dataInterfaces ( ) const

Returns all non-XML items available for save/load.

Definition at line 109 of file IOService.cpp.

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 }
QVector< SessionItem * > nonXMLItems() const
Returns all non-XML items.
Abstract base class to handle non-XML data save and load.

References m_applicationModels, and ApplicationModels::nonXMLItems().

Referenced by ProjectDocument::hasData(), loadDataFiles(), and save().

Here is the call graph for this function:

◆ loadDataFiles()

void DatafieldIOService::loadDataFiles ( const QString &  projectDir,
MessageService messageService = nullptr 
)

Definition at line 77 of file IOService.cpp.

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 }
@ Failed
the job aborted because it hit an error
Save history information for collection of items with non-XML data.
Definition: IOHistory.h:51
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
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 addWarning(QObject *sender, const QString &description)

References MessageService::addWarning(), dataInterfaces(), Failed, m_history, DatafieldDirHistory::markAsSaved(), GUI::Project::Utils::projectDir(), and DatafieldIOHistory::setHistory().

Referenced by ProjectDocument::loadProjectFile().

Here is the call graph for this function:

◆ save()

void DatafieldIOService::save ( const QString &  projectDir)

Definition at line 55 of file IOService.cpp.

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 }
QStringList savedFileNames() const
Returns list of file names used to save all items in a history.
Definition: IOHistory.cpp:75
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
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

References cleanOldFiles(), dataInterfaces(), DatafieldIOHistory::hasHistory(), m_history, DatafieldDirHistory::markAsSaved(), GUI::Project::Utils::projectDir(), DatafieldDirHistory::savedFileNames(), DatafieldIOHistory::savedFileNames(), DatafieldIOHistory::setHistory(), and DatafieldIOHistory::wasModifiedSinceLastSave().

Referenced by ProjectDocument::saveProjectData().

Here is the call graph for this function:

◆ setApplicationModels()

void DatafieldIOService::setApplicationModels ( ApplicationModels models)

Definition at line 50 of file IOService.cpp.

51 {
52  m_applicationModels = models;
53 }

References m_applicationModels.

Referenced by DatafieldIOService(), and ProjectDocument::ProjectDocument().

Member Data Documentation

◆ m_applicationModels

ApplicationModels* DatafieldIOService::m_applicationModels
private

Definition at line 49 of file IOService.h.

Referenced by dataInterfaces(), and setApplicationModels().

◆ m_history

DatafieldIOHistory DatafieldIOService::m_history
private

Definition at line 48 of file IOService.h.

Referenced by loadDataFiles(), and save().


The documentation for this class was generated from the following files: