From 1fc33aa1bac5222177bdf49ad84adc7cb589e498 Mon Sep 17 00:00:00 2001 From: plc-user <74435298+plc-user@users.noreply.github.com> Date: Sun, 9 Feb 2025 12:33:24 +0100 Subject: [PATCH] element-editor: limit decimal places for sizes and positions to two, when saving file --- sources/editor/graphicspart/partarc.cpp | 20 +++++++++++++------ .../graphicspart/partdynamictextfield.cpp | 10 +++++++--- sources/editor/graphicspart/partellipse.cpp | 15 +++++++++----- sources/editor/graphicspart/partline.cpp | 11 ++++++++-- sources/editor/graphicspart/partpolygon.cpp | 6 ++++-- sources/editor/graphicspart/partrectangle.cpp | 18 +++++++++++------ sources/editor/graphicspart/parttext.cpp | 9 ++++++--- sources/properties/terminaldata.cpp | 7 +++++-- 8 files changed, 67 insertions(+), 29 deletions(-) diff --git a/sources/editor/graphicspart/partarc.cpp b/sources/editor/graphicspart/partarc.cpp index e1404116f..cb697d42c 100644 --- a/sources/editor/graphicspart/partarc.cpp +++ b/sources/editor/graphicspart/partarc.cpp @@ -106,13 +106,21 @@ const QDomElement PartArc::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement("arc"); QPointF top_left(sceneTopLeft()); - xml_element.setAttribute("x", QString("%1").arg(top_left.x())); - xml_element.setAttribute("y", QString("%1").arg(top_left.y())); - xml_element.setAttribute("width", QString("%1").arg(rect().width())); - xml_element.setAttribute("height", QString("%1").arg(rect().height())); + + qreal x = qRound(top_left.x() * 100.0) / 100.0; + qreal y = qRound(top_left.y() * 100.0) / 100.0; + qreal w = qRound(rect().width() * 100.0) / 100.0; + qreal h = qRound(rect().height() * 100.0) / 100.0; + qreal s = qRound(m_start_angle * 100.0) / 100.0; + qreal a = qRound(m_span_angle * 100.0) / 100.0; + + xml_element.setAttribute("x", QString("%1").arg(x)); + xml_element.setAttribute("y", QString("%1").arg(y)); + xml_element.setAttribute("width", QString("%1").arg(w)); + xml_element.setAttribute("height", QString("%1").arg(h)); //to maintain compatibility with the previous version, we write the angle in degrees. - xml_element.setAttribute("start", QString("%1").arg(m_start_angle / 16)); - xml_element.setAttribute("angle", QString("%1").arg(m_span_angle / 16)); + xml_element.setAttribute("start", QString("%1").arg(s / 16)); + xml_element.setAttribute("angle", QString("%1").arg(a / 16)); stylesToXml(xml_element); return(xml_element); } diff --git a/sources/editor/graphicspart/partdynamictextfield.cpp b/sources/editor/graphicspart/partdynamictextfield.cpp index 0c5d95679..ed426cc2e 100644 --- a/sources/editor/graphicspart/partdynamictextfield.cpp +++ b/sources/editor/graphicspart/partdynamictextfield.cpp @@ -97,10 +97,14 @@ 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())); + qreal x = (qRound(pos().x() * 100.0) / 100.0); + qreal y = (qRound(pos().y() * 100.0) / 100.0); + qreal rot = (qRound(rotation() * 10.0) / 10.0); + + root_element.setAttribute("x", QString::number(x)); + root_element.setAttribute("y", QString::number(y)); root_element.setAttribute("z", QString::number(zValue())); - root_element.setAttribute("rotation", QString::number(QET::correctAngle(rotation()))); + root_element.setAttribute("rotation", QString::number(QET::correctAngle(rot))); root_element.setAttribute("font", font().toString()); root_element.setAttribute("uuid", m_uuid.toString()); root_element.setAttribute("frame", m_frame? "true" : "false"); diff --git a/sources/editor/graphicspart/partellipse.cpp b/sources/editor/graphicspart/partellipse.cpp index 0a94c52f7..9c2ed802b 100644 --- a/sources/editor/graphicspart/partellipse.cpp +++ b/sources/editor/graphicspart/partellipse.cpp @@ -89,19 +89,24 @@ const QDomElement PartEllipse::toXml(QDomDocument &xml_document) const QDomElement xml_element; if (qFuzzyCompare(rect().width(), rect().height())) { + double w = qRound(rect().width() * 100.0) / 100.0; xml_element = xml_document.createElement("circle"); - xml_element.setAttribute("diameter", QString("%1").arg(rect().width())); + xml_element.setAttribute("diameter", QString("%1").arg(w)); } else { + double w = qRound(rect().width() * 100.0) / 100.0; + double h = qRound(rect().height() * 100.0) / 100.0; xml_element = xml_document.createElement("ellipse"); - xml_element.setAttribute("width", QString("%1").arg(rect().width())); - xml_element.setAttribute("height", QString("%1").arg(rect().height())); + xml_element.setAttribute("width", QString("%1").arg(w)); + xml_element.setAttribute("height", QString("%1").arg(h)); } QPointF top_left(sceneTopLeft()); - xml_element.setAttribute("x", QString("%1").arg(top_left.x())); - xml_element.setAttribute("y", QString("%1").arg(top_left.y())); + double x = qRound(top_left.x() * 100.0) / 100.0; + double y = qRound(top_left.y() * 100.0) / 100.0; + xml_element.setAttribute("x", QString("%1").arg(x)); + xml_element.setAttribute("y", QString("%1").arg(y)); stylesToXml(xml_element); diff --git a/sources/editor/graphicspart/partline.cpp b/sources/editor/graphicspart/partline.cpp index 506a43c1f..0ad0e1e91 100644 --- a/sources/editor/graphicspart/partline.cpp +++ b/sources/editor/graphicspart/partline.cpp @@ -115,15 +115,22 @@ const QDomElement PartLine::toXml(QDomDocument &xml_document) const QPointF p1(sceneP1()); QPointF p2(sceneP2()); + p1.setX((qRound(p1.x() * 100.0)) / 100.0); + p1.setY((qRound(p1.y() * 100.0)) / 100.0); + p2.setX((qRound(p2.x() * 100.0)) / 100.0); + p2.setY((qRound(p2.y() * 100.0)) / 100.0); + qreal firstLength = ((qRound(first_length * 100.0)) / 100.0); + qreal secondLength = ((qRound(second_length * 100.0)) / 100.0); + QDomElement xml_element = xml_document.createElement("line"); xml_element.setAttribute("x1", QString("%1").arg(p1.x())); xml_element.setAttribute("y1", QString("%1").arg(p1.y())); xml_element.setAttribute("x2", QString("%1").arg(p2.x())); xml_element.setAttribute("y2", QString("%1").arg(p2.y())); xml_element.setAttribute("end1", Qet::endTypeToString(first_end)); - xml_element.setAttribute("length1", QString("%1").arg(first_length)); + xml_element.setAttribute("length1", QString("%1").arg(firstLength)); xml_element.setAttribute("end2", Qet::endTypeToString(second_end)); - xml_element.setAttribute("length2", QString("%1").arg(second_length)); + xml_element.setAttribute("length2", QString("%1").arg(secondLength)); stylesToXml(xml_element); return(xml_element); diff --git a/sources/editor/graphicspart/partpolygon.cpp b/sources/editor/graphicspart/partpolygon.cpp index 9a8f83c36..08813a359 100644 --- a/sources/editor/graphicspart/partpolygon.cpp +++ b/sources/editor/graphicspart/partpolygon.cpp @@ -126,8 +126,10 @@ const QDomElement PartPolygon::toXml(QDomDocument &xml_document) const int i = 1; foreach(QPointF point, m_polygon) { point = mapToScene(point); - xml_element.setAttribute(QString("x%1").arg(i), QString("%1").arg(point.x())); - xml_element.setAttribute(QString("y%1").arg(i), QString("%1").arg(point.y())); + qreal x = ((qRound(point.x() * 100.0)) / 100.0); + qreal y = ((qRound(point.y() * 100.0)) / 100.0); + xml_element.setAttribute(QString("x%1").arg(i), QString("%1").arg(x)); + xml_element.setAttribute(QString("y%1").arg(i), QString("%1").arg(y)); ++ i; } if (!m_closed) xml_element.setAttribute("closed", "false"); diff --git a/sources/editor/graphicspart/partrectangle.cpp b/sources/editor/graphicspart/partrectangle.cpp index 083c5f667..6b5e39391 100644 --- a/sources/editor/graphicspart/partrectangle.cpp +++ b/sources/editor/graphicspart/partrectangle.cpp @@ -91,13 +91,19 @@ const QDomElement PartRectangle::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement("rect"); QPointF top_left(sceneTopLeft()); - xml_element.setAttribute("x", QString("%1").arg(top_left.x())); - xml_element.setAttribute("y", QString("%1").arg(top_left.y())); - xml_element.setAttribute("width", QString("%1").arg(m_rect.width())); - xml_element.setAttribute("height", QString("%1").arg(m_rect.height())); + qreal x = (qRound(top_left.x() * 100.0) / 100.0); + qreal y = (qRound(top_left.y() * 100.0) / 100.0); + qreal w = (qRound(m_rect.width() * 100.0) / 100.0); + qreal h = (qRound(m_rect.height() * 100.0) / 100.0); + qreal rx = (qRound(m_xRadius * 100.0) / 100.0); + qreal ry = (qRound(m_yRadius * 100.0) / 100.0); - xml_element.setAttribute("rx", QString::number(m_xRadius)); - xml_element.setAttribute("ry", QString::number(m_yRadius)); + xml_element.setAttribute("x", QString::number(x)); + xml_element.setAttribute("y", QString::number(y)); + xml_element.setAttribute("width", QString::number(w)); + xml_element.setAttribute("height", QString::number(h)); + xml_element.setAttribute("rx", QString::number(rx)); + xml_element.setAttribute("ry", QString::number(ry)); stylesToXml(xml_element); return(xml_element); diff --git a/sources/editor/graphicspart/parttext.cpp b/sources/editor/graphicspart/parttext.cpp index 596864d1b..d8469f7b2 100644 --- a/sources/editor/graphicspart/parttext.cpp +++ b/sources/editor/graphicspart/parttext.cpp @@ -101,11 +101,14 @@ const QDomElement PartText::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement(xmlName()); - xml_element.setAttribute("x", QString::number(pos().x())); - xml_element.setAttribute("y", QString::number(pos().y())); + qreal x = (qRound(pos().x() * 100.0) / 100.0); + qreal y = (qRound(pos().y() * 100.0) / 100.0); + qreal rot = (qRound(rotation() * 10.0) / 10.0); + xml_element.setAttribute("x", QString::number(x)); + xml_element.setAttribute("y", QString::number(y)); xml_element.setAttribute("text", toPlainText()); xml_element.setAttribute("font", font().toString()); - xml_element.setAttribute("rotation", QString::number(rotation())); + xml_element.setAttribute("rotation", QString::number(rot)); xml_element.setAttribute("color", defaultTextColor().name()); return(xml_element); diff --git a/sources/properties/terminaldata.cpp b/sources/properties/terminaldata.cpp index 62e862381..a06be5326 100644 --- a/sources/properties/terminaldata.cpp +++ b/sources/properties/terminaldata.cpp @@ -96,8 +96,11 @@ QDomElement TerminalData::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement("terminal"); - xml_element.setAttribute("x", QString("%1").arg(q->scenePos().x())); - xml_element.setAttribute("y", QString("%1").arg(q->scenePos().y())); + qreal x = (qRound(q->scenePos().x() * 100.0) / 100.0); + qreal y = (qRound(q->scenePos().y() * 100.0) / 100.0); + + xml_element.setAttribute("x", QString("%1").arg(x)); + xml_element.setAttribute("y", QString("%1").arg(y)); xml_element.setAttribute("uuid", m_uuid.toString()); xml_element.setAttribute("name", m_name);