Keep an element editor text field in place when its width changes

In the element editor, changing the width of a right-aligned or centred
dynamic text field (with the width box) moved its aligned edge: the
field always grew or shrank from its left edge, so a right-aligned
field's right edge moved. PartDynamicTextField::setTextWidth() now
keeps the point chosen by the alignment in place, with the
prepareAlignment()/finishAlignment() pair setPlainText() and setFont()
already use. Not while the field is read from the .elmt, whose saved
position is the one to keep.

No file format change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Beat Hangartner
2026-10-05 17:57:32 +02:00
parent 2e4b761602
commit 6a681cde67
@@ -235,7 +235,10 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt) {
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false);
//The saved position is the one to keep
m_block_alignment = true;
setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble());
m_block_alignment = false;
QMetaEnum me = DynamicElementTextItem::textFromMetaEnum();
m_text_from = DynamicElementTextItem::TextFrom(
@@ -431,13 +434,23 @@ bool PartDynamicTextField::frame() const
return m_frame;
}
/**
@brief PartDynamicTextField::setTextWidth
Set the width of this text (-1 = automatic width). The point of the text
chosen by its alignment stays in place, as when the text changes: a
right-aligned or centred field no longer grows to the right only, and
undoing a width change puts it back where it was.
@param width
*/
void PartDynamicTextField::setTextWidth(qreal width) {
prepareAlignment();
this -> document() -> setTextWidth(width);
//Adjust the width, to ideal width if needed
if(width > 0 && document() -> size().width() > width)
document() -> setTextWidth(document() -> idealWidth());
finishAlignment();
m_text_width = document() -> textWidth();
emit textWidthChanged(m_text_width);
}