BornAgain  1.19.79
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/Model/Data/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 
16 #include "Device/IO/IOFactory.h"
17 #include "GUI/Util/ComboProperty.h"
18 #include "GUI/Util/Error.h"
19 
20 void DataItem::setDatafield(Datafield* data)
21 {
22  std::unique_lock<std::mutex> lock(m_update_data_mutex);
23  m_data.reset(data);
24 
25  setLastModified(QDateTime::currentDateTime());
26  emit datafieldChanged(data);
27 }
28 
29 void DataItem::setRawDataVector(const std::vector<double>& data)
30 {
31  if (m_data->size() != data.size())
32  throw Error("DataItem::setVector() -> Error. "
33  "Different data size.");
34  std::unique_lock<std::mutex> lock(m_update_data_mutex);
35  m_data->setVector(data);
36 
37  setLastModified(QDateTime::currentDateTime());
38  emit rawDataVectorChanged(data);
39 }
40 
41 QString DataItem::fileName() const
42 {
43  return getItemValue(P_FILE_NAME).toString();
44 }
45 
46 void DataItem::setFileName(const QString& filename)
47 {
48  setItemValue(P_FILE_NAME, filename);
49 
50  setLastModified(QDateTime::currentDateTime());
51  emit fileNameChanged(filename);
52 }
53 
54 QDateTime DataItem::lastModified() const
55 {
56  return m_last_modified;
57 }
58 
60 {
61  return static_cast<bool>(m_data);
62 }
63 
64 bool DataItem::loadData(const QString& projectDir)
65 {
66  const QString filename = fileName(projectDir);
67  auto* data = IOFactory::readDatafield(filename.toStdString());
68  if (!data)
69  return false;
70  setDatafield(data);
71  return true;
72 }
73 
74 bool DataItem::saveData(const QString& projectDir)
75 {
76  if (!containsNonXMLData())
77  return false;
78 
79  std::unique_lock<std::mutex> lock(m_update_data_mutex);
80  std::unique_ptr<Datafield> clone(getDatafield()->clone());
81  lock.unlock();
82  IOFactory::writeDatafield(*clone, fileName(projectDir).toStdString());
83  return true;
84 }
85 
86 void DataItem::setLastModified(const QDateTime& dtime)
87 {
88  m_last_modified = dtime;
89 }
90 
91 QString DataItem::selectedCoords() const
92 {
93  return getItemValue(P_AXES_UNITS).value<ComboProperty>().getValue();
94 }
95 
97 {
99 
100  emit axesUnitsChanged(units);
101 }
102 
104 {
105  return getItem(P_AXES_UNITS);
106 }
107 
109 {
111 }
112 
114 {
115  return name == P_AXES_UNITS;
116 }
117 
118 void DataItem::updateAxesUnits(SessionItem* item, const QString& name,
119  InstrumentItem* instrumentItem)
120 {
121  DataItem* dataItem = dynamic_cast<DataItem*>(item);
122  if (dataItem && isAxesUnitsPropertyName(name)) {
123  dataItem->updateCoords(instrumentItem);
124  }
125 }
126 
127 DataItem::DataItem(const QString& modelType)
128  : SessionItem(modelType)
129 {
130  // name of the file used to serialize given IntensityDataItem
131  addProperty(P_FILE_NAME, "undefined");
132 
133  addProperty(P_AXES_UNITS, ComboProperty::fromList({"nbins"}).variant());
134 }
Defines class ComboProperty.
Declares class DataItem.
Defines error class.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
static ComboProperty fromList(const QStringList &values, const QString &current_value="")
QVariant variant() const
Constructs variant enclosing given ComboProperty.
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
QDateTime lastModified() const override
Indicates last modification timepoint.
Definition: DataItem.cpp:54
static bool isAxesUnitsPropertyName(const QString &name)
Definition: DataItem.cpp:113
static constexpr auto P_AXES_UNITS
Definition: DataItem.h:38
virtual void setDatafield(Datafield *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:20
QDateTime m_last_modified
Definition: DataItem.h:89
static void updateAxesUnits(SessionItem *item, const QString &name, InstrumentItem *instrumentItem)
Definition: DataItem.cpp:118
void setLastModified(const QDateTime &dtime)
Definition: DataItem.cpp:86
DataItem(const QString &modelType)
Definition: DataItem.cpp:127
void datafieldChanged(Datafield *data)
Datafield * getDatafield()
Definition: DataItem.h:41
QString selectedCoords() const
Definition: DataItem.cpp:91
bool containsNonXMLData() const override
Checks if object owns non-XML data.
Definition: DataItem.cpp:59
bool saveData(const QString &projectDir) override
Saves non-XML data in projectDir and returns success flag.
Definition: DataItem.cpp:74
void setAxesUnits(const ComboProperty &units)
Definition: DataItem.cpp:96
virtual void updateCoords(const InstrumentItem *instrument)=0
std::mutex m_update_data_mutex
Definition: DataItem.h:90
SessionItem * getAxesUnitsItem() const
Definition: DataItem.cpp:103
void rawDataVectorChanged(const std::vector< double > &data)
void fileNameChanged(const QString &filename)
QString fileName() const override
Reports file name to save/load non-XML data.
Definition: DataItem.cpp:41
void setRawDataVector(const std::vector< double > &data)
Sets the raw data vector from external source. Checks only the equality of data size; no dimension ch...
Definition: DataItem.cpp:29
void axesUnitsChanged(const ComboProperty &units)
bool loadData(const QString &projectDir) override
Loads non-XML data from projectDir and returns success flag.
Definition: DataItem.cpp:64
std::unique_ptr< Datafield > m_data
simulation results
Definition: DataItem.h:88
void setFileName(const QString &filename)
Definition: DataItem.cpp:46
static constexpr auto P_FILE_NAME
Definition: DataItem.h:37
SelectionDescriptor< QString > axesUnits() const
Definition: DataItem.cpp:108
Abstract base class for instrument-specific item classes.
Describes a selection (various possibilities and the current one).
Base class for a GUI data item.
Definition: SessionItem.h:204
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag. name is the tag name and the display name....
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setItemValue(const QString &tag, const QVariant &variant) const
Directly set value of item under given tag.
T * item(const QString &tag) const
Definition: SessionItem.h:353
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString const & name(EShape k)
Definition: particles.cpp:20