BornAgain  1.19.0
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/coregui/DataLoaders/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/Data/OutputData.h"
23 #include "qcustomplot.h"
24 
26 {
27  return "Legacy from BornAgain 1.18";
28 }
29 
31 {
32  return "AutomaticDataLoader1D";
33 }
34 
36 {
37  auto loader = new AutomaticDataLoader1D();
38  loader->deserialize(serialize());
39  return loader;
40 }
41 
43 {
44  QByteArray a;
45  QDataStream s(&a, QIODevice::WriteOnly);
46  s.setVersion(QDataStream::Qt_5_12);
47 
48  s << (quint8)1; // version
49  s << m_fileContent;
50  s << m_error;
51 
52  return a;
53 }
54 
55 void AutomaticDataLoader1D::deserialize(const QByteArray& data)
56 {
57  m_fileContent.clear();
58  m_error.clear();
59 
60  QDataStream s(data);
61  s.setVersion(QDataStream::Qt_5_12);
62 
63  quint8 version;
64  s >> version;
65 
66  if (version == 1) {
67  s >> m_fileContent;
68  s >> m_error;
69  } else
71 
72  if (s.status() != QDataStream::Ok)
74 }
75 
76 void AutomaticDataLoader1D::setFileContents(const QByteArray& fileContent)
77 {
79 }
80 
82 {
83  return m_fileContent;
84 }
85 
87 {
88  ASSERT(m_item != nullptr);
90 
91  try {
92  std::stringstream str(m_fileContent.constData());
93  auto oData = OutputDataReadReflectometry().readOutputData(str);
94 
95  ImportDataInfo importInfo(std::move(*oData), Axes::Units::QSPACE);
96  m_item->setImportData(std::move(importInfo));
97  m_error.clear();
98  } catch (std::runtime_error& ex) {
101  m_error = QString::fromStdString(ex.what());
102  if (m_error.isEmpty())
103  m_error = "Unspecified error";
104  }
105 
106  emit contentsProcessed();
107 }
108 
110 {
112 }
113 
115 {
116  return m_error.isEmpty() ? 0 : 1;
117 }
118 
120 {
121  if (!m_error.isEmpty())
122  return {m_error};
123 
124  return {};
125 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class AutomaticDataLoader1DResultModel.
Defines class AutomaticDataLoader1D.
Defines class DeserializationException.
Defines ImportDataInfo helper struct.
Defines OutputDataReadReflectometry.
Defines and implements templated class OutputData.
Defines class RealDataItem.
Defines class SpecularDataItem.
Base class for result tables of data loaders.
Base class for all data loaders (classes which can import real data)
void contentsProcessed()
Emitted whenever contents have been processed.
The result model of a AutomaticDataLoader1D (for showing the import results in a table view).
Implements the legacy importer from BornAgain with no user interaction.
virtual QString persistentClassName() const override
A name which can be used for save/load purposes (which will not change ever more)
virtual void processContents() override
Process the file contents.
virtual void deserialize(const QByteArray &data) override
Initialize from serialization data.
virtual int numErrors() const override
Number of errors found while processing the content.
virtual AbstractDataLoader * clone() const override
Create a complete clone, including all internal states.
virtual QByteArray serialize() const override
Returns every internal setting so it can be restored completely.
virtual QString name() const override
The name shown in the format selection combo.
virtual AbstractDataLoaderResultModel * createResultModel() const override
Create a table model which contains the import information like original file content,...
virtual QStringList lineUnrelatedErrors() const override
Errors not related to a particular line.
virtual QByteArray fileContent() const override
Returns the original file content.
virtual void setFileContents(const QByteArray &fileContent) override
Sets the file contents to be imported.
static DeserializationException streamError()
static DeserializationException tooNew()
Carries information about loaded data.
Class for reading reflectometry data from ASCII file.
OutputData< double > * readOutputData(std::istream &input_stream)
void removeNativeData()
void setImportData(ImportDataInfo data)
Sets imported data to underlying item.
bool isSpecularData() const
SpecularDataItem * specularDataItem()
void setOutputData(OutputData< double > *data) override
The given pointer becomes owned by this class!!