From d4242b8af3e6eb5e9f68e5ff2efee97de11da892 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 8 Apr 2021 10:11:51 +0200 Subject: [PATCH] Revise deprecation of countriesForLanguage() It was originally marked \obsolete without any comment on what was to be used to replace it, or deprecation markings in the declaration, so it got missed at 6.0. More recently it's been deprecated in favor of a territory-based name; but actually it was obsoleted by (iterating the territory() of each return from) matchingLocales() in Qt 4.8. So back out of adding territoriesForLanguage to replace it and, instead, mark it as deprecated in the declaration, in favor of matchingLocales(). Also rewrite the implementation to be exactly that replacement. Rewrote the one example using it. Fixes: QTBUG-92484 Change-Id: Iedaf30378446dd9adac5128b7ee5fee48aab1636 Reviewed-by: Thiago Macieira --- .../widgets/widgets/calendarwidget/window.cpp | 14 ++++--- src/corelib/text/qlocale.cpp | 38 ++++--------------- src/corelib/text/qlocale.h | 6 +-- 3 files changed, 19 insertions(+), 39 deletions(-) diff --git a/examples/widgets/widgets/calendarwidget/window.cpp b/examples/widgets/widgets/calendarwidget/window.cpp index e88e41beb2..38e9798d83 100644 --- a/examples/widgets/widgets/calendarwidget/window.cpp +++ b/examples/widgets/widgets/calendarwidget/window.cpp @@ -237,6 +237,9 @@ void Window::createPreviewGroupBox() } //! [9] +// TODO: use loc.name() as label (but has underscore in place of slash) +// TODO: use locale() == loc instead of only comparing language and territory +// Needs someone familiar with this example to work out ramifications //! [10] void Window::createGeneralOptionsGroupBox() { @@ -247,15 +250,16 @@ void Window::createGeneralOptionsGroupBox() int index = 0; for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) { QLocale::Language lang = static_cast(_lang); - const auto territories = QLocale::territoriesForLanguage(lang); - for (auto territory : territories) { + const auto locales = + QLocale::matchingLocales(lang, QLocale::AnyScript, QLocale::AnyTerritory); + for (auto loc : locales) { QString label = QLocale::languageToString(lang); + auto territory = loc.territory(); label += QLatin1Char('/'); label += QLocale::territoryToString(territory); - QLocale locale(lang, territory); - if (this->locale().language() == lang && this->locale().territory() == territory) + if (locale().language() == lang && locale().territory() == territory) curLocaleIndex = index; - localeCombo->addItem(label, locale); + localeCombo->addItem(label, loc); ++index; } } diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 783d09a8b1..8c0ce2434e 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -2653,38 +2653,9 @@ QList QLocale::matchingLocales(QLocale::Language language, QLocale::Scr return result; } -/*! - \since 6.2 - - Returns the list of countries that have entries for \a language in Qt's locale - database. If the result is an empty list, then \a language is not represented in - Qt's locale database. - - \sa matchingLocales() -*/ -QList QLocale::territoriesForLanguage(QLocale::Language language) -{ - QList result; - if (language == C) { - result << AnyTerritory; - return result; - } - - unsigned language_id = language; - const QLocaleData *data = locale_data + locale_index[language_id]; - while (data->m_language_id == language_id) { - const QLocale::Territory territory = static_cast(data->m_territory_id); - if (!result.contains(territory)) - result.append(territory); - ++data; - } - - return result; -} - #if QT_DEPRECATED_SINCE(6, 6) /*! - \obsolete Use territoriesForLanguage(Language) instead. + \obsolete Use matchingLocales() instead and consult the territory() of each. \since 4.3 Returns the list of countries that have entries for \a language in Qt's locale @@ -2695,7 +2666,12 @@ QList QLocale::territoriesForLanguage(QLocale::Language lang */ QList QLocale::countriesForLanguage(Language language) { - return territoriesForLanguage(language); + const auto locales = matchingLocales(language, AnyScript, AnyCountry); + QList result; + result.reserve(locales.size()); + for (const auto &locale : locales) + result.append(locale.territory()); + return result; } #endif diff --git a/src/corelib/text/qlocale.h b/src/corelib/text/qlocale.h index 59a7339c24..77b3907b42 100644 --- a/src/corelib/text/qlocale.h +++ b/src/corelib/text/qlocale.h @@ -1108,10 +1108,10 @@ public: static QLocale c() { return QLocale(C); } static QLocale system(); - static QList matchingLocales(QLocale::Language language, QLocale::Script script, QLocale::Territory territory); - static QList territoriesForLanguage(Language lang); + static QList matchingLocales(QLocale::Language language, QLocale::Script script, + QLocale::Territory territory); #if QT_DEPRECATED_SINCE(6, 6) - QT_DEPRECATED_VERSION_X_6_6("Use territoriesForLanguage(Language) instead") + QT_DEPRECATED_VERSION_X_6_6("Query territory() on each entry from matchingLocales() instead") static QList countriesForLanguage(Language lang); #endif