mirror of
https://github.com/saitohirga/WSJT-X.git
synced 2026-06-18 21:58:34 -04:00
Improve frequency input validation
Allow group separators as well as decimal points in MHz input fields git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@7462 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "Radio.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#include <QString>
|
||||
#include <QChar>
|
||||
@@ -19,32 +20,53 @@ namespace Radio
|
||||
}
|
||||
|
||||
|
||||
Frequency frequency (QVariant const& v, int scale, QLocale const& locale)
|
||||
Frequency frequency (QVariant const& v, int scale, bool * ok, QLocale const& locale)
|
||||
{
|
||||
double value {0};
|
||||
double value {0.};
|
||||
if (QVariant::String == v.type ())
|
||||
{
|
||||
value = locale.toDouble (v.value<QString> ());
|
||||
value = locale.toDouble (v.value<QString> (), ok);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = v.toDouble ();
|
||||
if (ok) *ok = true;
|
||||
}
|
||||
return std::llround (value * std::pow (10., scale));
|
||||
value *= std::pow (10., scale);
|
||||
if (ok)
|
||||
{
|
||||
if (value < 0. || value > std::numeric_limits<Frequency>::max ())
|
||||
{
|
||||
value = 0.;
|
||||
*ok = false;
|
||||
}
|
||||
}
|
||||
return std::llround (value);
|
||||
}
|
||||
|
||||
FrequencyDelta frequency_delta (QVariant const& v, int scale, QLocale const& locale)
|
||||
FrequencyDelta frequency_delta (QVariant const& v, int scale, bool * ok, QLocale const& locale)
|
||||
{
|
||||
double value {0};
|
||||
double value {0.};
|
||||
if (QVariant::String == v.type ())
|
||||
{
|
||||
value = locale.toDouble (v.value<QString> ());
|
||||
value = locale.toDouble (v.value<QString> (), ok);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = v.toDouble ();
|
||||
if (ok) *ok = true;
|
||||
}
|
||||
return std::llround (value * std::pow (10., scale));
|
||||
value *= std::pow (10., scale);
|
||||
if (ok)
|
||||
{
|
||||
if (value < -std::numeric_limits<Frequency>::max ()
|
||||
|| value > std::numeric_limits<Frequency>::max ())
|
||||
{
|
||||
value = 0.;
|
||||
*ok = false;
|
||||
}
|
||||
}
|
||||
return std::llround (value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user