From 3e32335511a8989fe78abe986d02d6005b23b8b8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 5 Aug 2020 17:14:13 +0200 Subject: [PATCH] [libcalamares] Introduce a filtering model per-region --- src/libcalamares/locale/Tests.cpp | 55 ++++++++++++++++++++++++---- src/libcalamares/locale/TimeZone.cpp | 42 +++++++++++++++++++-- src/libcalamares/locale/TimeZone.h | 25 +++++++++++++ 3 files changed, 111 insertions(+), 11 deletions(-) diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index c660ccbfb..8a6a6ad84 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -267,6 +267,19 @@ LocaleTests::testRegions() } +static void +displayedNames( QAbstractItemModel& model, QStringList& names ) +{ + names.clear(); + for ( int i = 0; i < model.rowCount( QModelIndex() ); ++i ) + { + QVariant name = model.data( model.index( i, 0 ), Qt::DisplayRole ); + QVERIFY( name.isValid() ); + QVERIFY( !name.toString().isEmpty() ); + names.append( name.toString() ); + } +} + void LocaleTests::testSimpleZones() { @@ -276,14 +289,7 @@ LocaleTests::testSimpleZones() 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() ); - } - + displayedNames( zones, names ); QVERIFY( names.contains( "Amsterdam" ) ); if ( !names.contains( "New York" ) ) { @@ -304,6 +310,39 @@ void LocaleTests::testComplexZones() { using namespace CalamaresUtils::Locale; + ZonesModel zones; + RegionalZonesModel europe( &zones ); + + QStringList names; + displayedNames( zones, names ); + QVERIFY( names.contains( "New York" ) ); + QVERIFY( names.contains( "Prague" ) ); + QVERIFY( names.contains( "Abidjan" ) ); + + // No region set + displayedNames( europe, names ); + QVERIFY( names.contains( "New York" ) ); + QVERIFY( names.contains( "Prague" ) ); + QVERIFY( names.contains( "Abidjan" ) ); + + // Now filter + europe.setRegion( "Europe" ); + displayedNames( europe, names ); + QVERIFY( !names.contains( "New York" ) ); + QVERIFY( names.contains( "Prague" ) ); + QVERIFY( !names.contains( "Abidjan" ) ); + + europe.setRegion( "America" ); + displayedNames( europe, names ); + QVERIFY( names.contains( "New York" ) ); + QVERIFY( !names.contains( "Prague" ) ); + QVERIFY( !names.contains( "Abidjan" ) ); + + europe.setRegion( "Africa" ); + displayedNames( europe, names ); + QVERIFY( !names.contains( "New York" ) ); + QVERIFY( !names.contains( "Prague" ) ); + QVERIFY( names.contains( "Abidjan" ) ); } QTEST_GUILESS_MAIN( LocaleTests ) diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index 05c082738..3d517940a 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -170,7 +170,6 @@ public: double longitude ); QString tr() const override; -private: QString m_region; QString m_country; double m_latitude; @@ -318,7 +317,7 @@ RegionsModel::RegionsModel( QObject* parent ) RegionsModel::~RegionsModel() {} int -RegionsModel::rowCount( const QModelIndex& parent ) const +RegionsModel::rowCount( const QModelIndex& ) const { return m_private->m_regions.count(); } @@ -359,7 +358,7 @@ ZonesModel::ZonesModel( QObject* parent ) ZonesModel::~ZonesModel() {} int -ZonesModel::rowCount( const QModelIndex& parent ) const +ZonesModel::rowCount( const QModelIndex& ) const { return m_private->m_zones.count(); } @@ -390,6 +389,43 @@ ZonesModel::roleNames() const return { { NameRole, "name" }, { KeyRole, "key" } }; } +RegionalZonesModel::RegionalZonesModel( CalamaresUtils::Locale::ZonesModel* source, QObject* parent ) + : QSortFilterProxyModel( parent ) + , m_private( privateInstance() ) +{ + setSourceModel( source ); +} + +RegionalZonesModel::~RegionalZonesModel() {} + +void +RegionalZonesModel::setRegion( const QString& r ) +{ + if ( r != m_region ) + { + m_region = r; + invalidateFilter(); + emit regionChanged( r ); + } +} + +bool +RegionalZonesModel::filterAcceptsRow( int sourceRow, const QModelIndex& ) const +{ + if ( m_region.isEmpty() ) + { + return true; + } + + if ( sourceRow < 0 || sourceRow >= m_private->m_zones.count() ) + { + return false; + } + + const auto& zone = m_private->m_zones[ sourceRow ]; + return ( zone.m_region == m_region ); +} + } // namespace Locale } // namespace CalamaresUtils diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index d87a5a57a..cb91a8361 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -26,6 +26,7 @@ #include #include +#include #include namespace CalamaresUtils @@ -85,6 +86,30 @@ private: Private* m_private; }; +class DLLEXPORT RegionalZonesModel : public QSortFilterProxyModel +{ + Q_OBJECT + Q_PROPERTY( QString region READ region WRITE setRegion NOTIFY regionChanged ) + +public: + RegionalZonesModel( ZonesModel* source, QObject* parent = nullptr ); + ~RegionalZonesModel() override; + + bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const override; + + QString region() const { return m_region; } + +public Q_SLOTS: + void setRegion( const QString& r ); + +signals: + void regionChanged( const QString& ); + +private: + Private* m_private; + QString m_region; +}; + } // namespace Locale } // namespace CalamaresUtils