diff --git a/sources/diagramcontext.cpp b/sources/diagramcontext.cpp index 09722fc14..3010ca0c1 100644 --- a/sources/diagramcontext.cpp +++ b/sources/diagramcontext.cpp @@ -17,6 +17,7 @@ */ #include "diagramcontext.h" #include +#include "qet.h" /** @return a list containing all the keys in the context object. @@ -72,6 +73,13 @@ void DiagramContext::clear() { content_.clear(); } +/** + @return the number of key/value pairs stored in this object. +*/ +int DiagramContext::count() { + return(content_.count()); +} + bool DiagramContext::operator==(const DiagramContext &dc) const { return(content_ == dc.content_); } @@ -80,6 +88,62 @@ bool DiagramContext::operator!=(const DiagramContext &dc) const { return(!(*this == dc)); } +/** + Export this context properties under the \a e XML element, using tags + named \a tag_name (defaults to "property"). +*/ +void DiagramContext::toXml(QDomElement &e, const QString &tag_name) const { + foreach (QString key, keys()) { + QDomElement property = e.ownerDocument().createElement(tag_name); + property.setAttribute("name", key); + QDomText value = e.ownerDocument().createTextNode(content_[key].toString()); + property.appendChild(value); + e.appendChild(property); + } +} + +/** + Read this context properties from the \a e XML element, looking for tags named + \a tag_name (defaults to "property"). +*/ +void DiagramContext::fromXml(const QDomElement &e, const QString &tag_name) { + foreach (QDomElement property, QET::findInDomElement(e, tag_name)) { + if (!property.hasAttribute("name")) continue; + addValue(property.attribute("name"), QVariant(property.text())); + } +} + +/** + Export this context properties to \a settings by creating an array named \a + array_name. +*/ +void DiagramContext::toSettings(QSettings &settings, const QString &array_name) const { + settings.beginWriteArray(array_name); + int i = 0; + foreach (QString key, content_.keys()) { + settings.setArrayIndex(i); + settings.setValue("name", key); + settings.setValue("value", content_[key].toString()); + ++ i; + } + settings.endArray(); +} + +/** + Read this context properties from \a settings by running through the array + named \a array_name. +*/ +void DiagramContext::fromSettings(QSettings &settings, const QString &array_name) { + int size = settings.beginReadArray(array_name); + for (int i = 0 ; i < size; ++ i) { + settings.setArrayIndex(i); + QString key = settings.value("name").toString(); + if (key.isEmpty()) continue; + addValue(key, settings.value("value").toString()); + } + settings.endArray(); +} + /** @return the regular expression used to check whether a given key is acceptable. @see keyIsAcceptable() diff --git a/sources/diagramcontext.h b/sources/diagramcontext.h index 7b8d6c378..ed8923cb3 100644 --- a/sources/diagramcontext.h +++ b/sources/diagramcontext.h @@ -17,7 +17,9 @@ */ #ifndef DIAGRAM_CONTEXT_H #define DIAGRAM_CONTEXT_H +#include #include +#include #include #include /** @@ -37,10 +39,16 @@ class DiagramContext { const QVariant operator[](const QString &) const; bool addValue(const QString &, const QVariant &); void clear(); + int count(); bool operator==(const DiagramContext &) const; bool operator!=(const DiagramContext &) const; + void toXml(QDomElement &, const QString & = "property") const; + void fromXml(const QDomElement &, const QString & = "property"); + void toSettings(QSettings &, const QString &) const; + void fromSettings(QSettings &, const QString &); + static QString validKeyRegExp(); private: diff --git a/sources/qet.cpp b/sources/qet.cpp index 478945110..87c4edebf 100644 --- a/sources/qet.cpp +++ b/sources/qet.cpp @@ -246,6 +246,20 @@ QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_co return(text); } +/** + @return the list of \a tag_name elements directly under the \a e XML element. +*/ +QList QET::findInDomElement(const QDomElement &e, const QString &tag_name) { + QList return_list; + for (QDomNode node = e.firstChild() ; !node.isNull() ; node = node.nextSibling()) { + if (!node.isElement()) continue; + QDomElement element = node.toElement(); + if (element.isNull() || element.tagName() != tag_name) continue; + return_list << element; + } + return(return_list); +} + /** Etant donne un element XML e, renvoie la liste de tous les elements children imbriques dans les elements parent, eux-memes enfants de l'elememt e diff --git a/sources/qet.h b/sources/qet.h index f914c2463..cb446704d 100644 --- a/sources/qet.h +++ b/sources/qet.h @@ -131,6 +131,7 @@ namespace QET { bool attributeIsAnInteger(const QDomElement &, QString , int * = NULL); bool attributeIsAReal(const QDomElement &, QString , qreal * = NULL); QString ElementsAndConductorsSentence(int, int, int = 0); + QList findInDomElement(const QDomElement &, const QString &); QList findInDomElement(const QDomElement &, const QString &, const QString &); QList forbiddenCharacters(); QString forbiddenCharactersString(bool = false);