BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DataItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/DataItem.cpp
6 //! @brief Implements class IntensityDataItem
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 
20 
21 const QString DataItem::P_FILE_NAME = "FileName";
22 const QString DataItem::P_AXES_UNITS = "Axes Units";
23 
25 {
26  std::unique_lock<std::mutex> lock(m_update_data_mutex);
27  m_data.reset(data);
28 }
29 
30 void DataItem::setRawDataVector(std::vector<double> data)
31 {
32  if (m_data->getAllocatedSize() != data.size())
33  throw GUIHelpers::Error("DataItem::setRawDataVector() -> Error. "
34  "Different data size.");
35  std::unique_lock<std::mutex> lock(m_update_data_mutex);
36  m_data->setRawDataVector(std::move(data));
38 }
39 
40 QString DataItem::fileName() const
41 {
42  return getItemValue(P_FILE_NAME).toString();
43 }
44 
45 void DataItem::setFileName(const QString& filename)
46 {
48 }
49 
50 QDateTime DataItem::lastModified() const
51 {
52  return m_last_modified;
53 }
54 
56 {
57  return static_cast<bool>(m_data);
58 }
59 
60 bool DataItem::load(const QString& projectDir)
61 {
62  QString filename = fileName(projectDir);
63  auto data = IntensityDataIOFactory::readOutputData(filename.toStdString());
64  if (!data)
65  return false;
66  setOutputData(data);
67  return true;
68 }
69 
70 bool DataItem::save(const QString& projectDir)
71 {
72  if (!containsNonXMLData())
73  return false;
74 
75  std::unique_lock<std::mutex> lock(m_update_data_mutex);
76  std::unique_ptr<OutputData<double>> clone(getOutputData()->clone());
77  lock.unlock();
79  return true;
80 }
81 
82 void DataItem::setLastModified(const QDateTime& dtime)
83 {
84  m_last_modified = dtime;
85 }
86 
88 {
90  return combo.getValue();
91 }
92 
93 DataItem::DataItem(const QString& modelType) : SessionItem(modelType)
94 {
95  // name of the file used to serialize given IntensityDataItem
96  addProperty(P_FILE_NAME, "undefined")->setVisible(false);
97 
98  ComboProperty units = ComboProperty() << "nbins";
100 
101  mapper()->setOnPropertyChange([this](const QString& name) {
102  if (name == P_FILE_NAME)
104  });
105 
106  mapper()->setOnValueChange([this]() {
107  // OutputData was modified
109  });
110 }
Defines class ComboProperty.
Declares class DataItem.
Defines class GUIHelpers functions.
Defines ImportDataInfo helper struct.
Defines class IntensityDataIOFactory.
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
QVariant variant() const
Constructs variant enclosing given ComboProperty.
QString getValue() const
OutputData< double > * getOutputData()
Definition: DataItem.h:36
static const QString P_FILE_NAME
Definition: DataItem.h:31
virtual void setOutputData(OutputData< double > *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:24
QDateTime lastModified() const override
Indicates last modification timepoint.
Definition: DataItem.cpp:50
QDateTime m_last_modified
Definition: DataItem.h:73
void setLastModified(const QDateTime &dtime)
Definition: DataItem.cpp:82
bool load(const QString &projectDir) override
Loads non-XML data from projectDir and returns success flag.
Definition: DataItem.cpp:60
DataItem(const QString &modelType)
Definition: DataItem.cpp:93
bool containsNonXMLData() const override
Checks if object owns non-XML data.
Definition: DataItem.cpp:55
QString selectedAxesUnits() const
Definition: DataItem.cpp:87
void setRawDataVector(std::vector< double > data)
Sets the raw data vector from external source Checks only the equality of data size,...
Definition: DataItem.cpp:30
std::mutex m_update_data_mutex
Definition: DataItem.h:74
QString fileName() const override
Reports file name to save/load non-XML data.
Definition: DataItem.cpp:40
static const QString P_AXES_UNITS
Definition: DataItem.h:34
std::unique_ptr< OutputData< double > > m_data
simulation results
Definition: DataItem.h:72
void setFileName(const QString &filename)
Definition: DataItem.cpp:45
bool save(const QString &projectDir) override
Saves non-XML data in projectDir and returns success flag.
Definition: DataItem.cpp:70
static OutputData< double > * readOutputData(const std::string &file_name)
Reads file and returns newly created OutputData object.
static void writeOutputData(const OutputData< double > &data, const std::string &file_name)
Writes OutputData in file.
void setOnValueChange(std::function< void(void)> f, const void *caller=0)
Definition: ModelMapper.cpp:30
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
void setVisible(bool enabled)
Flags accessors.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.
void emitDataChanged(int role=Qt::DisplayRole)
Notify model about data changes.
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
QString currentDateTime()
Definition: GUIHelpers.cpp:210
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString const & name(EShape k)
Definition: particles.cpp:21