From 6092172f8dbdf59628d1794cefd9abc27e9a677a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 10 Dec 2019 15:32:15 +0100 Subject: [PATCH] [libcalamares] Regions and zones change to QList - By using QList< CStringPair* > consistently, we can save a bunch of model code at the cost of an occasional dynamic_cast; it's fairly rare for there to be a need for the derived pointer. --- src/libcalamares/locale/TimeZone.cpp | 50 ++++++++++++++++------------ src/libcalamares/locale/TimeZone.h | 30 ++++++++--------- src/modules/locale/LocalePage.cpp | 2 +- src/modules/locale/LocalePage.h | 5 +-- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index dd8ffbf41..e2e13c828 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -130,16 +130,16 @@ TZRegion::~TZRegion() qDeleteAll( m_zones ); } -TZRegionList +CStringPairList TZRegion::fromZoneTab() { return TZRegion::fromFile( TZ_DATA_FILE ); } -TZRegionList +CStringPairList TZRegion::fromFile( const char* fileName ) { - TZRegionList model; + CStringPairList model; QFile file( fileName ); if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) @@ -176,11 +176,11 @@ TZRegion::fromFile( const char* fileName ) continue; } - auto it - = std::find_if( model.begin(), model.end(), [®ion]( const TZRegion* r ) { return r->m_key == region; } ); + auto keyMatch = [®ion]( const CStringPair* r ) { return r->key() == region; }; + auto it = std::find_if( model.begin(), model.end(), keyMatch ); if ( it != model.end() ) { - thisRegion = *it; + thisRegion = dynamic_cast< TZRegion* >( *it ); } else { @@ -195,13 +195,19 @@ TZRegion::fromFile( const char* fileName ) } timezoneParts.removeFirst(); - thisRegion->m_zones.append( new TZZone( timezoneParts.join( '/' ).toUtf8().constData(), countryCode, list.at( 1 ) ) ); + thisRegion->m_zones.append( + new TZZone( timezoneParts.join( '/' ).toUtf8().constData(), countryCode, list.at( 1 ) ) ); } - std::sort( model.begin(), model.end(), []( const TZRegion* l, const TZRegion* r ) { return *l < *r; } ); - for ( auto& r : model ) + auto sorter = []( const CStringPair* l, const CStringPair* r ) { return *l < *r; }; + std::sort( model.begin(), model.end(), sorter ); + for ( auto& it : model ) { - std::sort( r->m_zones.begin(), r->m_zones.end(), []( const TZZone* l, const TZZone* r ) { return *l < *r; } ); + TZRegion* r = dynamic_cast< TZRegion* >( it ); + if ( r ) + { + std::sort( r->m_zones.begin(), r->m_zones.end(), sorter ); + } } return model; @@ -233,21 +239,21 @@ TZZone::print( QDebug& log ) const } -TZRegionModel::TZRegionModel( TZRegionList l ) - : m_regions( l ) +CStringListModel::CStringListModel( CStringPairList l ) + : m_list( l ) { } -TZRegionModel::~TZRegionModel() {} +CStringListModel::~CStringListModel() {} int -TZRegionModel::rowCount( const QModelIndex& parent ) const +CStringListModel::rowCount( const QModelIndex& parent ) const { - return m_regions.count(); + return m_list.count(); } QVariant -TZRegionModel::data( const QModelIndex& index, int role ) const +CStringListModel::data( const QModelIndex& index, int role ) const { if ( ( role != Qt::DisplayRole ) && ( role != Qt::UserRole ) ) { @@ -259,18 +265,18 @@ TZRegionModel::data( const QModelIndex& index, int role ) const return QVariant(); } - const TZRegion* region = m_regions.at( index.row() ); - return role == Qt::DisplayRole ? region->tr() : region->key(); + const auto* item = m_list.at( index.row() ); + return item ? ( role == Qt::DisplayRole ? item->tr() : item->key() ) : QVariant(); } -const TZRegion* -TZRegionModel::region( int index ) const +const CStringPair* +CStringListModel::item( int index ) const { - if ( ( index < 0 ) || ( index >= m_regions.count() ) ) + if ( ( index < 0 ) || ( index >= m_list.count() ) ) { return nullptr; } - return m_regions[ index ]; + return m_list[ index ]; } } // namespace Locale diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index 896661b99..0f09d48ca 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -57,7 +57,6 @@ public: /// @brief Give the localized human-readable form virtual QString tr() const = 0; - QString key() const { return m_key; } bool operator<( const CStringPair& other ) const { return m_key < other.m_key; } @@ -67,10 +66,7 @@ protected: QString m_key; }; -class TZZone; -class TZRegion; -using TZZoneList = QList< TZZone* >; -using TZRegionList = QList< TZRegion* >; +using CStringPairList = QList< CStringPair* >; /// @brief A pair of strings for timezone regions (e.g. "America") class TZRegion : public CStringPair @@ -80,20 +76,22 @@ public: virtual ~TZRegion(); QString tr() const override; - /** @brief Create model from a zone.tab-like file + /** @brief Create list from a zone.tab-like file * * Returns a list of all the regions; each region has a list - * of zones within that region. + * of zones within that region. Dyamically, the items in the + * returned list are TZRegions; their zones dynamically are + * TZZones even though all those lists have type CStringPairList. * * The list owns the regions, and the regions own their own list of zones. * When getting rid of the list, remember to qDeleteAll() on it. */ - static TZRegionList fromFile( const char* fileName ); + static CStringPairList fromFile( const char* fileName ); /// @brief Calls fromFile with the standard zone.tab name - static TZRegionList fromZoneTab(); + static CStringPairList fromZoneTab(); private: - TZZoneList m_zones; + CStringPairList m_zones; }; /// @brief A pair of strings for specific timezone names (e.g. "New_York") @@ -119,23 +117,23 @@ operator<<( QDebug& log, const TZZone& z ) return log; } -class DLLEXPORT TZRegionModel : public QAbstractListModel +class CStringListModel : public QAbstractListModel { public: /// @brief Create empty model - TZRegionModel(); + CStringListModel(); /// @brief Create model from list (non-owning) - TZRegionModel( TZRegionList ); - virtual ~TZRegionModel() override; + CStringListModel( CStringPairList ); + virtual ~CStringListModel() override; int rowCount( const QModelIndex& parent ) const override; QVariant data( const QModelIndex& index, int role ) const override; - const TZRegion* region( int index ) const; + const CStringPair* item( int index ) const; private: - TZRegionList m_regions; + CStringPairList m_list; }; } // namespace Locale diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index e5d14027f..bce12e817 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -148,7 +148,7 @@ 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::TZRegionModel >( m_regionList ); + m_regionModel = std::make_unique< CalamaresUtils::Locale::CStringListModel >( m_regionList ); m_regionCombo->setModel( m_regionModel.get() ); // Setup locations diff --git a/src/modules/locale/LocalePage.h b/src/modules/locale/LocalePage.h index 07f9fe0f2..7c2eba253 100644 --- a/src/modules/locale/LocalePage.h +++ b/src/modules/locale/LocalePage.h @@ -74,8 +74,9 @@ private: void changeLocale(); void changeFormats(); - CalamaresUtils::Locale::TZRegionList m_regionList; - std::unique_ptr< CalamaresUtils::Locale::TZRegionModel > m_regionModel; + // Dynamically, QList< TZRegion* > + CalamaresUtils::Locale::CStringPairList m_regionList; + std::unique_ptr< CalamaresUtils::Locale::CStringListModel > m_regionModel; TimeZoneWidget* m_tzWidget; QComboBox* m_regionCombo;