From 58aa9f49899a57d2069a5b5d174cc4ae785d0d93 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 19 Apr 2019 10:04:49 +0200 Subject: [PATCH] [libcalamares] Move LocaleLabel from libcalamaresui to libcalamares - This isn't a UI-dependent class - Doesn't make much sense in CalamaresUtilsGui either --- src/libcalamares/CMakeLists.txt | 1 + src/libcalamares/utils/LocaleLabel.cpp | 73 +++++++++++ src/libcalamares/utils/LocaleLabel.h | 113 ++++++++++++++++++ .../utils/CalamaresUtilsGui.cpp | 51 +------- src/libcalamaresui/utils/CalamaresUtilsGui.h | 82 ------------- 5 files changed, 188 insertions(+), 132 deletions(-) create mode 100644 src/libcalamares/utils/LocaleLabel.cpp create mode 100644 src/libcalamares/utils/LocaleLabel.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index a7dd1c0fe..fc1f026ae 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -23,6 +23,7 @@ set( utilsSources utils/CalamaresUtils.cpp utils/CalamaresUtilsSystem.cpp utils/CommandList.cpp + utils/LocaleLabel.cpp utils/Logger.cpp utils/PluginFactory.cpp utils/Retranslator.cpp diff --git a/src/libcalamares/utils/LocaleLabel.cpp b/src/libcalamares/utils/LocaleLabel.cpp new file mode 100644 index 000000000..275ece622 --- /dev/null +++ b/src/libcalamares/utils/LocaleLabel.cpp @@ -0,0 +1,73 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2015, Teo Mrnjavac + * Copyright 2017-2018, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "LocaleLabel.h" + +namespace CalamaresUtils +{ + +LocaleLabel::LocaleLabel() + : m_locale( QLocale() ) +{ + m_localeId = m_locale.name(); + + setLabels( QString(), LabelFormat::IfNeededWithCountry ); +} + +LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format ) + : m_locale( LocaleLabel::getLocale( locale ) ) + , m_localeId( locale ) +{ + setLabels( locale, format ); +} + +void +LocaleLabel::setLabels( const QString& locale, LabelFormat format ) +{ + //: language[name] (country[name]) + QString longFormat = QObject::tr( "%1 (%2)" ); + + QString languageName = m_locale.nativeLanguageName(); + QString englishName = m_locale.languageToString( m_locale.language() ); + QString countryName; + + if ( languageName.isEmpty() ) + languageName = QString( "* %1 (%2)" ).arg( locale, englishName ); + + bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) || + (locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 ); + + if ( needsCountryName ) + countryName = m_locale.nativeCountryName(); + m_label = needsCountryName ? longFormat.arg( languageName ).arg( countryName ) : languageName; + m_englishLabel = englishName; +} + +QLocale LocaleLabel::getLocale( const QString& localeName ) +{ + if ( localeName.contains( "@latin" ) ) + { + QLocale loc( localeName ); // Ignores @latin + return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() ); + } + else + return QLocale( localeName ); +} + +} // namespace diff --git a/src/libcalamares/utils/LocaleLabel.h b/src/libcalamares/utils/LocaleLabel.h new file mode 100644 index 000000000..d3113c869 --- /dev/null +++ b/src/libcalamares/utils/LocaleLabel.h @@ -0,0 +1,113 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2015, Teo Mrnjavac + * Copyright 2017-2018, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef LIBCALAMARES_LOCALELABEL_H +#define LIBCALAMARES_LOCALELABEL_H + +#include +#include + +namespace CalamaresUtils +{ + +/** + * @brief Consistent locale (language + country) naming. + * + * Support class to turn locale names (as used by Calamares's + * translation system) into QLocales, and also into consistent + * human-readable text labels. + */ +class LocaleLabel +{ +public: + /** @brief Formatting option for label -- add (country) to label. */ + enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ; + + /** @brief Empty locale. This uses the system-default locale. */ + LocaleLabel(); + + /** @brief Construct from a locale name. + * + * The @p localeName should be one that Qt recognizes, e.g. en_US or ar_EY. + * The @p format determines whether the country name is always present + * in the label (human-readable form) or only if needed for disambiguation. + */ + LocaleLabel( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry ); + + /** @brief Define a sorting order. + * + * English (@see isEnglish() -- it means en_US) is sorted at the top. + */ + bool operator <( const LocaleLabel& other ) const + { + return m_localeId < other.m_localeId; + } + + /** @brief Is this locale English? + * + * en_US and en (American English) is defined as English. The Queen's + * English -- proper English -- is relegated to non-English status. + */ + bool isEnglish() const + { + return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" ); + } + + /** @brief Get the human-readable name for this locale. */ + QString label() const + { + return m_label; + } + /** @brief Get the *English* human-readable name for this locale. */ + QString englishLabel() const + { + return m_englishLabel; + } + + /** @brief Get the Qt locale. */ + QLocale locale() const + { + return m_locale; + } + + QString name() const + { + return m_locale.name(); + } + + /** @brief Get a Qt locale for the given @p localeName + * + * This special-cases `sr@latin`, which is used as a translation + * name in Calamares, while Qt recognizes `sr@latn`. + */ + static QLocale getLocale( const QString& localeName ); + +protected: + void setLabels( const QString& name, LabelFormat format ); + + QLocale m_locale; + QString m_localeId; // the locale identifier, e.g. "en_GB" + QString m_label; // the native name of the locale + QString m_englishLabel; +} ; + + +} // namespace CalamaresUtils + +#endif // LIBCALAMARES_LOCALELABEL_H diff --git a/src/libcalamaresui/utils/CalamaresUtilsGui.cpp b/src/libcalamaresui/utils/CalamaresUtilsGui.cpp index 9f68cf742..ef239b9d8 100644 --- a/src/libcalamaresui/utils/CalamaresUtilsGui.cpp +++ b/src/libcalamaresui/utils/CalamaresUtilsGui.cpp @@ -262,53 +262,4 @@ clearLayout( QLayout* layout ) } } -LocaleLabel::LocaleLabel() - : m_locale( QLocale() ) -{ - m_localeId = m_locale.name(); - - setLabels( QString(), LabelFormat::IfNeededWithCountry ); -} - -LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format ) - : m_locale( LocaleLabel::getLocale( locale ) ) - , m_localeId( locale ) -{ - setLabels( locale, format ); -} - -void -LocaleLabel::setLabels( const QString& locale, LabelFormat format ) -{ - //: language[name] (country[name]) - QString longFormat = QObject::tr( "%1 (%2)" ); - - QString languageName = m_locale.nativeLanguageName(); - QString englishName = m_locale.languageToString( m_locale.language() ); - QString countryName; - - if ( languageName.isEmpty() ) - languageName = QString( "* %1 (%2)" ).arg( locale, englishName ); - - bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) || - (locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 ); - - if ( needsCountryName ) - countryName = m_locale.nativeCountryName(); - m_label = needsCountryName ? longFormat.arg( languageName ).arg( countryName ) : languageName; - m_englishLabel = englishName; -} - -QLocale LocaleLabel::getLocale( const QString& localeName ) -{ - if ( localeName.contains( "@latin" ) ) - { - QLocale loc( localeName ); // Ignores @latin - return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() ); - } - else - return QLocale( localeName ); -} - - -} +} // namespace diff --git a/src/libcalamaresui/utils/CalamaresUtilsGui.h b/src/libcalamaresui/utils/CalamaresUtilsGui.h index f1a20964f..135a57c43 100644 --- a/src/libcalamaresui/utils/CalamaresUtilsGui.h +++ b/src/libcalamaresui/utils/CalamaresUtilsGui.h @@ -127,88 +127,6 @@ constexpr int windowMinimumHeight = 520; constexpr int windowPreferredWidth = 1024; constexpr int windowPreferredHeight = 520; -/** - * @brief Consistent locale (language + country) naming. - * - * Support class to turn locale names (as used by Calamares's - * translation system) into QLocales, and also into consistent - * human-readable text labels. - */ -class LocaleLabel -{ -public: - /** @brief Formatting option for label -- add (country) to label. */ - enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ; - - /** @brief Empty locale. This uses the system-default locale. */ - LocaleLabel(); - - /** @brief Construct from a locale name. - * - * The @p localeName should be one that Qt recognizes, e.g. en_US or ar_EY. - * The @p format determines whether the country name is always present - * in the label (human-readable form) or only if needed for disambiguation. - */ - LocaleLabel( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry ); - - /** @brief Define a sorting order. - * - * English (@see isEnglish() -- it means en_US) is sorted at the top. - */ - bool operator <( const LocaleLabel& other ) const - { - return m_localeId < other.m_localeId; - } - - /** @brief Is this locale English? - * - * en_US and en (American English) is defined as English. The Queen's - * English -- proper English -- is relegated to non-English status. - */ - bool isEnglish() const - { - return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" ); - } - - /** @brief Get the human-readable name for this locale. */ - QString label() const - { - return m_label; - } - /** @brief Get the *English* human-readable name for this locale. */ - QString englishLabel() const - { - return m_englishLabel; - } - - /** @brief Get the Qt locale. */ - QLocale locale() const - { - return m_locale; - } - - QString LocaleLabel::name() const - { - return m_locale.name(); - } - - /** @brief Get a Qt locale for the given @p localeName - * - * This special-cases `sr@latin`, which is used as a translation - * name in Calamares, while Qt recognizes `sr@latn`. - */ - static QLocale getLocale( const QString& localeName ); - -protected: - void setLabels( const QString& name, LabelFormat format ); - - QLocale m_locale; - QString m_localeId; // the locale identifier, e.g. "en_GB" - QString m_label; // the native name of the locale - QString m_englishLabel; -} ; - - } // namespace CalamaresUtils #endif // CALAMARESUTILSGUI_H