+ implement input validator for range checking
This commit is contained in:
parent
9c07f81240
commit
61919e89ab
|
@ -40,6 +40,23 @@ using namespace Base;
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace Gui {
|
||||||
|
class InputValidator : public QValidator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
InputValidator(InputField* parent);
|
||||||
|
~InputValidator();
|
||||||
|
|
||||||
|
void fixup(QString& input) const;
|
||||||
|
State validate(QString& input, int& pos) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
InputField* dptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
InputField::InputField ( QWidget * parent )
|
InputField::InputField ( QWidget * parent )
|
||||||
: QLineEdit(parent),
|
: QLineEdit(parent),
|
||||||
StepSize(1.0),
|
StepSize(1.0),
|
||||||
|
@ -48,6 +65,7 @@ InputField::InputField ( QWidget * parent )
|
||||||
HistorySize(5),
|
HistorySize(5),
|
||||||
SaveSize(5)
|
SaveSize(5)
|
||||||
{
|
{
|
||||||
|
setValidator(new InputValidator(this));
|
||||||
iconLabel = new QLabel(this);
|
iconLabel = new QLabel(this);
|
||||||
iconLabel->setCursor(Qt::ArrowCursor);
|
iconLabel->setCursor(Qt::ArrowCursor);
|
||||||
QPixmap pixmap = BitmapFactory().pixmapFromSvg(":/icons/button_valid.svg", QSize(sizeHint().height(),sizeHint().height()));
|
QPixmap pixmap = BitmapFactory().pixmapFromSvg(":/icons/button_valid.svg", QSize(sizeHint().height(),sizeHint().height()));
|
||||||
|
@ -408,12 +426,61 @@ void InputField::wheelEvent (QWheelEvent * event)
|
||||||
{
|
{
|
||||||
double step = event->delta() > 0 ? StepSize : -StepSize;
|
double step = event->delta() > 0 ? StepSize : -StepSize;
|
||||||
double val = actUnitValue + step;
|
double val = actUnitValue + step;
|
||||||
|
if (val > Maximum)
|
||||||
|
val = Maximum;
|
||||||
|
else if (val < Minimum)
|
||||||
|
val = Minimum;
|
||||||
|
|
||||||
this->setText(QString::fromUtf8("%L1 %2").arg(val).arg(actUnitStr));
|
this->setText(QString::fromUtf8("%L1 %2").arg(val).arg(actUnitStr));
|
||||||
event->accept();
|
event->accept();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InputField::fixup(QString& input) const
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QValidator::State InputField::validate(QString& input, int& pos) const
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Quantity res;
|
||||||
|
res = Quantity::parse(input);
|
||||||
|
|
||||||
|
double factor;
|
||||||
|
QString unitStr;
|
||||||
|
res.getUserString(factor, unitStr);
|
||||||
|
double value = res.getValue()/factor;
|
||||||
|
// disallow to enter numbers out of range
|
||||||
|
if (value > this->Maximum || value < this->Minimum)
|
||||||
|
return QValidator::Invalid;
|
||||||
|
}
|
||||||
|
catch(Base::Exception&) {
|
||||||
|
// Actually invalid input but the newInput slot gives
|
||||||
|
// some feedback
|
||||||
|
return QValidator::Intermediate;
|
||||||
|
}
|
||||||
|
|
||||||
|
return QValidator::Acceptable;
|
||||||
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
|
InputValidator::InputValidator(InputField* parent) : QValidator(parent), dptr(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
InputValidator::~InputValidator()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputValidator::fixup(QString& input) const
|
||||||
|
{
|
||||||
|
dptr->fixup(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
QValidator::State InputValidator::validate(QString& input, int& pos) const
|
||||||
|
{
|
||||||
|
return dptr->validate(input, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#include "moc_InputField.cpp"
|
#include "moc_InputField.cpp"
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#ifndef GUI_INPUTFIELD_H
|
#ifndef GUI_INPUTFIELD_H
|
||||||
#define GUI_INPUTFIELD_H
|
#define GUI_INPUTFIELD_H
|
||||||
|
|
||||||
|
#include <QValidator>
|
||||||
#include <Base/Parameter.h>
|
#include <Base/Parameter.h>
|
||||||
#include <Base/Quantity.h>
|
#include <Base/Quantity.h>
|
||||||
#include "Widgets.h"
|
#include "Widgets.h"
|
||||||
|
@ -98,6 +99,10 @@ public:
|
||||||
QString getUnitText(void);
|
QString getUnitText(void);
|
||||||
/// set the number portion selected (use after setValue())
|
/// set the number portion selected (use after setValue())
|
||||||
void selectNumber(void);
|
void selectNumber(void);
|
||||||
|
/// input validation
|
||||||
|
void fixup(QString& input) const;
|
||||||
|
/// input validation
|
||||||
|
QValidator::State validate(QString& input, int& pos) const;
|
||||||
|
|
||||||
/** @name history and default management */
|
/** @name history and default management */
|
||||||
//@{
|
//@{
|
||||||
|
@ -165,8 +170,6 @@ private:
|
||||||
int SaveSize;
|
int SaveSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace Gui
|
} // namespace Gui
|
||||||
|
|
||||||
#endif // GUI_INPUTFIELD_H
|
#endif // GUI_INPUTFIELD_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user