Skip to content
Permalink
Browse files

improve field conversions (fixes #12004)

  • Loading branch information
jef-n committed Feb 12, 2015
1 parent 749e2a5 commit 065d190d8548a3eb4696b3c9ee4cc2db14cc8cac
Showing with 24 additions and 3 deletions.
  1. +3 −1 src/app/qgsattributetabledialog.cpp
  2. +16 −1 src/core/qgsfield.cpp
  3. +5 −1 src/gui/editorwidgets/qgstexteditwrapper.cpp
@@ -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() )
{
@@ -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;
}

@@ -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 )

0 comments on commit 065d190

Please sign in to comment.
You can’t perform that action at this time.