00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
#include "variantserializer.h"
00012
00013 void VariantSerializer::storeValue(
QDomElement & node,
const QVariant & var )
00014 {
00015
if (var.type() == QVariant::String)
00016
storeString(node, var);
00017
else if (var.type() == QVariant::StringList)
00018
storeStringList(node, var);
00019
else if (var.type() == QVariant::Int)
00020
storeInt(node, var);
00021
else if (var.type() == QVariant::Double)
00022
storeDouble(node, var);
00023
else if (var.type() == QVariant::Bool)
00024
storeBool(node, var);
00025 }
00026
00027 void VariantSerializer::storeString(
QDomElement & node,
const QVariant & var )
00028 {
00029 node.setAttribute(
"type",
"String");
00030
QDomText text = node.ownerDocument().createTextNode(var.toStringList().join(
":::"));
00031 node.appendChild(
text);
00032 }
00033
00034 void VariantSerializer::storeStringList(
QDomElement & node,
const QVariant & var )
00035 {
00036 node.setAttribute(
"type",
"StringList");
00037
QDomText text = node.ownerDocument().createTextNode(var.toStringList().join(
":::"));
00038 node.appendChild(
text);
00039 }
00040
00041 void VariantSerializer::storeBool(
QDomElement & node,
const QVariant & var )
00042 {
00043 node.setAttribute(
"type",
"Bool");
00044
QDomText text = node.ownerDocument().createTextNode(var.toString());
00045 node.appendChild(
text);
00046 }
00047
00048 void VariantSerializer::storeInt(
QDomElement & node,
const QVariant & var )
00049 {
00050 node.setAttribute(
"type",
"Int");
00051
QDomText text = node.ownerDocument().createTextNode(var.toString());
00052 node.appendChild(
text);
00053 }
00054
00055 void VariantSerializer::storeDouble(
QDomElement & node,
const QVariant & var )
00056 {
00057 node.setAttribute(
"type",
"Double");
00058
QDomText text = node.ownerDocument().createTextNode(var.toString());
00059 node.appendChild(
text);
00060 }
00061
00062 QVariant VariantSerializer::loadString(
const QDomText & node )
00063 {
00064
return QVariant(node.nodeValue());
00065 }
00066
00067 QVariant VariantSerializer::loadStringList(
const QDomText & node )
00068 {
00069
return QVariant(QStringList::split(
":::", node.nodeValue()));
00070 }
00071
00072 QVariant VariantSerializer::loadBool(
const QDomText & node )
00073 {
00074
if (node.nodeValue() ==
"false")
00075
return QVariant(
false);
00076
else
00077
return QVariant(
true);
00078 }
00079
00080 QVariant VariantSerializer::loadInt(
const QDomText & node )
00081 {
00082
return QVariant(node.nodeValue().toInt());
00083 }
00084
00085 QVariant VariantSerializer::loadDouble(
const QDomText & node )
00086 {
00087
return QVariant(node.nodeValue().toDouble());
00088 }
00089
00090 QVariant VariantSerializer::loadValue(
const QDomElement & el )
00091 {
00092
QString type = el.attribute(
"type",
"QString");
00093
QDomText val = el.firstChild().toText();
00094
if (type ==
"String")
00095
return loadString(val);
00096
else if (type ==
"StringList")
00097
return loadStringList(val);
00098
else if (type ==
"Bool")
00099
return loadBool(val);
00100
else if (type ==
"Int")
00101
return loadInt(val);
00102
else if (type ==
"Double")
00103
return loadDouble(val);
00104
return QVariant();
00105 }
00106
00107