bool QgsField::convertCompatible( QVariant& v ) const { if ( v.isNull() ) { v.convert( d->type ); return true; } if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() ) { v = QVariant( d->type ); return false; } //String representations of doubles in QVariant will return false to convert( QVariant::Int ) //work around this by first converting to double, and then checking whether the double is convertible to int if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) ) { bool ok = false; double dbl = v.toDouble( &ok ); if ( !ok ) { //couldn't convert to number v = QVariant( d->type ); return false; } double round = qgsRound( dbl ); if ( round > INT_MAX || round < -INT_MAX ) { //double too large to fit in int v = QVariant( d->type ); return false; } v = QVariant( qRound( dbl ) ); return true; } if ( !v.convert( d->type ) ) { v = QVariant( d->type ); return false; } if ( d->type == QVariant::Double && d->precision > 0 ) { double s = qPow( 10, d->precision ); double d = v.toDouble() * s; v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s ); return true; } if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length ) { v = v.toString().left( d->length ); return false; } return true; }
double QgsProcessingNumericWidgetWrapper::calculateStep( const double minimum, const double maximum ) { const double valueRange = maximum - minimum; if ( valueRange <= 1.0 ) { const double step = valueRange / 10.0; // round to 1 significant figure return qgsRound( step, -std::floor( std::log( step ) ) ); } else { return 1.0; } }