[libcalamares] CStringList::find() convenience function

- search for a key and return a type-cast pointer to the result
 - while here, simplify some other code
 - the find() function could be done with std::find_if but doesn't
   get any shorter or more elegant
This commit is contained in:
Adriaan de Groot 2019-12-10 15:54:43 +01:00
parent 6092172f8d
commit c75e870073
2 changed files with 25 additions and 4 deletions

View File

@ -66,7 +66,22 @@ protected:
QString m_key;
};
using CStringPairList = QList< CStringPair* >;
class CStringPairList : public QList< CStringPair* >
{
public:
template < typename T >
T* find( const QString& key ) const
{
for ( auto* p : *this )
{
if ( p->key() == key )
{
return dynamic_cast< T* >( p );
}
}
return nullptr;
}
};
/// @brief A pair of strings for timezone regions (e.g. "America")
class TZRegion : public CStringPair
@ -90,6 +105,8 @@ public:
/// @brief Calls fromFile with the standard zone.tab name
static CStringPairList fromZoneTab();
const CStringPairList& zones() const { return m_zones; }
private:
CStringPairList m_zones;
};

View File

@ -147,10 +147,13 @@ containsLocation( const QList< LocaleGlobal::Location >& locations, const QStrin
void
LocalePage::init( const QString& initialRegion, const QString& initialZone, const QString& localeGenPath )
{
m_regionList = CalamaresUtils::Locale::TZRegion::fromZoneTab();
m_regionModel = std::make_unique< CalamaresUtils::Locale::CStringListModel >( m_regionList );
using namespace CalamaresUtils::Locale;
m_regionList = TZRegion::fromZoneTab();
m_regionModel = std::make_unique< CStringListModel >( m_regionList );
m_regionCombo->setModel( m_regionModel.get() );
// Setup locations
QHash< QString, QList< LocaleGlobal::Location > > regions = LocaleGlobal::getLocations();
@ -159,7 +162,8 @@ LocalePage::init( const QString& initialRegion, const QString& initialZone, cons
m_regionCombo->currentIndexChanged( m_regionCombo->currentIndex() );
if ( keys.contains( initialRegion ) && containsLocation( regions.value( initialRegion ), initialZone ) )
auto* region = m_regionList.find< TZRegion >( initialRegion );
if ( region && region->zones().find< TZZone >( initialZone ) )
{
m_tzWidget->setCurrentLocation( initialRegion, initialZone );
}