make label item from LabelModel qobject based and expose properties
This commit is contained in:
parent
340ffd070c
commit
1b3d32ca79
@ -24,17 +24,19 @@ namespace CalamaresUtils
|
||||
namespace Locale
|
||||
{
|
||||
|
||||
Label::Label()
|
||||
: m_locale( QLocale() )
|
||||
Label::Label( QObject* parent )
|
||||
: QObject( parent )
|
||||
, m_locale( QLocale() )
|
||||
{
|
||||
m_localeId = m_locale.name();
|
||||
|
||||
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_localeId( locale )
|
||||
, QObject( parent )
|
||||
{
|
||||
setLabels( locale, format );
|
||||
}
|
||||
@ -42,6 +44,7 @@ Label::Label( const QString& locale, LabelFormat format )
|
||||
void
|
||||
Label::setLabels( const QString& locale, LabelFormat format )
|
||||
{
|
||||
emit localeIdChanged(m_localeId);
|
||||
//: language[name] (country[name])
|
||||
QString longFormat = QObject::tr( "%1 (%2)" );
|
||||
|
||||
@ -62,8 +65,10 @@ Label::setLabels( const QString& locale, LabelFormat format )
|
||||
countryName = m_locale.nativeCountryName();
|
||||
}
|
||||
m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName;
|
||||
emit labelChanged(m_label);
|
||||
m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
|
||||
: englishName;
|
||||
emit englishLabelChanged(m_englishLabel);
|
||||
}
|
||||
|
||||
QLocale
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
#include <QLocale>
|
||||
#include <QString>
|
||||
#include <QObject>
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
@ -35,8 +36,14 @@ namespace Locale
|
||||
* translation system) into QLocales, and also into consistent
|
||||
* 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:
|
||||
/** @brief Formatting option for label -- add (country) to label. */
|
||||
enum class LabelFormat
|
||||
@ -46,7 +53,7 @@ public:
|
||||
};
|
||||
|
||||
/** @brief Empty locale. This uses the system-default locale. */
|
||||
Label();
|
||||
Label(QObject* parent = nullptr);
|
||||
|
||||
/** @brief Construct from a locale name.
|
||||
*
|
||||
@ -54,7 +61,7 @@ public:
|
||||
* The @p format determines whether the country name is always present
|
||||
* 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.
|
||||
*
|
||||
@ -100,6 +107,11 @@ protected:
|
||||
QString m_localeId; // the locale identifier, e.g. "en_GB"
|
||||
QString m_label; // the native name of the locale
|
||||
QString m_englishLabel;
|
||||
|
||||
signals:
|
||||
void labelChanged( QString label );
|
||||
void englishLabelChanged( QString englishLabel );
|
||||
void localeIdChanged( QString localeIdChanged );
|
||||
};
|
||||
|
||||
} // namespace Locale
|
||||
|
@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "LabelModel.h"
|
||||
#include <QDebug>
|
||||
|
||||
#include "Lookup.h"
|
||||
|
||||
@ -36,7 +37,7 @@ LabelModel::LabelModel( const QStringList& locales, QObject* parent )
|
||||
|
||||
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 )
|
||||
{
|
||||
case LabelRole:
|
||||
return locale.label();
|
||||
return locale->label();
|
||||
case EnglishLabelRole:
|
||||
return locale.englishLabel();
|
||||
return locale->englishLabel();
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
@ -79,13 +80,13 @@ LabelModel::locale( int row ) const
|
||||
if ( ( row < 0 ) || ( row >= m_locales.count() ) )
|
||||
{
|
||||
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
|
||||
@ -93,7 +94,7 @@ LabelModel::find( std::function< bool( const Label& ) > predicate ) const
|
||||
{
|
||||
for ( int row = 0; row < m_locales.count(); ++row )
|
||||
{
|
||||
if ( predicate( m_locales[ row ] ) )
|
||||
if ( predicate( *m_locales[ row ] ) )
|
||||
{
|
||||
return row;
|
||||
}
|
||||
|
@ -33,6 +33,8 @@ namespace Locale
|
||||
|
||||
class DLLEXPORT LabelModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum
|
||||
{
|
||||
@ -69,7 +71,7 @@ public:
|
||||
int find( const QString& countryCode ) const;
|
||||
|
||||
private:
|
||||
QVector< Label > m_locales;
|
||||
QVector< Label* > m_locales;
|
||||
QStringList m_localeIds;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user