diff --git a/autotests/ktimecomboboxtest.h b/autotests/ktimecomboboxtest.h --- a/autotests/ktimecomboboxtest.h +++ b/autotests/ktimecomboboxtest.h @@ -36,6 +36,7 @@ void testTimeList(); void testOptions(); void testDisplayFormat(); + void testMask(); private: diff --git a/autotests/ktimecomboboxtest.cpp b/autotests/ktimecomboboxtest.cpp --- a/autotests/ktimecomboboxtest.cpp +++ b/autotests/ktimecomboboxtest.cpp @@ -184,3 +184,27 @@ QCOMPARE(m_combo->displayFormat(), format); delete m_combo; } + +void KTimeComboBoxTest::testMask() +{ + // Store the current locale, and set to one which reproduces the bug. + QLocale currentLocale; + + // Test that the line edit input mask AM/PM portion gets correctly + // replaced with aa. + QLocale::setDefault(QLocale(QLocale::English, QLocale::Australia)); + m_combo = new KTimeComboBox(nullptr); + QString mask = m_combo->lineEdit()->inputMask(); + QCOMPARE(mask.contains(QLatin1String("aa")), true); + delete m_combo; + + // For 24 hour time formats, no am/pm specifier in mask + QLocale::setDefault(QLocale(QLocale::Norwegian, QLocale::Norway)); + m_combo = new KTimeComboBox(nullptr); + mask = m_combo->lineEdit()->inputMask(); + QCOMPARE(mask.contains(QLatin1String("aa")), false); + delete m_combo; + + // Restore the previous locale + QLocale::setDefault(currentLocale); +} diff --git a/src/ktimecombobox.cpp b/src/ktimecombobox.cpp --- a/src/ktimecombobox.cpp +++ b/src/ktimecombobox.cpp @@ -105,10 +105,27 @@ null.replace(locale.toString(56), QLatin1String("")); mask.replace(locale.toString(789), QLatin1String("900")); null.replace(locale.toString(789), QLatin1String("")); - if (format.contains(QLatin1String("ap")) || - format.contains(QLatin1String("AP"))) { + + // See if this time format contains a specifier for + // AM/PM, regardless of case. + int ampmPos = format.indexOf(QLatin1String("AP"), 0, Qt::CaseInsensitive); + + if (ampmPos != -1) { + // Get the locale aware am/pm strings QString am = locale.amText(); QString pm = locale.pmText(); + + // Convert the am/pm strings to the same case + // as the input format. This is necessary to + // provide a correct mask to the line edit. + if (format[ampmPos].isUpper()) { + am = am.toUpper(); + pm = pm.toUpper(); + } else { + am = am.toLower(); + pm = pm.toLower(); + } + int ampmLen = qMax(am.length(), pm.length()); QString ampmMask; for (int i = 0; i < ampmLen; ++i) {