TextArea/TextField: properly update placeholder text alignment

Placeholder text alignment depends on actual alignment of a parent
control, e.g. TextArea or TextField. Thus, if the horizontal text
alignment of the parent control is set explicitly, the placeholder
text must have the same alignment. Otherwise, the placeholder text
alignment should respect to the natural alignment of the text.

In order to do this QQuickPlaceholderText is connected to
effectiveHorizontalAlignmentChanged() signal of the parent control.

The problem is that the signal may not be emitted when alignment
is set explicitly after the component creation, so the placeholder
text alignment will not be updated respectively. To solve this we
need to make sure that the signal is forcibly emitted every time
when the alignment is set explicitly.

Fixes: QTBUG-120052
Pick-to: 6.5
Change-Id: Ib66a7a46d523777cc54ca6b6883d3fecc800dfb2
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
(cherry picked from commit 694a677535)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 8b12eb7ed5)
This commit is contained in:
Vladimir Belyavsky 2023-12-11 21:45:22 +03:00
parent a631caf282
commit 8903bb3d64
2 changed files with 73 additions and 40 deletions

View File

@ -715,9 +715,8 @@ QQuickTextEdit::HAlignment QQuickTextEdit::hAlign() const
void QQuickTextEdit::setHAlign(HAlignment align)
{
Q_D(QQuickTextEdit);
bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror;
d->hAlignImplicit = false;
if (d->setHAlign(align, forceAlign) && isComponentComplete()) {
if (d->setHAlign(align, true) && isComponentComplete()) {
d->updateDefaultTextOption();
updateSize();
}
@ -752,21 +751,34 @@ QQuickTextEdit::HAlignment QQuickTextEdit::effectiveHAlign() const
return effectiveAlignment;
}
bool QQuickTextEditPrivate::setHAlign(QQuickTextEdit::HAlignment alignment, bool forceAlign)
bool QQuickTextEditPrivate::setHAlign(QQuickTextEdit::HAlignment align, bool forceAlign)
{
Q_Q(QQuickTextEdit);
if (hAlign != alignment || forceAlign) {
QQuickTextEdit::HAlignment oldEffectiveHAlign = q->effectiveHAlign();
hAlign = alignment;
emit q->horizontalAlignmentChanged(alignment);
if (oldEffectiveHAlign != q->effectiveHAlign())
emit q->effectiveHorizontalAlignmentChanged();
if (hAlign == align && !forceAlign)
return false;
const bool wasImplicit = hAlignImplicit;
const auto oldEffectiveHAlign = q->effectiveHAlign();
hAlignImplicit = !forceAlign;
if (hAlign != align) {
hAlign = align;
emit q->horizontalAlignmentChanged(align);
}
if (q->effectiveHAlign() != oldEffectiveHAlign) {
emit q->effectiveHorizontalAlignmentChanged();
return true;
}
if (forceAlign && wasImplicit) {
// QTBUG-120052 - when horizontal text alignment is set explicitly,
// we need notify any other controls that may depend on it, like QQuickPlaceholderText
emit q->effectiveHorizontalAlignmentChanged();
}
return false;
}
Qt::LayoutDirection QQuickTextEditPrivate::textDirection(const QString &text) const
{
const QChar *character = text.constData();
@ -789,20 +801,22 @@ Qt::LayoutDirection QQuickTextEditPrivate::textDirection(const QString &text) co
bool QQuickTextEditPrivate::determineHorizontalAlignment()
{
Q_Q(QQuickTextEdit);
if (hAlignImplicit && q->isComponentComplete()) {
Qt::LayoutDirection direction = contentDirection;
if (!hAlignImplicit || !q->isComponentComplete())
return false;
Qt::LayoutDirection direction = contentDirection;
#if QT_CONFIG(im)
if (direction == Qt::LayoutDirectionAuto) {
const QString preeditText = control->textCursor().block().layout()->preeditAreaText();
direction = textDirection(preeditText);
}
if (direction == Qt::LayoutDirectionAuto)
direction = qGuiApp->inputMethod()->inputDirection();
if (direction == Qt::LayoutDirectionAuto) {
const QString preeditText = control->textCursor().block().layout()->preeditAreaText();
direction = textDirection(preeditText);
}
if (direction == Qt::LayoutDirectionAuto)
direction = qGuiApp->inputMethod()->inputDirection();
#endif
return setHAlign(direction == Qt::RightToLeft ? QQuickTextEdit::AlignRight : QQuickTextEdit::AlignLeft);
}
return false;
const auto implicitHAlign = direction == Qt::RightToLeft ?
QQuickTextEdit::AlignRight : QQuickTextEdit::AlignLeft;
return setHAlign(implicitHAlign);
}
void QQuickTextEditPrivate::mirrorChange()

View File

@ -540,9 +540,8 @@ QQuickTextInput::HAlignment QQuickTextInput::hAlign() const
void QQuickTextInput::setHAlign(HAlignment align)
{
Q_D(QQuickTextInput);
bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror;
d->hAlignImplicit = false;
if (d->setHAlign(align, forceAlign) && isComponentComplete()) {
if (d->setHAlign(align, true) && isComponentComplete()) {
d->updateLayout();
updateCursorRectangle();
}
@ -577,17 +576,34 @@ QQuickTextInput::HAlignment QQuickTextInput::effectiveHAlign() const
return effectiveAlignment;
}
bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment alignment, bool forceAlign)
bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment align, bool forceAlign)
{
Q_Q(QQuickTextInput);
if ((hAlign != alignment || forceAlign) && alignment <= QQuickTextInput::AlignHCenter) { // justify not supported
QQuickTextInput::HAlignment oldEffectiveHAlign = q->effectiveHAlign();
hAlign = alignment;
emit q->horizontalAlignmentChanged(alignment);
if (oldEffectiveHAlign != q->effectiveHAlign())
emit q->effectiveHorizontalAlignmentChanged();
if (align > QQuickTextInput::AlignHCenter)
return false; // justify is not supported
if (hAlign == align && !forceAlign)
return false;
const bool wasImplicit = hAlignImplicit;
const auto oldEffectiveHAlign = q->effectiveHAlign();
hAlignImplicit = !forceAlign;
if (hAlign != align) {
hAlign = align;
emit q->horizontalAlignmentChanged(align);
}
if (q->effectiveHAlign() != oldEffectiveHAlign) {
emit q->effectiveHorizontalAlignmentChanged();
return true;
}
if (forceAlign && wasImplicit) {
// QTBUG-120052 - when horizontal text alignment is set explicitly,
// we need notify any other controls that may depend on it, like QQuickPlaceholderText
emit q->effectiveHorizontalAlignmentChanged();
}
return false;
}
@ -631,16 +647,19 @@ Qt::LayoutDirection QQuickTextInputPrivate::layoutDirection() const
bool QQuickTextInputPrivate::determineHorizontalAlignment()
{
if (hAlignImplicit) {
// if no explicit alignment has been set, follow the natural layout direction of the text
Qt::LayoutDirection direction = textDirection();
if (!hAlignImplicit)
return false;
// if no explicit alignment has been set, follow the natural layout direction of the text
Qt::LayoutDirection direction = textDirection();
#if QT_CONFIG(im)
if (direction == Qt::LayoutDirectionAuto)
direction = QGuiApplication::inputMethod()->inputDirection();
if (direction == Qt::LayoutDirectionAuto)
direction = QGuiApplication::inputMethod()->inputDirection();
#endif
return setHAlign(direction == Qt::RightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft);
}
return false;
const auto implicitHAlign = direction == Qt::RightToLeft ?
QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft;
return setHAlign(implicitHAlign);
}
QQuickTextInput::VAlignment QQuickTextInput::vAlign() const