improve field conversions (fixes #12004)

This commit is contained in:
Juergen E. Fischer 2015-02-12 23:40:37 +01:00
parent 749e2a5bdb
commit 065d190d85
3 changed files with 24 additions and 3 deletions

View File

@ -332,7 +332,6 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
bool calculationSuccess = true;
QString error;
QgsExpression exp( expression );
exp.setGeomCalculator( *myDa );
bool useGeometry = exp.needsGeometry();
@ -342,6 +341,8 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
int rownum = 1;
const QgsField &fld = layer->pendingFields()[ fieldindex ];
//go through all the features and change the new attributes
QgsFeatureIterator fit = layer->getFeatures( request );
QgsFeature feature;
@ -354,6 +355,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
exp.setCurrentRowNumber( rownum );
QVariant value = exp.evaluate( &feature );
fld.convertCompatible( value );
// Bail if we have a update error
if ( exp.hasEvalError() )
{

View File

@ -137,17 +137,32 @@ bool QgsField::convertCompatible( QVariant& v ) const
return true;
}
if ( mType == QVariant::Int && v.toInt() != v.toLongLong() )
{
v = QVariant( mType );
return false;
}
if ( !v.convert( mType ) )
{
v = QVariant( mType );
return false;
}
if ( mType == QVariant::Double && mPrecision > 0 )
{
v = qRound64( v.toDouble() * qPow( 10, mPrecision ) ) / qPow( 10, mPrecision );
double s = qPow( 10, mPrecision );
double d = v.toDouble() * s;
v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
return true;
}
if ( mType == QVariant::String && mLength >= 0 && v.toString().length() > mLength )
{
v = QVariant( mType );
return false;
}
return true;
}

View File

@ -58,8 +58,12 @@ QVariant QgsTextEditWrapper::value()
if (( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
v == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
return QVariant( field().type() );
QVariant res( v );
if ( field().convertCompatible( res ) )
return res;
else
return QVariant( v );
return QVariant( field().type() );
}
QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )