BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Serialize.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/XML/Serialize.cpp
6 //! @brief Implements class Streamer
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 
17 #include <QColor>
18 
19 void Serialize::rwValue(Streamer& s, const QString& tag, bool& val)
20 {
21  if (QXmlStreamWriter* w = s.xmlWriter()) {
22  w->writeStartElement(tag);
23  w->writeAttribute(XML::Tags::Value, val ? "1" : "0");
24  w->writeEndElement();
25  } else if (QXmlStreamReader* r = s.xmlReader()) {
26  r->readNextStartElement();
27  s.assertCurrentTag(tag);
28  val = r->attributes().value(XML::Tags::Value).toUInt() > 0;
29  s.gotoEndElementOfTag(tag);
30  } else
31  ASSERT(0);
32 }
33 
34 void Serialize::rwValue(Streamer& s, const QString& tag, int& val)
35 {
36  if (QXmlStreamWriter* w = s.xmlWriter()) {
37  w->writeStartElement(tag);
38  w->writeAttribute(XML::Tags::Value, QString::number(val));
39  w->writeEndElement();
40  } else if (QXmlStreamReader* r = s.xmlReader()) {
41  r->readNextStartElement();
42  s.assertCurrentTag(tag);
43  val = r->attributes().value(XML::Tags::Value).toInt();
44  s.gotoEndElementOfTag(tag);
45  } else
46  ASSERT(0);
47 }
48 
49 void Serialize::rwValue(Streamer& s, const QString& tag, double& val)
50 {
51  if (QXmlStreamWriter* w = s.xmlWriter()) {
52  w->writeStartElement(tag);
54  w->writeEndElement();
55  } else if (QXmlStreamReader* r = s.xmlReader()) {
56  r->readNextStartElement();
57  s.assertCurrentTag(tag);
58  val = r->attributes().value(XML::Tags::Value).toDouble();
59  s.gotoEndElementOfTag(tag);
60  } else
61  ASSERT(0);
62 }
63 
64 void Serialize::rwValue(Streamer& s, const QString& tag, QString& val)
65 {
66  if (QXmlStreamWriter* w = s.xmlWriter()) {
67  w->writeStartElement(tag);
68  w->writeAttribute(XML::Tags::Value, val);
69  w->writeEndElement();
70  } else if (QXmlStreamReader* r = s.xmlReader()) {
71  r->readNextStartElement();
72  s.assertCurrentTag(tag);
74  s.gotoEndElementOfTag(tag);
75  } else
76  ASSERT(0);
77 }
78 
79 void Serialize::rwValue(Streamer& s, const QString& tag, QColor& col)
80 {
81  if (QXmlStreamWriter* w = s.xmlWriter()) {
82  w->writeStartElement(tag);
83  w->writeAttribute(XML::Tags::Value, col.isValid() ? col.name(QColor::HexArgb) : "");
84  w->writeEndElement();
85  } else if (QXmlStreamReader* r = s.xmlReader()) {
86  r->readNextStartElement();
87  s.assertCurrentTag(tag);
88  QString colName = r->attributes().value(XML::Tags::Value).toString();
89  col = QColor(colName);
90  s.gotoEndElementOfTag(tag);
91  } else
92  ASSERT(0);
93 }
94 
95 void Serialize::rwValue(Streamer& s, const QString& tag, QByteArray& val)
96 {
97  if (QXmlStreamWriter* w = s.xmlWriter()) {
98  w->writeStartElement(tag);
99  w->writeCharacters(val.toBase64());
100  w->writeEndElement();
101  } else if (QXmlStreamReader* r = s.xmlReader()) {
102  r->readNextStartElement();
103  s.assertCurrentTag(tag);
104  QString valueAsBase64 = r->readElementText(QXmlStreamReader::SkipChildElements);
105  val = QByteArray::fromBase64(valueAsBase64.toLatin1());
106  s.gotoEndElementOfTag(tag);
107  } else
108  ASSERT(0);
109 }
110 
111 void Serialize::rwValue(Streamer& s, const QString& tag, R3& val)
112 {
113  if (QXmlStreamWriter* w = s.xmlWriter()) {
114  w->writeStartElement(tag);
116  w->writeEndElement();
117  } else if (QXmlStreamReader* r = s.xmlReader()) {
118  s.gotoStartElementOfTag(tag);
120  s.gotoEndElementOfTag(tag);
121  } else
122  ASSERT(0);
123 }
Defines class Streamer.
Defines.
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
void gotoEndElementOfTag(const QString &tag)
Definition: Streamer.cpp:39
void assertCurrentTag(const QString &expectedTag) const
Definition: Streamer.cpp:62
void gotoStartElementOfTag(const QString &tag)
Definition: Streamer.cpp:54
QXmlStreamWriter * xmlWriter()
Returns stream writer or nullptr.
Definition: Streamer.h:47
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
Definition: Streamer.h:48
void readAttribute(QXmlStreamReader *reader, const QString &attributeName, double *d)
Definition: UtilXML.cpp:193
void writeAttribute(QXmlStreamWriter *writer, const QString &attributeName, const QVariant &variant)
Write the variant's value as an attribute.
Definition: UtilXML.cpp:65
void rwValue(Streamer &s, const QString &tag, bool &val)
Definition: Serialize.cpp:19
constexpr auto Value("value")