[graphics/drawy/gsoc2026] src/gui/item: fix: text alignment bugs

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

fix: text alignment bugs
- fixes bug with serializing text alignment
- fixes bug with setting and getting text alignment property

M  +5    -5    src/gui/item/itemutils.cpp
M  +14   -1    src/gui/item/text.cpp

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

diff --git a/src/gui/item/itemutils.cpp b/src/gui/item/itemutils.cpp
index 45d18076..3550a757 100644
--- a/src/gui/item/itemutils.cpp
+++ b/src/gui/item/itemutils.cpp
@@ -167,15 +167,15 @@ Qt::BrushStyle ItemUtils::convertItemBackgroundTypeStringToBrushStyle(const QStr
     return Qt::BrushStyle::SolidPattern;
 }
 
-QString ItemUtils::convertTextAlignmentToString(int alignment)
+QString ItemUtils::convertTextAlignmentToString(const int alignment)
 {
-    if (alignment == Qt::AlignLeft) {
+    if (alignment & Qt::AlignLeft) {
         return u"AlignLeft"_s;
-    } else if (alignment == Qt::AlignRight) {
+    } else if (alignment & Qt::AlignRight) {
         return u"AlignRight"_s;
-    } else if (alignment == Qt::AlignCenter) {
+    } else if (alignment & Qt::AlignCenter) {
         return u"AlignCenter"_s;
-    } else if (alignment == Qt::AlignJustify) {
+    } else if (alignment & Qt::AlignJustify) {
         return u"AlignJustify"_s;
     }
     qCWarning(DRAWY_GUI_LOG) << "Item::TextAlignment is not defined for: " << alignment;
diff --git a/src/gui/item/text.cpp b/src/gui/item/text.cpp
index e1b7af95..e50092ca 100644
--- a/src/gui/item/text.cpp
+++ b/src/gui/item/text.cpp
@@ -332,7 +332,7 @@ void TextItem::updatePreedit(const QString &preedit, const QList<QInputMethodEve
             formats.append(range);
         }
     }
-    auto layout = m_cursor.block().layout();
+    const auto layout = m_cursor.block().layout();
     if (!m_preeditString.isEmpty()) {
         layout->setPreeditArea(m_cursor.positionInBlock(), m_preeditString);
         layout->setFormats(formats);
@@ -392,6 +392,10 @@ Property TextItem::property(const Property::Type propertyType) const
         }
         return Property{ItemUtils::convertFontStyleToString(style), Property::Type::FontStyle};
     }
+    case Property::Type::TextAlignment: {
+        const int alignment = cursor.blockFormat().alignment();
+        return Property{ItemUtils::convertTextAlignmentToString(alignment), Property::Type::TextAlignment};
+    }
     default:
         return Item::property(propertyType);
     }
@@ -482,6 +486,15 @@ void TextItem::setProperty(const Property::Type propertyType, const Property new
         }
         break;
     }
+    case Property::Type::TextAlignment: {
+        QTextBlockFormat blockFmt;
+        blockFmt.setAlignment(static_cast<Qt::Alignment>(ItemUtils::convertStringToTextAlignment(newObj.value<QString>())));
+        QTextCursor docCursor(&m_document);
+        docCursor.select(QTextCursor::Document);
+        docCursor.mergeBlockFormat(blockFmt);
+        Item::setProperty(propertyType, newObj);
+        return;
+    }
     default:
         Item::setProperty(propertyType, newObj);
         return;
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.