BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
AutomaticDataLoader1D.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Loaders/AutomaticDataLoader1D.cpp
6 //! @brief Implements class AutomaticDataLoader1D
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Device/IO/ReadReflectometry.h"
22 
24 {
25  return "Legacy from BornAgain 1.18";
26 }
27 
29 {
30  return "AutomaticDataLoader1D";
31 }
32 
34 {
35  auto* loader = new AutomaticDataLoader1D();
36  loader->deserialize(serialize());
37  return loader;
38 }
39 
41 {
42  QByteArray a;
43  QDataStream s(&a, QIODevice::WriteOnly);
44  s.setVersion(QDataStream::Qt_5_12);
45 
46  s << (quint8)1; // version
47  s << m_fileContent;
48  s << m_error;
49 
50  return a;
51 }
52 
53 void AutomaticDataLoader1D::deserialize(const QByteArray& data)
54 {
55  m_fileContent.clear();
56  m_error.clear();
57 
58  QDataStream s(data);
59  s.setVersion(QDataStream::Qt_5_12);
60 
61  quint8 version;
62  s >> version;
63 
64  if (version == 1) {
65  s >> m_fileContent;
66  s >> m_error;
67  } else
69 
70  if (s.status() != QDataStream::Ok)
72 }
73 
74 void AutomaticDataLoader1D::setFileContents(const QByteArray& fileContent)
75 {
77 }
78 
80 {
81  return m_fileContent;
82 }
83 
85 {
86  ASSERT(m_item != nullptr);
87  ASSERT(m_item->isSpecularData());
88 
89  try {
90  std::stringstream str(m_fileContent.constData());
91  auto* oData = ReadReflectometry().readDatafield(str);
92 
93  ImportDataInfo importInfo(std::move(*oData), Coords::QSPACE);
94  m_item->setImportData(std::move(importInfo));
95  m_error.clear();
96  } catch (std::runtime_error& ex) {
99  m_error = QString::fromStdString(ex.what());
100  if (m_error.isEmpty())
101  m_error = "Unspecified error";
102  }
103 
104  emit contentsProcessed();
105 }
106 
108 {
110 }
111 
113 {
114  return m_error.isEmpty() ? 0 : 1;
115 }
116 
118 {
119  if (!m_error.isEmpty())
120  return {m_error};
121 
122  return {};
123 }
Defines class AutomaticDataLoader1DResultModel.
Defines class AutomaticDataLoader1D.
Defines class DeserializationException.
Defines ImportDataInfo helper struct.
Defines class RealDataItem.
Defines class SpecularDataItem.
Base class for result tables of data loaders. Derive from this class and return an instance in YourDa...
Abstract base class for all data loaders (classes to import real data).
void contentsProcessed()
Emitted whenever contents have been processed.
RealDataItem * m_item
The real-data-item which owns this loader. Never delete this!
The result model of a AutomaticDataLoader1D (for showing the import results in a table view).
The legacy importer for reflectometry, with no user interaction.
QString persistentClassName() const override
A name which can be used for save/load purposes (which will not change ever more)
void processContents() override
Process the file contents. Can be called more than once, e.g. if the import settings have changed....
void deserialize(const QByteArray &data) override
Initialize from serialization data. If any error occurred, then a DeserializationException has to be ...
int numErrors() const override
Number of errors found while processing the content. An error means that either a particular content ...
AbstractDataLoader * clone() const override
Create a complete clone, including all internal states.
QByteArray serialize() const override
Returns every internal setting so it can be restored completely.
QString name() const override
The name shown in the format selection combo.
AbstractDataLoaderResultModel * createResultModel() const override
Create a table model which contains the import information like original file content,...
QStringList lineUnrelatedErrors() const override
Errors not related to a particular line.
QByteArray fileContent() const override
Returns the original file content. If not available any more (like for legacy project file import),...
void setFileContents(const QByteArray &fileContent) override
Sets the file contents to be imported. If the file was a compressed file, then the decompressed conte...
static DeserializationException streamError()
static DeserializationException tooNew()
Carries information about loaded data.
void removeNativeData()
bool isSpecularData() const
void setImportData(ImportDataInfo info)
Sets imported data to underlying item. Creates it if not existing. This is used for 1D import....
SpecularDataItem * specularDataItem()
void setDatafield(Datafield *data) override
The given pointer becomes owned by this class!!