diff --git a/src/Gui/InputField.cpp b/src/Gui/InputField.cpp index f2a07a272..a3a1ce118 100644 --- a/src/Gui/InputField.cpp +++ b/src/Gui/InputField.cpp @@ -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 ) : QLineEdit(parent), StepSize(1.0), @@ -48,6 +65,7 @@ InputField::InputField ( QWidget * parent ) HistorySize(5), SaveSize(5) { + setValidator(new InputValidator(this)); iconLabel = new QLabel(this); iconLabel->setCursor(Qt::ArrowCursor); 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 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(); } +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" diff --git a/src/Gui/InputField.h b/src/Gui/InputField.h index ceb68fe71..8084a627d 100644 --- a/src/Gui/InputField.h +++ b/src/Gui/InputField.h @@ -24,6 +24,7 @@ #ifndef GUI_INPUTFIELD_H #define GUI_INPUTFIELD_H +#include #include #include #include "Widgets.h" @@ -98,6 +99,10 @@ public: QString getUnitText(void); /// set the number portion selected (use after setValue()) 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 */ //@{ @@ -165,8 +170,6 @@ private: int SaveSize; }; - - } // namespace Gui #endif // GUI_INPUTFIELD_H