[libcalamares] Move loading to TZRegion

- don't make the model load files, provide convenience functions
   for loading in the value classes
 - create model from lists of value pointers
This commit is contained in:
Adriaan de Groot 2019-12-10 10:45:56 -01:00
parent da277fa7ba
commit f4509f3380
4 changed files with 55 additions and 30 deletions

View File

@ -94,25 +94,21 @@ TZRegion::tr() const
return QObject::tr( m_human, "tz_regions" ); return QObject::tr( m_human, "tz_regions" );
} }
QString TZRegion::~TZRegion()
TZZone::tr() const
{ {
// NOTE: context name must match what's used in zone-extractor.py qDeleteAll( m_zones );
return QObject::tr( m_human, "tz_names" );
} }
TZRegionModel::TZRegionModel() {} TZRegionList
TZRegion::fromZoneTab()
std::shared_ptr< TZRegionModel >
TZRegionModel::fromZoneTab()
{ {
return TZRegionModel::fromFile( TZ_DATA_FILE ); return TZRegion::fromFile( TZ_DATA_FILE );
} }
std::shared_ptr< TZRegionModel > TZRegionList
TZRegionModel::fromFile( const char* fileName ) TZRegion::fromFile( const char* fileName )
{ {
auto model = std::make_shared< TZRegionModel >(); TZRegionList model;
QFile file( fileName ); QFile file( fileName );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
@ -120,7 +116,6 @@ TZRegionModel::fromFile( const char* fileName )
return model; return model;
} }
model->m_regions.reserve( 12 ); // There's 10 in the file right now
QStringList regions; QStringList regions;
QTextStream in( &file ); QTextStream in( &file );
@ -154,21 +149,28 @@ TZRegionModel::fromFile( const char* fileName )
if ( !regions.contains( region ) ) if ( !regions.contains( region ) )
{ {
regions.append( region ); regions.append( region );
model->m_regions.append( new TZRegion( region.toUtf8().data() ) ); model.append( new TZRegion( region.toUtf8().data() ) );
} }
} }
std::sort( model->m_regions.begin(), model->m_regions.end(), []( const TZRegion* l, const TZRegion* r ) { std::sort( model.begin(), model.end(), []( const TZRegion* l, const TZRegion* r ) { return *l < *r; } );
return *l < *r;
} );
return model; return model;
} }
TZRegionModel::~TZRegionModel() QString
TZZone::tr() const
{ {
qDeleteAll( m_regions ); // NOTE: context name must match what's used in zone-extractor.py
return QObject::tr( m_human, "tz_names" );
} }
TZRegionModel::TZRegionModel( TZRegionList l )
: m_regions( l )
{
}
TZRegionModel::~TZRegionModel() {}
int int
TZRegionModel::rowCount( const QModelIndex& parent ) const TZRegionModel::rowCount( const QModelIndex& parent ) const
{ {
@ -197,7 +199,7 @@ TZRegionModel::region( int index ) const
{ {
if ( ( index < 0 ) || ( index >= m_regions.count() ) ) if ( ( index < 0 ) || ( index >= m_regions.count() ) )
{ {
index = 0; return nullptr;
} }
return m_regions[ index ]; return m_regions[ index ];
} }

View File

@ -63,14 +63,35 @@ protected:
QString m_key; QString m_key;
}; };
class TZZone;
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
{ {
public: public:
using CStringPair::CStringPair; using CStringPair::CStringPair;
virtual ~TZRegion();
QString tr() const override; QString tr() const override;
bool operator<( const TZRegion& other ) const { return m_key < other.m_key; } bool operator<( const TZRegion& other ) const { return m_key < other.m_key; }
/** @brief Create model from a zone.tab-like file
*
* Returns a list of all the regions; each region has a list
* of zones within that region.
*
* 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 );
/// @brief Calls fromFile with the standard zone.tab name
static TZRegionList fromZoneTab();
private:
TZZoneList 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")
@ -84,15 +105,12 @@ public:
class DLLEXPORT TZRegionModel : public QAbstractListModel class DLLEXPORT TZRegionModel : public QAbstractListModel
{ {
public: public:
/// @brief Create empty model (useless) /// @brief Create empty model
TZRegionModel(); TZRegionModel();
/// @brief Create model from list (non-owning)
TZRegionModel( TZRegionList );
virtual ~TZRegionModel() override; virtual ~TZRegionModel() override;
/// @brief Create model from a zone.tab-like file
static std::shared_ptr< TZRegionModel > fromFile( const char* fileName );
/// @brief Calls fromFile with the standard zone.tab name
static std::shared_ptr< TZRegionModel > fromZoneTab();
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;
@ -100,7 +118,7 @@ public:
const TZRegion* region( int index ) const; const TZRegion* region( int index ) const;
private: private:
QList< TZRegion* > m_regions; TZRegionList m_regions;
}; };
} // namespace Locale } // namespace Locale

View File

@ -110,7 +110,10 @@ LocalePage::LocalePage( QWidget* parent )
} }
LocalePage::~LocalePage() {} LocalePage::~LocalePage()
{
qDeleteAll( m_regionList );
}
void void
@ -144,7 +147,8 @@ containsLocation( const QList< LocaleGlobal::Location >& locations, const QStrin
void void
LocalePage::init( const QString& initialRegion, const QString& initialZone, const QString& localeGenPath ) LocalePage::init( const QString& initialRegion, const QString& initialZone, const QString& localeGenPath )
{ {
m_regionModel = CalamaresUtils::Locale::TZRegionModel::fromZoneTab(); m_regionList = CalamaresUtils::Locale::TZRegion::fromZoneTab();
m_regionModel = std::make_unique< CalamaresUtils::Locale::TZRegionModel >( m_regionList );
m_regionCombo->setModel( m_regionModel.get() ); m_regionCombo->setModel( m_regionModel.get() );
// Setup locations // Setup locations

View File

@ -74,7 +74,8 @@ private:
void changeLocale(); void changeLocale();
void changeFormats(); void changeFormats();
std::shared_ptr< CalamaresUtils::Locale::TZRegionModel > m_regionModel; CalamaresUtils::Locale::TZRegionList m_regionList;
std::unique_ptr< CalamaresUtils::Locale::TZRegionModel > m_regionModel;
TimeZoneWidget* m_tzWidget; TimeZoneWidget* m_tzWidget;
QComboBox* m_regionCombo; QComboBox* m_regionCombo;