diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index 51e3756c6..2790cf063 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -75,6 +75,16 @@ DynamicElementTextItem::DynamicElementTextItem(Element *parent_element) : QTextOption option = document()->defaultTextOption(); option.setAlignment(alignment & Qt::AlignHorizontal_Mask); document()->setDefaultTextOption(option); + fitAutoTextWidth(); + }); + connect(document(), &QTextDocument::contentsChanged, this, &DynamicElementTextItem::fitAutoTextWidth); + //The new font can make the lines wider or narrower than the width + //fitted for the old one: fit again, keeping the text's anchor + connect(this, &DiagramTextItem::fontChanged, this, [this]() + { + prepareAlignment(); + fitAutoTextWidth(); + finishAlignment(); }); } @@ -1796,9 +1806,28 @@ void DynamicElementTextItem::setTextWidth(qreal width) { this->document()->setTextWidth(width); m_text_width = width; + fitAutoTextWidth(); emit textWidthChanged(width); } +/** + @brief DynamicElementTextItem::fitAutoTextWidth + With no width set by the user, a QTextDocument has no width to centre + or right-align its lines in, so every line of a multi-line text stays + on the left whatever the alignment. Give the document the width of its + longest line instead. This does not change the size of the text, only + where the shorter lines sit. +*/ +void DynamicElementTextItem::fitAutoTextWidth() +{ + if (m_text_width > 0) + return; + + document()->setTextWidth(-1); + if (alignment() & (Qt::AlignHCenter | Qt::AlignRight)) + document()->setTextWidth(document()->idealWidth()); +} + void DynamicElementTextItem::setXref_item(Qt::AlignmentFlag m_exHrefPos, int slave_offset) { QRectF r = boundingRect(); diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.h b/sources/qetgraphicsitem/dynamicelementtextitem.h index 2e1ee9ee6..40228f984 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.h +++ b/sources/qetgraphicsitem/dynamicelementtextitem.h @@ -112,6 +112,10 @@ class DynamicElementTextItem : public DiagramTextItem QUuid uuid() const; void updateXref(); void setPlainText(const QString &text); + //Hide QGraphicsTextItem::textWidth(): with the width on "auto" + //the document is given the text's own width so its lines can be + //centred or right-aligned, but the width is still "auto" (-1). + qreal textWidth() const {return m_text_width;} void setTextWidth(qreal width); void setXref_item(Qt::AlignmentFlag m_exHrefPos, int slave_offset = 0); @@ -161,6 +165,7 @@ class DynamicElementTextItem : public DiagramTextItem void addResizeHandles(); void removeResizeHandles(); void updateResizeHandlesPos(); + void fitAutoTextWidth(); void handlerMousePressEvent(QetGraphicsHandlerItem *handle, QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent(QetGraphicsHandlerItem *handle, QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent(QetGraphicsHandlerItem *handle, QGraphicsSceneMouseEvent *event);