diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index e41e3a30b..c660ccbfb 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -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 diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index 9f356d958..05c082738 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -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" } }; } @@ -366,9 +367,29 @@ 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 diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index 0a5fbf7f6..d87a5a57a 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -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; };