From ce91cc3cc247745d981c9099f65fdf719b8068f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20St=C3=B8ren?= Date: Tue, 25 Jun 2019 16:50:03 +0200 Subject: [PATCH] AppFwk: PdmSettings: Added methods that reads and writes (only) valuefields of an object properly --- .../cafPdmXml/cafPdmSettings.cpp | 84 ++++++++++++++++++- .../cafPdmXml/cafPdmSettings.h | 3 + 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.cpp index faa6888b35..7e0c7dd049 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.cpp @@ -41,7 +41,6 @@ #include "cafPdmXmlObjectHandle.h" - namespace caf { @@ -138,5 +137,88 @@ void PdmSettings::writeFieldsToApplicationStore(const caf::PdmObjectHandle* obje } } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmSettings::readValueFieldsFromApplicationStore(caf::PdmObjectHandle* object, const QString folderName /*= ""*/) +{ + // Qt doc : + // + // Constructs a QSettings object for accessing settings of the application and organization + // set previously with a call to QCoreApplication::setOrganizationName(), + // QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName(). + QSettings settings; + + if ( folderName != "" ) + { + settings.beginGroup(folderName); + } + + std::vector fields; + + object->fields(fields); + size_t i; + for (i = 0; i < fields.size(); i++) + { + caf::PdmFieldHandle* fieldHandle = fields[i]; + caf::PdmValueField* valueField = dynamic_cast(fieldHandle); + + if (valueField) + { + QString key = fieldHandle->keyword(); + if ( settings.contains(key) ) + { + QVariant val = settings.value(key); + + QString fieldText = "" + val.toString() + ""; + QXmlStreamReader reader(fieldText); + + // Make stream point to the text data for the field + reader.readNext(); // StartDocument + reader.readNext(); // StartElement + reader.readNext(); // Characters + fieldHandle->xmlCapability()->readFieldData(reader, nullptr); + } + } + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmSettings::writeValueFieldsToApplicationStore(const caf::PdmObjectHandle* object, const QString folderName /*= ""*/) +{ + CAF_ASSERT(object); + + // Qt doc : + // + // Constructs a QSettings object for accessing settings of the application and organization + // set previously with a call to QCoreApplication::setOrganizationName(), + // QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName(). + QSettings settings; + + if ( folderName != "" ) + { + settings.beginGroup(folderName); + } + + std::vector fields; + object->fields(fields); + + size_t i; + for (i = 0; i < fields.size(); i++) + { + caf::PdmFieldHandle* fieldHandle = fields[i]; + caf::PdmValueField* valueField = dynamic_cast(fieldHandle); + if (valueField) + { + QString fieldText; + QXmlStreamWriter writer(&fieldText); + + fieldHandle->xmlCapability()->writeFieldData(writer); + settings.setValue(fieldHandle->keyword(), fieldText); + } + } +} } // namespace caf diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.h index 8de5adc6ea..4962fbb211 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmSettings.h @@ -49,6 +49,9 @@ class PdmSettings public: static void readFieldsFromApplicationStore(caf::PdmObjectHandle* object, const QString context = ""); static void writeFieldsToApplicationStore(const caf::PdmObjectHandle* object, const QString context = ""); + + static void readValueFieldsFromApplicationStore(caf::PdmObjectHandle* object, const QString folderName = ""); + static void writeValueFieldsToApplicationStore(const caf::PdmObjectHandle* object, const QString folderName = ""); };