Validator 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGUI_VALIDATOR
  14. #define OSGUI_VALIDATOR
  15. #include <osg/Object>
  16. #include <osgUI/Export>
  17. namespace osgUI
  18. {
  19. class OSGUI_EXPORT Validator : public osg::Object
  20. {
  21. public:
  22. Validator();
  23. Validator(const Validator& validator, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  24. META_Object(osgUI, Validator);
  25. enum State
  26. {
  27. INVALID,
  28. INTERMEDIATE,
  29. ACCEPTABLE
  30. };
  31. /** entry point to validate(..) method, checks for "validate" CallbackObject and calls it if present, otherwise calls validateImplementation(..)
  32. str parameter is the string that needs to be validated
  33. cursorpos is the position of the cursor within the str string.
  34. return validatidy State. */
  35. virtual State validate(std::string& /*str*/, int& cursorpos) const;
  36. /// override in subclasses to proviude the validate implementation.
  37. virtual State validateImplementation(std::string& /*str*/, int& /*cursorpos*/) const;
  38. /** entry point to fixup, checks for "validate" Callbac Object and calls it if present, otherwise calls validateImplementation(..)
  39. fixup(..) is called when user pressers return/enter in a field being edited.
  40. str parameter is string that needs to be corrected.*/
  41. virtual void fixup(std::string& /*str*/) const;
  42. /// override in subclass to provide the fixup implementation.
  43. virtual void fixupImplementation(std::string& /*str*/) const;
  44. protected:
  45. virtual ~Validator() {}
  46. };
  47. class OSGUI_EXPORT IntValidator : public Validator
  48. {
  49. public:
  50. IntValidator();
  51. IntValidator(const IntValidator& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  52. META_Object(osgUI, IntValidator);
  53. /// set the bottom value that is accepted as valid, default -INT_MAX
  54. void setBottom(int bottom) { _bottom = bottom; }
  55. int getBottom() const { return _bottom; }
  56. /// set the top value that is accepted as valid, default INT_MAX
  57. void setTop(int top) { _top = top; }
  58. int getTop() const { return _top; }
  59. /// override validate implementation.
  60. virtual State validateImplementation(std::string& str, int& cursorpos) const;
  61. /// override validate implementation.
  62. virtual void fixupImplementation(std::string& str) const;
  63. protected:
  64. virtual ~IntValidator() {}
  65. int _bottom;
  66. int _top;
  67. };
  68. class OSGUI_EXPORT DoubleValidator : public Validator
  69. {
  70. public:
  71. DoubleValidator();
  72. DoubleValidator(const DoubleValidator& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  73. META_Object(osgUI, DoubleValidator);
  74. /** set the number of decimal places to accept, default is -1,
  75. all negative values disable validation against maximum number of places thus allows any number of decimals places. */
  76. void setDecimals(int numDecimals) { _decimals = numDecimals; }
  77. int getDecimals() const { return _decimals; }
  78. /// set the bottom value that is accepted as valid, default -DBL_MAX
  79. void setBottom(double bottom) { _bottom = bottom; }
  80. double getBottom() const { return _bottom; }
  81. /// set the top value that is accepted as valid, default DBL_MAX
  82. void setTop(double top) { _top = top; }
  83. double getTop() const { return _top; }
  84. /// override validate implementation.
  85. virtual State validateImplementation(std::string& str, int& cursorpos) const;
  86. /// override validate implementation.
  87. virtual void fixupImplementation(std::string& str) const;
  88. protected:
  89. virtual ~DoubleValidator() {}
  90. int _decimals;
  91. double _bottom;
  92. double _top;
  93. };
  94. }
  95. #endif