diff --git a/src/modules/welcome/LocaleModel.cpp b/src/modules/welcome/LocaleModel.cpp index 67e306a3a..d2517a461 100644 --- a/src/modules/welcome/LocaleModel.cpp +++ b/src/modules/welcome/LocaleModel.cpp @@ -21,10 +21,11 @@ LocaleModel::LocaleModel(const QStringList& locales, QObject* parent) : QAbstractTableModel( parent ) { + Q_ASSERT( locales.count() > 0 ); m_locales.reserve( locales.count() ); for ( const auto& l : locales ) - m_locales.emplace_back( l ); + m_locales.push_back( CalamaresUtils::LocaleLabel( l ) ); } LocaleModel::~LocaleModel() @@ -40,7 +41,7 @@ LocaleModel::columnCount( const QModelIndex& ) const int LocaleModel::rowCount( const QModelIndex& ) const { - return m_locales.size(); + return m_locales.count(); } QVariant @@ -63,3 +64,33 @@ LocaleModel::data( const QModelIndex& index, int role ) const 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 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 predicate) const +{ + return find( [&]( const LocaleLabel& l ){ return predicate( l.locale() ); } ); +} diff --git a/src/modules/welcome/LocaleModel.h b/src/modules/welcome/LocaleModel.h index 9e622a43f..68cede172 100644 --- a/src/modules/welcome/LocaleModel.h +++ b/src/modules/welcome/LocaleModel.h @@ -20,24 +20,40 @@ #define WELCOME_LOCALEMODEL_H #include +#include -#include #include "utils/CalamaresUtilsGui.h" class LocaleModel : public QAbstractTableModel { public: + using LocaleLabel = CalamaresUtils::LocaleLabel; + LocaleModel( const QStringList& locales, QObject* parent = nullptr ); - virtual ~LocaleModel(); + virtual ~LocaleModel() override; int rowCount( const QModelIndex& parent ) const override; int columnCount( 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 predicate) const; + int find( std::function predicate) const; + private: - std::vector< CalamaresUtils::LocaleLabel > m_locales; + QVector< LocaleLabel > m_locales; } ; #endif