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 )
, m_locale( Label::getLocale( locale ) )
, m_localeId( locale.isEmpty() ? m_locale.name() : locale )
{
//: language[name] (country[name])
QString longFormat = QObject::tr( "%1 (%2)" );
QString languageName = m_locale.nativeLanguageName();

View File

@ -40,10 +40,6 @@ class Label : public QObject
{
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:
/** @brief Formatting option for label -- add (country) to label. */
enum class LabelFormat
@ -65,6 +61,7 @@ public:
LabelFormat format = LabelFormat::IfNeededWithCountry,
QObject* parent = nullptr );
/** @brief Define a sorting order.
*
* 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
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();
}
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*
CStringListModel::item( int index ) const
{

View File

@ -44,8 +44,9 @@ namespace Locale
* QPair<QString, QString> because there is API that needs
* C-style strings.
*/
class CStringPair
class CStringPair : public QObject
{
Q_OBJECT
public:
/// @brief An empty pair
CStringPair() {}
@ -86,6 +87,7 @@ public:
/// @brief A pair of strings for timezone regions (e.g. "America")
class TZRegion : public CStringPair
{
Q_OBJECT
public:
using CStringPair::CStringPair;
virtual ~TZRegion() override;
@ -117,6 +119,7 @@ private:
/// @brief A pair of strings for specific timezone names (e.g. "New_York")
class TZZone : public CStringPair
{
Q_OBJECT
public:
using CStringPair::CStringPair;
QString tr() const override;
@ -137,21 +140,47 @@ protected:
class CStringListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
public:
/// @brief Create empty model
CStringListModel();
CStringListModel() {};
/// @brief Create model from list (non-owning)
CStringListModel( CStringPairList );
virtual ~CStringListModel() override;
int rowCount( const QModelIndex& parent ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
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:
CStringPairList m_list;
int m_currentIndex = -1;
signals:
void currentIndexChanged();
};
} // namespace Locale