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:
Bill Somerville 2017-01-08 23:58:00 +00:00
parent 59d1ee3d62
commit 92cd8d9959
5 changed files with 81 additions and 18 deletions

View File

@ -1,16 +1,45 @@
#include "FrequencyLineEdit.hpp"
#include <QRegExpValidator>
#include <QRegExp>
#include <limits>
#include <QDoubleValidator>
#include <QString>
#include <QLocale>
#include "moc_FrequencyLineEdit.cpp"
namespace
{
class MHzValidator
: public QDoubleValidator
{
public:
MHzValidator (double bottom, double top, QObject * parent = nullptr)
: QDoubleValidator {bottom, top, 6, parent}
{
}
State validate (QString& input, int& pos) const override
{
State result = QDoubleValidator::validate (input, pos);
if (Acceptable == result)
{
bool ok;
(void)QLocale {}.toDouble (input, &ok);
if (!ok)
{
result = Intermediate;
}
}
return result;
}
};
}
FrequencyLineEdit::FrequencyLineEdit (QWidget * parent)
: QLineEdit (parent)
{
setValidator (new QRegExpValidator {QRegExp {QString {R"(\d{0,6}(\)"} + QLocale {}.decimalPoint () + R"(\d{0,6})?)"}, this});
setValidator (new MHzValidator {0., std::numeric_limits<Radio::Frequency>::max () / 10.e6, this});
}
auto FrequencyLineEdit::frequency () const -> Frequency
@ -27,7 +56,8 @@ void FrequencyLineEdit::frequency (Frequency f)
FrequencyDeltaLineEdit::FrequencyDeltaLineEdit (QWidget * parent)
: QLineEdit (parent)
{
setValidator (new QRegExpValidator {QRegExp {QString {R"(-?\d{0,6}(\)"} + QLocale {}.decimalPoint () + R"(\d{0,6})?)"}, this});
setValidator (new MHzValidator {-std::numeric_limits<FrequencyDelta>::max () / 10.e6,
std::numeric_limits<FrequencyDelta>::max () / 10.e6, this});
}
auto FrequencyDeltaLineEdit::frequency_delta () const -> FrequencyDelta

View File

@ -17,6 +17,7 @@
#include <QDebug>
#include <QDebugStateSaver>
#include "Radio.hpp"
#include "Bands.hpp"
#include "pimpl_impl.hpp"
@ -480,7 +481,7 @@ bool FrequencyList::impl::setData (QModelIndex const& model_index, QVariant cons
case frequency_column:
if (value.canConvert<Frequency> ())
{
auto frequency = value.value<Frequency> ();
Radio::Frequency frequency {qvariant_cast <Radio::Frequency> (value)};
if (frequency != item.frequency_)
{
item.frequency_ = frequency;

View File

@ -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);
}

View File

@ -34,8 +34,10 @@ namespace Radio
// QVariant argument is convertible to double and is assumed to
// be scaled by (10 ** -scale).
//
Frequency UDP_EXPORT frequency (QVariant const&, int scale, QLocale const& = QLocale ());
FrequencyDelta UDP_EXPORT frequency_delta (QVariant const&, int scale, QLocale const& = QLocale ());
Frequency UDP_EXPORT frequency (QVariant const&, int scale,
bool * ok = nullptr, QLocale const& = QLocale ());
FrequencyDelta UDP_EXPORT frequency_delta (QVariant const&, int scale,
bool * ok = nullptr, QLocale const& = QLocale ());
//
// Frequency type formatting

View File

@ -18,6 +18,7 @@
#include "pimpl_impl.hpp"
#include "Radio.hpp"
#include "Bands.hpp"
#include "FrequencyList.hpp"
@ -403,9 +404,16 @@ bool StationList::impl::setData (QModelIndex const& model_index, QVariant const&
case offset_column:
{
stations_[row].offset_ = value.value<FrequencyDelta> ();
Q_EMIT dataChanged (model_index, model_index, roles);
changed = true;
if (value.canConvert<FrequencyDelta> ())
{
FrequencyDelta offset {qvariant_cast<Radio::FrequencyDelta> (value)};
if (offset != stations_[row].offset_)
{
stations_[row].offset_ = offset;
Q_EMIT dataChanged (model_index, model_index, roles);
changed = true;
}
}
}
break;