From c75e87007317b142267338df657b1fa078a6deb1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 10 Dec 2019 15:54:43 +0100 Subject: [PATCH] [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 --- src/libcalamares/locale/TimeZone.h | 19 ++++++++++++++++++- src/modules/locale/LocalePage.cpp | 10 +++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index 0f09d48ca..86ddd516e 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -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; }; diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index bce12e817..4bda31d00 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -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 ); }