[libcalamares] Refactor finding-TZ algorithm

- introduce a distance function and use that, rather than coding it
  inside the find() function. This is prep-work for unifying the
  find() calls, based on various coordinate systems.
main
Adriaan de Groot 5 years ago
parent fdbc253623
commit 0fda1dcf7d

@ -336,6 +336,24 @@ ZonesModel::find( const QString& region, const QString& zone ) const
return nullptr; return nullptr;
} }
STATICTEST const TimeZoneData*
find( const ZoneVector& zones, std::function< double( const TimeZoneData* ) > distance )
{
double smallestDistance = 1000000.0;
const TimeZoneData* closest = nullptr;
for ( const auto* zone : zones )
{
double thisDistance = distance( zone );
if ( thisDistance < smallestDistance )
{
closest = zone;
smallestDistance = thisDistance;
}
}
return closest;
}
const TimeZoneData* const TimeZoneData*
ZonesModel::find( double latitude, double longitude ) const ZonesModel::find( double latitude, double longitude ) const
{ {
@ -344,12 +362,7 @@ ZonesModel::find( double latitude, double longitude ) const
* either N/S or E/W equal to any other; this obviously * either N/S or E/W equal to any other; this obviously
* falls apart at the poles. * falls apart at the poles.
*/ */
auto distance = [&]( const TimeZoneData* zone ) -> double {
double largestDifference = 720.0;
const TimeZoneData* closest = nullptr;
for ( const auto* zone : m_private->m_zones )
{
// Latitude doesn't wrap around: there is nothing north of 90 // Latitude doesn't wrap around: there is nothing north of 90
double latitudeDifference = abs( zone->latitude() - latitude ); double latitudeDifference = abs( zone->latitude() - latitude );
@ -368,13 +381,10 @@ ZonesModel::find( double latitude, double longitude ) const
longitudeDifference = abs( westerly - easterly ); longitudeDifference = abs( westerly - easterly );
} }
if ( latitudeDifference + longitudeDifference < largestDifference ) return latitudeDifference + longitudeDifference;
{ };
largestDifference = latitudeDifference + longitudeDifference;
closest = zone; return CalamaresUtils::Locale::find( m_private->m_zones, distance );
}
}
return closest;
} }
QObject* QObject*

Loading…
Cancel
Save