// readSettings() and writeSettings() are static members of a non-instantiable XMLSettings class, and are passed
// in the call QSettings::registerFormat()
{
bool bResult = false;
QXmlStreamReader reader( &device );
while ( !reader.atEnd() )
{
QXmlStreamReader::TokenType token = reader.readNext();
switch( token )
{
case QXmlStreamReader::StartElement:
if ( reader.name() != "Settings" )
{
path.
push_back( QString( bytes
) );
QXmlStreamAttributes attributes = reader.attributes();
if ( !attributes.isEmpty() )
{
QStringRef value = attributes.value( "value" );
if ( !value.isEmpty() )
{
stream >> var;
// Adds an entry of the form Group1/Subgroup1/Subsubgroup1 = value
map[ path.join( '/') ] = var;
}
}
}
break;
case QXmlStreamReader::EndElement:
if ( !path.isEmpty() )
path.removeLast();
break;
default:
break;
}
}
bResult = !map.isEmpty();
return bResult;
}
struct TreeNode
{
: mKey( key )
, mValue( value )
, mpParent( pParent )
{
}
~TreeNode()
{
QList<TreeNode *>::iterator it = mChildren.begin();
QList<TreeNode *>::iterator eIt = mChildren.end();
while( it != eIt )
{
delete *it++;
}
}
{
TreeNode * pChild = new TreeNode( key, value, this );
mChildren.push_back( pChild );
return pChild;
}
TreeNode
* findChild
( const QString & key
) const {
TreeNode * pChild = 0;
QList<TreeNode *>::const_iterator it = mChildren.begin();
QList<TreeNode *>::const_iterator eIt = mChildren.end();
while( it != eIt && !pChild )
{
TreeNode * pNode = *it++;
if ( pNode->mKey == key )
pChild = pNode;
}
return pChild;
}
TreeNode * mpParent;
QList< TreeNode * > mChildren;
};
static void writeTree( QXmlStreamWriter & writer, TreeNode * pNode )
{
if ( pNode )
{
writer.writeStartElement( pNode->mKey );
if ( !pNode->mValue.isNull() )
{
valueStream << pNode->mValue;
buffer.close();
QString value
( byteArray.
toBase64() );
writer.writeAttribute( "value", value );
writer.writeAttribute( "type", pNode->mValue.typeName() );
}
QList<TreeNode *>::const_iterator it = pNode->mChildren.begin();
QList<TreeNode *>::const_iterator eIt = pNode->mChildren.end();
while( it != eIt )
{
writeTree( writer, *it++ );
}
writer.writeEndElement();
}
}
bool CSAQtXMLSettings
::writeSettings( QIODevice & device,
const QSettings::SettingsMap & map
) {
bool bResult = false;
TreeNode * pRootNode = new TreeNode( "Settings" );
QSettings::SettingsMap::const_iterator it
= map.
begin();
QSettings::SettingsMap::const_iterator eIt
= map.
end();
while ( it != eIt )
{
qDebug() << "Key: " << key;
TreeNode * pTarget = pRootNode;
TreeNode * pChild = 0;
while ( lIt != lEIt )
{
pChild = pTarget->findChild( subPath );
if ( !pChild )
{
if ( lIt != lEIt )
pTarget = pTarget->addChild( subPath );
else
lastKey = subPath;
}
else
{
pTarget = pChild;
}
}
pTarget->addChild( lastKey, value );
it++;
}
if ( pRootNode->mChildren.size() )
{
bResult = true;
QXmlStreamWriter writer( &device );
writer.setAutoFormatting( true );
writer.writeStartDocument();
writeTree( writer, pRootNode );
writer.writeEndDocument();
}
delete pRootNode;
return bResult;
}
// readSettings() and writeSettings() are static members of a non-instantiable XMLSettings class, and are passed
// in the call QSettings::registerFormat()
bool XMLSettings::readSettings( QIODevice & device, QSettings::SettingsMap & map )
{
bool bResult = false;
QStringList path;
QXmlStreamReader reader( &device );
while ( !reader.atEnd() )
{
QXmlStreamReader::TokenType token = reader.readNext();
switch( token )
{
case QXmlStreamReader::StartElement:
if ( reader.name() != "Settings" )
{
QByteArray bytes( reader.name().toLatin1() );
path.push_back( QString( bytes ) );
QXmlStreamAttributes attributes = reader.attributes();
if ( !attributes.isEmpty() )
{
QStringRef value = attributes.value( "value" );
if ( !value.isEmpty() )
{
QByteArray valueBytes = QByteArray::fromBase64( value.toLatin1() );
QDataStream stream( valueBytes );
QVariant var;
stream >> var;
// Adds an entry of the form Group1/Subgroup1/Subsubgroup1 = value
map[ path.join( '/') ] = var;
}
}
}
break;
case QXmlStreamReader::EndElement:
if ( !path.isEmpty() )
path.removeLast();
break;
default:
break;
}
}
bResult = !map.isEmpty();
return bResult;
}
struct TreeNode
{
TreeNode( const QString & key = QString(), QVariant & value = QVariant(), TreeNode * pParent = 0 )
: mKey( key )
, mValue( value )
, mpParent( pParent )
{
}
~TreeNode()
{
QList<TreeNode *>::iterator it = mChildren.begin();
QList<TreeNode *>::iterator eIt = mChildren.end();
while( it != eIt )
{
delete *it++;
}
}
TreeNode * addChild( const QString & key, QVariant & value = QVariant() )
{
TreeNode * pChild = new TreeNode( key, value, this );
mChildren.push_back( pChild );
return pChild;
}
TreeNode * findChild( const QString & key ) const
{
TreeNode * pChild = 0;
QList<TreeNode *>::const_iterator it = mChildren.begin();
QList<TreeNode *>::const_iterator eIt = mChildren.end();
while( it != eIt && !pChild )
{
TreeNode * pNode = *it++;
if ( pNode->mKey == key )
pChild = pNode;
}
return pChild;
}
TreeNode * mpParent;
QList< TreeNode * > mChildren;
QString mKey;
QVariant mValue;
};
static void writeTree( QXmlStreamWriter & writer, TreeNode * pNode )
{
if ( pNode )
{
writer.writeStartElement( pNode->mKey );
if ( !pNode->mValue.isNull() )
{
QByteArray byteArray;
QBuffer buffer( &byteArray );
buffer.open( QIODevice::WriteOnly );
QDataStream valueStream( &buffer );
valueStream << pNode->mValue;
buffer.close();
QString value( byteArray.toBase64() );
writer.writeAttribute( "value", value );
writer.writeAttribute( "type", pNode->mValue.typeName() );
}
QList<TreeNode *>::const_iterator it = pNode->mChildren.begin();
QList<TreeNode *>::const_iterator eIt = pNode->mChildren.end();
while( it != eIt )
{
writeTree( writer, *it++ );
}
writer.writeEndElement();
}
}
bool CSAQtXMLSettings::writeSettings( QIODevice & device, const QSettings::SettingsMap & map )
{
bool bResult = false;
TreeNode * pRootNode = new TreeNode( "Settings" );
QSettings::SettingsMap::const_iterator it = map.begin();
QSettings::SettingsMap::const_iterator eIt = map.end();
while ( it != eIt )
{
const QString & key = it.key();
QVariant value = it.value();
qDebug() << "Key: " << key;
TreeNode * pTarget = pRootNode;
TreeNode * pChild = 0;
QString lastKey;
QStringList path = key.split( '/' );
QStringList::const_iterator lIt = path.begin();
QStringList::const_iterator lEIt = path.end();
while ( lIt != lEIt )
{
const QString & subPath = *lIt++;
pChild = pTarget->findChild( subPath );
if ( !pChild )
{
if ( lIt != lEIt )
pTarget = pTarget->addChild( subPath );
else
lastKey = subPath;
}
else
{
pTarget = pChild;
}
}
pTarget->addChild( lastKey, value );
it++;
}
if ( pRootNode->mChildren.size() )
{
bResult = true;
QXmlStreamWriter writer( &device );
writer.setAutoFormatting( true );
writer.writeStartDocument();
writeTree( writer, pRootNode );
writer.writeEndDocument();
}
delete pRootNode;
return bResult;
}
To copy to clipboard, switch view to plain text mode
Bookmarks