Element files: anchor="alignment" puts a static text's x at its alignment (#1251)

A static text's x/y in an .elmt file are the baseline-left of the text.
A program writing an element (a pinout generator, dxf2elmt) then has to
measure the text with QET's font metrics to right-align or centre it.

With the opt-in anchor="alignment", x is the left edge, centre or right
edge selected by Halignment; y stays the baseline. The box is the text
without the document margin, as ElementPictureFactory draws it, so the
editor and the folio agree. Rotation pivots on x/y as before.

The element editor reads it and writes it back for the texts that had
it; it does not add it to other texts. Files without anchor draw and
save as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ispyisail
2026-10-09 16:31:16 +13:00
parent bdeef6d306
commit 1db025ceae
6 changed files with 199 additions and 7 deletions
+29 -2
View File
@@ -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();