move all static xml functions to qetxml

This commit is contained in:
Martin Marmsoler
2021-03-06 20:01:31 +01:00
parent ea793125a5
commit 058824f29a
23 changed files with 725 additions and 675 deletions

View File

@@ -17,27 +17,14 @@
*/
#include "propertiesinterface.h"
#include <QDebug>
#include "../qetxml.h"
/*!
* Available property types
*/
namespace {
const QString integerS = "int";
const QString doubleS = "double";
const QString boolS = "bool";
const QString stringS = "string";
const QString uuidS = "uuid";
const QString colorS = "color";
const QString userPropertiesS = "userProperties";
PropertiesInterface::PropertyFlags debug(PropertiesInterface::PropertyFlags flag, const QDomElement &e, const QString& attribute_name, const QString& attr, const QString& type)
{
if (flag == PropertiesInterface::PropertyFlags::NoValidConversion)
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "No valid Conversion: " << attribute_name << ". type: " << type << ". value: " << attr;
return flag;
}
}
/**
@@ -90,287 +77,6 @@ bool PropertiesInterface::valideXml(QDomElement& element) {
return false;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const QString value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", stringS);
p.setAttribute("value", value);
return p;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const char* value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", stringS);
p.setAttribute("value", value);
return p;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const int value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", integerS);
p.setAttribute("value", QString::number(value));
return p;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const double value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", doubleS);
p.setAttribute("value", QString::number(value));
return p;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const bool value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", boolS);
p.setAttribute("value", QString::number(value));
return p;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const QUuid value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", uuidS);
p.setAttribute("value", value.toString());
return p;
}
QDomElement PropertiesInterface::createXmlProperty(const QString& name, const QColor value) {
QDomDocument doc;
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", colorS);
p.setAttribute("value", value.name());
return p;
}
QDomElement PropertiesInterface::property(const QDomElement& e, const QString& name) {
for (int i=0; i < e.childNodes().count(); i++) {
QDomElement child = e.childNodes().at(i).toElement();
if (!validXmlProperty(child))
continue; // there might also non property childs
if (child.attribute("name") == name)
return child;
}
return QDomElement();
}
/*!
* \brief PropertiesInterface::attribute
* Returns the property with the name \p attribute_name and type \p type
* \param e Xml element which contains the property
* \param attribute_name
* \param type Type of the property
* \param attr
* \return
*/
bool PropertiesInterface::attribute(const QDomElement& e, const QString& attribute_name, const QString& type, QString* attr) {
QDomElement p = property(e, attribute_name);
if (p.isNull()) {
// check if legacy property is available,
// where the property is inside the element as attribute
if (!e.hasAttribute(attribute_name)) {
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "Property " << attribute_name << "is not available";
return false;
}
*attr = e.attribute(attribute_name);
} else {
if (p.attribute("type") != type) {
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ", Property: " << attribute_name << "(" << p.attribute("type") << ") has not type: " << type;
return false;
}
*attr = p.attribute("value");
}
return true;
}
/*!
* \brief PropertiesInterface::propertyInteger
* Reads an interger from the XML element.
* \param e DomElement which contains the property attribute
* \param attribute_name Name of the attribute
* \param entier Return value if success
* \return True if reading an integer was successful, else False. If the attribute was not found,
* \p entier is not valid and the return value is False
*/
PropertiesInterface::PropertyFlags PropertiesInterface::propertyInteger(const QDomElement &e, const QString& attribute_name, int* entier) {
QString attr;
if (!attribute(e, attribute_name, integerS, &attr)) {
return PropertyFlags::NotFound;
}
return debug(propertyInteger(attr, entier), e, attribute_name, attr, integerS);
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyInteger(const QString& value, int* entier) {
// verifie la validite de l'attribut
bool ok;
int tmp = value.toInt(&ok);
if (!ok) {
return PropertyFlags::NoValidConversion;
}
if (entier != nullptr)
*entier = tmp;
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyDouble(const QDomElement &e, const QString& attribute_name, double* reel) {
QString attr;
if (!attribute(e, attribute_name, doubleS, &attr)) {
return PropertyFlags::NotFound;
}
return debug(propertyDouble(attr, reel), e, attribute_name, attr, doubleS);
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyDouble(const QString& value, double* reel)
{
// verifie la validite de l'attribut
bool ok;
double tmp = value.toDouble(&ok);
if (!ok) {
return PropertyFlags::NoValidConversion;
}
if (reel != nullptr)
*reel = tmp;
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean) {
QString attr;
if (!attribute(e, attribute_name, boolS, &attr)) {
return PropertyFlags::NotFound;
}
return debug(propertyBool(attr, boolean), e, attribute_name, attr, boolS);
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QString& value, bool* boolean)
{
// verifie la validite de l'attribut
bool ok;
bool tmp = value.toInt(&ok);
if (!ok) {
if (value == "true" || value == "1")
tmp = true;
else if (value == "false" || value == "0")
tmp = false;
else {
return PropertyFlags::NoValidConversion;
}
}
if (boolean != nullptr)
*boolean = tmp;
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyColor(const QDomElement &e, const QString& attribute_name, QColor* color) {
QString attr;
if (!attribute(e, attribute_name, colorS, &attr)) {
return PropertyFlags::NotFound;
}
return debug(propertyColor(attr, color), e, attribute_name, attr, colorS);
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyColor(const QString& value, QColor* color)
{
// verifie la validite de l'attribut
QColor tmp = QColor(value);
if (!tmp.isValid()) {
return PropertyFlags::NoValidConversion;
}
if (color != nullptr)
*color = tmp;
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid) {
QString attr;
if (!attribute(e, attribute_name, uuidS, &attr)) {
return PropertyFlags::NotFound;
}
return debug(propertyUuid(attr, uuid), e, attribute_name, attr, uuidS);
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyUuid(const QString& value, QUuid* uuid)
{
if (QUuid(value).isNull()){
return PropertyFlags::NoValidConversion;
}
if (uuid != nullptr)
*uuid = QUuid(value);
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyString(const QDomElement& e, const QString& attribute_name, QString* string) {
QString attr;
if (!attribute(e, attribute_name, stringS, &attr)) {
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
if (string != nullptr)
*string = attr;
return PropertyFlags::Success;
}
/*!
* \brief PropertiesInterface::validXmlProperty
* Check if the Xml element contains the needed fields
* \param e Xml Property
* \return True if name, type, value attribute are available, else false
*/
bool PropertiesInterface::validXmlProperty(const QDomElement& e) {
if (!e.hasAttribute("name"))
return false;
if (!e.hasAttribute("type"))
return false;
if (!e.hasAttribute("value"))
return false;
return true;
}
/**
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
en orientation. Si la chaine fait plusieurs caracteres, seul le
@@ -444,15 +150,15 @@ void PropertiesInterface::propertiesToXml(QDomElement& e) const
auto type = i.value().type();
switch(type) {
case QVariant::Type::String:
up.appendChild(createXmlProperty(i.key(), i.value().toString())); break;
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toString())); break;
case QVariant::Type::Int:
up.appendChild(createXmlProperty(i.key(), i.value().toInt())); break;
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toInt())); break;
case QVariant::Type::Double:
up.appendChild(createXmlProperty(i.key(), i.value().toDouble())); break;
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toDouble())); break;
case QVariant::Type::Bool:
up.appendChild(createXmlProperty(i.key(), i.value().toBool())); break;
up.appendChild(QETXML::createXmlProperty(i.key(), i.value().toBool())); break;
case QVariant::Type::Color:
up.appendChild(createXmlProperty(i.key(), QColor(i.value().value<QColor>()))); break;
up.appendChild(QETXML::createXmlProperty(i.key(), QColor(i.value().value<QColor>()))); break;
default:
break;
}
@@ -484,47 +190,47 @@ bool PropertiesInterface::propertiesFromXml(const QDomElement& e)
QString type = userProperty.attribute("type");
QString value = userProperty.attribute("value");
if (type == integerS)
if (type == QETXML::integerS)
{
int i;
if (propertyInteger(value, &i) == PropertyFlags::Success)
if (QETXML::propertyInteger(value, &i) == QETXML::PropertyFlags::Success)
properties[name] = QVariant(i);
else
return false;
}
else if (type == doubleS)
else if (type == QETXML::doubleS)
{
double d;
if (propertyDouble(value, &d) == PropertyFlags::Success)
if (QETXML::propertyDouble(value, &d) == QETXML::PropertyFlags::Success)
properties[name] = QVariant(d);
else
return false;
}
else if (type == boolS)
else if (type == QETXML::boolS)
{
bool b;
if (propertyBool(value, &b) == PropertyFlags::Success)
if (QETXML::propertyBool(value, &b) == QETXML::PropertyFlags::Success)
properties[name] = QVariant(b);
else
return false;
}
else if (type == uuidS)
else if (type == QETXML::uuidS)
{
QUuid u;
if (propertyUuid(value, &u) == PropertyFlags::Success)
if (QETXML::propertyUuid(value, &u) == QETXML::PropertyFlags::Success)
properties[name] = QVariant(u);
else
return false;
}
else if (type == colorS)
else if (type == QETXML::colorS)
{
QColor c;
if (propertyColor(value, &c) == PropertyFlags::Success)
if (QETXML::propertyColor(value, &c) == QETXML::PropertyFlags::Success)
properties[name] = QVariant(c);
else
return false;
}
else if (type == stringS)
else if (type == QETXML::stringS)
{
properties[name] = QVariant(value);
}

View File

@@ -112,43 +112,6 @@ class PropertiesInterface
static bool valideXml(QDomElement& element);
/*!
* Use this functions to add properties to the xml document
*/
static QDomElement createXmlProperty(const QString& name, const QString value);
static QDomElement createXmlProperty(const QString& name, const char* value);
static QDomElement createXmlProperty(const QString& name, const int value);
static QDomElement createXmlProperty(const QString& name, const double value);
static QDomElement createXmlProperty(const QString& name, const bool value);
static QDomElement createXmlProperty(const QString& name, const QUuid value);
static QDomElement createXmlProperty(const QString& name, const QColor value);
static QDomElement property(const QDomElement& e, const QString& name);
static bool attribute(const QDomElement& e, const QString& attribute_name, const QString& type, QString* attr);
enum PropertyFlags {
Success = 0,
NotFound = 1,
NoValidConversion = 2,
// = 4
};
static PropertyFlags propertyInteger(const QString& value, int* entry = nullptr);
static PropertyFlags propertyInteger(const QDomElement &e, const QString& attribute_name, int *entier = nullptr);
static PropertyFlags propertyDouble(const QString& value, double* entry = nullptr);
static PropertyFlags propertyDouble(const QDomElement &e, const QString& attribute_name, double *reel = nullptr);
static PropertyFlags propertyString(const QDomElement& e, const QString& attribute_name, QString* string = nullptr);
static PropertyFlags propertyBool(const QString& value, bool* entry = nullptr);
static PropertyFlags propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean = nullptr);
static PropertyFlags propertyUuid(const QString& value, QUuid* entry = nullptr);
static PropertyFlags propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid = nullptr);
static PropertyFlags propertyColor(const QString& value, QColor* entry = nullptr);
static PropertyFlags propertyColor(const QDomElement &e, const QString& attribute_name, QColor* color = nullptr);
static bool validXmlProperty(const QDomElement& e);
QVariant XmlProperty(const QDomElement& element);
void setTagName(const QString& tagname);
QString tagName() const;

View File

@@ -20,6 +20,8 @@
#include <QGraphicsObject>
#include <QDebug>
#include "../qetxml.h"
TerminalData::TerminalData():
PropertiesInterface("terminaldata")
{
@@ -99,11 +101,11 @@ void TerminalData::toXmlPriv(QDomElement& e) const
// m_pos cannot be stored, because in the partterminal it will not be updated.
// In PartTerminal m_pos is the position of the dock, in Terminal m_pos is the second side of the terminal
// This is hold for legacy compability reason
e.appendChild(createXmlProperty("x", m_pos.x()));
e.appendChild(createXmlProperty("y", m_pos.y()));
e.appendChild(createXmlProperty("name", m_name));
e.appendChild(createXmlProperty("orientation", orientationToString(m_orientation)));
e.appendChild(createXmlProperty("type", typeToString(m_type)));
e.appendChild(QETXML::createXmlProperty("x", m_pos.x()));
e.appendChild(QETXML::createXmlProperty("y", m_pos.y()));
e.appendChild(QETXML::createXmlProperty("name", m_name));
e.appendChild(QETXML::createXmlProperty("orientation", orientationToString(m_orientation)));
e.appendChild(QETXML::createXmlProperty("type", typeToString(m_type)));
}
/*
@@ -123,10 +125,10 @@ bool TerminalData::fromXmlPriv(const QDomElement &xml_element)
// reads the position of the terminal
// lit la position de la borne
if (propertyDouble(xml_element, "x", &term_x))
if (QETXML::propertyDouble(xml_element, "x", &term_x))
return false;
if (propertyDouble(xml_element, "y", &term_y))
if (QETXML::propertyDouble(xml_element, "y", &term_y))
return false;
m_pos = QPointF(term_x, term_y);
@@ -139,12 +141,12 @@ bool TerminalData::fromXmlPriv(const QDomElement &xml_element)
// older version of qet. So use the legacy approach
//if (propertyString(xml_element, "name", &m_name))
//if (QETXML::propertyString(xml_element, "name", &m_name))
// return false;
propertyString(xml_element, "name", &m_name); // some parts do not have a name. Example: affuteuse_250h.qet, Terminal at x="0" y="-20"
QETXML::propertyString(xml_element, "name", &m_name); // some parts do not have a name. Example: affuteuse_250h.qet, Terminal at x="0" y="-20"
QString o;
if (propertyString(xml_element, "orientation", &o))
if (QETXML::propertyString(xml_element, "orientation", &o))
return false;
// read the orientation of the terminal
@@ -152,29 +154,29 @@ bool TerminalData::fromXmlPriv(const QDomElement &xml_element)
m_orientation = orientationFromString(o);
QString type;
if (propertyString(xml_element, "type", &type) == PropertyFlags::Success)
if (QETXML::propertyString(xml_element, "type", &type) == QETXML::PropertyFlags::Success)
m_type = typeFromString(type);
return true;
}
bool TerminalData::valideXml(const QDomElement& xml_element) {
if (propertyDouble(xml_element, "x"))
if (QETXML::propertyDouble(xml_element, "x"))
return false;
// Old projects do not have this property.
// if (propertyString(xml_element, "type"))
// if (QETXML::propertyString(xml_element, "type"))
// return false;
// legacy elements do not have an uuid
// if (propertyUuid(xml_element, "uuid"))
// if (QETXML::propertyUuid(xml_element, "uuid"))
// return false;
//if (propertyString(xml_element, "name")) // some parts do not have a name. Example: affuteuse_250h.qet, Terminal at x="0" y="-20"
//if (QETXML::propertyString(xml_element, "name")) // some parts do not have a name. Example: affuteuse_250h.qet, Terminal at x="0" y="-20"
// return false;
if (propertyString(xml_element, "orientation"))
if (QETXML::propertyString(xml_element, "orientation"))
return false;
return true;
}

View File

@@ -22,6 +22,8 @@
#include <QHash>
#include <QMetaEnum>
#include "../qetxml.h"
/**
@brief XRefProperties::XRefProperties
Default Constructor
@@ -96,20 +98,20 @@ void XRefProperties::fromSettings(QSettings &settings,
void XRefProperties::toXmlPriv(QDomElement& e) const
{
e.appendChild(createXmlProperty("type", m_key));
e.appendChild(createXmlProperty("showpowerctc", m_show_power_ctc));
e.appendChild(createXmlProperty("displayhas", m_display == Cross? "cross" : "contacts"));
e.appendChild(createXmlProperty("snapto", m_snap_to == Bottom? "bottom" : "label"));
e.appendChild(QETXML::createXmlProperty("type", m_key));
e.appendChild(QETXML::createXmlProperty("showpowerctc", m_show_power_ctc));
e.appendChild(QETXML::createXmlProperty("displayhas", m_display == Cross? "cross" : "contacts"));
e.appendChild(QETXML::createXmlProperty("snapto", m_snap_to == Bottom? "bottom" : "label"));
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
e.appendChild(createXmlProperty("xrefpos", var.valueToKey(m_xref_pos)));
e.appendChild(createXmlProperty("offset", m_offset));
e.appendChild(createXmlProperty("master_label", m_master_label));
e.appendChild(createXmlProperty("slave_label", m_slave_label));
e.appendChild(QETXML::createXmlProperty("xrefpos", var.valueToKey(m_xref_pos)));
e.appendChild(QETXML::createXmlProperty("offset", m_offset));
e.appendChild(QETXML::createXmlProperty("master_label", m_master_label));
e.appendChild(QETXML::createXmlProperty("slave_label", m_slave_label));
foreach (QString key, m_prefix.keys()) {
e.appendChild(createXmlProperty(key + "prefix", m_prefix.value(key)));
e.appendChild(QETXML::createXmlProperty(key + "prefix", m_prefix.value(key)));
}
}
@@ -120,32 +122,32 @@ void XRefProperties::toXmlPriv(QDomElement& e) const
*/
bool XRefProperties::fromXmlPriv(const QDomElement &xml_element) {
if (propertyBool(xml_element, "showpowerctc", &m_show_power_ctc))
if (QETXML::propertyBool(xml_element, "showpowerctc", &m_show_power_ctc))
return false;
QString display;
if (propertyString(xml_element, "displayhas", &display) != PropertyFlags::NotFound) {
if (QETXML::propertyString(xml_element, "displayhas", &display) != QETXML::PropertyFlags::NotFound) {
display == "cross"? m_display = Cross : m_display = Contacts;
}
QString snap;
if (propertyString(xml_element, "snapto", &snap) != PropertyFlags::NotFound) {
if (QETXML::propertyString(xml_element, "snapto", &snap) != QETXML::PropertyFlags::NotFound) {
snap == "bottom"? m_snap_to = Bottom : m_snap_to = Label;
}
QString xrefpos;
if (propertyString(xml_element, "xrefpos", &xrefpos) != PropertyFlags::NotFound) {
if (QETXML::propertyString(xml_element, "xrefpos", &xrefpos) != QETXML::PropertyFlags::NotFound) {
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
m_xref_pos = Qt::AlignmentFlag(var.keyToValue(xrefpos.toStdString().data()));
}
// TODO: why it compiles without this true??
propertyInteger(xml_element, "offset", &m_offset);
propertyString(xml_element, "master_label", &m_master_label);
propertyString(xml_element, "slave_label", &m_slave_label);
QETXML::propertyInteger(xml_element, "offset", &m_offset);
QETXML::propertyString(xml_element, "master_label", &m_master_label);
QETXML::propertyString(xml_element, "slave_label", &m_slave_label);
QString value;
foreach (QString key, m_prefix_keys) {
if (!propertyString(xml_element, key + "prefix", &value))
if (!QETXML::propertyString(xml_element, key + "prefix", &value))
m_prefix.insert(key, value);
}
return true;