diff --git a/sources/borderproperties.cpp b/sources/borderproperties.cpp index 1522d89f9..8c87933b7 100644 --- a/sources/borderproperties.cpp +++ b/sources/borderproperties.cpp @@ -94,13 +94,17 @@ bool BorderProperties::operator!=(const BorderProperties &bp) { - XML element to which attributes will be added - Element XML auquel seront ajoutes des attributs */ -void BorderProperties::toXml(QDomElement &e) const { - e.setAttribute("cols", columns_count); - e.setAttribute("colsize", QString("%1").arg(columns_width)); - e.setAttribute("rows", rows_count); - e.setAttribute("rowsize", QString("%1").arg(rows_height)); - e.setAttribute("displaycols", display_columns ? "true" : "false"); - e.setAttribute("displayrows", display_rows ? "true" : "false"); +QDomElement BorderProperties::toXml(QDomDocument &dom_doc) const { + + QDomElement e = dom_doc.createElement("border"); + + e.appendChild(createXmlProperty(dom_doc, "cols", columns_count)); + e.appendChild(createXmlProperty(dom_doc, "colsize", columns_width)); + e.appendChild(createXmlProperty(dom_doc, "rows", rows_count)); + e.appendChild(createXmlProperty(dom_doc, "rowsize", rows_height)); + e.appendChild(createXmlProperty(dom_doc, "displayrows", display_rows)); + + return e; } /** @@ -111,13 +115,28 @@ void BorderProperties::toXml(QDomElement &e) const { - XML element whose attributes will be read - Element XML dont les attributs seront lus */ -void BorderProperties::fromXml(QDomElement &e) { - if (e.hasAttribute("cols")) columns_count = e.attribute("cols").toInt(); - if (e.hasAttribute("colsize")) columns_width = e.attribute("colsize").toInt(); - if (e.hasAttribute("rows")) rows_count = e.attribute("rows").toInt(); - if (e.hasAttribute("rowsize")) rows_height = e.attribute("rowsize").toInt(); - if (e.hasAttribute("displaycols")) display_columns = e.attribute("displaycols") == "true"; - if (e.hasAttribute("displayrows")) display_rows = e.attribute("displayrows") == "true"; +bool BorderProperties::fromXml(const QDomElement &e) { + + if (propertyInteger(e, "cols", &columns_count) == PropertyFlags::NoValidConversion || + propertyDouble(e, "colsize", &columns_width) == PropertyFlags::NoValidConversion || + propertyInteger(e, "rows", &rows_count) == PropertyFlags::NoValidConversion || + propertyDouble(e, "rowsize", &rows_height) == PropertyFlags::NoValidConversion || + propertyBool(e, "displaycols", &display_columns) == PropertyFlags::NoValidConversion || + propertyBool(e, "displayrows", &display_rows) == PropertyFlags::NoValidConversion) + return false; + + return true; +} + +bool BorderProperties::valideXml(QDomElement& e) const { + + if (propertyInteger(e, "cols") == PropertyFlags::Success && + propertyDouble(e, "colsize") == PropertyFlags::Success && + propertyInteger(e, "rows") == PropertyFlags::Success && + propertyDouble(e, "rowsize") == PropertyFlags::Success && + propertyBool(e, "displaycols") == PropertyFlags::Success && + propertyBool(e, "displayrows") == PropertyFlags::Success) + return true; } /** @@ -151,7 +170,7 @@ void BorderProperties::toSettings(QSettings &settings, const QString &prefix) co - prefix to be added before the names of the parameters - prefixe a ajouter devant les noms des parametres */ -void BorderProperties::fromSettings(QSettings &settings, const QString &prefix) { +void BorderProperties::fromSettings(const QSettings &settings, const QString &prefix) { columns_count = settings.value(prefix + "cols", columns_count).toInt(); columns_width = qRound(settings.value(prefix + "colsize", columns_width).toDouble()); display_columns = settings.value(prefix + "displaycols", display_columns).toBool(); diff --git a/sources/borderproperties.h b/sources/borderproperties.h index 74df1e8bb..93d72c78b 100644 --- a/sources/borderproperties.h +++ b/sources/borderproperties.h @@ -20,13 +20,15 @@ #include #include +#include "propertiesinterface.h" + /** @brief The BorderProperties class This class is a container for dimensions and display properties of a diagram. @remark Attributes are public */ -class BorderProperties { +class BorderProperties : public PropertiesInterface { public: // constructor, destructor, operators BorderProperties(); @@ -35,10 +37,11 @@ class BorderProperties { bool operator==(const BorderProperties &); bool operator!=(const BorderProperties &); - void toXml(QDomElement &) const; - void fromXml(QDomElement &); - void toSettings(QSettings &, const QString & = QString()) const; - void fromSettings(QSettings &, const QString & = QString()); + QDomElement toXml(QDomDocument &dom_doc) const override; + bool fromXml(const QDomElement &) override; + bool valideXml(QDomElement& e) const override; + void toSettings(QSettings &, const QString & = QString()) const override; + void fromSettings(const QSettings &, const QString & = QString()) override; static BorderProperties defaultProperties(); diff --git a/sources/conductorproperties.cpp b/sources/conductorproperties.cpp index 1a5b05dab..76f401c20 100644 --- a/sources/conductorproperties.cpp +++ b/sources/conductorproperties.cpp @@ -245,8 +245,10 @@ ConductorProperties::~ConductorProperties() { * Export conductor propertie, in the XML element 'e' * @param e the xml element */ -void ConductorProperties::toXml(QDomElement &e) const +void ConductorProperties::toXml(QDomDocument& doc) const { + + QDomElement conductor_elmt = xml_document.createElement("conductors"); e.setAttribute("type", typeToString(type)); if (color != QColor(Qt::black)) diff --git a/sources/conductorproperties.h b/sources/conductorproperties.h index 3356228ea..396933f0b 100644 --- a/sources/conductorproperties.h +++ b/sources/conductorproperties.h @@ -22,12 +22,14 @@ #include #include +#include "propertiesinterface.h" + class QPainter; /** This class represents the properties of a singleline conductor. */ -class SingleLineProperties { +class SingleLineProperties: public PropertiesInterface { public: SingleLineProperties(); virtual ~SingleLineProperties(); @@ -36,10 +38,10 @@ class SingleLineProperties { unsigned short int phasesCount(); bool isPen() const; void draw(QPainter *, QET::ConductorSegmentType, const QRectF &); - void toXml(QDomElement &) const; - void fromXml(QDomElement &); - void toSettings(QSettings &, const QString & = QString()) const; - void fromSettings(QSettings &, const QString & = QString()); + void toXml(QDomElement &) const override; + void fromXml(QDomElement &) override; + void toSettings(QSettings &, const QString & = QString()) const override; + void fromSettings(QSettings &, const QString & = QString()) override; /// Whether the singleline conductor should display the ground symbol bool hasGround; @@ -62,7 +64,7 @@ class SingleLineProperties { This class represents the functional properties of a particular conductor, i.e. properties other than path and terminals. */ -class ConductorProperties +class ConductorProperties: public PropertiesInterface { public: ConductorProperties(); @@ -109,10 +111,10 @@ class ConductorProperties SingleLineProperties singleLineProperties; // methods - void toXml(QDomElement &) const; - void fromXml(QDomElement &); - void toSettings(QSettings &, const QString & = QString()) const; - void fromSettings(QSettings &, const QString & = QString()); + void toXml(QDomDocument &doc) const override; + void fromXml(QDomElement &) override; + void toSettings(QSettings &, const QString & = QString()) const override; + void fromSettings(QSettings &, const QString & = QString()) override; static QString typeToString(ConductorType); void applyForEqualAttributes(QList list); diff --git a/sources/editor/graphicspart/partarc.cpp b/sources/editor/graphicspart/partarc.cpp index c9e526087..b5ac5cbe7 100644 --- a/sources/editor/graphicspart/partarc.cpp +++ b/sources/editor/graphicspart/partarc.cpp @@ -95,7 +95,7 @@ void PartArc::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, * @param xml_document : Xml document to use for create the xml element. * @return : an xml element that describe this arc */ -const QDomElement PartArc::toXml(QDomDocument &xml_document) const { +QDomElement PartArc::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement("arc"); QPointF top_left(sceneTopLeft()); @@ -118,23 +118,26 @@ const QDomElement PartArc::toXml(QDomDocument &xml_document) const { * Import the properties of this arc from a xml element. * @param qde : Xml document to use. */ -void PartArc::fromXml(const QDomElement &qde) { +bool PartArc::fromXml(const QDomElement &qde) { stylesFromXml(qde); - double x = 0, y = 0, w = 0, h = 0; // default values - propertyDouble(qde, "x", &x); - propertyDouble(qde, "y", &y); - propertyDouble(qde, "width", &w); - propertyDouble(qde, "height", &h); + double x, y, w, h; + if (propertyDouble(qde, "x", &x, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "y", &y, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "width", &w, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "height", &h, 0) == PropertyFlags::NoValidConversion) + return false; m_rect = QRectF(mapFromScene(x, y), QSizeF(w, h) ); m_start_angle = 0; - propertyDouble(qde, "start", &m_start_angle); + if (propertyDouble(qde, "start", &m_start_angle) == PropertyFlags::NoValidConversion) + return false; m_start_angle *= 16; m_span_angle = -1440; - propertyDouble(qde, "angle", &m_span_angle); + if (propertyDouble(qde, "angle", &m_span_angle) == PropertyFlags::NoValidConversion) + return false; m_span_angle *= 16; } diff --git a/sources/editor/graphicspart/partarc.h b/sources/editor/graphicspart/partarc.h index b0c8a8816..736a8c9de 100644 --- a/sources/editor/graphicspart/partarc.h +++ b/sources/editor/graphicspart/partarc.h @@ -51,8 +51,8 @@ class PartArc : public AbstractPartEllipse //Name and XML QString name() const override { return(QObject::tr("arc", "element part name")); } QString xmlName() const override { return(QString("arc")); } - const QDomElement toXml (QDomDocument &) const override; - void fromXml (const QDomElement &) override; + QDomElement toXml (QDomDocument &) const override; + bool fromXml (const QDomElement &) override; QPainterPath shape() const override; QPainterPath shadowShape() const override; diff --git a/sources/editor/graphicspart/partdynamictextfield.cpp b/sources/editor/graphicspart/partdynamictextfield.cpp index a9f368ca9..2d09dcb5f 100644 --- a/sources/editor/graphicspart/partdynamictextfield.cpp +++ b/sources/editor/graphicspart/partdynamictextfield.cpp @@ -85,37 +85,38 @@ void PartDynamicTextField::handleUserTransformation(const QRectF &initial_select * @param document * @return */ -const QDomElement PartDynamicTextField::toXml(QDomDocument &dom_doc) const +QDomElement PartDynamicTextField::toXml(QDomDocument &dom_doc) const { QDomElement root_element = dom_doc.createElement(xmlName()); - - root_element.setAttribute("x", QString::number(pos().x())); - root_element.setAttribute("y", QString::number(pos().y())); - root_element.setAttribute("z", QString::number(zValue())); - root_element.setAttribute("rotation", QString::number(QET::correctAngle(rotation()))); - root_element.setAttribute("font", font().toString()); - root_element.setAttribute("uuid", m_uuid.toString()); - root_element.setAttribute("frame", m_frame? "true" : "false"); - root_element.setAttribute("text_width", QString::number(m_text_width)); + + root_element.appendChild(createXmlProperty(dom_doc, "x", pos().x())); + root_element.appendChild(createXmlProperty(dom_doc, "y", pos().y())); + root_element.appendChild(createXmlProperty(dom_doc, "z", zValue())); + root_element.appendChild(createXmlProperty(dom_doc, "rotation", QET::correctAngle(rotation()))); + + root_element.appendChild(createXmlProperty(dom_doc, "font", font().toString())); + root_element.appendChild(createXmlProperty(dom_doc, "uuid", m_uuid)); + root_element.appendChild(createXmlProperty(dom_doc, "frame", m_frame)); + root_element.appendChild(createXmlProperty(dom_doc, "text_width", m_text_width)); QMetaEnum me = DynamicElementTextItem::textFromMetaEnum(); - root_element.setAttribute("text_from", me.valueToKey(m_text_from)); + root_element.appendChild(createXmlProperty(dom_doc, "text_from", me.valueToKey(m_text_from))); me = QMetaEnum::fromType(); if(this->alignment() &Qt::AlignRight) - root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignRight)); + root_element.appendChild(createXmlProperty(dom_doc, "Halignment", me.valueToKey(Qt::AlignRight))); else if(this->alignment() &Qt::AlignLeft) - root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignLeft)); + root_element.appendChild(createXmlProperty(dom_doc, "Halignment", me.valueToKey(Qt::AlignLeft))); else if(this->alignment() &Qt::AlignHCenter) - root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignHCenter)); + root_element.appendChild(createXmlProperty(dom_doc, "Halignment", me.valueToKey(Qt::AlignHCenter))); if(this->alignment() &Qt::AlignBottom) - root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignBottom)); + root_element.appendChild(createXmlProperty(dom_doc, "Valignment", me.valueToKey(Qt::AlignBottom))); else if(this->alignment() & Qt::AlignTop) - root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignTop)); + root_element.appendChild(createXmlProperty(dom_doc, "Valignment", me.valueToKey(Qt::AlignTop))); else if(this->alignment() &Qt::AlignVCenter) - root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignVCenter)); + root_element.appendChild(createXmlProperty(dom_doc, "Valignment", me.valueToKey(Qt::AlignVCenter))); QDomElement dom_text = dom_doc.createElement("text"); dom_text.appendChild(dom_doc.createTextNode(toPlainText())); @@ -152,27 +153,36 @@ const QDomElement PartDynamicTextField::toXml(QDomDocument &dom_doc) const * @brief PartDynamicTextField::fromXml * @param element */ -void PartDynamicTextField::fromXml(const QDomElement &dom_elmt) +bool PartDynamicTextField::fromXml(const QDomElement &dom_elmt) { if (dom_elmt.tagName() != xmlName()) { qDebug() << "PartDynamicTextField::fromXml : Wrong tagg name"; - return; + return false; } - - QGraphicsTextItem::setPos(dom_elmt.attribute("x", QString::number(0)).toDouble(), - dom_elmt.attribute("y", QString::number(0)).toDouble()); - setZValue(dom_elmt.attribute("z", QString::number(zValue())).toDouble()); - QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble()); - if (dom_elmt.hasAttribute("font")) + double x, y, z, rot; + + if (propertyDouble(dom_elmt, "x", &x, 0) == PropertyFlags::NoValidConversion || + propertyDouble(dom_elmt, "y", &y, 0) == PropertyFlags::NoValidConversion || + propertyDouble(dom_elmt, "z", &z, 0) == PropertyFlags::NoValidConversion || + propertyDouble(dom_elmt, "rotation", &rot, 0) == PropertyFlags::NoValidConversion) + return false; + + QGraphicsTextItem::setPos(x, y); + setZValue(z); + QGraphicsTextItem::setRotation(rot); + + QString font; + if (propertyString(dom_elmt, "font", &font) == PropertyFlags::Success) { QFont font_; - font_.fromString(dom_elmt.attribute("font")); + font_.fromString(font); setFont(font_); } else { //Keep compatibility TODO remove in futur setFont(QETApp::dynamicTextsItemFont(9)); } + propertyUuid(dom_elmt, "uuid", &m_uuid, QUuid::createUuid()); m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString())); setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false); setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble()); @@ -181,10 +191,11 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt) m_text_from = DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data())); me = QMetaEnum::fromType(); - if(dom_elmt.hasAttribute("Halignment")) - setAlignment(Qt::Alignment(me.keyToValue(dom_elmt.attribute("Halignment").toStdString().data()))); - if(dom_elmt.hasAttribute(("Valignment"))) - setAlignment(Qt::Alignment(me.keyToValue(dom_elmt.attribute("Valignment").toStdString().data())) | this->alignment()); + QString alignment; + if(propertyString(dom_elmt, "Halignment", &alignment) != PropertyFlags::NotFound) + setAlignment(Qt::Alignment(me.keyToValue(alignment.toStdString().data()))); + if(propertyString(dom_elmt, "Valignment", &alignment) != PropertyFlags::NotFound) + setAlignment(Qt::Alignment(me.keyToValue(alignment.toStdString().data())) | this->alignment()); //Text QDomElement dom_text = dom_elmt.firstChildElement("text"); diff --git a/sources/editor/graphicspart/partdynamictextfield.h b/sources/editor/graphicspart/partdynamictextfield.h index e190afad8..aadfb5044 100644 --- a/sources/editor/graphicspart/partdynamictextfield.h +++ b/sources/editor/graphicspart/partdynamictextfield.h @@ -74,8 +74,8 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart void startUserTransformation(const QRectF &initial_selection_rect) override; void handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) override; - const QDomElement toXml(QDomDocument &dom_doc) const override; - void fromXml(const QDomElement &dom_elmt) override; + QDomElement toXml(QDomDocument &dom_doc) const override; + bool fromXml(const QDomElement &dom_elmt) override; void fromTextFieldXml(const QDomElement &dom_element); DynamicElementTextItem::TextFrom textFrom() const; diff --git a/sources/editor/graphicspart/partellipse.cpp b/sources/editor/graphicspart/partellipse.cpp index 226719010..50079eecb 100644 --- a/sources/editor/graphicspart/partellipse.cpp +++ b/sources/editor/graphicspart/partellipse.cpp @@ -76,7 +76,7 @@ void PartEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio * @param xml_document : Xml document to use for create the xml element. * @return : an xml element that describe this ellipse */ -const QDomElement PartEllipse::toXml(QDomDocument &xml_document) const +QDomElement PartEllipse::toXml(QDomDocument &xml_document) const { QDomElement xml_element; if (qFuzzyCompare(rect().width(), rect().height())) @@ -105,26 +105,31 @@ const QDomElement PartEllipse::toXml(QDomDocument &xml_document) const * Import the properties of this ellipse from a xml element. * @param qde : Xml document to use. */ -void PartEllipse::fromXml(const QDomElement &qde) +bool PartEllipse::fromXml(const QDomElement &qde) { stylesFromXml(qde); - double x = 0, y = 0, width = 0, height = 0; + double x, y, width, height; if (qde.tagName() == "ellipse") { - propertyDouble(qde, "width", &width); - propertyDouble(qde, "height", &height); + if (propertyDouble(qde, "width", &width, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "height", &height, 0) == PropertyFlags::NoValidConversion) + return false; } else { - propertyDouble(qde, "diameter", &width); + if (propertyDouble(qde, "diameter", &width, 0) == PropertyFlags::NoValidConversion) + return false; height = width; } - propertyDouble(qde, "x", &x); - propertyDouble(qde, "y", &y); + if (propertyDouble(qde, "x", &x, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "y", &y, 0) == PropertyFlags::NoValidConversion) + return false; m_rect = QRectF(mapFromScene(x, y), QSizeF(width, height)); + + return true; } /** diff --git a/sources/editor/graphicspart/partellipse.h b/sources/editor/graphicspart/partellipse.h index b7f42dcf7..af2e822be 100644 --- a/sources/editor/graphicspart/partellipse.h +++ b/sources/editor/graphicspart/partellipse.h @@ -52,8 +52,8 @@ class PartEllipse : public AbstractPartEllipse //Name and XML QString name() const override { return(QObject::tr("ellipse", "element part name")); } QString xmlName() const override { return(QString("ellipse")); } - const QDomElement toXml (QDomDocument &) const override; - void fromXml (const QDomElement &) override; + QDomElement toXml (QDomDocument &) const override; + bool fromXml (const QDomElement &) override; QPainterPath shape() const override; QPainterPath shadowShape() const override; void setRect(const QRectF &rect) override {AbstractPartEllipse::setRect(rect); adjusteHandlerPos();} diff --git a/sources/editor/graphicspart/partline.cpp b/sources/editor/graphicspart/partline.cpp index 57d75900d..228b0b2c2 100644 --- a/sources/editor/graphicspart/partline.cpp +++ b/sources/editor/graphicspart/partline.cpp @@ -102,7 +102,7 @@ void PartLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, * @param xml_document : Xml document to use for create the xml element. * @return an xml element that describe this line */ -const QDomElement PartLine::toXml(QDomDocument &xml_document) const +QDomElement PartLine::toXml(QDomDocument &xml_document) const { QPointF p1(sceneP1()); QPointF p2(sceneP2()); @@ -128,30 +128,37 @@ const QDomElement PartLine::toXml(QDomDocument &xml_document) const * Import the properties of this line from a xml element. * @param qde : Xml document to use */ -void PartLine::fromXml(const QDomElement &qde) { +bool PartLine::fromXml(const QDomElement &qde) { stylesFromXml(qde); double x1 = 0, y1 = 0, x2 = 0, y2 = 0; - propertyDouble(qde, "x1", &x1); - propertyDouble(qde, "y1", &y1); - propertyDouble(qde, "x2", &x2); - propertyDouble(qde, "y2", &y2); + if (propertyDouble(qde, "x1", &x1) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "y1", &y1) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "x2", &x2) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "y2", &y2) == PropertyFlags::NoValidConversion) + return false; m_line = QLineF(mapFromScene(x1, y1), mapFromScene(x2, y2)); QString s; - propertyString(qde, "end1", &s); + if (propertyString(qde, "end1", &s) != PropertyFlags::Success) + return false; first_end = Qet::endTypeFromString(s); - propertyString(qde, "end2", &s); + if (propertyString(qde, "end2", &s) != PropertyFlags::Success) + return false; + first_end = Qet::endTypeFromString(s); first_length = 1.5; second_length = 1.5; - propertyDouble(qde, "length1", &first_length); - propertyDouble(qde, "length2", &second_length); + if (propertyDouble(qde, "length1", &first_length) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "length2", &second_length) == PropertyFlags::NoValidConversion) + return false; + + return true; } /** diff --git a/sources/editor/graphicspart/partline.h b/sources/editor/graphicspart/partline.h index 9c2d858d7..96f0185fa 100644 --- a/sources/editor/graphicspart/partline.h +++ b/sources/editor/graphicspart/partline.h @@ -70,8 +70,8 @@ class PartLine : public CustomElementGraphicPart void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override; QString name() const override { return(QObject::tr("ligne", "element part name")); } QString xmlName() const override { return(QString("line")); } - const QDomElement toXml(QDomDocument &) const override; - void fromXml(const QDomElement &) override; + QDomElement toXml(QDomDocument &) const override; + bool fromXml(const QDomElement &) override; virtual QPointF sceneP1() const; virtual QPointF sceneP2() const; QPainterPath shape() const override; diff --git a/sources/editor/graphicspart/partpolygon.cpp b/sources/editor/graphicspart/partpolygon.cpp index 2e518bf65..a6558f461 100644 --- a/sources/editor/graphicspart/partpolygon.cpp +++ b/sources/editor/graphicspart/partpolygon.cpp @@ -82,15 +82,16 @@ void PartPolygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio * Import the properties of this polygon from a xml element * @param qde : Xml document to use */ -void PartPolygon::fromXml(const QDomElement &qde) +bool PartPolygon::fromXml(const QDomElement &qde) { stylesFromXml(qde); + int error_counter = 0; int i = 1; while(true) { - if (propertyDouble(qde, QString("x%1").arg(i)) && - propertyDouble(qde, QString("y%1").arg(i))) + if (propertyDouble(qde, QString("x%1").arg(i)) == PropertyFlags::Success && + propertyDouble(qde, QString("y%1").arg(i)) == PropertyFlags::Success) i++; else break; @@ -100,13 +101,18 @@ void PartPolygon::fromXml(const QDomElement &qde) double x, y; for (int j = 1 ; j < i ; ++ j) { - propertyDouble(qde, QString("x%1").arg(j), &x); - propertyDouble(qde, QString("y%1").arg(j), &y); + error_counter += propertyDouble(qde, QString("x%1").arg(j), &x); + error_counter += propertyDouble(qde, QString("y%1").arg(j), &y); + if (error_counter) + return false; temp_polygon << QPointF(x, y); } m_polygon = temp_polygon; - propertyBool(qde, "closed", &m_closed); + if (propertyBool(qde, "closed", &m_closed) != PropertyFlags::Success) + return false; + + return true; } /** @@ -115,7 +121,7 @@ void PartPolygon::fromXml(const QDomElement &qde) * @param xml_document : Xml document to use for create the xml element * @return an xml element that describe this polygon */ -const QDomElement PartPolygon::toXml(QDomDocument &xml_document) const +QDomElement PartPolygon::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement("polygon"); int i = 1; diff --git a/sources/editor/graphicspart/partpolygon.h b/sources/editor/graphicspart/partpolygon.h index e10eadff1..d0fbb93ff 100644 --- a/sources/editor/graphicspart/partpolygon.h +++ b/sources/editor/graphicspart/partpolygon.h @@ -61,8 +61,8 @@ class PartPolygon : public CustomElementGraphicPart QString name() const override { return(QObject::tr("polygone", "element part name")); } QString xmlName() const override { return(QString("polygon")); } - void fromXml(const QDomElement &) override; - const QDomElement toXml(QDomDocument &) const override; + bool fromXml(const QDomElement &) override; + QDomElement toXml(QDomDocument &) const override; QPainterPath shape () const override; QPainterPath shadowShape() const override; diff --git a/sources/editor/graphicspart/partrectangle.cpp b/sources/editor/graphicspart/partrectangle.cpp index 96ad18a1f..708d52b3f 100644 --- a/sources/editor/graphicspart/partrectangle.cpp +++ b/sources/editor/graphicspart/partrectangle.cpp @@ -77,7 +77,7 @@ void PartRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt * @param xml_document : Xml document to use for create the xml element. * @return an xml element that describe this ellipse */ -const QDomElement PartRectangle::toXml(QDomDocument &xml_document) const +QDomElement PartRectangle::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement("rect"); QPointF top_left(sceneTopLeft()); @@ -112,18 +112,33 @@ const QDomElement PartRectangle::toXml(QDomDocument &xml_document) const * Import the properties of this rectangle from a xml element. * @param qde : Xml document to use. */ -void PartRectangle::fromXml(const QDomElement &qde) +bool PartRectangle::fromXml(const QDomElement &qde) { stylesFromXml(qde); - setPos(mapFromScene(qde.attribute("x", "0").toDouble(), - qde.attribute("y", "0").toDouble())); - QRectF rect(QPointF(0,0), QSizeF(qde.attribute("width", "0").toDouble(), - qde.attribute("height", "0").toDouble())); + double x, y, w, h, rx, ry; + if (propertyDouble(qde, "x", &x, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "y", &y, 0) == PropertyFlags::NoValidConversion) + return false; + + setPos(mapFromScene(x, y)); + + if (propertyDouble(qde, "width", &w, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "width", &h, 0) == PropertyFlags::NoValidConversion) + return false; + + QRectF rect(QPointF(0,0), QSizeF(w, h)); setRect(rect.normalized()); - setXRadius(qde.attribute("rx", "0").toDouble()); - setYRadius(qde.attribute("ry", "0").toDouble()); + + if (propertyDouble(qde, "rx", &rx, 0) == PropertyFlags::NoValidConversion || + propertyDouble(qde, "ry", &ry, 0) == PropertyFlags::NoValidConversion) + return false; + + setXRadius(rx); + setYRadius(ry); + + return true; } /** diff --git a/sources/editor/graphicspart/partrectangle.h b/sources/editor/graphicspart/partrectangle.h index d6d7a6608..cc42de702 100644 --- a/sources/editor/graphicspart/partrectangle.h +++ b/sources/editor/graphicspart/partrectangle.h @@ -60,8 +60,8 @@ class PartRectangle : public CustomElementGraphicPart QString name () const override { return(QObject::tr("rectangle", "element part name")); } QString xmlName () const override { return(QString("rect")); } - const QDomElement toXml (QDomDocument &) const override; - void fromXml (const QDomElement &) override; + QDomElement toXml (QDomDocument &) const override; + bool fromXml (const QDomElement &) override; QRectF rect() const; void setRect(const QRectF &rect); diff --git a/sources/editor/graphicspart/partterminal.cpp b/sources/editor/graphicspart/partterminal.cpp index 38924646e..92ade5134 100644 --- a/sources/editor/graphicspart/partterminal.cpp +++ b/sources/editor/graphicspart/partterminal.cpp @@ -42,10 +42,14 @@ PartTerminal::~PartTerminal() { Importe les proprietes d'une borne depuis un element XML @param xml_elmt Element XML a lire */ -void PartTerminal::fromXml(const QDomElement &xml_elmt) { - d->fromXml(xml_elmt); +bool PartTerminal::fromXml(const QDomElement &xml_elmt) { + if (!d->fromXml(xml_elmt)) + return false; + setPos(d->m_pos); updateSecondPoint(); + + return true; } /** @@ -53,7 +57,7 @@ void PartTerminal::fromXml(const QDomElement &xml_elmt) { @param xml_document Document XML a utiliser pour creer l'element XML @return un element XML decrivant la borne */ -const QDomElement PartTerminal::toXml(QDomDocument &xml_document) const { +QDomElement PartTerminal::toXml(QDomDocument &xml_document) const { return d->toXml(xml_document); } diff --git a/sources/editor/graphicspart/partterminal.h b/sources/editor/graphicspart/partterminal.h index a66aeb2fc..1c44b82eb 100644 --- a/sources/editor/graphicspart/partterminal.h +++ b/sources/editor/graphicspart/partterminal.h @@ -57,8 +57,8 @@ class PartTerminal : public CustomElementGraphicPart int type() const override { return Type; } QString name() const override { return d->m_name; } QString xmlName() const override { return(QString("terminal")); } - void fromXml(const QDomElement &) override; - const QDomElement toXml(QDomDocument &) const override; + bool fromXml(const QDomElement &) override; + QDomElement toXml(QDomDocument &) const override; void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override; QPainterPath shape() const override; diff --git a/sources/editor/graphicspart/parttext.cpp b/sources/editor/graphicspart/parttext.cpp index e3dbf794a..369355151 100644 --- a/sources/editor/graphicspart/parttext.cpp +++ b/sources/editor/graphicspart/parttext.cpp @@ -55,10 +55,8 @@ PartText::~PartText() { Importe les proprietes d'un texte statique depuis un element XML @param xml_element Element XML a lire */ -void PartText::fromXml(const QDomElement &xml_element) +bool PartText::fromXml(const QDomElement &xml_element) { - bool ok; - int size; QString font; @@ -76,6 +74,8 @@ void PartText::fromXml(const QDomElement &xml_element) QFont font_; font_.fromString(font); setFont(font_); + } else { + return false; } QString color; @@ -88,12 +88,16 @@ void PartText::fromXml(const QDomElement &xml_element) setPlainText(text); double x, y, rot; - propertyDouble(xml_element, "x", &x, 0); - propertyDouble(xml_element, "y", &y, 0); + if (propertyDouble(xml_element, "x", &x, 0) == PropertyFlags::NoValidConversion || + propertyDouble(xml_element, "y", &y, 0) == PropertyFlags::NoValidConversion) + return false; setPos(x, y); - propertyDouble(xml_element, "rotation", &rot, 0); + if (propertyDouble(xml_element, "rotation", &rot, 0) == PropertyFlags::NoValidConversion) + return false; setRotation(rot); + + return true; } /** @@ -101,7 +105,7 @@ void PartText::fromXml(const QDomElement &xml_element) @param xml_document Document XML a utiliser pour creer l'element XML @return un element XML decrivant le texte statique */ -const QDomElement PartText::toXml(QDomDocument &xml_document) const +QDomElement PartText::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement(xmlName()); diff --git a/sources/editor/graphicspart/parttext.h b/sources/editor/graphicspart/parttext.h index 8945f618d..77a670d36 100644 --- a/sources/editor/graphicspart/parttext.h +++ b/sources/editor/graphicspart/parttext.h @@ -59,8 +59,8 @@ class PartText : public QGraphicsTextItem, public CustomElementPart int type() const override { return Type; } QString name() const override { return(QObject::tr("texte", "element part name")); } QString xmlName() const override { return(QString("text")); } - void fromXml(const QDomElement &) override; - const QDomElement toXml(QDomDocument &) const override; + bool fromXml(const QDomElement &) override; + QDomElement toXml(QDomDocument &) const override; void setRotation(qreal angle) {(QGraphicsObject::setRotation(QET::correctAngle(angle)));} bool isUseless() const override; QRectF sceneGeometricRect() const override; diff --git a/sources/properties/propertiesinterface.cpp b/sources/properties/propertiesinterface.cpp index 9ffd197a5..b4bd2bef8 100644 --- a/sources/properties/propertiesinterface.cpp +++ b/sources/properties/propertiesinterface.cpp @@ -25,6 +25,7 @@ namespace { const QString doubleS = "double"; const QString boolS = "bool"; const QString stringS = "string"; + const QString uuidS = "uuid"; } PropertiesInterface::PropertiesInterface() @@ -63,11 +64,19 @@ QDomElement PropertiesInterface::createXmlProperty(QDomDocument& doc, const QStr return p; } +QDomElement PropertiesInterface::createXmlProperty(QDomDocument& doc, const QString& name, const QUuid value) const { + QDomElement p = doc.createElement("property"); + p.setAttribute("name", name); + p.setAttribute("type", uuidS); + p.setAttribute("value", value.toString()); + 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(); + continue; // there might also non property childs if (child.attribute("name") == name) return child; @@ -155,12 +164,12 @@ PropertiesInterface::PropertyFlags PropertiesInterface::propertyDouble(const QDo return PropertyFlags::Success; } -PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean, bool defaulValue) { +PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomElement &e, const QString& attribute_name, bool* boolean, bool defaultValue) { QString attr; if (!attribute(e, attribute_name, integerS, &attr)) { - *boolean = defaulValue; + *boolean = defaultValue; return PropertyFlags::NotFound; } @@ -176,6 +185,20 @@ PropertiesInterface::PropertyFlags PropertiesInterface::propertyBool(const QDomE return PropertyFlags::Success; } +PropertiesInterface::PropertyFlags PropertiesInterface::propertyUuid(const QDomElement &e, const QString& attribute_name, QUuid* uuid, QUuid defaultValue) { + QString attr; + + if (!attribute(e, attribute_name, uuidS, &attr)) { + *uuid = defaultValue; + return PropertyFlags::NotFound; + } + + if (uuid != nullptr) + *uuid = QUuid(attr); + + return PropertyFlags::Success; +} + PropertiesInterface::PropertyFlags PropertiesInterface::propertyString(const QDomElement& e, const QString& attribute_name, QString* string, QString defaultValue) { QString attr; diff --git a/sources/properties/propertiesinterface.h b/sources/properties/propertiesinterface.h index 0c4bc5793..ebd60e533 100644 --- a/sources/properties/propertiesinterface.h +++ b/sources/properties/propertiesinterface.h @@ -23,6 +23,7 @@ #include #include #include "qet.h" +#include /** * @brief The PropertiesInterface class @@ -33,8 +34,8 @@ class PropertiesInterface public: PropertiesInterface(); // Save/load properties to setting file. QString is use for prefix a word befor the name of each paramètre - virtual void toSettings (QSettings &settings, const QString = QString()) const =0; - virtual void fromSettings (const QSettings &settings, const QString = QString()) =0; + virtual void toSettings (QSettings &settings, const QString& = QString()) const =0; + virtual void fromSettings (const QSettings &settings, const QString& = QString()) =0; // Save/load properties to xml element virtual QDomElement toXml (QDomDocument &xml_document) const =0; virtual bool fromXml (const QDomElement &xml_element) =0; @@ -47,12 +48,13 @@ class PropertiesInterface 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; + QDomElement createXmlProperty(QDomDocument& doc, const QString& name, const QUuid 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, + Success = 0, NotFound, NoValidConversion, }; @@ -60,7 +62,8 @@ class PropertiesInterface static PropertyFlags propertyInteger(const QDomElement &e, const QString& attribute_name, int *entier = nullptr, int defaultValue = std::numeric_limits::quiet_NaN()); static PropertyFlags propertyDouble(const QDomElement &e, const QString& attribute_name, double *reel = nullptr, double defaultValue = std::numeric_limits::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 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 bool validXmlProperty(const QDomElement& e); diff --git a/sources/properties/xrefproperties.cpp b/sources/properties/xrefproperties.cpp index 360ad9b5f..1992d920d 100644 --- a/sources/properties/xrefproperties.cpp +++ b/sources/properties/xrefproperties.cpp @@ -41,7 +41,7 @@ XRefProperties::XRefProperties() * @param settings: QSettings to use * @param prefix: prefix before properties name */ -void XRefProperties::toSettings(QSettings &settings, const QString prefix) const { +void XRefProperties::toSettings(QSettings &settings, const QString &prefix) const { settings.setValue(prefix + "showpowerctc", m_show_power_ctc); QString display = m_display == Cross? "cross" : "contacts"; settings.setValue(prefix + "displayhas", display); @@ -69,7 +69,7 @@ void XRefProperties::toSettings(QSettings &settings, const QString prefix) const * @param settings: QSettings to use * @param prefix: prefix before properties name */ -void XRefProperties::fromSettings(const QSettings &settings, const QString prefix) +void XRefProperties::fromSettings(const QSettings &settings, const QString &prefix) { m_show_power_ctc = settings.value(prefix + "showpowerctc", true).toBool(); QString display = settings.value(prefix + "displayhas", "cross").toString(); diff --git a/sources/properties/xrefproperties.h b/sources/properties/xrefproperties.h index e10ee872a..e154827bb 100644 --- a/sources/properties/xrefproperties.h +++ b/sources/properties/xrefproperties.h @@ -40,8 +40,8 @@ class XRefProperties : public PropertiesInterface Label }; - void toSettings (QSettings &settings, const QString = QString()) const override; - void fromSettings (const QSettings &settings, const QString = QString()) override; + void toSettings (QSettings &settings, const QString& = QString()) const override; + 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; diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 1368c63d7..59d4bb5a6 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -1483,19 +1483,13 @@ void QETProject::writeDefaultPropertiesXml(QDomElement &xml_element) QDomDocument xml_document = xml_element.ownerDocument(); // export size of border - QDomElement border_elmt = xml_document.createElement("border"); - default_border_properties_.toXml(border_elmt); - xml_element.appendChild(border_elmt); + xml_element.appendChild(default_border_properties_.toXml(xml_document)); // export content of titleblock - QDomElement titleblock_elmt = xml_document.createElement("inset"); - default_titleblock_properties_.toXml(titleblock_elmt); - xml_element.appendChild(titleblock_elmt); + xml_element.appendChild(default_titleblock_properties_.toXml(xml_document)); // exporte default conductor - QDomElement conductor_elmt = xml_document.createElement("conductors"); - default_conductor_properties_.toXml(conductor_elmt); - xml_element.appendChild(conductor_elmt); + xml_element.appendChild(default_conductor_properties_.toXml(xml_document)); // export default report properties QDomElement report_elmt = xml_document.createElement("report"); diff --git a/sources/titleblockproperties.cpp b/sources/titleblockproperties.cpp index c645c3779..06a9a1e4a 100644 --- a/sources/titleblockproperties.cpp +++ b/sources/titleblockproperties.cpp @@ -73,22 +73,26 @@ bool TitleBlockProperties::operator!=(const TitleBlockProperties &ip) { Exporte le cartouche sous formes d'attributs XML ajoutes a l'element e. @param e Element XML auquel seront ajoutes des attributs */ -void TitleBlockProperties::toXml(QDomElement &e) const { - e.setAttribute("author", author); - e.setAttribute("title", title); - e.setAttribute("filename", filename); - e.setAttribute("plant", plant); - e.setAttribute("locmach", locmach); - e.setAttribute("indexrev",indexrev); - e.setAttribute("version", version); - e.setAttribute("folio", folio); - e.setAttribute("auto_page_num", auto_page_num); - e.setAttribute("date", exportDate()); - e.setAttribute("displayAt", (display_at == Qt::BottomEdge? "bottom" : "right")); +QDomElement TitleBlockProperties::toXml(QDomDocument &xml_document) const { + + QDomElement e = xml_document.createElement("inset"); + + e.appendChild(createXmlProperty(xml_document, "author", author)); + e.appendChild(createXmlProperty(xml_document, "title", title)); + e.appendChild(createXmlProperty(xml_document, "filename", filename)); + e.appendChild(createXmlProperty(xml_document, "plant", plant)); + e.appendChild(createXmlProperty(xml_document, "locmach", locmach)); + e.appendChild(createXmlProperty(xml_document, "indexrev", indexrev)); + e.appendChild(createXmlProperty(xml_document, "version", version)); + e.appendChild(createXmlProperty(xml_document, "folio", folio)); + e.appendChild(createXmlProperty(xml_document, "auto_page_num", auto_page_num)); + e.appendChild(createXmlProperty(xml_document, "date", exportDate())); + e.appendChild(createXmlProperty(xml_document, "displayAt", display_at == Qt::BottomEdge? "bottom" : "right")); + if (!template_name.isEmpty()) { - e.setAttribute("titleblocktemplate", template_name); - e.setAttribute("titleblocktemplateCollection", QET::qetCollectionToString(collection)); + e.appendChild(createXmlProperty(xml_document, "titleblocktemplate", template_name)); + e.appendChild(createXmlProperty(xml_document, "titleblocktemplateCollection", QET::qetCollectionToString(collection))); } if (context.keys().count()) { @@ -96,13 +100,15 @@ void TitleBlockProperties::toXml(QDomElement &e) const { context.toXml(properties); e.appendChild(properties); } + + return e; } /** Importe le cartouche a partir des attributs XML de l'element e @param e Element XML dont les attributs seront lus */ -void TitleBlockProperties::fromXml(const QDomElement &e) { +bool TitleBlockProperties::fromXml(const QDomElement &e) { // reads the historical fields if (e.hasAttribute("author")) author = e.attribute("author"); if (e.hasAttribute("title")) title = e.attribute("title"); diff --git a/sources/titleblockproperties.h b/sources/titleblockproperties.h index 7f9ebeb0e..480a0265f 100644 --- a/sources/titleblockproperties.h +++ b/sources/titleblockproperties.h @@ -21,12 +21,14 @@ #include "diagramcontext.h" #include "qet.h" +#include "propertiesinterface.h" + /** This class provides a container for the properties of a particular title block, i.e. title, author, date, filename, folio, template, custom properties, ... */ -class TitleBlockProperties { +class TitleBlockProperties: public PropertiesInterface { public: TitleBlockProperties(); virtual ~TitleBlockProperties(); @@ -39,8 +41,8 @@ class TitleBlockProperties { bool operator==(const TitleBlockProperties &); bool operator!=(const TitleBlockProperties &); - void toXml(QDomElement &) const; - void fromXml(const QDomElement &); + QDomElement toXml(QDomDocument &xml_document) const override; + bool fromXml(const QDomElement &) override; void toSettings(QSettings &, const QString & = QString()) const; void fromSettings(QSettings &, const QString & = QString());