This commit is contained in:
Martin Marmsoler
2020-09-14 21:21:32 +02:00
parent c5f1705745
commit 6080a7b9c9
34 changed files with 237 additions and 198 deletions

View File

@@ -17,6 +17,8 @@
*/
#include "propertiesinterface.h"
#include <QDebug>
/*!
* Available property types
*/
@@ -46,6 +48,14 @@ QDomElement PropertiesInterface::createXmlProperty(QDomDocument &doc, const QStr
return p;
}
QDomElement PropertiesInterface::createXmlProperty(QDomDocument &doc, const QString& name, const char* 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);
@@ -112,14 +122,18 @@ bool PropertiesInterface::attribute(const QDomElement& e, const QString& attribu
if (p.isNull()) {
// check if legacy property is available,
// where the property is inside the element as attribute
if (!e.hasAttribute(attribute_name))
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)
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");
@@ -136,20 +150,23 @@ bool PropertiesInterface::attribute(const QDomElement& e, const QString& attribu
* \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) {
PropertiesInterface::PropertyFlags PropertiesInterface::propertyInteger(const QDomElement &e, const QString& attribute_name, int* entier, bool setDefaultValue, int defaultValue) {
QString attr;
if (!attribute(e, attribute_name, integerS, &attr)) {
*entier = defaultValue;
if (entier && setDefaultValue)
*entier = defaultValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
bool ok;
int tmp = attr.toInt(&ok);
if (!ok)
if (!ok) {
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "No valid Conversion: " << attribute_name << ". type: " << integerS << ". value: " << attr;
return PropertyFlags::NoValidConversion;
}
if (entier != nullptr)
*entier = tmp;
@@ -157,20 +174,23 @@ PropertiesInterface::PropertyFlags PropertiesInterface::propertyInteger(const QD
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyDouble(const QDomElement &e, const QString& attribute_name, double* reel, double defaultValue) {
PropertiesInterface::PropertyFlags PropertiesInterface::propertyDouble(const QDomElement &e, const QString& attribute_name, double* reel, bool setDefaultValue, double defaultValue) {
QString attr;
if (!attribute(e, attribute_name, doubleS, &attr)) {
*reel = defaultValue;
if (reel && setDefaultValue)
*reel = defaultValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
bool ok;
double tmp = attr.toDouble(&ok);
if (!ok)
if (!ok) {
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "No valid Conversion: " << attribute_name << ". type: " << doubleS << ". value: " << attr;
return PropertyFlags::NoValidConversion;
}
if (reel != nullptr)
*reel = tmp;
@@ -178,20 +198,29 @@ PropertiesInterface::PropertyFlags PropertiesInterface::propertyDouble(const QDo
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean, bool defaultValue) {
PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean, bool setDefaultValue, bool defaultValue) {
QString attr;
if (!attribute(e, attribute_name, integerS, &attr)) {
*boolean = defaultValue;
if (!attribute(e, attribute_name, boolS, &attr)) {
if (boolean && setDefaultValue)
*boolean = defaultValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
bool ok;
bool tmp = attr.toInt(&ok);
if (!ok)
return PropertyFlags::NoValidConversion;
if (!ok) {
if (attr == "true")
tmp = true;
else if (attr == "false")
tmp = false;
else {
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "No valid Conversion: " << attribute_name << ". type: " << integerS << ". value: " << attr;
return PropertyFlags::NoValidConversion;
}
}
if (boolean != nullptr)
*boolean = tmp;
@@ -199,19 +228,22 @@ PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomE
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyColor(const QDomElement &e, const QString& attribute_name, QColor* color, QColor defaultValue) {
PropertiesInterface::PropertyFlags PropertiesInterface::propertyColor(const QDomElement &e, const QString& attribute_name, QColor* color, bool setDefaultValue, QColor defaultValue) {
QString attr;
if (!attribute(e, attribute_name, colorS, &attr)) {
*color = defaultValue;
if (color && setDefaultValue)
*color = defaultValue;
return PropertyFlags::NotFound;
}
// verifie la validite de l'attribut
QColor tmp = QColor(attr);
if (!tmp.isValid())
if (!tmp.isValid()) {
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "No valid Conversion: " << attribute_name << ". type: " << colorS << ". value: " << attr;
return PropertyFlags::NoValidConversion;
}
if (color != nullptr)
*color = tmp;
@@ -219,25 +251,33 @@ PropertiesInterface::PropertyFlags PropertiesInterface::propertyColor(const QDom
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid, QUuid defaultValue) {
PropertiesInterface::PropertyFlags PropertiesInterface::propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid, bool setDefaultValue, QUuid defaultValue) {
QString attr;
if (!attribute(e, attribute_name, uuidS, &attr)) {
*uuid = defaultValue;
if (uuid && setDefaultValue)
*uuid = defaultValue;
return PropertyFlags::NotFound;
}
if (QUuid(attr).isNull()){
qDebug() << "\t\t\t" << "Tagname: " << e.tagName() << ". " << "No valid Conversion: " << attribute_name << ". type: " << uuidS << ". value: " << attr;
return PropertyFlags::NoValidConversion;
}
if (uuid != nullptr)
*uuid = QUuid(attr);
return PropertyFlags::Success;
}
PropertiesInterface::PropertyFlags PropertiesInterface::propertyString(const QDomElement& e, const QString& attribute_name, QString* string, QString defaultValue) {
PropertiesInterface::PropertyFlags PropertiesInterface::propertyString(const QDomElement& e, const QString& attribute_name, QString* string, bool setDefaultValue, QString defaultValue) {
QString attr;
if (!attribute(e, attribute_name, stringS, &attr)) {
*string = defaultValue;
if (string && setDefaultValue)
*string = defaultValue;
return PropertyFlags::NotFound;
}
@@ -263,6 +303,8 @@ bool PropertiesInterface::validXmlProperty(const QDomElement& e) {
if (!e.hasAttribute("value"))
return false;
return true;
}
/**
@@ -275,10 +317,12 @@ bool PropertiesInterface::validXmlProperty(const QDomElement& e) {
*/
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);
// in some cases/ old projects? (affuteuse_250h.qet) numbers instead of characters are
// used for the orientation
if (c == 'e' || c == '1') return(Qet::East);
else if (c == 's' || c == '2') return(Qet::South);
else if (c == 'w' || c == '3') return (Qet::West);
else return(Qet::North); // c == '0'
}
/**

View File

@@ -46,6 +46,7 @@ class PropertiesInterface
* 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 char* 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;
@@ -62,12 +63,15 @@ class PropertiesInterface
// = 4
};
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 defaultValue = false);
static PropertyFlags propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid = nullptr, QUuid defaultValue = QUuid());
static PropertyFlags propertyColor(const QDomElement &e, const QString& attribute_name, QColor* color = nullptr, QColor defaultValue = QColor());
/*!
* Try not using the default Value feature. It is better to initialize the class members in the class definition!
*/
static PropertyFlags propertyInteger(const QDomElement &e, const QString& attribute_name, int *entier = nullptr, bool setDefaultValue = false, int defaultValue = std::numeric_limits<int>::quiet_NaN());
static PropertyFlags propertyDouble(const QDomElement &e, const QString& attribute_name, double *reel = nullptr, bool setDefaultValue = false, double defaultValue = std::numeric_limits<double>::quiet_NaN());
static PropertyFlags propertyString(const QDomElement& e, const QString& attribute_name, QString* string = nullptr, bool setDefaultValue = false, QString defaultValue = QString());
static PropertyFlags propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean = nullptr, bool setDefaultValue = false, bool defaultValue = false);
static PropertyFlags propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid = nullptr, bool setDefaultValue = false, QUuid defaultValue = QUuid());
static PropertyFlags propertyColor(const QDomElement &e, const QString& attribute_name, QColor* color = nullptr, bool setDefaultValue = false, QColor defaultValue = QColor());
static bool validXmlProperty(const QDomElement& e);

View File

@@ -45,42 +45,41 @@ QDomElement TerminalData::toXml(QDomDocument &xml_document) const
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, "uuid", m_uuid));
xml_element.appendChild(createXmlProperty(xml_document, "name", m_name));
xml_element.appendChild(createXmlProperty(xml_document, "orientation", orientationToString(m_orientation)));
return(xml_element);
}
bool TerminalData::fromXml (const QDomElement &xml_element)
bool TerminalData::fromXml (const QDomElement &xml_element) // RETURNS True
{
// lit la position de la borne
qreal term_x = 0.0, term_y = 0.0;
if (!propertyDouble(xml_element, "x", &term_x))
if (propertyDouble(xml_element, "x", &term_x))
return false;
if (!propertyDouble(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;
if (!propertyString(xml_element, "uuid", &uuid))
return false;
QUuid uuid;
// 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())
if (propertyUuid(xml_element, "uuid", &uuid) == PropertyFlags::Success)
m_uuid = QUuid(uuid);
if (!propertyString(xml_element, "name", &m_name))
return false;
//if (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"
QString o;
if (!propertyString(xml_element, "orientation", &o))
if (propertyString(xml_element, "orientation", &o))
return false;
// lit l'orientation de la borne
@@ -89,18 +88,19 @@ bool TerminalData::fromXml (const QDomElement &xml_element)
return true;
}
bool TerminalData::valideXml(QDomElement& xml_element) {
bool TerminalData::valideXml(const QDomElement& xml_element) {
if (propertyDouble(xml_element, "x"))
return false;
if (propertyDouble(xml_element, "y"))
return false;
if (propertyString(xml_element, "uuid"))
return false;
// legacy elements do not have an uuid
// if (propertyUuid(xml_element, "uuid"))
// return false;
if (propertyString(xml_element, "name"))
return false;
//if (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"))
return false;

View File

@@ -35,7 +35,7 @@ public:
QDomElement toXml(QDomDocument &xml_element) const override;
bool fromXml(const QDomElement &xml_element) override;
static bool valideXml(QDomElement &xml_element);
static bool valideXml(const QDomElement &xml_element);
// must be public, because this class is a private member of PartTerminal/Terminal and they must
// access this data
@@ -44,12 +44,12 @@ public:
* \brief m_orientation
* Orientation of the terminal
*/
Qet::Orientation m_orientation;
Qet::Orientation m_orientation{Qet::Orientation::North};
/*!
* \brief second_point
* Position of the second point of the terminal in scene coordinates
*/
QPointF second_point;
QPointF second_point{0,0};
/*!
* \brief m_uuid
* Uuid of the terminal.
@@ -60,7 +60,7 @@ public:
* uuid, the conductor after updating the part is anymore valid. So if in the loaded document a uuid exists,
* use this one and don't create a new one.
*/
QUuid m_uuid;
QUuid m_uuid; // default is an invalid uuid.
/*!
* \brief m_name
* Name of the element. It can be used to create wiring harness tables
@@ -73,7 +73,7 @@ public:
* Important: this variable is only updated during read from xml and not during mouse move!
* It is used to store the initial position so that PartTerminal and Terminal have access to it.
*/
QPointF m_pos;
QPointF m_pos{0,0};
private:
QGraphicsObject* q{nullptr};
};

View File

@@ -116,14 +116,14 @@ QDomElement XRefProperties::toXml(QDomDocument &xml_document) const {
return xml_element;
}
/**
/** RETURNS True
* @brief XRefProperties::fromXml
* Load from xml
* @param xml_element: QDomElement to use for load
*/
bool XRefProperties::fromXml(const QDomElement &xml_element) {
if (!propertyBool(xml_element, "showpowerctc", &m_show_power_ctc))
if (propertyBool(xml_element, "showpowerctc", &m_show_power_ctc))
return false;
QString display;