[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 void
LocaleTests::testRegions() LocaleTests::testRegions()
{ {
CalamaresUtils::Locale::RegionsModel regions; using namespace CalamaresUtils::Locale;
RegionsModel regions;
QVERIFY( regions.rowCount( QModelIndex() ) > 3 ); // Africa, America, Asia QVERIFY( regions.rowCount( QModelIndex() ) > 3 ); // Africa, America, Asia
QStringList names; QStringList names;
for ( int i = 0; i < regions.rowCount( QModelIndex() ); ++i ) 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.isValid() );
QVERIFY( !name.toString().isEmpty() ); QVERIFY( !name.toString().isEmpty() );
names.append( name.toString() ); names.append( name.toString() );
@ -269,9 +270,34 @@ LocaleTests::testRegions()
void void
LocaleTests::testSimpleZones() LocaleTests::testSimpleZones()
{ {
CalamaresUtils::Locale::ZonesModel zones; using namespace CalamaresUtils::Locale;
ZonesModel zones;
QVERIFY( zones.rowCount( QModelIndex() ) > 24 ); 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 void

View File

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

View File

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