openscenegraph
osgUI/Validator
Go to the documentation of this file.
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
14#ifndef OSGUI_VALIDATOR
15#define OSGUI_VALIDATOR
16
17#include <osg/Object>
18#include <osgUI/Export>
19
20namespace osgUI
21{
22
23class OSGUI_EXPORT Validator : public osg::Object
24{
25public:
26 Validator();
27 Validator(const Validator& validator, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
28 META_Object(osgUI, Validator);
29
30 enum State
31 {
32 INVALID,
33 INTERMEDIATE,
34 ACCEPTABLE
35 };
36
37 /** entry point to validate(..) method, checks for "validate" CallbackObject and calls it if present, otherwise calls validateImplementation(..)
38 str parameter is the string that needs to be validated
39 cursorpos is the position of the cursor within the str string.
40 return validatidy State. */
41 virtual State validate(std::string& /*str*/, int& cursorpos) const;
42
43 /// override in subclasses to proviude the validate implementation.
44 virtual State validateImplementation(std::string& /*str*/, int& /*cursorpos*/) const;
45
46 /** entry point to fixup, checks for "validate" Callbac Object and calls it if present, otherwise calls validateImplementation(..)
47 fixup(..) is called when user pressers return/enter in a field being edited.
48 str parameter is string that needs to be corrected.*/
49 virtual void fixup(std::string& /*str*/) const;
50
51 /// override in subclass to provide the fixup implementation.
52 virtual void fixupImplementation(std::string& /*str*/) const;
53
54protected:
55 virtual ~Validator() {}
56};
57
58class OSGUI_EXPORT IntValidator : public Validator
59{
60public:
61 IntValidator();
62 IntValidator(const IntValidator& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
63 META_Object(osgUI, IntValidator);
64
65 /// set the bottom value that is accepted as valid, default -INT_MAX
66 void setBottom(int bottom) { _bottom = bottom; }
67 int getBottom() const { return _bottom; }
68
69 /// set the top value that is accepted as valid, default INT_MAX
70 void setTop(int top) { _top = top; }
71 int getTop() const { return _top; }
72
73 /// override validate implementation.
74 virtual State validateImplementation(std::string& str, int& cursorpos) const;
75 /// override validate implementation.
76 virtual void fixupImplementation(std::string& str) const;
77
78protected:
79 virtual ~IntValidator() {}
80 int _bottom;
81 int _top;
82};
83
84class OSGUI_EXPORT DoubleValidator : public Validator
85{
86public:
87 DoubleValidator();
88 DoubleValidator(const DoubleValidator& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
89 META_Object(osgUI, DoubleValidator);
90
91 /** set the number of decimal places to accept, default is -1,
92 all negative values disable validation against maximum number of places thus allows any number of decimals places. */
93 void setDecimals(int numDecimals) { _decimals = numDecimals; }
94 int getDecimals() const { return _decimals; }
95
96 /// set the bottom value that is accepted as valid, default -DBL_MAX
97 void setBottom(double bottom) { _bottom = bottom; }
98 double getBottom() const { return _bottom; }
99
100 /// set the top value that is accepted as valid, default DBL_MAX
101 void setTop(double top) { _top = top; }
102 double getTop() const { return _top; }
103
104 /// override validate implementation.
105 virtual State validateImplementation(std::string& str, int& cursorpos) const;
106 /// override validate implementation.
107 virtual void fixupImplementation(std::string& str) const;
108
109protected:
110 virtual ~DoubleValidator() {}
111 int _decimals;
112 double _bottom;
113 double _top;
114};
115
116}
117
118#endif