mirror of
https://github.com/parchlinux/calamares.git
synced 2025-02-24 10:55:46 -05:00
[welcome] Switch to QVector
- QVector is a better match with passing in QStringList, otherwise you end up dealing with Qt's int indexes vs. std::vector's uint indexes everywhere. - Introduce find()
This commit is contained in:
parent
bd0af4bb77
commit
314aee8d68
2 changed files with 52 additions and 5 deletions
|
@ -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<bool (const LocaleLabel &)> 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<bool (const QLocale &)> predicate) const
|
||||
{
|
||||
return find( [&]( const LocaleLabel& l ){ return predicate( l.locale() ); } );
|
||||
}
|
||||
|
|
|
@ -20,24 +20,40 @@
|
|||
#define WELCOME_LOCALEMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QVector>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#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<bool(const QLocale&)> predicate) const;
|
||||
int find( std::function<bool(const LocaleLabel&)> predicate) const;
|
||||
|
||||
private:
|
||||
std::vector< CalamaresUtils::LocaleLabel > m_locales;
|
||||
QVector< LocaleLabel > m_locales;
|
||||
} ;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue