[libcalamares] Regions and zones change to QList<Base*>

- 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.
main
Adriaan de Groot 5 years ago
parent fc8364ea54
commit 6092172f8d

@ -130,16 +130,16 @@ TZRegion::~TZRegion()
qDeleteAll( m_zones ); qDeleteAll( m_zones );
} }
TZRegionList CStringPairList
TZRegion::fromZoneTab() TZRegion::fromZoneTab()
{ {
return TZRegion::fromFile( TZ_DATA_FILE ); return TZRegion::fromFile( TZ_DATA_FILE );
} }
TZRegionList CStringPairList
TZRegion::fromFile( const char* fileName ) TZRegion::fromFile( const char* fileName )
{ {
TZRegionList model; CStringPairList model;
QFile file( fileName ); QFile file( fileName );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
@ -176,11 +176,11 @@ TZRegion::fromFile( const char* fileName )
continue; continue;
} }
auto it auto keyMatch = [&region]( const CStringPair* r ) { return r->key() == region; };
= std::find_if( model.begin(), model.end(), [&region]( const TZRegion* r ) { return r->m_key == region; } ); auto it = std::find_if( model.begin(), model.end(), keyMatch );
if ( it != model.end() ) if ( it != model.end() )
{ {
thisRegion = *it; thisRegion = dynamic_cast< TZRegion* >( *it );
} }
else else
{ {
@ -195,13 +195,19 @@ TZRegion::fromFile( const char* fileName )
} }
timezoneParts.removeFirst(); 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; } ); auto sorter = []( const CStringPair* l, const CStringPair* r ) { return *l < *r; };
for ( auto& r : model ) 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; return model;
@ -233,21 +239,21 @@ TZZone::print( QDebug& log ) const
} }
TZRegionModel::TZRegionModel( TZRegionList l ) CStringListModel::CStringListModel( CStringPairList l )
: m_regions( l ) : m_list( l )
{ {
} }
TZRegionModel::~TZRegionModel() {} CStringListModel::~CStringListModel() {}
int int
TZRegionModel::rowCount( const QModelIndex& parent ) const CStringListModel::rowCount( const QModelIndex& parent ) const
{ {
return m_regions.count(); return m_list.count();
} }
QVariant QVariant
TZRegionModel::data( const QModelIndex& index, int role ) const CStringListModel::data( const QModelIndex& index, int role ) const
{ {
if ( ( role != Qt::DisplayRole ) && ( role != Qt::UserRole ) ) if ( ( role != Qt::DisplayRole ) && ( role != Qt::UserRole ) )
{ {
@ -259,18 +265,18 @@ TZRegionModel::data( const QModelIndex& index, int role ) const
return QVariant(); return QVariant();
} }
const TZRegion* region = m_regions.at( index.row() ); const auto* item = m_list.at( index.row() );
return role == Qt::DisplayRole ? region->tr() : region->key(); return item ? ( role == Qt::DisplayRole ? item->tr() : item->key() ) : QVariant();
} }
const TZRegion* const CStringPair*
TZRegionModel::region( int index ) const CStringListModel::item( int index ) const
{ {
if ( ( index < 0 ) || ( index >= m_regions.count() ) ) if ( ( index < 0 ) || ( index >= m_list.count() ) )
{ {
return nullptr; return nullptr;
} }
return m_regions[ index ]; return m_list[ index ];
} }
} // namespace Locale } // namespace Locale

@ -57,7 +57,6 @@ public:
/// @brief Give the localized human-readable form /// @brief Give the localized human-readable form
virtual QString tr() const = 0; virtual QString tr() const = 0;
QString key() const { return m_key; } QString key() const { return m_key; }
bool operator<( const CStringPair& other ) const { return m_key < other.m_key; } bool operator<( const CStringPair& other ) const { return m_key < other.m_key; }
@ -67,10 +66,7 @@ protected:
QString m_key; QString m_key;
}; };
class TZZone; using CStringPairList = QList< CStringPair* >;
class TZRegion;
using TZZoneList = QList< TZZone* >;
using TZRegionList = QList< TZRegion* >;
/// @brief A pair of strings for timezone regions (e.g. "America") /// @brief A pair of strings for timezone regions (e.g. "America")
class TZRegion : public CStringPair class TZRegion : public CStringPair
@ -80,20 +76,22 @@ public:
virtual ~TZRegion(); virtual ~TZRegion();
QString tr() const override; 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 * 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. * 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. * 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 /// @brief Calls fromFile with the standard zone.tab name
static TZRegionList fromZoneTab(); static CStringPairList fromZoneTab();
private: private:
TZZoneList m_zones; CStringPairList m_zones;
}; };
/// @brief A pair of strings for specific timezone names (e.g. "New_York") /// @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; return log;
} }
class DLLEXPORT TZRegionModel : public QAbstractListModel class CStringListModel : public QAbstractListModel
{ {
public: public:
/// @brief Create empty model /// @brief Create empty model
TZRegionModel(); CStringListModel();
/// @brief Create model from list (non-owning) /// @brief Create model from list (non-owning)
TZRegionModel( TZRegionList ); CStringListModel( CStringPairList );
virtual ~TZRegionModel() override; virtual ~CStringListModel() override;
int rowCount( const QModelIndex& parent ) const override; int rowCount( const QModelIndex& parent ) const override;
QVariant data( const QModelIndex& index, int role ) const override; QVariant data( const QModelIndex& index, int role ) const override;
const TZRegion* region( int index ) const; const CStringPair* item( int index ) const;
private: private:
TZRegionList m_regions; CStringPairList m_list;
}; };
} // namespace Locale } // namespace Locale

@ -148,7 +148,7 @@ void
LocalePage::init( const QString& initialRegion, const QString& initialZone, const QString& localeGenPath ) LocalePage::init( const QString& initialRegion, const QString& initialZone, const QString& localeGenPath )
{ {
m_regionList = CalamaresUtils::Locale::TZRegion::fromZoneTab(); 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() ); m_regionCombo->setModel( m_regionModel.get() );
// Setup locations // Setup locations

@ -74,8 +74,9 @@ private:
void changeLocale(); void changeLocale();
void changeFormats(); void changeFormats();
CalamaresUtils::Locale::TZRegionList m_regionList; // Dynamically, QList< TZRegion* >
std::unique_ptr< CalamaresUtils::Locale::TZRegionModel > m_regionModel; CalamaresUtils::Locale::CStringPairList m_regionList;
std::unique_ptr< CalamaresUtils::Locale::CStringListModel > m_regionModel;
TimeZoneWidget* m_tzWidget; TimeZoneWidget* m_tzWidget;
QComboBox* m_regionCombo; QComboBox* m_regionCombo;

Loading…
Cancel
Save