ccCustomDoubleValidator.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_CUSTOM_DOUBLE_VALIDATOR_HEADER
  18. #define CC_CUSTOM_DOUBLE_VALIDATOR_HEADER
  19. //Qt
  20. #include <QValidator>
  21. #include <QString>
  22. //! Validator class (accepts only double numbers and replaces the comma by a point automatically)
  23. class ccCustomDoubleValidator : public QValidator
  24. {
  25. Q_OBJECT
  26. public:
  27. //! Default constructor
  28. explicit ccCustomDoubleValidator(QObject * parent = 0) : QValidator(parent)
  29. {}
  30. //reimplemented from QValidator
  31. State validate(QString& input, int& pos) const
  32. {
  33. for (int i=0; i<input.size(); ++i)
  34. {
  35. QChar c = input[i];
  36. if (c == ',')
  37. {
  38. input[i] = '.';
  39. continue;
  40. }
  41. else if (c == '.' || c == '-' || c.isDigit())
  42. {
  43. continue;
  44. }
  45. else
  46. {
  47. return Invalid;
  48. }
  49. }
  50. return Acceptable;
  51. }
  52. };
  53. #endif // CC_CUSTOM_DOUBLE_VALIDATOR_HEADER