[libcalamares] Give zones data, too

- while here, fix bug in TimeZoneData that didn't munge names
  (so it reported "New_York")
This commit is contained in:
Adriaan de Groot 2020-08-05 16:45:41 +02:00
parent 33e39b92fb
commit 1afdcc9c82
3 changed files with 64 additions and 8 deletions

View File

@ -248,14 +248,15 @@ LocaleTests::testTranslatableConfig2()
void
LocaleTests::testRegions()
{
CalamaresUtils::Locale::RegionsModel regions;
using namespace CalamaresUtils::Locale;
RegionsModel regions;
QVERIFY( regions.rowCount( QModelIndex() ) > 3 ); // Africa, America, Asia
QStringList names;
for ( int i = 0; i < regions.rowCount( QModelIndex() ); ++i )
{
QVariant name = regions.data( regions.index( i ), Qt::DisplayRole );
QVariant name = regions.data( regions.index( i ), RegionsModel::NameRole );
QVERIFY( name.isValid() );
QVERIFY( !name.toString().isEmpty() );
names.append( name.toString() );
@ -269,9 +270,34 @@ LocaleTests::testRegions()
void
LocaleTests::testSimpleZones()
{
CalamaresUtils::Locale::ZonesModel zones;
using namespace CalamaresUtils::Locale;
ZonesModel zones;
QVERIFY( zones.rowCount( QModelIndex() ) > 24 );
QStringList names;
for ( int i = 0; i < zones.rowCount( QModelIndex() ); ++i )
{
QVariant name = zones.data( zones.index( i ), ZonesModel::NameRole );
QVERIFY( name.isValid() );
QVERIFY( !name.toString().isEmpty() );
names.append( name.toString() );
}
QVERIFY( names.contains( "Amsterdam" ) );
if ( !names.contains( "New York" ) )
{
for ( const auto& s : names )
{
if ( s.startsWith( 'N' ) )
{
cDebug() << s;
}
}
}
QVERIFY( names.contains( "New York" ) );
QVERIFY( !names.contains( "America" ) );
QVERIFY( !names.contains( "New_York" ) );
}
void

View File

@ -148,7 +148,7 @@ CStringPair::CStringPair( const char* s1 )
}
CStringPair::CStringPair( const QString& s )
: m_human( strdup( s.toUtf8().constData() ) )
: m_human( munge( s.toUtf8().constData() ) )
, m_key( s )
{
}
@ -331,13 +331,14 @@ RegionsModel::data( const QModelIndex& index, int role ) const
return QVariant();
}
if ( role == Qt::DisplayRole )
const auto& region = m_private->m_regions[ index.row() ];
if ( role == NameRole )
{
return m_private->m_regions[ index.row() ].tr();
return region.tr();
}
if ( role == KeyRole )
{
return m_private->m_regions[ index.row() ].key();
return region.key();
}
return QVariant();
}
@ -345,7 +346,7 @@ RegionsModel::data( const QModelIndex& index, int role ) const
QHash< int, QByteArray >
RegionsModel::roleNames() const
{
return { { Qt::DisplayRole, "name" }, { KeyRole, "key" } };
return { { NameRole, "name" }, { KeyRole, "key" } };
}
@ -365,10 +366,30 @@ ZonesModel::rowCount( const QModelIndex& parent ) const
QVariant
ZonesModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() || index.row() < 0 || index.row() >= m_private->m_zones.count() )
{
return QVariant();
}
const auto& zone = m_private->m_zones[ index.row() ];
if ( role == NameRole )
{
return zone.tr();
}
if ( role == KeyRole )
{
return zone.key();
}
return QVariant();
}
QHash< int, QByteArray >
ZonesModel::roleNames() const
{
return { { NameRole, "name" }, { KeyRole, "key" } };
}
} // namespace Locale
} // namespace CalamaresUtils

View File

@ -46,6 +46,7 @@ class DLLEXPORT RegionsModel : public QAbstractListModel
public:
enum Roles
{
NameRole = Qt::DisplayRole,
KeyRole = Qt::UserRole + 1
};
@ -66,12 +67,20 @@ class DLLEXPORT ZonesModel : public QAbstractListModel
Q_OBJECT
public:
enum Roles
{
NameRole = Qt::DisplayRole,
KeyRole = Qt::UserRole + 1
};
ZonesModel( QObject* parent = nullptr );
virtual ~ZonesModel() override;
int rowCount( const QModelIndex& parent ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
QHash< int, QByteArray > roleNames() const override;
private:
Private* m_private;
};