bool FloatingPointValidatorBase::DoValidateNumber(wxString * errMsg) const
{
   wxTextEntry * const control = GetTextEntry();
   if ( !control )
      return false;

   wxString s(control->GetValue());
   wxChar thousandsSep;
   if ( NumberFormatter::GetThousandsSeparatorIfUsed(&thousandsSep) )
      s.Replace(wxString(thousandsSep), wxString());

   if ( s.empty() )
   {
      if ( HasFlag(NUM_VAL_ZERO_AS_BLANK) )
         return true; //Is blank, but allowed. Stop here
      else
      {
         *errMsg = _("Empty value");
         return false; //We can't do any checks with an empty string
      }
   }

   LongestValueType value;
   bool res = FromString(s, &value); // Can it be converted to a value?
   if ( !res )
      *errMsg = _("Value overflow");
   else
   {
      res = ValidatePrecision(s);
      if ( !res )
         *errMsg = _("Too many decimal digits");
      else
      {
         res = IsInRange(value);
         if ( !res )
         {
            wxString strMin = wxString::Format(wxT("%f"), m_min);
            wxString strMax = wxString::Format(wxT("%f"), m_max);
            NumberFormatter::RemoveTrailingZeroes(strMin);
            NumberFormatter::RemoveTrailingZeroes(strMax);

            if (m_minSet && m_maxSet)
            {
               errMsg->Printf(_("Value not in range: %s to %s"),
                              strMin, strMax);
            }
            else if (m_minSet)
            {
               errMsg->Printf(_("Value must not be less than %s"), strMin);
            }
            else if (m_maxSet)
            {
               errMsg->Printf(_("Value must not be greather than %s"), strMax);
            }
         }
      }
   }

   return res;
}
Example #2
0
static bool ValidateCoordinate(const QString& name, double value)
{
  auto hasValidPrecision = ValidatePrecision(value);
  if (!hasValidPrecision)
    EmitWarning(QString("Point set %1 coordinate is outside double precision range.").arg(name), "Invalid point set input");

  return hasValidPrecision;
}
bool
FloatingPointValidatorBase::IsCharOk(const wxString& val,
                                       int pos,
                                       wxChar ch) const
{
   if ( ch == '-' )
   {
      // We may accept minus sign if we can represent negative numbers at all.
      if ( pos == 0 )
         return m_min < 0 && IsMinusOk(val, pos);
      // or for the exponent definition
      else if ( val[pos-1] != 'e' && val[pos-1] != 'E' )
         return false;

      return true;
   }
   else if ( ch == '+' )
   {
      if ( pos == 0 )
         return m_max >= 0;
      else if ( val[pos-1] != 'e' && val[pos-1] != 'E' )
         return false;

      return true;
   }

   const wxChar separator = NumberFormatter::GetDecimalSeparator();
   if ( ch == separator )
   {
      if ( val.find(separator) != wxString::npos )
      {
         // There is already a decimal separator, can't insert another one.
         return false;
      }

      // Prepending a separator before the sign isn't allowed.
      if ( pos == 0 && !val.empty() && ( val[0] == '-' || val[0] == '+' ) )
         return false;

      // Otherwise always accept it, adding a decimal separator doesn't
      // change the number value and, in particular, can't make it invalid.
      // OTOH the checks below might not pass because strings like "." or
      // "-." are not valid numbers so parsing them would fail, hence we need
      // to treat it specially here.
      return true;
   }

   // Must be a digit, an exponent or a thousands separator.
   if( ( ch < '0' || ch > '9' ) && ch != 'E' && ch != 'e' )
   {
      wxChar thousands;
      if ( NumberFormatter::GetThousandsSeparatorIfUsed(&thousands) )
      {
//            if (ch != thousands)
               return false;
      }
      else
      {
         return false;
      }
   }

   // Check the number of decimal digits in the final string
   wxString str(val);
   str.insert(pos, ch);
   return ValidatePrecision(str);
}