diff --git a/sources/editor/graphicspart/parttext.cpp b/sources/editor/graphicspart/parttext.cpp index d6a77765b..4fa4d9c4d 100644 --- a/sources/editor/graphicspart/parttext.cpp +++ b/sources/editor/graphicspart/parttext.cpp @@ -164,6 +164,10 @@ void PartText::fromXml(const QDomElement &xml_element) { setPos(xml_element.attribute("x").toDouble(), xml_element.attribute("y").toDouble()); QGraphicsObject::setRotation(QET::correctAngle(xml_element.attribute("rotation", QString::number(0)).toDouble())); + + m_anchor_to_alignment = xml_element.attribute("anchor") == QLatin1String("alignment"); + if (m_anchor_to_alignment) + setPos(pos() - anchorOffset()); } /** @@ -175,8 +179,10 @@ const QDomElement PartText::toXml(QDomDocument &xml_document) const { QDomElement xml_element = xml_document.createElement(xmlName()); - qreal x = (qRound(pos().x() * 100.0) / 100.0); - qreal y = (qRound(pos().y() * 100.0) / 100.0); + const QPointF anchor = m_anchor_to_alignment ? pos() + anchorOffset() + : pos(); + qreal x = (qRound(anchor.x() * 100.0) / 100.0); + qreal y = (qRound(anchor.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)); @@ -197,6 +203,8 @@ const QDomElement PartText::toXml(QDomDocument &xml_document) const xml_element.setAttribute("Valignment", me.valueToKey(Qt::AlignBottom)); else if (m_alignment & Qt::AlignVCenter) xml_element.setAttribute("Valignment", me.valueToKey(Qt::AlignVCenter)); + if (m_anchor_to_alignment) + xml_element.setAttribute("anchor", "alignment"); return(xml_element); } @@ -444,6 +452,25 @@ void PartText::finishAlignment() setPos(pos() - (pa - p)); } +/** + @brief PartText::anchorOffset + @return the vector from the baseline-left of the text to the point + written as x/y with anchor="alignment" (#1251): the left edge, centre or + right edge of the text, without the document margin, on the baseline. + Same width as ElementPictureFactory::parseText, which draws without + a margin. +*/ +QPointF PartText::anchorOffset() const +{ + qreal width = document()->idealWidth() - 2 * document()->documentMargin(); + qreal dx = 0; + if (m_alignment & Qt::AlignRight) + dx = width; + else if (m_alignment & Qt::AlignHCenter) + dx = width / 2; + return QTransform().rotate(rotation()).map(QPointF(dx, 0)); +} + void PartText::setFont(const QFont &font) { if (font != this -> font()) { prepareAlignment(); diff --git a/sources/editor/graphicspart/parttext.h b/sources/editor/graphicspart/parttext.h index 2acea51ed..d127cd98e 100644 --- a/sources/editor/graphicspart/parttext.h +++ b/sources/editor/graphicspart/parttext.h @@ -109,6 +109,7 @@ class PartText : public QGraphicsTextItem, public CustomElementPart { void applyLineAlignment(); void prepareAlignment(); void finishAlignment(); + QPointF anchorOffset() const; QString previous_text; qreal real_font_size_; QPointF saved_point_; @@ -117,5 +118,8 @@ class PartText : public QGraphicsTextItem, public CustomElementPart { QPointF m_origin_pos; Qt::Alignment m_alignment = (Qt::AlignTop | Qt::AlignLeft); QRectF m_alignment_rect; + /// x/y in the .elmt are the aligned point, not the + /// baseline-left (anchor="alignment", #1251) + bool m_anchor_to_alignment = false; }; #endif diff --git a/sources/factory/elementpicturefactory.cpp b/sources/factory/elementpicturefactory.cpp index 5ed9ca9ab..cbab85622 100644 --- a/sources/factory/elementpicturefactory.cpp +++ b/sources/factory/elementpicturefactory.cpp @@ -704,10 +704,11 @@ void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter, //adjusts the offset by the margin of the text document text_document.setDocumentMargin(0.0); - //Optional line alignment of multi-line texts (the anchor behaviour - //of the alignment is handled in the element editor; the saved x/y - //always stay the baseline-left of the text block). The document - //only honors the text option once a text width is set. + //Optional line alignment of multi-line texts. The document only + //honors the text option once a text width is set. + //x/y are the baseline-left of the text block, unless + //anchor="alignment": x is then the left edge, centre or right edge + //selected by Halignment, and y stays the baseline (#1251). if (dom.hasAttribute("Halignment")) { const QMetaEnum me = QMetaEnum::fromType(); const Qt::Alignment h_alignment = Qt::Alignment( @@ -717,12 +718,18 @@ void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter, option.setAlignment(h_alignment & Qt::AlignHorizontal_Mask); text_document.setDefaultTextOption(option); text_document.setTextWidth(text_document.idealWidth()); + if (dom.attribute("anchor") == QLatin1String("alignment")) + qpainter_offset.rx() -= h_alignment & Qt::AlignRight + ? text_document.idealWidth() + : text_document.idealWidth() / 2; } } QTransform text_transform; text_transform.translate(dom.attribute("x").toDouble(), dom.attribute("y").toDouble()); text_transform.rotate(dom.attribute("rotation", "0").toDouble()); + //baseline-left of the text block, for the dxf export below + const QPointF baseline_left = text_transform.map(QPointF(qpainter_offset.x(), 0)); text_transform.translate(qpainter_offset.x(), qpainter_offset.y()); if (!m_build_texts_undo.isIdentity()) @@ -748,7 +755,7 @@ void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter, QGraphicsSimpleTextItem *qgsti = new QGraphicsSimpleTextItem(); qgsti->setText(dom.attribute("text")); qgsti->setFont(font_); - qgsti->setPos(dom.attribute("x").toDouble(), dom.attribute("y").toDouble()); + qgsti->setPos(baseline_left); qgsti->setRotation(dom.attribute("rotation", "0").toDouble()); prim.m_texts << qgsti; diff --git a/tests/qttest/CMakeLists.txt b/tests/qttest/CMakeLists.txt index b33b94130..29c339e6f 100644 --- a/tests/qttest/CMakeLists.txt +++ b/tests/qttest/CMakeLists.txt @@ -619,6 +619,17 @@ target_link_libraries(tst_dxfcurves PRIVATE Qt::Test) target_compile_definitions(tst_dxfcurves PRIVATE "QET_TEST_BINARY_PATH=\"$\"") +# A static text with anchor="alignment" has its x at the edge or centre its +# Halignment selects, not at its left edge (#1251). +add_executable( + tst_statictextanchor + tst_statictextanchor.cpp) +add_test(NAME tst_statictextanchor COMMAND tst_statictextanchor) +add_dependencies(tst_statictextanchor qelectrotech) +target_link_libraries(tst_statictextanchor PRIVATE Qt::Test) +target_compile_definitions(tst_statictextanchor PRIVATE + "QET_TEST_BINARY_PATH=\"$\"") + # --export-dxf --dxf-blocks: a symbol placed twice is one BLOCK and two # INSERTs (issue #1339). add_executable( diff --git a/tests/qttest/fixtures/static_text_anchor.qet b/tests/qttest/fixtures/static_text_anchor.qet new file mode 100644 index 000000000..7863d5049 --- /dev/null +++ b/tests/qttest/fixtures/static_text_anchor.qet @@ -0,0 +1,59 @@ + + + 10/2/26 + 02-10-2026 + 2026-10-02 + static_text_anchor + 18:39 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Imported elements + + + + + + Anchored static texts + + + + + + + + + + + + + + diff --git a/tests/qttest/tst_statictextanchor.cpp b/tests/qttest/tst_statictextanchor.cpp new file mode 100644 index 000000000..ddb996c2e --- /dev/null +++ b/tests/qttest/tst_statictextanchor.cpp @@ -0,0 +1,84 @@ +/* + Copyright 2006-2026 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include +#include +#include + +/** + A static text with anchor="alignment" has its x at the edge or centre + its Halignment selects, instead of its left edge (#1251). The symbol in + fixtures/static_text_anchor.qet has the same text five times, all at + x = 0: left, right and centre anchored, right aligned without anchor, + and anchored without Halignment. --export-dxf writes where each is drawn. +*/ +class tst_statictextanchor : public QObject +{ + Q_OBJECT + +private slots: + void anchoredX() + { + const QString project = QFINDTESTDATA("fixtures/static_text_anchor.qet"); + QVERIFY2(!project.isEmpty(), "fixture project not found"); + QTemporaryDir dir; + QVERIFY(dir.isValid()); + + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + env.insert(QStringLiteral("QT_QPA_PLATFORM"), QStringLiteral("offscreen")); + QProcess proc; + proc.setProcessEnvironment(env); + proc.start(QStringLiteral(QET_TEST_BINARY_PATH), {QStringLiteral("--export-dxf"), project, dir.path()}); + QVERIFY2(proc.waitForFinished(60000), "--export-dxf timed out"); + QCOMPARE(proc.exitCode(), 0); + + QFile file(dir.filePath(QStringLiteral("01_diagram.dxf"))); + QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); + const QStringList values = QString::fromUtf8(file.readAll()).split(QLatin1Char('\n')); + + //x (group code 10) of each TEXT entity of the symbol, in order + QList xs; + for (int i = 0 ; i + 1 < values.size() ; i += 2) { + if (values.at(i).trimmed() != QLatin1String("0") || values.at(i + 1).trimmed() != QLatin1String("TEXT")) + continue; + qreal x = 0; + QString text; + for (int j = i + 2 ; j + 1 < values.size() && values.at(j).trimmed() != QLatin1String("0") ; j += 2) { + if (values.at(j).trimmed() == QLatin1String("10")) + x = values.at(j + 1).trimmed().toDouble(); + else if (values.at(j).trimmed() == QLatin1String("1")) + text = values.at(j + 1).trimmed(); + } + if (text == QLatin1String("GPIO21 PCM_DOUT")) + xs << x; + } + QCOMPARE(xs.size(), 5); + + const qreal left = xs.at(0), right = xs.at(1), centre = xs.at(2); + //anchored on the right edge: drawn one text width to the left + QVERIFY2(left - right > 20, qPrintable(QStringLiteral("left %1 right %2").arg(left).arg(right))); + //anchored on the centre: half as far + QVERIFY(qAbs(2 * (left - centre) - (left - right)) < 0.01); + //without anchor, x stays the left edge whatever the alignment + QCOMPARE(xs.at(3), left); + //anchor without Halignment is the left edge too + QCOMPARE(xs.at(4), left); + } +}; + +QTEST_APPLESS_MAIN(tst_statictextanchor) +#include "tst_statictextanchor.moc"