[libcalamares] Introduce a filtering model per-region

This commit is contained in:
Adriaan de Groot 2020-08-05 17:14:13 +02:00
parent 1afdcc9c82
commit 3e32335511
3 changed files with 111 additions and 11 deletions

View File

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

View File

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

View File

@ -26,6 +26,7 @@
#include <QAbstractListModel>
#include <QObject>
#include <QSortFilterProxyModel>
#include <QVariant>
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