diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-02-21 16:18:51 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-02-28 11:36:49 +0000 |
commit | 70828223f3e0f1a168e14b7d6bc556a52fb7a1e2 (patch) | |
tree | fce5c4ad90a9b9784063f0903e9ab6d2e29cb8f2 /tests | |
parent | 8b925ca18e6007da90dcc85a5b94c441d44188e8 (diff) |
QGuiApplication: use translation-based layout direction unless explicitly set
The stored layout direction used to get changed during initialization
to what was auto-detected based on the translation. Changing the
translation then overwrote that stored value, even if an explicit call
to setLayoutDirection was made by the application.
Calling QGuiApplication::setLayoutDirection(Auto) has so far been a
no-op.
Change this logic so that the stored layout direction continues to be
LayoutDirectionAuto also if it's set based on auto-detection, and only
overwrite it when explicitly called with a non-Auto value. This way,
applications can set a layout direction that stays unchanged even when
translators are installed.
Add test coverage that uses a QTranslator.
In practice, this is not a change of behavior, unless applications called
setLayoutDirection(Auto) (which is no longer a no-op), or called
setLayoutDirection() and then installed a translator and expected the
translator's layout direction to come into effect in spite of the explicit
setting.
[ChangeLog][Gui][QGuiApplication] Calling setLayoutDirection with a non-
auto value now disables the auto-detection based on installed
translators. Applications that explicitly set a layout direction and also
want translators installed afterwards to take effect should reset the
layout direction to Auto, which is now no longer a no-op.
Fixes: QTBUG-100632
Change-Id: I1fdcebd43a9b1b468ff95bf15f53f441bb214e08
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
(cherry picked from commit 4cca8ee527bfa94947f897d4d9b91aa8eb63d9c3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp index 5eab76898b..e3b4bd296f 100644 --- a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp +++ b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp @@ -1158,8 +1158,8 @@ void tst_QGuiApplication::layoutDirection() { qRegisterMetaType<Qt::LayoutDirection>(); - Qt::LayoutDirection oldDirection = QGuiApplication::layoutDirection(); - Qt::LayoutDirection newDirection = oldDirection == Qt::LeftToRight ? Qt::RightToLeft : Qt::LeftToRight; + const Qt::LayoutDirection oldDirection = QGuiApplication::layoutDirection(); + const Qt::LayoutDirection newDirection = oldDirection == Qt::LeftToRight ? Qt::RightToLeft : Qt::LeftToRight; QGuiApplication::setLayoutDirection(newDirection); QCOMPARE(QGuiApplication::layoutDirection(), newDirection); @@ -1177,8 +1177,64 @@ void tst_QGuiApplication::layoutDirection() QGuiApplication::setLayoutDirection(oldDirection); QCOMPARE(QGuiApplication::layoutDirection(), oldDirection); QCOMPARE(signalSpy.count(), 1); + + // with QGuiApplication instantiated, install a translator that gives us control + class LayoutDirectionTranslator : public QTranslator + { + public: + LayoutDirectionTranslator(Qt::LayoutDirection direction) + : direction(direction) + {} + + bool isEmpty() const override { return false; } + QString translate(const char *context, const char *sourceText, const char *disambiguation, int n) const override + { + if (QByteArrayView(sourceText) == "QT_LAYOUT_DIRECTION") + return direction == Qt::LeftToRight ? QLatin1String("LTR") : QLatin1String("RTL"); + return QTranslator::translate(context, sourceText, disambiguation, n); + } + + const Qt::LayoutDirection direction; + }; + + int layoutDirectionChangedCount = 0; + // reset to auto-detection, should be back to oldDirection now + QGuiApplication::setLayoutDirection(Qt::LayoutDirectionAuto); + QCOMPARE(QGuiApplication::layoutDirection(), oldDirection); + signalSpy.clear(); + { + // this translator doesn't change the direction + LayoutDirectionTranslator translator(oldDirection); + QGuiApplication::installTranslator(&translator); + QCOMPARE(QGuiApplication::layoutDirection(), translator.direction); + QCOMPARE(signalSpy.count(), layoutDirectionChangedCount); + } + QCOMPARE(signalSpy.count(), layoutDirectionChangedCount); // ltrTranslator removed, no change + + // install a new translator that changes the direction + { + LayoutDirectionTranslator translator(newDirection); + QGuiApplication::installTranslator(&translator); + QCOMPARE(QGuiApplication::layoutDirection(), translator.direction); + QCOMPARE(signalSpy.count(), ++layoutDirectionChangedCount); + } + // rtlTranslator removed + QCOMPARE(signalSpy.count(), ++layoutDirectionChangedCount); + + // override translation + QGuiApplication::setLayoutDirection(newDirection); + QCOMPARE(signalSpy.count(), ++layoutDirectionChangedCount); + { + // this translator will be ignored + LayoutDirectionTranslator translator(oldDirection); + QGuiApplication::installTranslator(&translator); + QCOMPARE(QGuiApplication::layoutDirection(), newDirection); + QCOMPARE(signalSpy.count(), layoutDirectionChangedCount); + } + QCOMPARE(signalSpy.count(), layoutDirectionChangedCount); } + void tst_QGuiApplication::globalShareContext() { #ifndef QT_NO_OPENGL |