WSJT-X/MaidenheadLocatorValidator.cpp
Bill Somerville b6e81cfe8b Add proper Qt validation for callsigns and grids
Two      new     validator      classes     CallsignValidator      and
MaidenheadLocatorValidator   are   introduced    and   used   in   the
Configuration and MainWindow implementations.

MaidenheadLocatorValidator  supports  different  lengths  and  minimum
required  lengths  with  a  default of  subsquare  with  square  being
required.

The  message_aggregator  application has  been  enhanced  to show  the
current DX call and DX grid as shown in the WSJT-X main window.

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6903 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
2016-07-10 22:20:30 +00:00

74 lines
2.4 KiB
C++

#include "MaidenheadLocatorValidator.hpp"
MaidenheadLocatorValidator::MaidenheadLocatorValidator (QObject * parent, Length length, Length required)
: QValidator {parent}
{
switch (length)
{
case Length::field:
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})$)"});
break;
case Length::square:
if (Length::field == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})([0-9]{2}){0,1}$)"});
}
else
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}$)"});
}
break;
case Length::subsquare:
if (Length::field == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})([0-9]{2}((?<subsquare>[A-Xa-x]{2}){0,1})){0,1}$)"});
}
else if (Length::square == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2}){0,1}$)"});
}
else
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2})$)"});
}
break;
case Length::extended:
if (Length::field == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})([0-9]{2}((?<subsquare>[A-Xa-x]{2}){0,1}([0-9]{2}){0,1})){0,1}$)"});
}
else if (Length::square == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}((?<subsquare>[A-Xa-x]{2})([0-9]{2}){0,1}){0,1}$)"});
}
else if (Length::subsquare == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2})([0-9]{2}){0,1}$)"});
}
else
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2})[0-9]{2}$)"});
}
break;
}
}
auto MaidenheadLocatorValidator::validate (QString& input, int& pos) const -> State
{
auto match = re_.match (input, 0, QRegularExpression::PartialPreferCompleteMatch);
auto field = match.captured ("field");
if (field.size ())
{
input.replace (match.capturedStart ("field"), match.capturedLength ("field"), field.toUpper ());
}
auto subsquare = match.captured ("subsquare");
if (subsquare.size ())
{
input.replace (match.capturedStart ("subsquare"), match.capturedLength ("subsquare"), subsquare.toLower ());
}
if (match.hasMatch ()) return Acceptable;
if (!input.size () || match.hasPartialMatch ()) return Intermediate;
pos = input.size ();
return Invalid;
}