void SpinBox::setType(SpinBoxTypeEnum type) { _imp->type = type; delete _imp->doubleValidator; _imp->doubleValidator = 0; delete _imp->intValidator; _imp->intValidator = 0; switch (_imp->type) { case eSpinBoxTypeDouble: _imp->mini.setValue<double>(-DBL_MAX); _imp->maxi.setValue<double>(DBL_MAX); _imp->doubleValidator = new QDoubleValidator; _imp->doubleValidator->setTop(DBL_MAX); _imp->doubleValidator->setBottom(-DBL_MAX); setValue_internal(value(), true); break; case eSpinBoxTypeInt: _imp->intValidator = new QIntValidator; _imp->mini.setValue<int>(INT_MIN); _imp->maxi.setValue<int>(INT_MAX); _imp->intValidator->setTop(INT_MAX); _imp->intValidator->setBottom(INT_MIN); setValue_internal( (int)std::floor(value() + 0.5), true ); break; } }
bool SpinBox::validateWithCustomValidator(const QString& txt) { if (_imp->customValidator) { double valueToDisplay; if (_imp->customValidator->validateInput(txt, &valueToDisplay)) { setValue_internal(_imp->type == eSpinBoxTypeDouble ? valueToDisplay : (int)valueToDisplay, true); return true; } } setValue_internal(_imp->valueAfterLastValidation, true); return false; }
bool SpinBox::validateInternal() { QString txt = text(); const QValidator* validator = 0; if (_imp->type == eSpinBoxTypeDouble) { validator = _imp->doubleValidator; } else { assert(_imp->type == eSpinBoxTypeInt); validator = _imp->intValidator; } assert(validator); int tmp; QValidator::State st = QValidator::Invalid; if ( !txt.isEmpty() ) { st = validator->validate(txt, tmp); } double val; double maxiD, miniD; if (_imp->type == eSpinBoxTypeDouble) { val = txt.toDouble(); maxiD = _imp->maxi.toDouble(); miniD = _imp->mini.toDouble(); } else { assert(_imp->type == eSpinBoxTypeInt); val = (double)txt.toInt(); maxiD = _imp->maxi.toInt(); miniD = _imp->mini.toInt(); } if (st == QValidator::Invalid) { return validateWithCustomValidator(txt); } else if ( (val < miniD) || (val > maxiD) ) { setValue_internal(_imp->valueAfterLastValidation, true); return false; } else { setValue_internal(val, false); return true; } }
void SpinBox::setValue(double d) { setValue_internal(d, false); }