From 37c211fd14d8a6a35d02296e774be7da2e9c42be Mon Sep 17 00:00:00 2001 From: Adriaan de Groot <groot@kde.org> Date: Wed, 5 Aug 2020 23:46:14 +0200 Subject: [PATCH] [libcalamares] Add an iterator for the full zones model --- src/libcalamares/locale/Tests.cpp | 26 +++++++++++++++++++ src/libcalamares/locale/TimeZone.cpp | 16 +++++++++++- src/libcalamares/locale/TimeZone.h | 39 +++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index db2ed2fe2..bb5365ee0 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -51,6 +51,7 @@ private Q_SLOTS: void testSimpleZones(); void testComplexZones(); void testTZLookup(); + void testTZIterator(); }; LocaleTests::LocaleTests() {} @@ -360,6 +361,31 @@ LocaleTests::testTZLookup() QVERIFY( !zones.find( "America", "New York" ) ); } +void +LocaleTests::testTZIterator() +{ + using namespace CalamaresUtils::Locale; + const ZonesModel zones; + + QVERIFY( zones.find( "Europe", "Rome" ) ); + + int count = 0; + bool seenRome = false; + bool seenGnome = false; + for ( auto it = zones.begin(); it; ++it ) + { + QVERIFY( *it ); + QVERIFY( !( *it )->zone().isEmpty() ); + seenRome |= ( *it )->zone() == QStringLiteral( "Rome" ); + seenGnome |= ( *it )->zone() == QStringLiteral( "Gnome" ); + count++; + } + + QVERIFY( seenRome ); + QVERIFY( !seenGnome ); + QCOMPARE( count, zones.rowCount( QModelIndex() ) ); +} + QTEST_GUILESS_MAIN( LocaleTests ) diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index d5d4938a8..e8b861d69 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -282,7 +282,7 @@ ZonesModel::roleNames() const } const TimeZoneData* -ZonesModel::find( const QString& region, const QString& zone ) +ZonesModel::find( const QString& region, const QString& zone ) const { for ( const auto* p : m_private->m_zones ) { @@ -294,6 +294,20 @@ ZonesModel::find( const QString& region, const QString& zone ) return nullptr; } +ZonesModel::Iterator::operator bool() const +{ + return 0 <= m_index && m_index < m_p->m_zones.count(); +} + +const TimeZoneData* ZonesModel::Iterator::operator*() const +{ + if ( *this ) + { + return m_p->m_zones[ m_index ]; + } + return nullptr; +} + RegionalZonesModel::RegionalZonesModel( CalamaresUtils::Locale::ZonesModel* source, QObject* parent ) : QSortFilterProxyModel( parent ) , m_private( privateInstance() ) diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index afe32963c..9b8569b21 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -126,7 +126,44 @@ public: * * Returns @c nullptr if not found. */ - const TimeZoneData* find( const QString& region, const QString& zone ); + const TimeZoneData* find( const QString& region, const QString& zone ) const; + + /** @brief Iterator for testing purposes + * + * This is primarily for testing, but who knows, it might be useful + * elsewhere, and it's convenient when it can access Private. + * + * Iterates over all the zones in the model. Operator * may return + * a @c nullptr when the iterator is not valid. Typical usage: + * + * ``` + * for( auto it = model.begin(); it; ++it ) + * { + * const auto* zonedata = *it; + * ... + * } + */ + class Iterator + { + friend class ZonesModel; + Iterator( const Private* m ) + : m_index( 0 ) + , m_p( m ) + { + } + + public: + operator bool() const; + void operator++() { ++m_index; } + const TimeZoneData* operator*() const; + int index() const { return m_index; } + + private: + int m_index; + const Private* m_p; + }; + + Iterator begin() const { return Iterator( m_private ); } private: Private* m_private;