From 0948963d86c6fdd7c3272da88dd10884cf05e525 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sat, 8 Aug 2020 22:01:10 +1000 Subject: [PATCH] [locale] Port TZ widget lookup to new find() method - The TZ widget uses a different coordinate system (mapping lat and lon to pixel locations, and then calculating Manhattan distance from that), so needs a different distance function. - Simplify code: there's just one "closest TZ" function. --- .../locale/timezonewidget/timezonewidget.cpp | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/modules/locale/timezonewidget/timezonewidget.cpp b/src/modules/locale/timezonewidget/timezonewidget.cpp index b1d3cfeaa..778f0535d 100644 --- a/src/modules/locale/timezonewidget/timezonewidget.cpp +++ b/src/modules/locale/timezonewidget/timezonewidget.cpp @@ -190,28 +190,15 @@ TimeZoneWidget::mousePressEvent( QMouseEvent* event ) { return; } - // Set nearest location - int nX = 999999, mX = event->pos().x(); - int nY = 999999, mY = event->pos().y(); - using namespace CalamaresUtils::Locale; - const TimeZoneData* closest = nullptr; - for ( auto it = m_zonesData->begin(); it; ++it ) - { - const auto* zone = *it; - if ( zone ) - { - QPoint locPos = TimeZoneImageList::getLocationPosition( zone->longitude(), zone->latitude() ); - - if ( ( abs( mX - locPos.x() ) + abs( mY - locPos.y() ) < abs( mX - nX ) + abs( mY - nY ) ) ) - { - closest = zone; - nX = locPos.x(); - nY = locPos.y(); - } - } - } + int mX = event->pos().x(); + int mY = event->pos().y(); + auto distance = [&]( const CalamaresUtils::Locale::TimeZoneData* zone ) { + QPoint locPos = TimeZoneImageList::getLocationPosition( zone->longitude(), zone->latitude() ); + return double( abs( mX - locPos.x() ) + abs( mY - locPos.y() ) ); + }; + const auto* closest = m_zonesData->find( distance ); if ( closest ) { // Set zone image and repaint widget