[graphics/drawy/gsoc2026] src/gui: feat: expand text box according to alignment direction

Abdelhadi Wael <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit a32e652fd8c14ce3c02513caa36178bd120f0f57 by Abdelhadi Wael.
Committed on 16/07/2026 at 07:57.
Pushed by mlaurent into branch 'gsoc2026'.

feat: expand text box according to alignment direction

- if it is right aligned we expand to left
- if it is center aligned we expand to both left and right

M  +21   -1    src/gui/item/text.cpp
M  +1    -0    src/gui/item/text.hpp
M  +3    -0    src/gui/serializer/textdeserializer.cpp

https://invent.kde.org/graphics/drawy/-/commit/a32e652fd8c14ce3c02513caa36178bd120f0f57

diff --git a/src/gui/item/text.cpp b/src/gui/item/text.cpp
index e50092ca..b1b745c7 100644
--- a/src/gui/item/text.cpp
+++ b/src/gui/item/text.cpp
@@ -60,6 +60,12 @@ void TextItem::createTextBox(const QPointF position)
     setDirty(true);
 }
 
+void TextItem::setBoundingBoxTopLeft(const QPointF &topLeft)
+{
+    m_boundingBox.moveTopLeft(topLeft);
+    setDirty(true);
+}
+
 bool TextItem::intersects(const QRectF &rect)
 {
     return m_transform.map(m_boundingBox).intersects(rect);
@@ -239,7 +245,21 @@ void TextItem::updateBoundingBox()
         }
     }
 
-    m_boundingBox.setSize(m_document.size());
+    const auto oldSize = m_boundingBox.size();
+    const auto newSize = m_document.size();
+    if (oldSize != newSize) {
+        const qreal diff = newSize.width() - oldSize.width();
+        const int alignment = m_cursor.blockFormat().alignment();
+
+        auto topLeft = m_boundingBox.topLeft();
+        if (alignment & Qt::AlignRight) {
+            topLeft.setX(topLeft.x() - diff);
+        } else if (alignment & Qt::AlignCenter) {
+            topLeft.setX(topLeft.x() - (diff / 2.0));
+        }
+        m_boundingBox.moveTopLeft(topLeft);
+        m_boundingBox.setSize(newSize);
+    }
     setDirty(true);
 }
 
diff --git a/src/gui/item/text.hpp b/src/gui/item/text.hpp
index 1057e2d7..13ec2ddc 100644
--- a/src/gui/item/text.hpp
+++ b/src/gui/item/text.hpp
@@ -30,6 +30,7 @@ public:
     [[nodiscard]] bool lockAspectRatioWhenResizing() const override;
 
     void createTextBox(const QPointF position);
+    void setBoundingBoxTopLeft(const QPointF &topLeft);
 
     void setWrapWidth(qreal wrapWidth);
     [[nodiscard]] qreal wrapWidth() const;
diff --git a/src/gui/serializer/textdeserializer.cpp b/src/gui/serializer/textdeserializer.cpp
index 5be6f21c..ed5c8966 100644
--- a/src/gui/serializer/textdeserializer.cpp
+++ b/src/gui/serializer/textdeserializer.cpp
@@ -30,4 +30,7 @@ void TextDeserializer::deserialize(const QJsonObject &obj)
     } else {
         textItem->cursor().insertText(text);
     }
+
+    // cancel any shift that occurred due to alignment expansion logic
+    textItem->setBoundingBoxTopLeft(topLeft);
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.