Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve field conversions (fixes #12004)
  • Loading branch information
jef-n committed Feb 12, 2015
1 parent 749e2a5 commit 065d190
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/app/qgsattributetabledialog.cpp
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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() )
{
Expand Down
17 changes: 16 additions & 1 deletion src/core/qgsfield.cpp
Expand Up @@ -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;
}

This comment has been minimized.

Copy link
@wonder-sk

wonder-sk Feb 18, 2015

Member

@jef-n at least spatialite and memory layers use length=0 for text fields, so all text values from attribute form get nullified here. OK to remove this bit of code?

Even if the string does not fit into the field, shouldn't we just cut it instead of completely rejecting it?

This comment has been minimized.

Copy link
@jef-n

jef-n Feb 18, 2015

Author Member

fixed in b6dd1e4

return true;
}

Expand Down
6 changes: 5 additions & 1 deletion src/gui/editorwidgets/qgstexteditwrapper.cpp
Expand Up @@ -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 )
Expand Down

0 comments on commit 065d190

Please sign in to comment.