[qet] [1890] Improved the DiagramContext class.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1890
Author:   xavier
Date:     2012-07-01 23:54:05 +0200 (Sun, 01 Jul 2012)
Log Message:
-----------
Improved the DiagramContext class.

Modified Paths:
--------------
    trunk/sources/diagramcontext.cpp
    trunk/sources/diagramcontext.h
    trunk/sources/qet.cpp
    trunk/sources/qet.h

Modified: trunk/sources/diagramcontext.cpp
===================================================================
--- trunk/sources/diagramcontext.cpp	2012-07-01 21:54:03 UTC (rev 1889)
+++ trunk/sources/diagramcontext.cpp	2012-07-01 21:54:05 UTC (rev 1890)
@@ -17,6 +17,7 @@
 */
 #include "diagramcontext.h"
 #include <QRegExp>
+#include "qet.h"
 
 /**
 	@return a list containing all the keys in the context object.
@@ -72,6 +73,13 @@
 	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_);
 }
@@ -81,6 +89,62 @@
 }
 
 /**
+	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()
 */

Modified: trunk/sources/diagramcontext.h
===================================================================
--- trunk/sources/diagramcontext.h	2012-07-01 21:54:03 UTC (rev 1889)
+++ trunk/sources/diagramcontext.h	2012-07-01 21:54:05 UTC (rev 1890)
@@ -17,7 +17,9 @@
 */
 #ifndef DIAGRAM_CONTEXT_H
 #define DIAGRAM_CONTEXT_H
+#include <QDomElement>
 #include <QHash>
+#include <QSettings>
 #include <QString>
 #include <QVariant>
 /**
@@ -37,10 +39,16 @@
 	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:

Modified: trunk/sources/qet.cpp
===================================================================
--- trunk/sources/qet.cpp	2012-07-01 21:54:03 UTC (rev 1889)
+++ trunk/sources/qet.cpp	2012-07-01 21:54:05 UTC (rev 1890)
@@ -247,6 +247,20 @@
 }
 
 /**
+	@return the list of \a tag_name elements directly under the \a e XML element.
+*/
+QList<QDomElement> QET::findInDomElement(const QDomElement &e, const QString &tag_name) {
+	QList<QDomElement> 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
 	@param e Element XML a explorer

Modified: trunk/sources/qet.h
===================================================================
--- trunk/sources/qet.h	2012-07-01 21:54:03 UTC (rev 1889)
+++ trunk/sources/qet.h	2012-07-01 21:54:05 UTC (rev 1890)
@@ -131,6 +131,7 @@
 	bool attributeIsAnInteger(const QDomElement &, QString , int * = NULL);
 	bool attributeIsAReal(const QDomElement &, QString , qreal * = NULL);
 	QString ElementsAndConductorsSentence(int, int, int = 0);
+	QList<QDomElement> findInDomElement(const QDomElement &, const QString &);
 	QList<QDomElement> findInDomElement(const QDomElement &, const QString &, const QString &);
 	QList<QChar> forbiddenCharacters();
 	QString forbiddenCharactersString(bool = false);


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/