BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
QREDataLoader.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/DataLoaders/QREDataLoader.h
6 //! @brief Defines class QREDataLoader
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 
15 #ifndef GUI_COREGUI_DATALOADERS_QREDATALOADER_H
16 #define GUI_COREGUI_DATALOADERS_QREDATALOADER_H
17 
19 #include <QVector>
20 #include <variant>
21 
22 class QString;
24 
25 //! Real data loader for Q/R/E reflectometry CSV files
26 
28 public:
29  QREDataLoader();
30  virtual QString name() const override;
31  virtual QString persistentClassName() const override;
32  virtual void populateImportSettingsWidget(QWidget* parent) override;
33  virtual void initWithDefaultImportSettings() override;
34  virtual void applyImportSettings() override;
35  virtual QByteArray serialize() const override;
36  virtual void deserialize(const QByteArray& data) override;
37  virtual AbstractDataLoader* clone() const override;
38  virtual QByteArray fileContent() const override;
39  virtual void setFileContents(const QByteArray& fileContent) override;
40  virtual void guessSettings() override;
41  virtual void processContents() override;
42  virtual int numErrors() const override;
43  virtual int numLineRelatedErrors() const override;
44  virtual QStringList lineUnrelatedErrors() const override;
45  virtual AbstractDataLoaderResultModel* createResultModel() const override;
46 
47 private:
48  void parseFileContent() const;
49  void calculateFromParseResult() const;
51 
52 private:
54 
56  bool enabled; //< shall data type be used
57  int column; //< read the value from this raw column
58  UnitInFile unit; //< the unit of the data in the file
59  double factor; //< in case the raw data shall be multiplied
60 
61  bool operator==(const ColumnDefinition& other) const;
62  };
63 
64  enum class DataType { Q, R, dR };
65 
66  //! Settings for importing the file
67  struct ImportSettings {
68  QString separator; //!< column separator
69  QString headerPrefix; //!< prefix denoting header line
70  QString linesToSkip; //!< pattern denoting line to skip (i.e. '1,10-12,42')
71  QMap<DataType, ColumnDefinition> columnDefinitions;
72 
73  bool operator!=(const ImportSettings& other) const;
74  QByteArray serialize() const;
75  void deserialize(const QByteArray& data);
77 
78  //! Contains a line related error (stored in the import result). Used for showing
79  //! line related errors in the import data table.
80  struct ErrorDefinition {
81  // Attention: numbers are serialized! Do not change them!
82  enum Type {
83  none = 0,
88  RLessZero = 5
89  };
90 
91  ErrorDefinition(Type t = none, int d = 0);
92  ErrorDefinition(Type t, double d);
93 
94  //! Human readable error text
95  QString toString() const;
96 
98  std::variant<int, double> data; //!< Additional data; meaning depends on the error type (see
99  //!< implementation of toString() for more information)
100  };
101 
102  //! Result of the file import. Some of the contained data is only relevant for showing the
103  //! results in the result table. The usage of vectors which cover also invalid or skipped
104  //! lines has its reason also in this result showing (to improve presentation performance).
105  struct ImportResult {
106  void clear();
107  void clearCalculatedValues();
108  void addError(int line, ErrorDefinition::Type type, int data = 0);
109  void addError(int line, ErrorDefinition::Type type, double data);
110  QString errorText(int line) const;
111 
112  QVector<QPair<bool, QString>> lines; //!< bool describes whether line is skipped
113  //!< index is 0-based line number
114  QVector<QVector<double>> rawValues; //!< index is 0-based line number
115  QVector<double> qValues; //!< index is 0-based line number
116  QVector<double> rValues; //!< index is 0-based line number
117  QVector<double> eValues; //!< index is 0-based line number
118  int validCalculatedLines; //!< number of valid resulting data rows
119  int maxColumnCount; //!< max found columns in raw data
120  QMap<int, ErrorDefinition>
121  calculationErrors; //!< calculation error per line; line is 0-based
122  QString error; //!< error unrelated to lines
123  ImportSettings importSettings; //!< Settings used for the import
124  };
126  QByteArray m_fileContent;
127 
128  QPointer<QREDataLoaderProperties> m_propertiesWidget;
129 
130  friend QDataStream& operator<<(QDataStream& stream, const QREDataLoader::ImportSettings& s);
131  friend QDataStream& operator>>(QDataStream& stream, QREDataLoader::ImportSettings& s);
132  friend QDataStream& operator<<(QDataStream& stream, const QREDataLoader::ErrorDefinition& s);
133  friend QDataStream& operator>>(QDataStream& stream, QREDataLoader::ErrorDefinition& s);
135 };
136 
137 QDataStream& operator<<(QDataStream& stream, const QREDataLoader::ImportSettings& s);
138 QDataStream& operator>>(QDataStream& stream, QREDataLoader::ImportSettings& s);
139 
140 #endif // GUI_COREGUI_DATALOADERS_QREDATALOADER_H
Defines class AbstractDataLoader1D.
QDataStream & operator>>(QDataStream &stream, QREDataLoader::ImportSettings &s)
Base class for data loaders for 1D import.
Base class for result tables of data loaders.
Base class for all data loaders (classes which can import real data)
std::ostream & operator<<(std::ostream &os, const BasicVector3D< T > &a)
Output to stream.
Properties widget for the QREDataLoader.
The result model of a QREDataLoader (for showing the import results in a table view).
Real data loader for Q/R/E reflectometry CSV files.
Definition: QREDataLoader.h:27
virtual QByteArray fileContent() const override
Returns the original file content.
virtual QString persistentClassName() const override
A name which can be used for save/load purposes (which will not change ever more)
friend QDataStream & operator<<(QDataStream &stream, const QREDataLoader::ImportSettings &s)
virtual void guessSettings() override
Guess appropriate settings (for example the separator in a CSV file).
virtual int numLineRelatedErrors() const override
Number of errors related to a specific line.
virtual int numErrors() const override
Number of errors found while processing the content.
virtual void initWithDefaultImportSettings() override
Set import settings to defaults.
virtual void setFileContents(const QByteArray &fileContent) override
Sets the file contents to be imported.
virtual QByteArray serialize() const override
Returns every internal setting so it can be restored completely.
void calculateFromParseResult() const
virtual void deserialize(const QByteArray &data) override
Initialize from serialization data.
virtual void applyImportSettings() override
Read all values from the properties UI into the internal variables.
QByteArray m_fileContent
virtual AbstractDataLoader * clone() const override
Create a complete clone, including all internal states.
virtual AbstractDataLoaderResultModel * createResultModel() const override
Create a table model which contains the import information like original file content,...
virtual void populateImportSettingsWidget(QWidget *parent) override
Fills the widget on the import dialog pane.
virtual QStringList lineUnrelatedErrors() const override
Errors not related to a particular line.
virtual QString name() const override
The name shown in the format selection combo.
QPointer< QREDataLoaderProperties > m_propertiesWidget
struct QREDataLoader::ImportSettings m_importSettings
ImportResult m_importResult
void createOutputDataFromParsingResult(RealDataItem *item) const
void parseFileContent() const
virtual void processContents() override
Process the file contents.
friend QDataStream & operator>>(QDataStream &stream, QREDataLoader::ImportSettings &s)
The RealDataItem class represents intensity data imported from file and intended for fitting.
Definition: RealDataItem.h:35
bool operator==(const ColumnDefinition &other) const
Contains a line related error (stored in the import result).
Definition: QREDataLoader.h:80
std::variant< int, double > data
Additional data; meaning depends on the error type (see implementation of toString() for more informa...
Definition: QREDataLoader.h:98
ErrorDefinition(Type t=none, int d=0)
QString toString() const
Human readable error text.
Result of the file import.
QVector< double > rValues
index is 0-based line number
int maxColumnCount
max found columns in raw data
QString error
error unrelated to lines
void addError(int line, ErrorDefinition::Type type, int data=0)
int validCalculatedLines
number of valid resulting data rows
QVector< QVector< double > > rawValues
index is 0-based line number
QMap< int, ErrorDefinition > calculationErrors
calculation error per line; line is 0-based
QVector< double > eValues
index is 0-based line number
QString errorText(int line) const
ImportSettings importSettings
Settings used for the import.
QVector< double > qValues
index is 0-based line number
QVector< QPair< bool, QString > > lines
bool describes whether line is skipped index is 0-based line number
Settings for importing the file.
Definition: QREDataLoader.h:67
QString separator
column separator
Definition: QREDataLoader.h:68
void deserialize(const QByteArray &data)
QMap< DataType, ColumnDefinition > columnDefinitions
Definition: QREDataLoader.h:71
QString headerPrefix
prefix denoting header line
Definition: QREDataLoader.h:69
bool operator!=(const ImportSettings &other) const
QString linesToSkip
pattern denoting line to skip (i.e. '1,10-12,42')
Definition: QREDataLoader.h:70