make label item from LabelModel qobject based and expose properties

This commit is contained in:
Camilo Higuita 2019-12-12 10:41:37 -05:00 committed by Adriaan de Groot
parent 340ffd070c
commit 1b3d32ca79
4 changed files with 35 additions and 15 deletions

View File

@ -24,17 +24,19 @@ namespace CalamaresUtils
namespace Locale namespace Locale
{ {
Label::Label() Label::Label( QObject* parent )
: m_locale( QLocale() ) : QObject( parent )
, m_locale( QLocale() )
{ {
m_localeId = m_locale.name(); m_localeId = m_locale.name();
setLabels( QString(), LabelFormat::IfNeededWithCountry ); setLabels( QString(), LabelFormat::IfNeededWithCountry );
} }
Label::Label( const QString& locale, LabelFormat format ) Label::Label( const QString& locale, LabelFormat format, QObject* parent )
: m_locale( Label::getLocale( locale ) ) : m_locale( Label::getLocale( locale ) )
, m_localeId( locale ) , m_localeId( locale )
, QObject( parent )
{ {
setLabels( locale, format ); setLabels( locale, format );
} }
@ -42,6 +44,7 @@ Label::Label( const QString& locale, LabelFormat format )
void void
Label::setLabels( const QString& locale, LabelFormat format ) Label::setLabels( const QString& locale, LabelFormat format )
{ {
emit localeIdChanged(m_localeId);
//: language[name] (country[name]) //: language[name] (country[name])
QString longFormat = QObject::tr( "%1 (%2)" ); QString longFormat = QObject::tr( "%1 (%2)" );
@ -62,8 +65,10 @@ Label::setLabels( const QString& locale, LabelFormat format )
countryName = m_locale.nativeCountryName(); countryName = m_locale.nativeCountryName();
} }
m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName; m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName;
emit labelChanged(m_label);
m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) ) m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
: englishName; : englishName;
emit englishLabelChanged(m_englishLabel);
} }
QLocale QLocale

View File

@ -22,6 +22,7 @@
#include <QLocale> #include <QLocale>
#include <QString> #include <QString>
#include <QObject>
namespace CalamaresUtils namespace CalamaresUtils
{ {
@ -35,8 +36,14 @@ namespace Locale
* 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 Label class Label : public QObject
{ {
Q_OBJECT
Q_PROPERTY(QString label READ label NOTIFY labelChanged CONSTANT FINAL)
Q_PROPERTY(QString englishLabel READ englishLabel NOTIFY englishLabelChanged CONSTANT FINAL)
Q_PROPERTY(QString localeId MEMBER m_localeId NOTIFY localeIdChanged CONSTANT FINAL)
public: public:
/** @brief Formatting option for label -- add (country) to label. */ /** @brief Formatting option for label -- add (country) to label. */
enum class LabelFormat enum class LabelFormat
@ -46,7 +53,7 @@ public:
}; };
/** @brief Empty locale. This uses the system-default locale. */ /** @brief Empty locale. This uses the system-default locale. */
Label(); Label(QObject* parent = nullptr);
/** @brief Construct from a locale name. /** @brief Construct from a locale name.
* *
@ -54,7 +61,7 @@ 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.
*/ */
Label( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry ); Label( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry, QObject* parent = nullptr );
/** @brief Define a sorting order. /** @brief Define a sorting order.
* *
@ -100,6 +107,11 @@ protected:
QString m_localeId; // the locale identifier, e.g. "en_GB" QString m_localeId; // the locale identifier, e.g. "en_GB"
QString m_label; // the native name of the locale QString m_label; // the native name of the locale
QString m_englishLabel; QString m_englishLabel;
signals:
void labelChanged( QString label );
void englishLabelChanged( QString englishLabel );
void localeIdChanged( QString localeIdChanged );
}; };
} // namespace Locale } // namespace Locale

View File

@ -17,6 +17,7 @@
*/ */
#include "LabelModel.h" #include "LabelModel.h"
#include <QDebug>
#include "Lookup.h" #include "Lookup.h"
@ -36,7 +37,7 @@ LabelModel::LabelModel( const QStringList& locales, QObject* parent )
for ( const auto& l : locales ) for ( const auto& l : locales )
{ {
m_locales.push_back( Label( l ) ); m_locales.push_back( new Label( l, Label::LabelFormat::IfNeededWithCountry, this ) );
} }
} }
@ -65,9 +66,9 @@ LabelModel::data( const QModelIndex& index, int role ) const
switch ( role ) switch ( role )
{ {
case LabelRole: case LabelRole:
return locale.label(); return locale->label();
case EnglishLabelRole: case EnglishLabelRole:
return locale.englishLabel(); return locale->englishLabel();
default: default:
return QVariant(); return QVariant();
} }
@ -79,13 +80,13 @@ LabelModel::locale( int row ) const
if ( ( row < 0 ) || ( row >= m_locales.count() ) ) if ( ( row < 0 ) || ( row >= m_locales.count() ) )
{ {
for ( const auto& l : m_locales ) for ( const auto& l : m_locales )
if ( l.isEnglish() ) if ( l->isEnglish() )
{ {
return l; return *l;
} }
return m_locales[ 0 ]; return *m_locales[ 0 ];
} }
return m_locales[ row ]; return *m_locales[ row ];
} }
int int
@ -93,7 +94,7 @@ 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 )
{ {
if ( predicate( m_locales[ row ] ) ) if ( predicate( *m_locales[ row ] ) )
{ {
return row; return row;
} }

View File

@ -33,6 +33,8 @@ namespace Locale
class DLLEXPORT LabelModel : public QAbstractListModel class DLLEXPORT LabelModel : public QAbstractListModel
{ {
Q_OBJECT
public: public:
enum enum
{ {
@ -69,7 +71,7 @@ public:
int find( const QString& countryCode ) const; int find( const QString& countryCode ) const;
private: private:
QVector< Label > m_locales; QVector< Label* > m_locales;
QStringList m_localeIds; QStringList m_localeIds;
}; };