From eb1a2a27939b7da0ac0e8b03f6ad3bb8873f035b Mon Sep 17 00:00:00 2001 From: Matthias Puchner <github@mpuchner.de> Date: Thu, 23 Dec 2021 14:54:02 +0100 Subject: [PATCH] simplify code; move to private ns --- GUI/Model/Session/SessionXML.cpp | 108 +++++++++++++++++-------------- GUI/Model/Session/SessionXML.h | 36 +++-------- 2 files changed, 68 insertions(+), 76 deletions(-) diff --git a/GUI/Model/Session/SessionXML.cpp b/GUI/Model/Session/SessionXML.cpp index 3b19c2dc88e..ae0eac4e47b 100644 --- a/GUI/Model/Session/SessionXML.cpp +++ b/GUI/Model/Session/SessionXML.cpp @@ -26,6 +26,23 @@ namespace { +namespace Tags { +constexpr auto TagAttribute("Tag"); +constexpr auto ModelTypeAttribute("ModelType"); +constexpr auto DisplayNameAttribute("DisplayName"); +constexpr auto ParameterTag("Parameter"); +constexpr auto BinaryData("BinaryData"); +constexpr auto NonSessionItemData("NonSessionItemData"); +constexpr auto ParameterNameAttribute("ParName"); +constexpr auto ParameterTypeAttribute("ParType"); +constexpr auto ParameterValueAttribute("ParValue"); +constexpr auto ParameterRoleAttribute("ParRole"); +constexpr auto ParameterExtAttribute("ParExt"); +constexpr auto ExternalPropertyColorAtt("Color"); +constexpr auto ExternalPropertyIdentifierAtt("Identifier"); + +} // namespace Tags + void assertCurrentTag(QXmlStreamReader* reader, const QString& expectedTag) { ASSERT(reader); @@ -108,15 +125,15 @@ void GUI::Session::XML::writeItemAndChildItems(QXmlStreamWriter* writer, const S { ASSERT(item); if (item->parent()) { - writer->writeStartElement(GUI::Session::XML::ItemTag); - writer->writeAttribute(GUI::Session::XML::ModelTypeAttribute, item->modelType()); + writer->writeStartElement(ItemTag); + writer->writeAttribute(ModelTypeAttribute, item->modelType()); QString tag = item->parent()->tagFromItem(item); - writer->writeAttribute(GUI::Session::XML::TagAttribute, tag); - writer->writeAttribute(GUI::Session::XML::DisplayNameAttribute, + writer->writeAttribute(Tags::TagAttribute, tag); + writer->writeAttribute(Tags::DisplayNameAttribute, item->roleProperty(SessionFlags::DisplayNameRole).toString()); for (int role : item->getRoles()) { if (role == Qt::DisplayRole || role == Qt::EditRole) - GUI::Session::XML::writeVariant(writer, item->value(), role); + writeVariant(writer, item->value(), role); } } @@ -126,20 +143,19 @@ void GUI::Session::XML::writeItemAndChildItems(QXmlStreamWriter* writer, const S QByteArray a = item->serializeBinaryData(); if (!a.isEmpty()) { - writer->writeStartElement(GUI::Session::XML::BinaryData); - writer->writeAttribute(GUI::Session::XML::Version, "1"); + writer->writeStartElement(Tags::BinaryData); + writer->writeAttribute(XML::Version, "1"); writer->writeCharacters(a.toBase64()); writer->writeEndElement(); } QString nonSessionItemContent; QXmlStreamWriter nonSessionItemStream(&nonSessionItemContent); - nonSessionItemStream.writeStartElement( - GUI::Session::XML::NonSessionItemData); // for following writeAttrib + nonSessionItemStream.writeStartElement(Tags::NonSessionItemData); // for following writeAttrib const auto sizeBefore = nonSessionItemContent.size(); item->writeNonSessionItems(&nonSessionItemStream); if (sizeBefore != nonSessionItemContent.size()) { - writer->writeStartElement(GUI::Session::XML::NonSessionItemData); + writer->writeStartElement(Tags::NonSessionItemData); item->writeNonSessionItems(writer); writer->writeEndElement(); } @@ -171,7 +187,7 @@ void GUI::Session::XML::writeAttribute(QXmlStreamWriter* writer, const QString& } else if (QString(variant.typeName()) == "ComboProperty") { auto combo = variant.value<ComboProperty>(); writer->writeAttribute(attributeName, combo.stringOfSelections()); - writer->writeAttribute(GUI::Session::XML::ParameterExtAttribute, combo.stringOfValues()); + writer->writeAttribute(Tags::ParameterExtAttribute, combo.stringOfValues()); } else throw Error("GUI::Session::XML::writeVariant: Parameter type not supported " + QString(variant.typeName())); @@ -225,10 +241,10 @@ void GUI::Session::XML::writeVariant(QXmlStreamWriter* writer, QVariant variant, { ASSERT(writer); if (variant.isValid()) { - writer->writeStartElement(GUI::Session::XML::ParameterTag); - writer->writeAttribute(GUI::Session::XML::ParameterTypeAttribute, variant.typeName()); - writer->writeAttribute(GUI::Session::XML::ParameterRoleAttribute, QString::number(role)); - writeAttribute(writer, GUI::Session::XML::ParameterValueAttribute, variant); + writer->writeStartElement(Tags::ParameterTag); + writer->writeAttribute(Tags::ParameterTypeAttribute, variant.typeName()); + writer->writeAttribute(Tags::ParameterRoleAttribute, QString::number(role)); + writeAttribute(writer, Tags::ParameterValueAttribute, variant); writer->writeEndElement(); // end ParameterTag } } @@ -243,9 +259,9 @@ void GUI::Session::XML::readItems(QXmlStreamReader* reader, SessionItem* parent, while (!reader->atEnd()) { reader->readNext(); if (reader->isStartElement()) { - if (reader->name() == GUI::Session::XML::ItemTag) { + if (reader->name() == XML::ItemTag) { const QString model_type = - reader->attributes().value(GUI::Session::XML::ModelTypeAttribute).toString(); + reader->attributes().value(Tags::ModelTypeAttribute).toString(); if (model_type == "ParticleDistribution") { if (!legacyDistributionFound) { @@ -261,10 +277,9 @@ void GUI::Session::XML::readItems(QXmlStreamReader* reader, SessionItem* parent, continue; } - QString tag = - reader->attributes().value(GUI::Session::XML::TagAttribute).toString(); + QString tag = reader->attributes().value(Tags::TagAttribute).toString(); QString displayName = - reader->attributes().value(GUI::Session::XML::DisplayNameAttribute).toString(); + reader->attributes().value(Tags::DisplayNameAttribute).toString(); if (!topTag.isEmpty()) { // to handle copying of item to another parent @@ -283,12 +298,12 @@ void GUI::Session::XML::readItems(QXmlStreamReader* reader, SessionItem* parent, parent = newItem; parent->setDisplayName(displayName); } - ASSERT(reader->name() == GUI::Session::XML::ItemTag); - } else if (reader->name() == GUI::Session::XML::ParameterTag) { - GUI::Session::XML::readProperty(reader, parent, messageService); - goToEndElementOfTag(reader, GUI::Session::XML::ParameterTag); - } else if (reader->name() == GUI::Session::XML::BinaryData) { - if (reader->attributes().value(GUI::Session::XML::Version).toInt() == 1) { + ASSERT(reader->name() == ItemTag); + } else if (reader->name() == ParameterTag) { + readProperty(reader, parent, messageService); + goToEndElementOfTag(reader, ParameterTag); + } else if (reader->name() == Tags::BinaryData) { + if (reader->attributes().value(XML::Version).toInt() == 1) { QString valueAsBase64 = reader->readElementText(QXmlStreamReader::SkipChildElements); const auto data = QByteArray::fromBase64( @@ -296,13 +311,13 @@ void GUI::Session::XML::readItems(QXmlStreamReader* reader, SessionItem* parent, parent->deserializeBinaryData(data); } else throw DeserializationException::tooNew(); - goToEndElementOfTag(reader, GUI::Session::XML::BinaryData); - } else if (reader->name() == GUI::Session::XML::NonSessionItemData) { + goToEndElementOfTag(reader, Tags::BinaryData); + } else if (reader->name() == Tags::NonSessionItemData) { parent->readNonSessionItems(reader); - goToEndElementOfTag(reader, GUI::Session::XML::NonSessionItemData); + goToEndElementOfTag(reader, Tags::NonSessionItemData); } } else if (reader->isEndElement()) { - if (reader->name() == GUI::Session::XML::ItemTag && parent) + if (reader->name() == XML::ItemTag && parent) parent = parent->parent(); if (reader->name() == start_type) break; @@ -315,10 +330,10 @@ QString GUI::Session::XML::readProperty(QXmlStreamReader* reader, SessionItem* i { ASSERT(reader); const QString parameter_name = - reader->attributes().value(GUI::Session::XML::ParameterNameAttribute).toString(); + reader->attributes().value(Tags::ParameterNameAttribute).toString(); const QString parameter_type = - reader->attributes().value(GUI::Session::XML::ParameterTypeAttribute).toString(); - const int role = reader->attributes().value(GUI::Session::XML::ParameterRoleAttribute).toInt(); + reader->attributes().value(Tags::ParameterTypeAttribute).toString(); + const int role = reader->attributes().value(Tags::ParameterRoleAttribute).toInt(); if (!item) { QString message = @@ -330,27 +345,25 @@ QString GUI::Session::XML::readProperty(QXmlStreamReader* reader, SessionItem* i QVariant variant; if (parameter_type == "double") { double parameter_value = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toDouble(); + reader->attributes().value(Tags::ParameterValueAttribute).toDouble(); variant = parameter_value; } else if (parameter_type == "int") { - int parameter_value = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toInt(); + int parameter_value = reader->attributes().value(Tags::ParameterValueAttribute).toInt(); variant = parameter_value; } else if (parameter_type == "uint") { unsigned parameter_value = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toUInt(); + reader->attributes().value(Tags::ParameterValueAttribute).toUInt(); variant = parameter_value; } else if (parameter_type == "bool") { - bool parameter_value = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toInt(); + bool parameter_value = reader->attributes().value(Tags::ParameterValueAttribute).toInt(); variant = parameter_value; } else if (parameter_type == "QString") { QString parameter_value = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toString(); + reader->attributes().value(Tags::ParameterValueAttribute).toString(); variant = parameter_value; } else if (parameter_type == "QColor") { QString parameter_value = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toString(); + reader->attributes().value(Tags::ParameterValueAttribute).toString(); variant = QColor(parameter_value); } else if (parameter_type == "ExternalProperty") { const bool isMaterialItemColor = @@ -362,7 +375,7 @@ QString GUI::Session::XML::readProperty(QXmlStreamReader* reader, SessionItem* i // has been changed to a simple QColor property => use only the color value of the // "ExternalProperty" and store it as QColor in the variant const QString colorName = - reader->attributes().value(GUI::Session::XML::ExternalPropertyColorAtt).toString(); + reader->attributes().value(Tags::ExternalPropertyColorAtt).toString(); variant = QColor(colorName); } else { // This handles legacy XML files for items referring to a material: @@ -371,16 +384,13 @@ QString GUI::Session::XML::readProperty(QXmlStreamReader* reader, SessionItem* i // QString) // => use only the identifier value of the "ExternalProperty" and store it as QString in // the variant - const QString identifier = reader->attributes() - .value(GUI::Session::XML::ExternalPropertyIdentifierAtt) - .toString(); + const QString identifier = + reader->attributes().value(Tags::ExternalPropertyIdentifierAtt).toString(); variant = identifier; } } else if (parameter_type == "ComboProperty") { - QString selections = - reader->attributes().value(GUI::Session::XML::ParameterValueAttribute).toString(); - QString values = - reader->attributes().value(GUI::Session::XML::ParameterExtAttribute).toString(); + QString selections = reader->attributes().value(Tags::ParameterValueAttribute).toString(); + QString values = reader->attributes().value(Tags::ParameterExtAttribute).toString(); ComboProperty combo_property; combo_property.setStringOfValues(values); diff --git a/GUI/Model/Session/SessionXML.h b/GUI/Model/Session/SessionXML.h index f7fefe608a8..e9cb72e2135 100644 --- a/GUI/Model/Session/SessionXML.h +++ b/GUI/Model/Session/SessionXML.h @@ -30,33 +30,15 @@ class QUuid; namespace GUI::Session::XML { -const QString ItemMimeType = "application/org.bornagainproject.xml.item.z"; -const QString LinkMimeType = "application/org.bornagainproject.fittinglink"; - -const QString InstrumentModelTag("InstrumentModel"); -const QString SampleModelTag("SampleModel"); -const QString MaterialModelTag("MaterialModel"); -const QString JobModelTag("JobModel"); -const QString DocumentModelTag("DocumentModel"); -const QString RealDataModelTag("RealDataModel"); - -const QString TagAttribute("Tag"); - -const QString ItemTag("Item"); -const QString ModelTypeAttribute("ModelType"); -const QString DisplayNameAttribute("DisplayName"); -const QString ParameterTag("Parameter"); -const QString BinaryData("BinaryData"); -const QString NonSessionItemData("NonSessionItemData"); -const QString Version("Version"); -const QString ParameterNameAttribute("ParName"); -const QString ParameterTypeAttribute("ParType"); -const QString ParameterValueAttribute("ParValue"); -const QString ParameterRoleAttribute("ParRole"); -const QString ParameterExtAttribute("ParExt"); - -const QString ExternalPropertyColorAtt("Color"); -const QString ExternalPropertyIdentifierAtt("Identifier"); +constexpr auto ItemMimeType = "application/org.bornagainproject.xml.item.z"; +constexpr auto LinkMimeType = "application/org.bornagainproject.fittinglink"; +constexpr auto InstrumentModelTag("InstrumentModel"); +constexpr auto JobModelTag("JobModel"); +constexpr auto RealDataModelTag("RealDataModel"); +constexpr auto ItemTag("Item"); +constexpr auto ModelTypeAttribute("ModelType"); +constexpr auto ParameterTag("Parameter"); +constexpr auto Version("Version"); void writeModel(QXmlStreamWriter* writer, SessionItem* modelRootItem); void writeItemAndChildItems(QXmlStreamWriter* writer, const SessionItem* item); -- GitLab