initial work to move everything into the propertiesinterface which is related to it

This commit is contained in:
Martin Marmsoler
2020-08-13 23:27:11 +02:00
parent 70ef559874
commit 385d0ffd69
18 changed files with 555 additions and 226 deletions

View File

@@ -17,6 +17,224 @@
*/
#include "propertiesinterface.h"
/*!
* Available property types
*/
namespace {
const QString integerS = "int";
const QString doubleS = "double";
const QString boolS = "bool";
const QString stringS = "string";
}
PropertiesInterface::PropertiesInterface()
{
}
QDomElement PropertiesInterface::createXmlProperty(QDomDocument &doc, const QString& name, const QString value) const {
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", stringS);
p.setAttribute("value", value);
return p;
}
QDomElement PropertiesInterface::createXmlProperty(QDomDocument& doc, const QString& name, const int value) const {
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", integerS);
p.setAttribute("value", value);
return p;
}
QDomElement PropertiesInterface::createXmlProperty(QDomDocument& doc, const QString& name, const double value) const {
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", doubleS);
p.setAttribute("value", value);
return p;
}
QDomElement PropertiesInterface::createXmlProperty(QDomDocument& doc, const QString& name, const bool value) const {
QDomElement p = doc.createElement("property");
p.setAttribute("name", name);
p.setAttribute("type", boolS);
p.setAttribute("value", value);
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))
return QDomElement();
if (child.attribute("name") == name)
return child;
}
return QDomElement();
}
/*!
* \brief PropertiesInterface::attribute
* Returns the property with the name \p attribute_name
* \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))
return false;
*attr = e.attribute(attribute_name);
} else {
if (p.attribute("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, int defaultValue) {
QString attr;
if (!attribute(e, attribute_name, integerS, &attr)) {
*entier = defaultValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
bool ok;
int tmp = attr.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, double defaultValue) {
QString attr;
if (!attribute(e, attribute_name, doubleS, &attr)) {
*reel = defaultValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
bool ok;
double tmp = attr.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, bool defaulValue) {
QString attr;
if (!attribute(e, attribute_name, integerS, &attr)) {
*boolean = defaulValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
bool ok;
bool tmp = attr.toInt(&ok);
if (!ok)
return PropertyFlags::NoValidConversion;
if (boolean != nullptr)
*boolean = tmp;
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyString(const QDomElement& e, const QString& attribute_name, QString* string, QString defaultValue) {
QString attr;
if (!attribute(e, attribute_name, stringS, &attr)) {
*string = defaultValue;
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;
}
/**
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
en orientation. Si la chaine fait plusieurs caracteres, seul le
premier est pris en compte. En cas d'incoherence, Qet::North est
retourne.
@param s Chaine de caractere cense representer une orientation
@return l'orientation designee par la chaine de caractere
*/
Qet::Orientation PropertiesInterface::orientationFromString(const QString &s) {
QChar c = s[0];
if (c == 'e') return(Qet::East);
else if (c == 's') return(Qet::South);
else if (c == 'w') return (Qet::West);
else return(Qet::North);
}
/**
@param o une orientation
@return une chaine de caractere representant l'orientation
*/
QString PropertiesInterface::orientationToString(Qet::Orientation o) {
QString ret;
switch(o) {
case Qet::North: ret = "n"; break;
case Qet::East : ret = "e"; break;
case Qet::South: ret = "s"; break;
case Qet::West : ret = "w"; break;
}
return(ret);
}

View File

@@ -21,6 +21,8 @@
#include <QString>
#include <QSettings>
#include <QDomElement>
#include <limits>
#include "qet.h"
/**
* @brief The PropertiesInterface class
@@ -36,6 +38,49 @@ class PropertiesInterface
// Save/load properties to xml element
virtual QDomElement toXml (QDomDocument &xml_document) const =0;
virtual bool fromXml (const QDomElement &xml_element) =0;
virtual bool valideXml(QDomElement& element) const = 0;
/*!
* Use this functions to add properties to the xml document
*/
QDomElement createXmlProperty(QDomDocument& doc, const QString& name, const QString value) const;
QDomElement createXmlProperty(QDomDocument& doc, const QString& name, const int value) const;
QDomElement createXmlProperty(QDomDocument& doc, const QString& name, const double value) const;
QDomElement createXmlProperty(QDomDocument& doc, const QString& name, const bool value) const;
static QDomElement property(const QDomElement& e, const QString& name);
static bool attribute(const QDomElement& e, const QString& attribute_name, const QString& type, QString* attr);
typedef enum PropertyFlags {
Success,
NotFound,
NoValidConversion,
};
static PropertyFlags propertyInteger(const QDomElement &e, const QString& attribute_name, int *entier = nullptr, int defaultValue = std::numeric_limits<int>::quiet_NaN());
static PropertyFlags propertyDouble(const QDomElement &e, const QString& attribute_name, double *reel = nullptr, double defaultValue = std::numeric_limits<double>::quiet_NaN());
static PropertyFlags propertyString(const QDomElement& e, const QString& attribute_name, QString* string = nullptr, QString defaultValue = QString());
static PropertyFlags propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean = nullptr, bool defaulValue = false);
static bool validXmlProperty(const QDomElement& e);
QVariant XmlProperty(const QDomElement& element);
/**
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
en orientation. Si la chaine fait plusieurs caracteres, seul le
premier est pris en compte. En cas d'incoherence, Qet::North est
retourne.
@param s Chaine de caractere cense representer une orientation
@return l'orientation designee par la chaine de caractere
*/
static Qet::Orientation orientationFromString(const QString &s);
/**
@param o une orientation
@return une chaine de caractere representant l'orientation
*/
static QString orientationToString(Qet::Orientation o);
};
#endif // PROPERTIESINTERFACE_H

View File

@@ -43,17 +43,11 @@ QDomElement TerminalData::toXml(QDomDocument &xml_document) const
{
QDomElement xml_element = xml_document.createElement("terminal");
// ecrit la position de la borne
xml_element.setAttribute("x", QString("%1").arg(q->scenePos().x()));
xml_element.setAttribute("y", QString("%1").arg(q->scenePos().y()));
xml_element.setAttribute("uuid", m_uuid.toString());
xml_element.setAttribute("name", m_name);
// ecrit l'orientation de la borne
xml_element.setAttribute("orientation", Qet::orientationToString(m_orientation));
// Write name and number to XML
xml_element.appendChild(createXmlProperty(xml_document, "x", q->scenePos().x()));
xml_element.appendChild(createXmlProperty(xml_document, "y", q->scenePos().y()));
xml_element.appendChild(createXmlProperty(xml_document, "uuid", m_uuid.toString()));
xml_element.appendChild(createXmlProperty(xml_document, "name", m_name));
xml_element.appendChild(createXmlProperty(xml_document, "orientation", orientationToString(m_orientation)));
return(xml_element);
}
@@ -61,27 +55,40 @@ bool TerminalData::fromXml (const QDomElement &xml_element)
{
// lit la position de la borne
qreal term_x = 0.0, term_y = 0.0;
if (!QET::attributeIsAReal(xml_element, "x", &term_x))
if (!propertyDouble(xml_element, "x", &term_x))
return false;
if (!QET::attributeIsAReal(xml_element, "y", &term_y))
if (!propertyDouble(xml_element, "y", &term_y))
return false;
m_pos = QPointF(term_x, term_y);
//emit posFromXML(QPointF(term_x, term_y));
QString uuid = xml_element.attribute("uuid");
QString uuid;
if (!propertyString(xml_element, "uuid", &uuid))
return false;
// update part and add uuid, which is used in the new version to connect terminals together
// if the attribute not exists, means, the element is created with an older version of qet. So use the legacy approach
// to identify terminals
if (!uuid.isEmpty())
m_uuid = QUuid(uuid);
m_name = xml_element.attribute("name");
if (!propertyString(xml_element, "name", &m_name))
return false;
QString o;
if (!propertyString(xml_element, "orientation", &o))
return false;
// lit l'orientation de la borne
m_orientation = Qet::orientationFromString(xml_element.attribute("orientation"));
m_orientation = orientationFromString(o);
return true;
}
bool TerminalData::valideXml(QDomElement& element) const {
return true;
}

View File

@@ -35,6 +35,24 @@ public:
QDomElement toXml(QDomDocument &xml_element) const override;
bool fromXml(const QDomElement &xml_element) override;
bool valideXml(QDomElement &element) const override;
/**
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
en orientation. Si la chaine fait plusieurs caracteres, seul le
premier est pris en compte. En cas d'incoherence, Qet::North est
retourne.
@param s Chaine de caractere cense representer une orientation
@return l'orientation designee par la chaine de caractere
*/
static Qet::Orientation orientationFromString(const QString &s);
/**
@param o une orientation
@return une chaine de caractere representant l'orientation
*/
static QString orientationToString(Qet::Orientation o);
// must be public, because this class is a private member of PartTerminal/Terminal and they must
// access this data
public:

View File

@@ -96,27 +96,21 @@ void XRefProperties::fromSettings(const QSettings &settings, const QString prefi
QDomElement XRefProperties::toXml(QDomDocument &xml_document) const {
QDomElement xml_element = xml_document.createElement("xref");
xml_element.setAttribute("type", m_key);
xml_element.setAttribute("showpowerctc", m_show_power_ctc? "true" : "false");
QString display = m_display == Cross? "cross" : "contacts";
xml_element.setAttribute("displayhas", display);
QString snap = m_snap_to == Bottom? "bottom" : "label";
xml_element.setAttribute("snapto", snap);
xml_element.appendChild(createXmlProperty(xml_document, "type", m_key));
xml_element.appendChild(createXmlProperty(xml_document, "showpowerctc", m_show_power_ctc));
xml_element.appendChild(createXmlProperty(xml_document, "displayhas", m_display == Cross? "cross" : "contacts"));
xml_element.appendChild(createXmlProperty(xml_document, "snapto", m_snap_to == Bottom? "bottom" : "label"));
QString xrefpos;
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
xml_element.setAttribute("xrefpos", var.valueToKey(m_xref_pos));
int offset = m_offset;
xml_element.setAttribute("offset", QString::number(offset));
QString master_label = m_master_label;
xml_element.setAttribute("master_label", master_label);
QString slave_label = m_slave_label;
xml_element.setAttribute("slave_label", slave_label);
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
xml_element.appendChild(createXmlProperty(xml_document, "xrefpos", var.valueToKey(m_xref_pos)));
xml_element.appendChild(createXmlProperty(xml_document, "offset", m_offset));
xml_element.appendChild(createXmlProperty(xml_document, "master_label", m_master_label));
xml_element.appendChild(createXmlProperty(xml_document, "slave_label", m_slave_label));
foreach (QString key, m_prefix.keys()) {
xml_element.setAttribute(key + "prefix", m_prefix.value(key));
xml_element.appendChild(createXmlProperty(xml_document, key + "prefix", m_prefix.value(key)));
}
return xml_element;
@@ -128,30 +122,42 @@ QDomElement XRefProperties::toXml(QDomDocument &xml_document) const {
* @param xml_element: QDomElement to use for load
*/
bool XRefProperties::fromXml(const QDomElement &xml_element) {
m_show_power_ctc = xml_element.attribute("showpowerctc") == "true";
QString display = xml_element.attribute("displayhas", "cross");
if (!propertyBool(xml_element, "showpowerctc", &m_show_power_ctc))
return false;
QString display;
propertyString(xml_element, "displayhas", &display, "cross");
display == "cross"? m_display = Cross : m_display = Contacts;
QString snap = xml_element.attribute("snapto", "label");
QString snap;
propertyString(xml_element, "snapto", &snap, "label");
snap == "bottom"? m_snap_to = Bottom : m_snap_to = Label;
QString xrefpos = xml_element.attribute("xrefpos","Left");
QString xrefpos;
if (propertyString(xml_element, "xrefpos", &xrefpos, "Left") == PropertyFlags::NotFound) {
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
m_xref_pos = Qt::AlignmentFlag(var.keyToValue(xrefpos.toStdString().data()));
} else
m_xref_pos = Qt::AlignBottom;
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
if(xml_element.hasAttribute("xrefpos"))
m_xref_pos = Qt::AlignmentFlag(var.keyToValue(xml_element.attribute("xrefpos").toStdString().data()));
else
m_xref_pos = Qt::AlignBottom;
m_offset = xml_element.attribute("offset", "0").toInt();
m_master_label = xml_element.attribute("master_label", "%f-%l%c");
m_slave_label = xml_element.attribute("slave_label","(%f-%l%c)");
propertyInteger(xml_element, "offset", &m_offset, 0);
propertyString(xml_element, "master_label", &m_master_label, "%f-%l%c");
propertyString(xml_element, "slave_label", &m_slave_label, "(%f-%l%c)");
QString value;
foreach (QString key, m_prefix_keys) {
m_prefix.insert(key, xml_element.attribute(key + "prefix"));
propertyString(xml_element, key + "prefix", &value);
m_prefix.insert(key, value);
}
return true;
}
bool XRefProperties::valideXml(QDomElement& element) const {
// TODO: implement
return true;
}
/**
* @brief XRefProperties::defaultProperties
* @return the default properties stored in the setting file

View File

@@ -44,6 +44,7 @@ class XRefProperties : public PropertiesInterface
void fromSettings (const QSettings &settings, const QString = QString()) override;
QDomElement toXml (QDomDocument &xml_document) const override;
bool fromXml(const QDomElement &xml_element) override;
bool valideXml(QDomElement& element) const override;
static QHash<QString, XRefProperties> defaultProperties();