BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
UtilXML.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/XML/UtilXML.cpp
6 //! @brief Implements
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 "Base/Util/Assert.h"
17 #include "GUI/Util/ComboProperty.h"
19 #include "GUI/Util/Error.h"
21 #include <QColor>
22 #include <QUuid>
23 #include <QXmlStreamWriter>
24 
25 namespace {
26 
27 void assertCurrentTag(QXmlStreamReader* reader, const QString& expectedTag)
28 {
29 
30  if (reader->name() != expectedTag) {
31 #ifdef _DEBUG
32  // to simplify debugging: what is the current tag
33  QString foundTag = reader->name().toString();
34  Q_UNUSED(foundTag);
35 #endif
37  }
38 }
39 
40 void assertCurrentToken(QXmlStreamReader* reader, QXmlStreamReader::TokenType token)
41 {
42  if (reader->tokenType() != token)
44 }
45 
46 } // namespace
47 
48 
49 void GUI::Session::XML::gotoEndElementOfTag(QXmlStreamReader* reader, const QString& tag)
50 {
51  ASSERT(reader);
52  if (reader->name() != tag) {
53  if (!reader->isEndElement())
54  reader->skipCurrentElement();
55  reader->skipCurrentElement();
56  }
57  assertCurrentTag(reader, tag);
58  if (!reader->isEndElement())
59  reader->skipCurrentElement();
60 
61  assertCurrentToken(reader, QXmlStreamReader::EndElement);
62  assertCurrentTag(reader, tag);
63 }
64 
65 void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& attributeName,
66  const QVariant& variant)
67 {
68  ASSERT(writer);
69  ASSERT(variant.isValid());
70 
71  if (variant.type() == QVariant::Double)
72  writeAttribute(writer, attributeName, variant.toDouble());
73  else if (variant.type() == QVariant::Int)
74  writeAttribute(writer, attributeName, variant.toInt());
75  else if (variant.type() == QVariant::UInt)
76  writeAttribute(writer, attributeName, variant.toUInt());
77  else if (variant.type() == QVariant::Bool)
78  writer->writeAttribute(attributeName, QString::number(variant.toBool()));
79  else if (variant.type() == QVariant::String)
80  writer->writeAttribute(attributeName, variant.toString());
81  else if (variant.type() == QVariant::Color) {
82  const auto col = variant.value<QColor>();
83  const QString tcol = col.isValid() ? col.name(QColor::HexArgb) : "";
84  writer->writeAttribute(attributeName, tcol);
85  } else if (QString(variant.typeName()) == "ComboProperty") {
86  auto combo = variant.value<ComboProperty>();
87  writer->writeAttribute(attributeName, combo.stringOfSelections());
88  writer->writeAttribute(Tags::ParameterExtAttribute, combo.stringOfValues());
89  } else
90  throw Error("GUI::Session::XML::writeVariant: Parameter type not supported "
91  + QString(variant.typeName()));
92 }
93 
94 void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& attributeBaseName,
95  const R3& vec)
96 {
97  writeAttribute(writer, attributeBaseName + "X", vec.x());
98  writeAttribute(writer, attributeBaseName + "Y", vec.y());
99  writeAttribute(writer, attributeBaseName + "Z", vec.z());
100 }
101 
102 void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& attributeBaseName,
103  const complex_t& c)
104 {
105  writeAttribute(writer, attributeBaseName + "Re", c.real());
106  writeAttribute(writer, attributeBaseName + "Im", c.imag());
107 }
108 
109 void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& attributeName,
110  double d)
111 {
112  writer->writeAttribute(attributeName, d == 0.0 ? "0" : QString::number(d, 'e', 12));
113 }
114 
115 void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& attributeName,
116  int d)
117 {
118  writer->writeAttribute(attributeName, QString::number(d));
119 }
120 
121 void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& attributeName,
122  unsigned d)
123 {
124  writer->writeAttribute(attributeName, QString::number(d));
125 }
126 
127 void GUI::Session::XML::writeUid(QXmlStreamWriter* writer, const QString& id)
128 {
129  writer->writeAttribute(Tags::Id, id);
130 }
131 
132 void GUI::Session::XML::writeUid(QXmlStreamWriter* writer, const QUuid& id)
133 {
134  writer->writeAttribute(Tags::Id, id.toString());
135 }
136 
137 void GUI::Session::XML::writeVariant(QXmlStreamWriter* writer, QVariant variant, int role)
138 {
139  ASSERT(writer);
140  if (variant.isValid()) {
141  writer->writeStartElement(Tags::ParameterTag);
142  writer->writeAttribute(Tags::ParameterTypeAttribute, variant.typeName());
143  writer->writeAttribute(Tags::ParameterRoleAttribute, QString::number(role));
145  writer->writeEndElement(); // end ParameterTag
146  }
147 }
148 
149 bool GUI::Session::XML::readBoolAttribute(QXmlStreamReader* reader, const QString& attributeName)
150 {
151  return reader->attributes().value(attributeName).toUInt() != 0;
152 }
153 
154 unsigned GUI::Session::XML::readUIntAttribute(QXmlStreamReader* reader,
155  const QString& attributeName)
156 {
157  return reader->attributes().value(attributeName).toUInt();
158 }
159 
160 void GUI::Session::XML::readAttribute(QXmlStreamReader* reader, const QString& attributeName,
161  QString* s)
162 {
163  *s = reader->attributes().value(attributeName).toString();
164 }
165 
166 void GUI::Session::XML::readAttribute(QXmlStreamReader* reader, const QString& attributeName,
167  QColor* c)
168 {
169  const QString parameter_value = reader->attributes().value(attributeName).toString();
170  *c = QColor(parameter_value);
171 }
172 
173 void GUI::Session::XML::readAttribute(QXmlStreamReader* reader, const QString& attributeBaseName,
174  R3* vec)
175 {
176  double x, y, z;
177  readAttribute(reader, attributeBaseName + "X", &x);
178  readAttribute(reader, attributeBaseName + "Y", &y);
179  readAttribute(reader, attributeBaseName + "Z", &z);
180  *vec = {x, y, z};
181 }
182 
183 void GUI::Session::XML::readAttribute(QXmlStreamReader* reader, const QString& attributeBaseName,
184  complex_t* c)
185 {
186  double r, i;
187  readAttribute(reader, attributeBaseName + "Re", &r);
188  readAttribute(reader, attributeBaseName + "Im", &i);
189  c->real(r);
190  c->imag(i);
191 }
192 
193 void GUI::Session::XML::readAttribute(QXmlStreamReader* reader, const QString& attributeName,
194  double* d)
195 {
196  *d = reader->attributes().value(attributeName).toDouble();
197 }
198 
199 void GUI::Session::XML::assertExpectedTag(QXmlStreamReader* reader, const QString& expectedTag)
200 {
201  if (reader->name() != expectedTag) {
202 #ifdef _DEBUG
203  // to simplify debugging: what is the current tag
204  QString foundTag = reader->name().toString();
205  Q_UNUSED(foundTag);
206 #endif
208  }
209 }
Defines class ComboProperty.
Defines class DeserializationException.
Defines error class.
Defines MessageService class.
Defines.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
static DeserializationException streamError()
Definition: Error.h:21
constexpr auto ParameterRoleAttribute("ParRole")
constexpr auto ParameterTypeAttribute("ParType")
constexpr auto ParameterValueAttribute("ParValue")
constexpr auto ParameterExtAttribute("ParExt")
void writeVariant(QXmlStreamWriter *writer, QVariant variant, int role)
Write the variant as a complete tag, including the given role.
Definition: UtilXML.cpp:137
constexpr auto ParameterTag("Parameter")
void readAttribute(QXmlStreamReader *reader, const QString &attributeName, double *d)
Definition: UtilXML.cpp:193
bool readBoolAttribute(QXmlStreamReader *reader, const QString &attributeName)
Definition: UtilXML.cpp:149
void writeUid(QXmlStreamWriter *writer, const QString &id)
Definition: UtilXML.cpp:127
void writeAttribute(QXmlStreamWriter *writer, const QString &attributeName, const QVariant &variant)
Write the variant's value as an attribute.
Definition: UtilXML.cpp:65
void gotoEndElementOfTag(QXmlStreamReader *reader, const QString &tag)
Definition: UtilXML.cpp:49
void assertExpectedTag(QXmlStreamReader *reader, const QString &tag)
Definition: UtilXML.cpp:199
unsigned readUIntAttribute(QXmlStreamReader *reader, const QString &attributeName)
Definition: UtilXML.cpp:154
QString toString(const QModelIndex &index)
Provides string representation of index data.
constexpr auto Id("id")