[libcalamares] Move more into the locale service

- Use namespace CalamaresUtils::Locale consistently for this service.
 - Move locale-related non-GUI support code from the Welcome module
   to libcalamares; these are generally useful. Both Label (naming a locale)
   and LabelModel (managing a bunch of those Labels) have been moved.
This commit is contained in:
Adriaan de Groot 2019-05-10 11:46:20 -04:00
parent b490e30a5e
commit 18ed4c74ef
12 changed files with 93 additions and 75 deletions

View File

@ -31,6 +31,8 @@ set( libSources
geoip/Handler.cpp geoip/Handler.cpp
# Locale-data service # Locale-data service
locale/Label.cpp
locale/LabelModel.cpp
locale/Lookup.cpp locale/Lookup.cpp
# Partition service # Partition service
@ -40,7 +42,6 @@ set( libSources
utils/CalamaresUtilsSystem.cpp utils/CalamaresUtilsSystem.cpp
utils/CommandList.cpp utils/CommandList.cpp
utils/Dirs.cpp utils/Dirs.cpp
utils/LocaleLabel.cpp
utils/Logger.cpp utils/Logger.cpp
utils/PluginFactory.cpp utils/PluginFactory.cpp
utils/Retranslator.cpp utils/Retranslator.cpp

View File

@ -1,7 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org> * Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org> * Copyright 2017-2019, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,12 +17,12 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "LocaleLabel.h" #include "Label.h"
namespace CalamaresUtils namespace CalamaresUtils::Locale
{ {
LocaleLabel::LocaleLabel() Label::Label()
: m_locale( QLocale() ) : m_locale( QLocale() )
{ {
m_localeId = m_locale.name(); m_localeId = m_locale.name();
@ -30,15 +30,15 @@ LocaleLabel::LocaleLabel()
setLabels( QString(), LabelFormat::IfNeededWithCountry ); setLabels( QString(), LabelFormat::IfNeededWithCountry );
} }
LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format ) Label::Label( const QString& locale, LabelFormat format )
: m_locale( LocaleLabel::getLocale( locale ) ) : m_locale( Label::getLocale( locale ) )
, m_localeId( locale ) , m_localeId( locale )
{ {
setLabels( locale, format ); setLabels( locale, format );
} }
void void
LocaleLabel::setLabels( const QString& locale, LabelFormat format ) Label::setLabels( const QString& locale, LabelFormat format )
{ {
//: language[name] (country[name]) //: language[name] (country[name])
QString longFormat = QObject::tr( "%1 (%2)" ); 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; 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" ) ) if ( localeName.contains( "@latin" ) )
{ {

View File

@ -1,7 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org> * Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org> * Copyright 2017-2019, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,13 +17,14 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef UTILS_LOCALELABEL_H #ifndef LOCALE_LABEL_H
#define UTILS_LOCALELABEL_H #define LOCALE_LABEL_H
#include <QLocale> #include <QLocale>
#include <QString> #include <QString>
namespace CalamaresUtils namespace CalamaresUtils {}
namespace CalamaresUtils::Locale
{ {
/** /**
@ -33,14 +34,14 @@ namespace CalamaresUtils
* translation system) into QLocales, and also into consistent * translation system) into QLocales, and also into consistent
* human-readable text labels. * human-readable text labels.
*/ */
class LocaleLabel class Label
{ {
public: public:
/** @brief Formatting option for label -- add (country) to label. */ /** @brief Formatting option for label -- add (country) to label. */
enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ; enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ;
/** @brief Empty locale. This uses the system-default locale. */ /** @brief Empty locale. This uses the system-default locale. */
LocaleLabel(); Label();
/** @brief Construct from a locale name. /** @brief Construct from a locale name.
* *
@ -48,13 +49,13 @@ public:
* The @p format determines whether the country name is always present * The @p format determines whether the country name is always present
* in the label (human-readable form) or only if needed for disambiguation. * 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. /** @brief Define a sorting order.
* *
* English (@see isEnglish() -- it means en_US) is sorted at the top. * 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; return m_localeId < other.m_localeId;
} }

View File

@ -16,30 +16,33 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#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 ) : QAbstractListModel( parent )
{ {
Q_ASSERT( locales.count() > 0 ); Q_ASSERT( locales.count() > 0 );
m_locales.reserve( locales.count() ); m_locales.reserve( locales.count() );
for ( const auto& l : locales ) for ( const auto& l : locales )
m_locales.push_back( CalamaresUtils::LocaleLabel( l ) ); m_locales.push_back( Label( l ) );
} }
LocaleModel::~LocaleModel() LabelModel::~LabelModel()
{ {
} }
int int
LocaleModel::rowCount( const QModelIndex& ) const LabelModel::rowCount( const QModelIndex& ) const
{ {
return m_locales.count(); return m_locales.count();
} }
QVariant QVariant
LocaleModel::data( const QModelIndex& index, int role ) const LabelModel::data( const QModelIndex& index, int role ) const
{ {
if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) ) if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) )
return QVariant(); return QVariant();
@ -59,8 +62,8 @@ LocaleModel::data( const QModelIndex& index, int role ) const
} }
} }
const CalamaresUtils::LocaleLabel& const Label&
LocaleModel::locale( int row ) LabelModel::locale( int row )
{ {
if ( ( row < 0 ) || ( row >= m_locales.count() ) ) if ( ( row < 0 ) || ( row >= m_locales.count() ) )
{ {
@ -73,7 +76,7 @@ LocaleModel::locale( int row )
} }
int int
LocaleModel::find( std::function<bool ( const LocaleLabel& )> predicate ) const LabelModel::find( std::function<bool ( const Label& )> predicate ) const
{ {
for ( int row = 0; row < m_locales.count() ; ++row ) for ( int row = 0; row < m_locales.count() ; ++row )
{ {
@ -84,26 +87,21 @@ LocaleModel::find( std::function<bool ( const LocaleLabel& )> predicate ) const
} }
int int
LocaleModel::find( std::function<bool ( const QLocale& )> predicate ) const LabelModel::find( std::function<bool ( const QLocale& )> predicate ) const
{ {
return find( [&]( const LocaleLabel& l ) return find( [&]( const Label& l )
{ {
return predicate( l.locale() ); return predicate( l.locale() );
} ); } );
} }
int 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(); return locale == l.locale();
} ); } );
} }
void } // namespace
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() );
}

View File

@ -16,28 +16,31 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef WELCOME_LOCALEMODEL_H #ifndef LOCALE_LABELMODEL_H
#define WELCOME_LOCALEMODEL_H #define LOCALE_LABELMODEL_H
#include "DllMacro.h"
#include "Label.h"
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QStyledItemDelegate>
#include <QVector> #include <QVector>
#include "utils/LocaleLabel.h"
class LocaleModel : public QAbstractListModel namespace CalamaresUtils {}
namespace CalamaresUtils::Locale
{
DLLEXPORT class LabelModel : public QAbstractListModel
{ {
public: public:
using LocaleLabel = CalamaresUtils::LocaleLabel;
enum enum
{ {
LabelRole = Qt::DisplayRole, LabelRole = Qt::DisplayRole,
EnglishLabelRole = Qt::UserRole + 1 EnglishLabelRole = Qt::UserRole + 1
}; };
LocaleModel( const QStringList& locales, QObject* parent = nullptr ); LabelModel( const QStringList& locales, QObject* parent = nullptr );
virtual ~LocaleModel() override; virtual ~LabelModel() override;
int rowCount( const QModelIndex& parent ) const 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, * This is the backing data for the model; if @p row is out-of-range,
* returns a reference to en_US. * 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 /** @brief Searches for an item that matches @p predicate
* *
* Returns the row number of the first match, or -1 if there isn't one. * Returns the row number of the first match, or -1 if there isn't one.
*/ */
int find( std::function<bool( const QLocale& )> predicate ) const; int find( std::function<bool( const QLocale& )> predicate ) const;
int find( std::function<bool( const LocaleLabel& )> predicate ) const; int find( std::function<bool( const Label& )> predicate ) const;
int find( const QLocale& ) const; int find( const QLocale& ) const;
private: private:
QVector< LocaleLabel > m_locales; QVector< Label > m_locales;
} ;
class LocaleTwoColumnDelegate : public QStyledItemDelegate
{
public:
using QStyledItemDelegate::QStyledItemDelegate;
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override;
} ; } ;
} // namespace
#endif #endif

View File

@ -20,7 +20,7 @@
#include "CountryData_p.cpp" #include "CountryData_p.cpp"
namespace Calamares namespace CalamaresUtils::Locale
{ {
struct TwoChar struct TwoChar

View File

@ -24,7 +24,8 @@
#include <QLocale> #include <QLocale>
#include <QPair> #include <QPair>
namespace Calamares namespace CalamaresUtils {}
namespace CalamaresUtils::Locale
{ {
/* All the functions in this file do lookups of locale data /* All the functions in this file do lookups of locale data
* based on CLDR tables; these are lookups that you can't (easily) * based on CLDR tables; these are lookups that you can't (easily)

View File

@ -27,8 +27,8 @@
#include "LCLocaleDialog.h" #include "LCLocaleDialog.h"
#include "Settings.h" #include "Settings.h"
#include "locale/Label.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include "utils/LocaleLabel.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/Retranslator.h" #include "utils/Retranslator.h"
@ -387,10 +387,10 @@ LocalePage::init( const QString& initialRegion,
std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfiguration& lc ) const std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfiguration& lc ) const
{ {
using CalamaresUtils::LocaleLabel; using CalamaresUtils::Locale::Label;
LocaleLabel lang( lc.language(), LocaleLabel::LabelFormat::AlwaysWithCountry ); Label lang( lc.language(), Label::LabelFormat::AlwaysWithCountry );
LocaleLabel num( lc.lc_numeric, LocaleLabel::LabelFormat::AlwaysWithCountry ); Label num( lc.lc_numeric, Label::LabelFormat::AlwaysWithCountry );
return std::make_pair< QString, QString >( return std::make_pair< QString, QString >(
tr( "The system language will be set to %1." ).arg( lang.label() ), tr( "The system language will be set to %1." ).arg( lang.label() ),

View File

@ -27,7 +27,6 @@ calamares_add_plugin( welcome
EXPORT_MACRO PLUGINDLLEXPORT_PRO EXPORT_MACRO PLUGINDLLEXPORT_PRO
SOURCES SOURCES
${CHECKER_SOURCES} ${CHECKER_SOURCES}
LocaleModel.cpp
WelcomeViewStep.cpp WelcomeViewStep.cpp
WelcomePage.cpp WelcomePage.cpp
UI UI

View File

@ -21,16 +21,16 @@
#include "WelcomePage.h" #include "WelcomePage.h"
#include "ui_WelcomePage.h" #include "ui_WelcomePage.h"
#include "LocaleModel.h"
#include "checker/CheckerContainer.h" #include "checker/CheckerContainer.h"
#include "Branding.h" #include "Branding.h"
#include "CalamaresVersion.h" #include "CalamaresVersion.h"
#include "Settings.h" #include "Settings.h"
#include "ViewManager.h" #include "ViewManager.h"
#include "locale/LabelModel.h"
#include "modulesystem/ModuleManager.h" #include "modulesystem/ModuleManager.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include "utils/LocaleLabel.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/Retranslator.h" #include "utils/Retranslator.h"
@ -131,7 +131,7 @@ WelcomePage::initLanguages()
ui->languageWidget->clear(); ui->languageWidget->clear();
ui->languageWidget->setInsertPolicy( QComboBox::InsertAtBottom ); 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->setModel( m_languages );
ui->languageWidget->setItemDelegate( new LocaleTwoColumnDelegate( ui->languageWidget ) ); ui->languageWidget->setItemDelegate( new LocaleTwoColumnDelegate( ui->languageWidget ) );
@ -261,3 +261,11 @@ bool WelcomePage::verdict() const
{ {
return m_checkingWidget->verdict(); 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() );
}

View File

@ -20,6 +20,9 @@
#ifndef WELCOMEPAGE_H #ifndef WELCOMEPAGE_H
#define WELCOMEPAGE_H #define WELCOMEPAGE_H
#include "locale/LabelModel.h"
#include <QStyledItemDelegate>
#include <QWidget> #include <QWidget>
namespace Ui namespace Ui
@ -28,7 +31,6 @@ class WelcomePage;
} }
class CheckerContainer; class CheckerContainer;
class LocaleModel;
class WelcomePage : public QWidget class WelcomePage : public QWidget
{ {
@ -53,7 +55,19 @@ private:
Ui::WelcomePage* ui; Ui::WelcomePage* ui;
CheckerContainer* m_checkingWidget; 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 #endif // WELCOMEPAGE_H

View File

@ -154,7 +154,7 @@ WelcomeViewStep::setCountry( const QString& countryCode )
return; return;
} }
auto c_l = Calamares::countryData( countryCode ); auto c_l = CalamaresUtils::Locale::countryData( countryCode );
if ( c_l.first == QLocale::Country::AnyCountry ) if ( c_l.first == QLocale::Country::AnyCountry )
{ {
cDebug() << "Unusable country code" << countryCode; cDebug() << "Unusable country code" << countryCode;