[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
main
Adriaan de Groot 5 years ago
parent da277fa7ba
commit f4509f3380

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

@ -63,14 +63,35 @@ protected:
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")
class TZRegion : public CStringPair
{
public:
using CStringPair::CStringPair;
virtual ~TZRegion();
QString tr() const override;
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")
@ -84,15 +105,12 @@ public:
class DLLEXPORT TZRegionModel : public QAbstractListModel
{
public:
/// @brief Create empty model (useless)
/// @brief Create empty model
TZRegionModel();
/// @brief Create model from list (non-owning)
TZRegionModel( TZRegionList );
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;
QVariant data( const QModelIndex& index, int role ) const override;
@ -100,7 +118,7 @@ public:
const TZRegion* region( int index ) const;
private:
QList< TZRegion* > m_regions;
TZRegionList m_regions;
};
} // namespace Locale

@ -110,7 +110,10 @@ LocalePage::LocalePage( QWidget* parent )
}
LocalePage::~LocalePage() {}
LocalePage::~LocalePage()
{
qDeleteAll( m_regionList );
}
void
@ -144,7 +147,8 @@ containsLocation( const QList< LocaleGlobal::Location >& locations, const QStrin
void
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() );
// Setup locations

@ -74,7 +74,8 @@ private:
void changeLocale();
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;
QComboBox* m_regionCombo;

Loading…
Cancel
Save