diff options
| author | Hatem ElKharashy <hatem.elkharashy@qt.io> | 2023-11-08 14:39:49 +0200 |
|---|---|---|
| committer | Hatem ElKharashy <hatem.elkharashy@qt.io> | 2023-11-11 14:15:37 +0000 |
| commit | 7e678acbb6167218d349c49436499121b2025af5 (patch) | |
| tree | 8746445b627cd7d3a16686b1fa909cedf2d849d0 /src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp | |
| parent | bde55ad574ac84440e2cdc9c1122a344bb1cb67a (diff) | |
Material: respect horizontalAlignment in placeholder text
This allows placeholder text to follow the Alignment set to
the TextField or TextArea components when using Material style.
The placeholder text will float to the left, right, or center
depending on the alignment set, and the arc will be drawn properly
in case of Material.Outline container style.
Fixes: QTBUG-118856
Pick-to: 6.6 6.5
Change-Id: Ic9cede806dc2f6109e7e2c4b2b2fc960d9c6a1b6
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp')
| -rw-r--r-- | src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp b/src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp index e987ce5610..ab07c9d9b2 100644 --- a/src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp +++ b/src/quickcontrols/material/impl/qquickmaterialtextcontainer.cpp @@ -118,6 +118,20 @@ void QQuickMaterialTextContainer::setPlaceholderTextWidth(qreal placeholderTextW update(); } +QQuickMaterialTextContainer::PlaceHolderHAlignment QQuickMaterialTextContainer::placeholderTextHAlign() const +{ + return m_placeholderTextHAlign; +} + +void QQuickMaterialTextContainer::setPlaceholderTextHAlign(PlaceHolderHAlignment placeholderTextHAlign) +{ + if (m_placeholderTextHAlign == placeholderTextHAlign) + return; + + m_placeholderTextHAlign = placeholderTextHAlign; + update(); +} + bool QQuickMaterialTextContainer::controlHasActiveFocus() const { return m_controlHasActiveFocus; @@ -208,6 +222,25 @@ void QQuickMaterialTextContainer::paint(QPainter *painter) // This is coincidentally the same as cornerRadius, but use different variable names // to keep the code understandable. const qreal gapPadding = 4; + // When animating focus on outlined containers, we need to make a gap + // at the top left for the placeholder text. + // If the text is too wide for the container, it will be elided, so + // we shouldn't need to clamp its width here. TODO: check that this is the case for TextArea. + const qreal halfPlaceholderWidth = m_placeholderTextWidth / 2; + // Take care of different Alignment cases for the placeholder text. + qreal gapCenterX; + switch (m_placeholderTextHAlign) { + case PlaceHolderHAlignment::AlignHCenter: + gapCenterX = width() / 2; + break; + case PlaceHolderHAlignment::AlignRight: + gapCenterX = width() - halfPlaceholderWidth - m_horizontalPadding; + break; + default: + gapCenterX = m_horizontalPadding + halfPlaceholderWidth; + break; + } + QPainterPath path; QPointF startPos; @@ -216,15 +249,6 @@ void QQuickMaterialTextContainer::paint(QPainter *painter) if (m_filled || m_focusAnimationProgress == 0) { startPos = QPointF(cornerRadius, 0); } else { - // When animating focus on outlined containers, we need to make a gap - // at the top left for the placeholder text. - // If the text is too wide for the container, it will be elided, so - // we shouldn't need to clamp its width here. TODO: check that this is the case for TextArea. - const qreal halfPlaceholderWidth = m_placeholderTextWidth / 2; - // Left padding plus half of the placeholder text gives the center of the placeholder text gap. - // Note that the placeholder gap is always aligned to the left side of the TextField, - // not the center, so we can't just use half the container's width. - const qreal gapCenterX = m_horizontalPadding + halfPlaceholderWidth; // Start at the center of the gap and animate outwards towards the left-hand side. // Subtract gapPadding to account for the gap between the line and the placeholder text. // Also subtract the pen width because otherwise it extends by that distance too much to the right. @@ -259,11 +283,6 @@ void QQuickMaterialTextContainer::paint(QPainter *painter) // Back to the start. path.lineTo(startPos.x(), startPos.y()); } else { - // Go to the end (right-hand side) of the gap. - const qreal halfPlaceholderWidth = (/*placeholderTextGap * 2 + */m_placeholderTextWidth) / 2; - const qreal gapCenterX = m_horizontalPadding + halfPlaceholderWidth; - // Just "+ placeholderTextGap" should be enough to get us to the correct place - not - // sure why doubling it is necessary... path.lineTo(gapCenterX + (m_focusAnimationProgress * halfPlaceholderWidth) + gapPadding, startPos.y()); } |
