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
+12 -5
View File
@@ -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<Qt::Alignment>();
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;