some models need to be updated to work with locale qml view step

This commit is contained in:
Camilo Higuita 2020-03-24 10:04:14 -05:00
parent 8ff1996e12
commit e02077d58e
4 changed files with 61 additions and 10 deletions

View File

@ -33,9 +33,7 @@ Label::Label( const QString& locale, LabelFormat format, QObject* parent )
: QObject( parent ) : QObject( parent )
, m_locale( Label::getLocale( locale ) ) , m_locale( Label::getLocale( locale ) )
, m_localeId( locale.isEmpty() ? m_locale.name() : locale ) , m_localeId( locale.isEmpty() ? m_locale.name() : locale )
{ {
//: language[name] (country[name])
QString longFormat = QObject::tr( "%1 (%2)" ); QString longFormat = QObject::tr( "%1 (%2)" );
QString languageName = m_locale.nativeLanguageName(); QString languageName = m_locale.nativeLanguageName();

View File

@ -40,10 +40,6 @@ class Label : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY( QString label READ label CONSTANT FINAL )
Q_PROPERTY( QString englishLabel READ englishLabel CONSTANT FINAL )
Q_PROPERTY( QString localeId MEMBER m_localeId 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
@ -65,6 +61,7 @@ public:
LabelFormat format = LabelFormat::IfNeededWithCountry, LabelFormat format = LabelFormat::IfNeededWithCountry,
QObject* parent = nullptr ); QObject* parent = nullptr );
/** @brief Define a sorting order. /** @brief Define a sorting order.
* *
* Locales are sorted by their id, which means the ISO 2-letter code + country. * Locales are sorted by their id, which means the ISO 2-letter code + country.

View File

@ -239,7 +239,12 @@ CStringListModel::CStringListModel( CStringPairList l )
{ {
} }
CStringListModel::~CStringListModel() {} void CStringListModel::setList(CalamaresUtils::Locale::CStringPairList l)
{
beginResetModel();
m_list = l;
endResetModel();
}
int int
CStringListModel::rowCount( const QModelIndex& ) const CStringListModel::rowCount( const QModelIndex& ) const
@ -264,6 +269,28 @@ CStringListModel::data( const QModelIndex& index, int role ) const
return item ? ( role == Qt::DisplayRole ? item->tr() : item->key() ) : QVariant(); return item ? ( role == Qt::DisplayRole ? item->tr() : item->key() ) : QVariant();
} }
void
CStringListModel::setCurrentIndex(const int &index)
{
if ( ( index < 0 ) || ( index >= m_list.count() ) )
return;
m_currentIndex = index;
emit currentIndexChanged();
}
int
CStringListModel::currentIndex() const
{
return m_currentIndex;
}
QHash<int, QByteArray>
CStringListModel::roleNames() const
{
return {{Qt::DisplayRole,"label"}, {Qt::UserRole, "key"}};
}
const CStringPair* const CStringPair*
CStringListModel::item( int index ) const CStringListModel::item( int index ) const
{ {

View File

@ -44,8 +44,9 @@ namespace Locale
* QPair<QString, QString> because there is API that needs * QPair<QString, QString> because there is API that needs
* C-style strings. * C-style strings.
*/ */
class CStringPair class CStringPair : public QObject
{ {
Q_OBJECT
public: public:
/// @brief An empty pair /// @brief An empty pair
CStringPair() {} CStringPair() {}
@ -86,6 +87,7 @@ public:
/// @brief A pair of strings for timezone regions (e.g. "America") /// @brief A pair of strings for timezone regions (e.g. "America")
class TZRegion : public CStringPair class TZRegion : public CStringPair
{ {
Q_OBJECT
public: public:
using CStringPair::CStringPair; using CStringPair::CStringPair;
virtual ~TZRegion() override; virtual ~TZRegion() override;
@ -117,6 +119,7 @@ private:
/// @brief A pair of strings for specific timezone names (e.g. "New_York") /// @brief A pair of strings for specific timezone names (e.g. "New_York")
class TZZone : public CStringPair class TZZone : public CStringPair
{ {
Q_OBJECT
public: public:
using CStringPair::CStringPair; using CStringPair::CStringPair;
QString tr() const override; QString tr() const override;
@ -137,21 +140,47 @@ protected:
class CStringListModel : public QAbstractListModel class CStringListModel : public QAbstractListModel
{ {
Q_OBJECT
Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
public: public:
/// @brief Create empty model /// @brief Create empty model
CStringListModel(); CStringListModel() {};
/// @brief Create model from list (non-owning) /// @brief Create model from list (non-owning)
CStringListModel( CStringPairList ); CStringListModel( CStringPairList );
virtual ~CStringListModel() override;
int rowCount( const QModelIndex& parent ) const override; int rowCount( const QModelIndex& parent ) const override;
QVariant data( const QModelIndex& index, int role ) const override; QVariant data( const QModelIndex& index, int role ) const override;
const CStringPair* item( int index ) const; const CStringPair* item( int index ) const;
QHash<int, QByteArray> roleNames() const override;
void setCurrentIndex(const int &index);
int currentIndex() const;
void setList(CStringPairList);
inline int indexOf(const QString &key)
{
const auto it = std::find_if(m_list.constBegin(), m_list.constEnd(), [&](const CalamaresUtils::Locale::CStringPair *item) -> bool
{
return item->key() == key;
});
if(it != m_list.constEnd())
return std::distance(m_list.constBegin(), it);
else return -1;
}
private: private:
CStringPairList m_list; CStringPairList m_list;
int m_currentIndex = -1;
signals:
void currentIndexChanged();
}; };
} // namespace Locale } // namespace Locale