mirror of https://github.com/cutefishos/calamares
commit
c44e221fb6
@ -0,0 +1,73 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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, countryName ) : languageName;
|
||||
m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) ) : 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
|
@ -0,0 +1,113 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBCALAMARES_LOCALELABEL_H
|
||||
#define LIBCALAMARES_LOCALELABEL_H
|
||||
|
||||
#include <QLocale>
|
||||
#include <QString>
|
||||
|
||||
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
|
@ -0,0 +1,109 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "LocaleModel.h"
|
||||
|
||||
LocaleModel::LocaleModel( 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 ) );
|
||||
}
|
||||
|
||||
LocaleModel::~LocaleModel()
|
||||
{
|
||||
}
|
||||
|
||||
int
|
||||
LocaleModel::rowCount( const QModelIndex& ) const
|
||||
{
|
||||
return m_locales.count();
|
||||
}
|
||||
|
||||
QVariant
|
||||
LocaleModel::data( const QModelIndex& index, int role ) const
|
||||
{
|
||||
if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) )
|
||||
return QVariant();
|
||||
|
||||
if ( !index.isValid() )
|
||||
return QVariant();
|
||||
|
||||
const auto& locale = m_locales.at( index.row() );
|
||||
switch ( role )
|
||||
{
|
||||
case LabelRole:
|
||||
return locale.label();
|
||||
case EnglishLabelRole:
|
||||
return locale.englishLabel();
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
const CalamaresUtils::LocaleLabel&
|
||||
LocaleModel::locale( int row )
|
||||
{
|
||||
if ( ( row < 0 ) || ( row >= m_locales.count() ) )
|
||||
{
|
||||
for ( const auto& l : m_locales )
|
||||
if ( l.isEnglish() )
|
||||
return l;
|
||||
return m_locales[0];
|
||||
}
|
||||
return m_locales[row];
|
||||
}
|
||||
|
||||
int
|
||||
LocaleModel::find( std::function<bool ( const LocaleLabel& )> predicate ) const
|
||||
{
|
||||
for ( int row = 0; row < m_locales.count() ; ++row )
|
||||
{
|
||||
if ( predicate( m_locales[row] ) )
|
||||
return row;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
LocaleModel::find( std::function<bool ( const QLocale& )> predicate ) const
|
||||
{
|
||||
return find( [&]( const LocaleLabel& l )
|
||||
{
|
||||
return predicate( l.locale() );
|
||||
} );
|
||||
}
|
||||
|
||||
int
|
||||
LocaleModel::find( const QLocale& locale ) const
|
||||
{
|
||||
return find( [&]( const LocaleLabel& 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() );
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef WELCOME_LOCALEMODEL_H
|
||||
#define WELCOME_LOCALEMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QVector>
|
||||
|
||||
#include "utils/LocaleLabel.h"
|
||||
|
||||
class LocaleModel : 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;
|
||||
|
||||
int rowCount( const QModelIndex& parent ) const override;
|
||||
|
||||
QVariant data( const QModelIndex& index, int role ) const override;
|
||||
|
||||
/** @brief Gets locale information for entry #n
|
||||
*
|
||||
* 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 );
|
||||
|
||||
/** @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<bool( const QLocale& )> predicate ) const;
|
||||
int find( std::function<bool( const LocaleLabel& )> 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;
|
||||
} ;
|
||||
|
||||
#endif
|
Binary file not shown.
After Width: | Height: | Size: 4.5 KiB |
Binary file not shown.
After Width: | Height: | Size: 2.3 KiB |
@ -0,0 +1,6 @@
|
||||
<RCC>
|
||||
<qresource prefix="welcome">
|
||||
<file>language-icon-128px.png</file>
|
||||
<file>language-icon-48px.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
Loading…
Reference in New Issue