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();
+4
View File
@@ -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
+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;