diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index ad1c7f336..959b0a9db 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -31,6 +31,8 @@ set( libSources geoip/Handler.cpp # Locale-data service + locale/Label.cpp + locale/LabelModel.cpp locale/Lookup.cpp # Partition service @@ -40,7 +42,6 @@ set( libSources utils/CalamaresUtilsSystem.cpp utils/CommandList.cpp utils/Dirs.cpp - utils/LocaleLabel.cpp utils/Logger.cpp utils/PluginFactory.cpp utils/Retranslator.cpp diff --git a/src/libcalamares/utils/LocaleLabel.cpp b/src/libcalamares/locale/Label.cpp similarity index 84% rename from src/libcalamares/utils/LocaleLabel.cpp rename to src/libcalamares/locale/Label.cpp index 26480ef14..ca528dc75 100644 --- a/src/libcalamares/utils/LocaleLabel.cpp +++ b/src/libcalamares/locale/Label.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2015, Teo Mrnjavac - * Copyright 2017-2018, Adriaan de Groot + * Copyright 2017-2019, 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 @@ -17,12 +17,12 @@ * along with Calamares. If not, see . */ -#include "LocaleLabel.h" +#include "Label.h" -namespace CalamaresUtils +namespace CalamaresUtils::Locale { -LocaleLabel::LocaleLabel() +Label::Label() : m_locale( QLocale() ) { m_localeId = m_locale.name(); @@ -30,15 +30,15 @@ LocaleLabel::LocaleLabel() setLabels( QString(), LabelFormat::IfNeededWithCountry ); } -LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format ) - : m_locale( LocaleLabel::getLocale( locale ) ) +Label::Label( const QString& locale, LabelFormat format ) + : m_locale( Label::getLocale( locale ) ) , m_localeId( locale ) { setLabels( locale, format ); } void -LocaleLabel::setLabels( const QString& locale, LabelFormat format ) +Label::setLabels( const QString& locale, LabelFormat format ) { //: language[name] (country[name]) QString longFormat = QObject::tr( "%1 (%2)" ); @@ -59,7 +59,7 @@ LocaleLabel::setLabels( const QString& locale, LabelFormat format ) m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) ) : englishName; } -QLocale LocaleLabel::getLocale( const QString& localeName ) +QLocale Label::getLocale( const QString& localeName ) { if ( localeName.contains( "@latin" ) ) { diff --git a/src/libcalamares/utils/LocaleLabel.h b/src/libcalamares/locale/Label.h similarity index 90% rename from src/libcalamares/utils/LocaleLabel.h rename to src/libcalamares/locale/Label.h index b56b29f33..a436d4c62 100644 --- a/src/libcalamares/utils/LocaleLabel.h +++ b/src/libcalamares/locale/Label.h @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2015, Teo Mrnjavac - * Copyright 2017-2018, Adriaan de Groot + * Copyright 2017-2019, 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 @@ -17,13 +17,14 @@ * along with Calamares. If not, see . */ -#ifndef UTILS_LOCALELABEL_H -#define UTILS_LOCALELABEL_H +#ifndef LOCALE_LABEL_H +#define LOCALE_LABEL_H #include #include -namespace CalamaresUtils +namespace CalamaresUtils {} +namespace CalamaresUtils::Locale { /** @@ -33,14 +34,14 @@ namespace CalamaresUtils * translation system) into QLocales, and also into consistent * human-readable text labels. */ -class LocaleLabel +class Label { 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(); + Label(); /** @brief Construct from a locale name. * @@ -48,13 +49,13 @@ public: * 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 ); + Label( 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 + bool operator <( const Label& other ) const { return m_localeId < other.m_localeId; } diff --git a/src/modules/welcome/LocaleModel.cpp b/src/libcalamares/locale/LabelModel.cpp similarity index 65% rename from src/modules/welcome/LocaleModel.cpp rename to src/libcalamares/locale/LabelModel.cpp index 0ecf0fd1c..312d96121 100644 --- a/src/modules/welcome/LocaleModel.cpp +++ b/src/libcalamares/locale/LabelModel.cpp @@ -16,30 +16,33 @@ * along with Calamares. If not, see . */ -#include "LocaleModel.h" +#include "LabelModel.h" -LocaleModel::LocaleModel( const QStringList& locales, QObject* parent ) +namespace CalamaresUtils::Locale +{ + +LabelModel::LabelModel( const QStringList& locales, QObject* parent ) : QAbstractListModel( parent ) { Q_ASSERT( locales.count() > 0 ); m_locales.reserve( locales.count() ); for ( const auto& l : locales ) - m_locales.push_back( CalamaresUtils::LocaleLabel( l ) ); + m_locales.push_back( Label( l ) ); } -LocaleModel::~LocaleModel() +LabelModel::~LabelModel() { } int -LocaleModel::rowCount( const QModelIndex& ) const +LabelModel::rowCount( const QModelIndex& ) const { return m_locales.count(); } QVariant -LocaleModel::data( const QModelIndex& index, int role ) const +LabelModel::data( const QModelIndex& index, int role ) const { if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) ) return QVariant(); @@ -59,8 +62,8 @@ LocaleModel::data( const QModelIndex& index, int role ) const } } -const CalamaresUtils::LocaleLabel& -LocaleModel::locale( int row ) +const Label& +LabelModel::locale( int row ) { if ( ( row < 0 ) || ( row >= m_locales.count() ) ) { @@ -73,7 +76,7 @@ LocaleModel::locale( int row ) } int -LocaleModel::find( std::function predicate ) const +LabelModel::find( std::function predicate ) const { for ( int row = 0; row < m_locales.count() ; ++row ) { @@ -84,26 +87,21 @@ LocaleModel::find( std::function predicate ) const } int -LocaleModel::find( std::function predicate ) const +LabelModel::find( std::function predicate ) const { - return find( [&]( const LocaleLabel& l ) + return find( [&]( const Label& l ) { return predicate( l.locale() ); } ); } int -LocaleModel::find( const QLocale& locale ) const +LabelModel::find( const QLocale& locale ) const { - return find( [&]( const LocaleLabel& l ) + return find( [&]( const Label& l ) { return locale == l.locale(); } ); } -void -LocaleTwoColumnDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const -{ - QStyledItemDelegate::paint( painter, option, index ); - option.widget->style()->drawItemText( painter, option.rect, Qt::AlignRight | Qt::AlignVCenter, option.palette, false, index.data( LocaleModel::EnglishLabelRole ).toString() ); -} +} // namespace diff --git a/src/modules/welcome/LocaleModel.h b/src/libcalamares/locale/LabelModel.h similarity index 68% rename from src/modules/welcome/LocaleModel.h rename to src/libcalamares/locale/LabelModel.h index b1566d336..092ee638a 100644 --- a/src/modules/welcome/LocaleModel.h +++ b/src/libcalamares/locale/LabelModel.h @@ -16,28 +16,31 @@ * along with Calamares. If not, see . */ -#ifndef WELCOME_LOCALEMODEL_H -#define WELCOME_LOCALEMODEL_H +#ifndef LOCALE_LABELMODEL_H +#define LOCALE_LABELMODEL_H + +#include "DllMacro.h" +#include "Label.h" #include -#include #include -#include "utils/LocaleLabel.h" -class LocaleModel : public QAbstractListModel +namespace CalamaresUtils {} +namespace CalamaresUtils::Locale +{ + +DLLEXPORT class LabelModel : public QAbstractListModel { public: - using LocaleLabel = CalamaresUtils::LocaleLabel; - enum { LabelRole = Qt::DisplayRole, EnglishLabelRole = Qt::UserRole + 1 }; - LocaleModel( const QStringList& locales, QObject* parent = nullptr ); - virtual ~LocaleModel() override; + LabelModel( const QStringList& locales, QObject* parent = nullptr ); + virtual ~LabelModel() override; int rowCount( const QModelIndex& parent ) const override; @@ -48,26 +51,19 @@ public: * This is the backing data for the model; if @p row is out-of-range, * returns a reference to en_US. */ - const LocaleLabel& locale( int row ); + const Label& locale( int row ); /** @brief Searches for an item that matches @p predicate * * Returns the row number of the first match, or -1 if there isn't one. */ int find( std::function predicate ) const; - int find( std::function predicate ) const; + int find( std::function predicate ) const; int find( const QLocale& ) const; private: - QVector< LocaleLabel > m_locales; -} ; - -class LocaleTwoColumnDelegate : public QStyledItemDelegate -{ -public: - using QStyledItemDelegate::QStyledItemDelegate; - - void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override; + QVector< Label > m_locales; } ; +} // namespace #endif diff --git a/src/libcalamares/locale/Lookup.cpp b/src/libcalamares/locale/Lookup.cpp index d38d417d4..a096bc679 100644 --- a/src/libcalamares/locale/Lookup.cpp +++ b/src/libcalamares/locale/Lookup.cpp @@ -20,7 +20,7 @@ #include "CountryData_p.cpp" -namespace Calamares +namespace CalamaresUtils::Locale { struct TwoChar @@ -35,7 +35,7 @@ struct TwoChar cc2 = code[1].toLatin1(); } } - + char cc1; char cc2; }; @@ -44,7 +44,7 @@ static const CountryData* lookup( TwoChar c ) { if ( !c.cc1 ) return nullptr; - + const CountryData* p = std::find_if(country_data_table, country_data_table + country_data_size, [c=c]( const CountryData& d ){ return (d.cc1 == c.cc1) && (d.cc2 == c.cc2); } ); @@ -52,7 +52,7 @@ static const CountryData* lookup( TwoChar c ) return nullptr; return p; } - + QLocale::Country countryForCode(const QString& code) { const CountryData* p = lookup( TwoChar( code ) ); diff --git a/src/libcalamares/locale/Lookup.h b/src/libcalamares/locale/Lookup.h index 976c4dc21..5712a1120 100644 --- a/src/libcalamares/locale/Lookup.h +++ b/src/libcalamares/locale/Lookup.h @@ -24,25 +24,26 @@ #include #include -namespace Calamares +namespace CalamaresUtils {} +namespace CalamaresUtils::Locale { /* All the functions in this file do lookups of locale data * based on CLDR tables; these are lookups that you can't (easily) * do with just QLocale (e.g. from 2-letter country code to a likely * locale). */ - + /// @brief Map a 2-letter code to a Country, or AnyCountry if not found DLLEXPORT QLocale::Country countryForCode( const QString& code ); /** @brief Map a Country to a Language, or AnyLanguage if not found - * + * * This is a *likely* language for the given country, based on the * CLDR tables. For instance, this maps Belgium to Dutch. */ DLLEXPORT QLocale::Language languageForCountry( QLocale::Country country ); /// @brief Map a 2-letter code to a Language, or AnyLanguage if not found DLLEXPORT QLocale::Language languageForCountry( const QString& code ); - + /// @brief Get both Country and Language for a 2-letter code DLLEXPORT QPair< QLocale::Country, QLocale::Language > countryData( const QString& code ); /// @brief Get a likely locale for a 2-letter country code diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index c8076866e..f6cdba436 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -27,8 +27,8 @@ #include "LCLocaleDialog.h" #include "Settings.h" +#include "locale/Label.h" #include "utils/CalamaresUtilsGui.h" -#include "utils/LocaleLabel.h" #include "utils/Logger.h" #include "utils/Retranslator.h" @@ -387,10 +387,10 @@ LocalePage::init( const QString& initialRegion, std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfiguration& lc ) const { - using CalamaresUtils::LocaleLabel; + using CalamaresUtils::Locale::Label; - LocaleLabel lang( lc.language(), LocaleLabel::LabelFormat::AlwaysWithCountry ); - LocaleLabel num( lc.lc_numeric, LocaleLabel::LabelFormat::AlwaysWithCountry ); + Label lang( lc.language(), Label::LabelFormat::AlwaysWithCountry ); + Label num( lc.lc_numeric, Label::LabelFormat::AlwaysWithCountry ); return std::make_pair< QString, QString >( tr( "The system language will be set to %1." ).arg( lang.label() ), diff --git a/src/modules/welcome/CMakeLists.txt b/src/modules/welcome/CMakeLists.txt index e6ddd2bd7..e25b7f5d0 100644 --- a/src/modules/welcome/CMakeLists.txt +++ b/src/modules/welcome/CMakeLists.txt @@ -27,7 +27,6 @@ calamares_add_plugin( welcome EXPORT_MACRO PLUGINDLLEXPORT_PRO SOURCES ${CHECKER_SOURCES} - LocaleModel.cpp WelcomeViewStep.cpp WelcomePage.cpp UI diff --git a/src/modules/welcome/WelcomePage.cpp b/src/modules/welcome/WelcomePage.cpp index 65a475310..11a17c2f0 100644 --- a/src/modules/welcome/WelcomePage.cpp +++ b/src/modules/welcome/WelcomePage.cpp @@ -21,16 +21,16 @@ #include "WelcomePage.h" #include "ui_WelcomePage.h" -#include "LocaleModel.h" #include "checker/CheckerContainer.h" #include "Branding.h" #include "CalamaresVersion.h" #include "Settings.h" #include "ViewManager.h" + +#include "locale/LabelModel.h" #include "modulesystem/ModuleManager.h" #include "utils/CalamaresUtilsGui.h" -#include "utils/LocaleLabel.h" #include "utils/Logger.h" #include "utils/Retranslator.h" @@ -131,7 +131,7 @@ WelcomePage::initLanguages() ui->languageWidget->clear(); ui->languageWidget->setInsertPolicy( QComboBox::InsertAtBottom ); - m_languages = new LocaleModel( QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';') ); + m_languages = new CalamaresUtils::Locale::LabelModel( QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';') ); ui->languageWidget->setModel( m_languages ); ui->languageWidget->setItemDelegate( new LocaleTwoColumnDelegate( ui->languageWidget ) ); @@ -261,3 +261,11 @@ bool WelcomePage::verdict() const { return m_checkingWidget->verdict(); } + + +void +LocaleTwoColumnDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + QStyledItemDelegate::paint( painter, option, index ); + option.widget->style()->drawItemText( painter, option.rect, Qt::AlignRight | Qt::AlignVCenter, option.palette, false, index.data( CalamaresUtils::Locale::LabelModel::EnglishLabelRole ).toString() ); +} diff --git a/src/modules/welcome/WelcomePage.h b/src/modules/welcome/WelcomePage.h index ec689735b..53a30d95b 100644 --- a/src/modules/welcome/WelcomePage.h +++ b/src/modules/welcome/WelcomePage.h @@ -20,6 +20,9 @@ #ifndef WELCOMEPAGE_H #define WELCOMEPAGE_H +#include "locale/LabelModel.h" + +#include #include namespace Ui @@ -28,7 +31,6 @@ class WelcomePage; } class CheckerContainer; -class LocaleModel; class WelcomePage : public QWidget { @@ -53,7 +55,19 @@ private: Ui::WelcomePage* ui; CheckerContainer* m_checkingWidget; - LocaleModel *m_languages; + CalamaresUtils::Locale::LabelModel *m_languages; }; +/** @brief Delegate to display language information in two columns. + * + * Displays the native language name and the English language name. + */ +class LocaleTwoColumnDelegate : public QStyledItemDelegate +{ +public: + using QStyledItemDelegate::QStyledItemDelegate; + + void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override; +} ; + #endif // WELCOMEPAGE_H diff --git a/src/modules/welcome/WelcomeViewStep.cpp b/src/modules/welcome/WelcomeViewStep.cpp index 5f9bb4657..97b378330 100644 --- a/src/modules/welcome/WelcomeViewStep.cpp +++ b/src/modules/welcome/WelcomeViewStep.cpp @@ -154,7 +154,7 @@ WelcomeViewStep::setCountry( const QString& countryCode ) return; } - auto c_l = Calamares::countryData( countryCode ); + auto c_l = CalamaresUtils::Locale::countryData( countryCode ); if ( c_l.first == QLocale::Country::AnyCountry ) { cDebug() << "Unusable country code" << countryCode;