Element editor: optional alignment for static texts (#549)

Static texts (PartText) gain an optional alignment, exposed via the
existing AlignmentTextDialog behind a new "Alignement" button in the
static text editor:

- The horizontal part aligns the lines of a multi-line text relative
  to each other (centered block labels no longer need one hand-placed
  text per line).
- The full alignment defines the anchor: when the content or font
  changes later, the selected corner/center of the bounding rect keeps
  its place instead of always growing right/down from the top-left
  (same prepareAlignment/finishAlignment logic as DiagramTextItem).

Format: the <text> node takes the same optional Halignment/Valignment
attributes as dynamic_text, written only when they differ from the
historical top-left behaviour - existing .elmt files are untouched and
round-trip byte-identical. The saved x/y stay the baseline-left of the
text block in all cases; ElementPictureFactory only needs the line
alignment (the anchor is editor-side behaviour), so rendered elements
match the editor exactly.

German translations for the three new strings included (qet_de stays
complete, 2686/2686).

Verified headless: a project embedding a two-line text once with
Halignment=AlignHCenter and once without exports to SVG with the short
line centered under the long one (x 102.5 vs 126.5) in the aligned
block, and identical x for both lines in the legacy block. Editor-side
anchor behaviour follows the proven DiagramTextItem implementation but
was not manually exercised in the GUI yet.
This commit is contained in:
Dieter Mayer
2026-07-27 08:27:13 +02:00
parent 66129fd15c
commit 787335582b
4 changed files with 184 additions and 2 deletions
+16
View File
@@ -534,6 +534,22 @@ 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.
if (dom.hasAttribute("Halignment")) {
const QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
const Qt::Alignment h_alignment = Qt::Alignment(
me.keyToValue(dom.attribute("Halignment").toStdString().data()));
if (h_alignment & (Qt::AlignHCenter | Qt::AlignRight)) {
QTextOption option = text_document.defaultTextOption();
option.setAlignment(h_alignment & Qt::AlignHorizontal_Mask);
text_document.setDefaultTextOption(option);
text_document.setTextWidth(text_document.idealWidth());
}
}
painter.translate(qpainter_offset);
// force the palette used to render the QTextDocument