[libcalamares] Expose distanceFunc-find for timezones

This commit is contained in:
Adriaan de Groot 2020-08-08 13:45:32 +02:00
parent 0fda1dcf7d
commit 2f871acbfd
2 changed files with 24 additions and 4 deletions

View File

@ -337,14 +337,14 @@ ZonesModel::find( const QString& region, const QString& zone ) const
} }
STATICTEST const TimeZoneData* STATICTEST const TimeZoneData*
find( const ZoneVector& zones, std::function< double( const TimeZoneData* ) > distance ) find( const ZoneVector& zones, const std::function< double( const TimeZoneData* ) >& distanceFunc )
{ {
double smallestDistance = 1000000.0; double smallestDistance = 1000000.0;
const TimeZoneData* closest = nullptr; const TimeZoneData* closest = nullptr;
for ( const auto* zone : zones ) for ( const auto* zone : zones )
{ {
double thisDistance = distance( zone ); double thisDistance = distanceFunc( zone );
if ( thisDistance < smallestDistance ) if ( thisDistance < smallestDistance )
{ {
closest = zone; closest = zone;
@ -354,6 +354,12 @@ find( const ZoneVector& zones, std::function< double( const TimeZoneData* ) > di
return closest; return closest;
} }
const TimeZoneData*
ZonesModel::find( const std::function< double( const TimeZoneData* ) >& distanceFunc ) const
{
return CalamaresUtils::Locale::find( m_private->m_zones, distanceFunc );
}
const TimeZoneData* const TimeZoneData*
ZonesModel::find( double latitude, double longitude ) const ZonesModel::find( double latitude, double longitude ) const
{ {
@ -384,7 +390,7 @@ ZonesModel::find( double latitude, double longitude ) const
return latitudeDifference + longitudeDifference; return latitudeDifference + longitudeDifference;
}; };
return CalamaresUtils::Locale::find( m_private->m_zones, distance ); return find( distance );
} }
QObject* QObject*

View File

@ -167,6 +167,17 @@ public:
Iterator begin() const { return Iterator( m_private ); } Iterator begin() const { return Iterator( m_private ); }
/** @brief Look up TZ data based on an arbitrary distance function
*
* This is a generic method that can define distance in whatever
* coordinate system is wanted; returns the zone with the smallest
* distance. The @p distanceFunc must return "the distance" for
* each zone. It would be polite to return something non-negative.
*
* Note: not a slot, because the parameter isn't moc-able.
*/
const TimeZoneData* find( const std::function< double( const TimeZoneData* ) >& distanceFunc ) const;
public Q_SLOTS: public Q_SLOTS:
/** @brief Look up TZ data based on its name. /** @brief Look up TZ data based on its name.
* *
@ -176,7 +187,10 @@ public Q_SLOTS:
/** @brief Look up TZ data based on the location. /** @brief Look up TZ data based on the location.
* *
* Returns the nearest zone to the given lat and lon. * Returns the nearest zone to the given lat and lon. This is a
* convenience function for calling find(), below, with a standard
* distance function based on the distance between the given
* location (lat and lon) and each zone's given location.
*/ */
const TimeZoneData* find( double latitude, double longitude ) const; const TimeZoneData* find( double latitude, double longitude ) const;