bool DomConvenience::variantToElement(const QVariant& v, QDomElement& e) { bool ok = true; clearAttributes(e); switch (v.type()) { case QVariant::String: e.setTagName("string"); e.setAttribute("value", v.toString().utf8()); break; case QVariant::CString: e.setTagName("string"); e.setAttribute("value", v.toCString()); break; case QVariant::Int: e.setTagName("int"); e.setAttribute("value", v.toInt()); break; case QVariant::UInt: e.setTagName("uint"); e.setAttribute("value", v.toUInt()); break; case QVariant::Double: e.setTagName("double"); e.setAttribute("value", v.toDouble()); break; case QVariant::Bool: e.setTagName("bool"); e.setAttribute("value", boolString(v.toBool())); break; case QVariant::Color: { e.setTagName("color"); QColor color = v.toColor(); e.setAttribute("red", color.red()); e.setAttribute("green", color.green()); e.setAttribute("blue", color.blue()); } break; case QVariant::Pen: { e.setTagName("pen"); QPen pen = v.toPen(); e.setAttribute("red", pen.color().red()); e.setAttribute("green", pen.color().green()); e.setAttribute("blue", pen.color().blue()); e.setAttribute("style", pen.style()); e.setAttribute("cap", pen.capStyle()); e.setAttribute("join", pen.joinStyle()); } break; case QVariant::Brush: { e.setTagName("brush"); QBrush brush = v.toBrush(); e.setAttribute("red", brush.color().red()); e.setAttribute("green", brush.color().green()); e.setAttribute("blue", brush.color().blue()); e.setAttribute("style", brush.style()); } break; case QVariant::Point: { e.setTagName("point"); QPoint point = v.toPoint(); e.setAttribute("x", point.x()); e.setAttribute("y", point.y()); } break; case QVariant::Rect: { e.setTagName("rect"); QRect rect = v.toRect(); e.setAttribute("x", rect.x()); e.setAttribute("y", rect.y()); e.setAttribute("width", rect.width()); e.setAttribute("height", rect.height()); } break; case QVariant::Size: { e.setTagName("size"); QSize qsize = v.toSize(); e.setAttribute("width", qsize.width()); e.setAttribute("height", qsize.height()); } break; case QVariant::Font: { e.setTagName("font"); QFont f(v.toFont()); e.setAttribute("family", f.family()); e.setAttribute("pointsize", f.pointSize()); e.setAttribute("bold", boolString(f.bold())); e.setAttribute("italic", boolString(f.italic())); e.setAttribute("underline", boolString(f.underline())); e.setAttribute("strikeout", boolString(f.strikeOut())); } break; case QVariant::SizePolicy: { e.setTagName("sizepolicy"); QSizePolicy sp(v.toSizePolicy()); e.setAttribute("hsizetype", sp.horData()); e.setAttribute("vsizetype", sp.verData()); #if (QT_VERSION >= 300) e.setAttribute("horstretch", sp.horStretch()); e.setAttribute("verstretch", sp.verStretch()); #endif } break; case QVariant::Cursor: e.setTagName("cursor"); e.setAttribute("shape", v.toCursor().shape()); break; case QVariant::StringList: { e.setTagName("stringlist"); uint j; QDomNode n; QDomNodeList stringNodeList = e.elementsByTagName("string"); QDomElement stringElem; QStringList stringList = v.toStringList(); QStringList::Iterator it = stringList.begin(); for (j = 0; ((j < stringNodeList.length()) && (it != stringList.end())); j++) { // get the current string element stringElem = stringNodeList.item(j).toElement(); // set it to the current string variantToElement(QVariant(*it), stringElem); // iterate to the next string ++it; } // more nodes in previous stringlist then current, remove excess nodes if (stringNodeList.count() > stringList.count()) { while (j < stringNodeList.count()) e.removeChild(stringNodeList.item(j).toElement()); } else if (j <stringList.count()) { while (it != stringList.end()) { // create a new element stringElem = m_doc.createElement("string"); // set it to the currentstring variantToElement(QVariant(*it), stringElem); // append it to the current element e.appendChild(stringElem); // iterate to the next string ++it; } } } break; #if QT_VERSION >= 300 case QVariant::KeySequence: e.setTagName("key"); e.setAttribute("sequence", (QString)v.toKeySequence()); break; #endif case QVariant::ByteArray: // this is only for [u]int64_t { e.setTagName("uint64"); QByteArray ba = v.toByteArray(); // make sure this only handles [u]int64_t's if (ba.size() != sizeof(uint64_t)) { qWarning("Don't know how to persist variant of type: %s (%d) (size=%d)!", v.typeName(), v.type(), ba.size()); ok = false; break; } // convert the data back into a uint64_t uint64_t num = *(uint64_t*)ba.data(); QChar buff[33]; QChar* p = &buff[32]; const char* digitSet = "0123456789abcdef"; int len = 0; // construct the string do { *--p = digitSet[((int)(num%16))]; num = num >> 4; // divide by 16 len++; } while ( num ); // store it in a QString QString storage; storage.setUnicode(p, len); // set the value e.setAttribute("value", storage); } break; #if 0 case QVariant::List: case QVaraint::Map: #endif default: qWarning("Don't know how to persist variant of type: %s (%d)!", v.typeName(), v.type()); ok = false; break; } return ok; }
bool DomConvenience::variantToElement(const QVariant& v, QDomElement& e) { bool ok = true; clearAttributes(e); switch (v.type()) { case QVariant::String: e.setTagName("string"); e.setAttribute("value", v.toString().utf8()); break; case QVariant::CString: e.setTagName("string"); e.setAttribute("value", v.toCString()); break; case QVariant::Int: e.setTagName("int"); e.setAttribute("value", v.toInt()); break; case QVariant::UInt: e.setTagName("uint"); e.setAttribute("value", v.toUInt()); break; case QVariant::Double: e.setTagName("double"); e.setAttribute("value", v.toDouble()); break; case QVariant::Bool: e.setTagName("bool"); e.setAttribute("value", boolString(v.toBool())); break; case QVariant::Color: { e.setTagName("color"); QColor color = v.toColor(); e.setAttribute("red", color.red()); e.setAttribute("green", color.green()); e.setAttribute("blue", color.blue()); } break; case QVariant::Point: { e.setTagName("point"); QPoint point = v.toPoint(); e.setAttribute("x", point.x()); e.setAttribute("y", point.y()); } break; case QVariant::Rect: { e.setTagName("rect"); QRect rect = v.toRect(); e.setAttribute("x", rect.x()); e.setAttribute("y", rect.y()); e.setAttribute("width", rect.width()); e.setAttribute("height", rect.height()); } break; case QVariant::Size: { e.setTagName("size"); QSize qsize = v.toSize(); e.setAttribute("width", qsize.width()); e.setAttribute("height", qsize.height()); } break; case QVariant::Font: { e.setTagName("font"); QFont f(v.toFont()); e.setAttribute("family", f.family()); e.setAttribute("pointsize", f.pointSize()); e.setAttribute("bold", boolString(f.bold())); e.setAttribute("italic", boolString(f.italic())); e.setAttribute("underline", boolString(f.underline())); e.setAttribute("strikeout", boolString(f.strikeOut())); } break; case QVariant::SizePolicy: { e.setTagName("sizepolicy"); QSizePolicy sp(v.toSizePolicy()); e.setAttribute("hsizetype", sp.horData()); e.setAttribute("vsizetype", sp.verData()); #if (QT_VERSION >= 300) e.setAttribute("horstretch", sp.horStretch()); e.setAttribute("verstretch", sp.verStretch()); #endif } break; case QVariant::Cursor: e.setTagName("cursor"); e.setAttribute("shape", v.toCursor().shape()); break; case QVariant::StringList: { e.setTagName("stringlist"); uint j; QDomNode n; QDomNodeList stringNodeList = e.elementsByTagName("string"); QDomElement stringElem; QStringList stringList = v.toStringList(); QStringList::Iterator it = stringList.begin(); for (j = 0; ((j < stringNodeList.length()) && (it != stringList.end())); j++) { // get the current string element stringElem = stringNodeList.item(j).toElement(); // set it to the current string variantToElement(QVariant(*it), stringElem); // iterate to the next string ++it; } // more nodes in previous stringlist then current, remove excess nodes if (stringNodeList.count() > stringList.count()) { while (j < stringNodeList.count()) e.removeChild(stringNodeList.item(j).toElement()); } else if (j <stringList.count()) { while (it != stringList.end()) { // create a new element stringElem = m_doc.createElement("string"); // set it to the currentstring variantToElement(QVariant(*it), stringElem); // append it to the current element e.appendChild(stringElem); // iterate to the next string ++it; } } } break; #if QT_VERSION >= 300 case QVariant::KeySequence: e.setTagName("key"); e.setAttribute("sequence", (QString)v.toKeySequence()); break; #endif #if 0 case QVariant::List: case QVaraint::Map: #endif default: qWarning("Don't know how to persist variant of type: %s (%d)!", v.typeName(), v.type()); ok = false; break; } return ok; }