AppFwk: PdmSettings: Added methods that reads and writes (only) valuefields of an object properly

This commit is contained in:
Jacob Støren 2019-06-25 16:50:03 +02:00
parent 183988befd
commit ce91cc3cc2
2 changed files with 86 additions and 1 deletions

View File

@ -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<caf::PdmFieldHandle*> fields;
object->fields(fields);
size_t i;
for (i = 0; i < fields.size(); i++)
{
caf::PdmFieldHandle* fieldHandle = fields[i];
caf::PdmValueField* valueField = dynamic_cast<caf::PdmValueField*>(fieldHandle);
if (valueField)
{
QString key = fieldHandle->keyword();
if ( settings.contains(key) )
{
QVariant val = settings.value(key);
QString fieldText = "<Element>" + val.toString() + "</Element>";
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<caf::PdmFieldHandle*> fields;
object->fields(fields);
size_t i;
for (i = 0; i < fields.size(); i++)
{
caf::PdmFieldHandle* fieldHandle = fields[i];
caf::PdmValueField* valueField = dynamic_cast<caf::PdmValueField*>(fieldHandle);
if (valueField)
{
QString fieldText;
QXmlStreamWriter writer(&fieldText);
fieldHandle->xmlCapability()->writeFieldData(writer);
settings.setValue(fieldHandle->keyword(), fieldText);
}
}
}
} // namespace caf

View File

@ -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 = "");
};