mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-09-13 20:23:04 +02:00
Improved the DiagramContext class.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@1890 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
parent
47ea8fef37
commit
dd93f59b26
@ -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 @@ 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()
|
||||
|
@ -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 @@ 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:
|
||||
|
@ -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<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
|
||||
|
@ -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<QDomElement> findInDomElement(const QDomElement &, const QString &);
|
||||
QList<QDomElement> findInDomElement(const QDomElement &, const QString &, const QString &);
|
||||
QList<QChar> forbiddenCharacters();
|
||||
QString forbiddenCharactersString(bool = false);
|
||||
|
Loading…
x
Reference in New Issue
Block a user