BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
IOHistory.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/IO/IOHistory.cpp
6 //! @brief Defines DatafieldIOHistory 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/Util/Assert.h"
18 #include "GUI/Util/Error.h"
19 
20 //! Static method to create info for just saved item.
21 
23 {
24  ASSERT(item);
25 
26  DatafieldSaveInfo result;
27  result.m_data = item;
28  result.m_file_name = item->fileName();
29  result.m_last_saved = QDateTime::currentDateTime();
30  return result;
31 }
32 
34 {
36 }
37 
38 //! Returns true if IntensityDataItem was saved before given time.
39 
40 bool DatafieldSaveInfo::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 Error("DatafieldDirHistory::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 (const auto& info : m_history)
80  result.append(info.fileName());
81 
82  return result;
83 }
84 
86 {
87  for (const auto& info : m_history) {
88  if (info.item() == item)
89  return info;
90  }
91 
92  throw Error("DatafieldDirHistory::itemInfo() -> Error. No info exists.");
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 bool DatafieldIOHistory::hasHistory(const QString& dirname) const
98 {
99  return m_dir_history.find(dirname) != m_dir_history.end();
100 }
101 
103  const SaveLoadInterface* item)
104 {
105  if (!hasHistory(dirname))
106  throw Error("DatafieldIOHistory::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 DatafieldIOHistory::setHistory(const QString& dirname, const DatafieldDirHistory& history)
115 {
116  ASSERT(!dirname.isEmpty());
117 
118  m_dir_history[dirname] = history;
119 }
120 
121 QStringList DatafieldIOHistory::savedFileNames(const QString& dirname) const
122 {
123  if (!hasHistory(dirname))
124  throw Error("DatafieldIOHistory::savedFileNames() -> Error. "
125  "No info for directory '"
126  + dirname + "'.");
127 
128  return m_dir_history[dirname].savedFileNames();
129 }
Defines error class.
Defines DatafieldIOHistory classes.
Defines save/load interface.
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
DatafieldSaveInfo itemInfo(const SaveLoadInterface *item) const
Definition: IOHistory.cpp:85
bool wasModifiedSinceLastSave(const SaveLoadInterface *item)
Definition: IOHistory.cpp:58
bool contains(const SaveLoadInterface *item)
Definition: IOHistory.cpp:64
QVector< DatafieldSaveInfo > m_history
Definition: IOHistory.h:66
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
QMap< QString, DatafieldDirHistory > m_dir_history
< Correspondence of directory name to save history.
Definition: IOHistory.h:83
bool hasHistory(const QString &dirname) const
Definition: IOHistory.cpp:97
Holds information about last save for items with non-XML data.
Definition: IOHistory.h:26
static DatafieldSaveInfo createSaved(const SaveLoadInterface *item)
Static method to create info for just saved item.
Definition: IOHistory.cpp:22
const SaveLoadInterface * m_data
Definition: IOHistory.h:46
const SaveLoadInterface * item() const
Definition: IOHistory.h:35
QDateTime m_last_saved
Definition: IOHistory.h:44
bool wasSavedBefore(const QDateTime &dtime) const
Returns true if IntensityDataItem was saved before given time.
Definition: IOHistory.cpp:40
QString m_file_name
Definition: IOHistory.h:45
bool wasModifiedSinceLastSave() const
Definition: IOHistory.cpp:33
Abstract base class 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.