From 97d9e88614a764bf5d639c28ae85519c814da1db Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Sun, 30 Oct 2016 22:36:13 +0000 Subject: [PATCH] Choose the nearest preset frequency in a band when forced to align This ensures that an already selected preset frequency is not changed when exiting the settings dialog. Probably would be better to not reset the frequency when exiting the settings dialog but that is buried inside other function calls that are probably necessary. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@7268 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- FrequencyList.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/FrequencyList.cpp b/FrequencyList.cpp index 53add51f6..7ab164b8e 100644 --- a/FrequencyList.cpp +++ b/FrequencyList.cpp @@ -1,6 +1,8 @@ #include "FrequencyList.hpp" +#include #include +#include #include #include @@ -189,14 +191,22 @@ int FrequencyList::best_working_frequency (Frequency f) const auto const& target_band = m_->bands_->find (f); if (!target_band.isEmpty ()) { + Radio::FrequencyDelta delta {std::numeric_limits::max ()}; // find a frequency in the same band that is allowed for (int row = 0; row < rowCount (); ++row) { auto const& source_row = mapToSource (index (row, 0)).row (); - auto const& band = m_->bands_->find (m_->frequency_list_[source_row].frequency_); + auto const& candidate_frequency = m_->frequency_list_[source_row].frequency_; + auto const& band = m_->bands_->find (candidate_frequency); if (band == target_band) { - return row; + // take closest band match + Radio::FrequencyDelta new_delta = f - candidate_frequency; + if (std::abs (new_delta) < std::abs (delta)) + { + delta = new_delta; + result = row; + } } } }