[libcalamares] Add an iterator for the full zones model

This commit is contained in:
Adriaan de Groot 2020-08-05 23:46:14 +02:00
parent 7ea2ad7dc6
commit 37c211fd14
3 changed files with 79 additions and 2 deletions

View File

@ -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 )

View File

@ -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() )

View File

@ -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;