BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataIOHistory.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/mainwindow/OutputDataIOHistory.cpp
6 //! @brief Defines OutputDataIOHistory classes
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 "Base/Utils/Assert.h"
19 
20 //! Static method to create info for just saved item.
21 
23 {
24  ASSERT(item);
25 
26  OutputDataSaveInfo result;
27  result.m_data = item;
28  result.m_file_name = item->fileName();
30  return result;
31 }
32 
34 {
36 }
37 
38 //! Returns true if IntensityDataItem was saved before given time.
39 
40 bool OutputDataSaveInfo::wasSavedBefore(const QDateTime& dtime) const
41 {
42  // positive number means that m_last_saved is older than dtime
43  return m_last_saved.msecsTo(dtime) > 0;
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
50  if (contains(item))
51  throw GUIHelpers::Error("OutputDataDirHistory::markAsSaved() -> Error. "
52  "Already existing item.");
53  // Don't create any history info for empty items
54  if (item->containsNonXMLData())
56 }
57 
59 {
60  // non existing item is treated as modified since last save
61  return contains(item) ? itemInfo(item).wasModifiedSinceLastSave() : true;
62 }
63 
65 {
66  for (auto& info : m_history)
67  if (info.item() == item)
68  return true;
69 
70  return false;
71 }
72 
73 //! Returns list of file names used to save all items in a history.
74 
76 {
77  QStringList result;
78 
79  for (auto& info : m_history)
80  result.append(info.fileName());
81 
82  return result;
83 }
84 
86 {
87  for (auto& info : m_history) {
88  if (info.item() == item)
89  return info;
90  }
91 
92  throw GUIHelpers::Error("OutputDataDirHistory::itemInfo() -> Error. No info exists.");
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 bool OutputDataIOHistory::hasHistory(const QString& dirname) const
98 {
99  return m_dir_history.find(dirname) == m_dir_history.end() ? false : true;
100 }
101 
103  const SaveLoadInterface* item)
104 {
105  if (!hasHistory(dirname))
106  throw GUIHelpers::Error("OutputDataIOHistory::wasModifiedSinceLastSave() -> Error. "
107  "No info for directory '"
108  + dirname + "'.");
109  return m_dir_history[dirname].wasModifiedSinceLastSave(item);
110 }
111 
112 //! Sets history for given directory. Previous history will be rewritten.
113 
114 void OutputDataIOHistory::setHistory(const QString& dirname, const OutputDataDirHistory& history)
115 {
116  ASSERT(dirname.isEmpty() == false);
117 
118  m_dir_history[dirname] = history;
119 }
120 
121 QStringList OutputDataIOHistory::savedFileNames(const QString& dirname) const
122 {
123  if (!hasHistory(dirname))
124  throw GUIHelpers::Error("OutputDataIOHistory::savedFileNames() -> Error. "
125  "No info for directory '"
126  + dirname + "'.");
127 
128  return m_dir_history[dirname].savedFileNames();
129 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines class GUIHelpers functions.
Defines OutputDataIOHistory classes.
Defines save/load interface.
Save history information for collection of items with non-XML data.
bool wasModifiedSinceLastSave(const SaveLoadInterface *item)
QVector< OutputDataSaveInfo > m_history
void markAsSaved(const SaveLoadInterface *item)
QStringList savedFileNames() const
Returns list of file names used to save all items in a history.
OutputDataSaveInfo itemInfo(const SaveLoadInterface *item) const
bool contains(const SaveLoadInterface *item)
bool wasModifiedSinceLastSave(const QString &dirname, const SaveLoadInterface *item)
bool hasHistory(const QString &dirname) const
QStringList savedFileNames(const QString &dirname) const
QMap< QString, OutputDataDirHistory > m_dir_history
< Correspondance of directory name to save history.
void setHistory(const QString &dirname, const OutputDataDirHistory &history)
Sets history for given directory. Previous history will be rewritten.
Holds information about last save for items with non-XML data.
bool wasModifiedSinceLastSave() const
bool wasSavedBefore(const QDateTime &dtime) const
Returns true if IntensityDataItem was saved before given time.
static OutputDataSaveInfo createSaved(const SaveLoadInterface *item)
Static method to create info for just saved item.
const SaveLoadInterface * item() const
const SaveLoadInterface * m_data
Purely virtual interface to handle non-XML data save and load.
virtual QString fileName() const =0
Reports file name to save/load non-XML data.
virtual bool containsNonXMLData() const =0
Checks if object owns non-XML data.
virtual QDateTime lastModified() const =0
Indicates last modification timepoint.
QString currentDateTime()
Definition: GUIHelpers.cpp:210