Implemented osgUI::Validator, IntValidator and DoubleValidator classes that manage validation/specialization of LineEdit widgets to work with just integer or double values.

git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14398 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
Robert Osfield
2014-08-05 18:32:45 +00:00
parent 0335e9c63a
commit fdd9efe45a
10 changed files with 491 additions and 35 deletions

View File

@@ -123,6 +123,14 @@ inline CallbackObject* getCallbackObject(osg::Object* object, const std::string&
return udc ? dynamic_cast<osg::CallbackObject*>(udc->getUserObject(name)) : 0;
}
/** Convinience function for getting the CallbackObject associated with specificed name from an Object's UserDataContainer.*/
inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
{
const osg::UserDataContainer* udc = object->getUserDataContainer();
return udc ? dynamic_cast<const osg::CallbackObject*>(udc->getUserObject(name)) : 0;
}
/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
{

View File

@@ -15,6 +15,7 @@
#define OSGUI_LINEEDIT
#include <osgUI/Widget>
#include <osgUI/Validator>
#include <osgText/Text>
namespace osgUI
@@ -35,9 +36,9 @@ public:
virtual void enterImplementation();
virtual void leaveImplementation();
virtual bool validate(const std::string& text);
virtual bool validateImplementation(const std::string& text);
void setValidator(Validator* validator) { _validator = validator; }
Validator* getValidator() { return _validator.get(); }
const Validator* getValidator() const { return _validator.get(); }
virtual void textChanged(const std::string& text);
virtual void textChangedImplementation(const std::string& text);
@@ -48,6 +49,8 @@ public:
protected:
virtual ~LineEdit() {}
osg::ref_ptr<Validator> _validator;
std::string _text;
// implementation detail

118
include/osgUI/Validator Normal file
View File

@@ -0,0 +1,118 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGUI_VALIDATOR
#define OSGUI_VALIDATOR
#include <osg/Object>
#include <osgUI/Export>
namespace osgUI
{
class OSGUI_EXPORT Validator : public osg::Object
{
public:
Validator();
Validator(const Validator& validator, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgUI, Validator);
enum State
{
INVALID,
INTERMEDIATE,
ACCEPTABLE
};
/** entry point to validate(..) method, checks for "validate" CallbackObject and calls it if present, otherwise calls validateImplementation(..)
str parameter is the string that needs to be validated
cursorpos is the position of the cursor within the str string.
return validatidy State. */
virtual State validate(std::string& /*str*/, int& cursorpos) const;
/// override in subclasses to proviude the validate implementation.
virtual State validateImplementation(std::string& /*str*/, int& /*cursorpos*/) const;
/** entry point to fixup, checks for "validate" Callbac Object and calls it if present, otherwise calls validateImplementation(..)
fixup(..) is called when user pressers return/enter in a field being edited.
str parameter is string that needs to be corrected.*/
virtual void fixup(std::string& /*str*/) const;
/// override in subclass to provide the fixup implementation.
virtual void fixupImplementation(std::string& /*str*/) const;
protected:
virtual ~Validator() {}
};
class OSGUI_EXPORT IntValidator : public Validator
{
public:
IntValidator();
IntValidator(const IntValidator& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgUI, IntValidator);
/// set the bottom value that is accepted as valid, default -INT_MAX
void setBottom(int bottom) { _bottom = bottom; }
int getBottom() const { return _bottom; }
/// set the top value that is accepted as valid, default INT_MAX
void setTop(int top) { _top = top; }
int getTop() const { return _top; }
/// override validate implementation.
virtual State validateImplementation(std::string& str, int& cursorpos) const;
/// override validate implementation.
virtual void fixupImplementation(std::string& str) const;
protected:
virtual ~IntValidator() {}
int _bottom;
int _top;
};
class OSGUI_EXPORT DoubleValidator : public Validator
{
public:
DoubleValidator();
DoubleValidator(const DoubleValidator& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgUI, DoubleValidator);
/** set the number of decimal places to accept, default is -1,
all negative values disable validation against maximum number of places thus allows any number of decimals places. */
void setDecimals(int numDecimals) { _decimals = numDecimals; }
int getDecimals() const { return _decimals; }
/// set the bottom value that is accepted as valid, default -DBL_MAX
void setBottom(double bottom) { _bottom = bottom; }
double getBottom() const { return _bottom; }
/// set the top value that is accepted as valid, default DBL_MAX
void setTop(double top) { _top = top; }
double getTop() const { return _top; }
/// override validate implementation.
virtual State validateImplementation(std::string& str, int& cursorpos) const;
/// override validate implementation.
virtual void fixupImplementation(std::string& str) const;
protected:
virtual ~DoubleValidator() {}
int _decimals;
double _bottom;
double _top;
};
}
#endif