Skip to content

Commit 065d190

Browse files
committedFeb 12, 2015
improve field conversions (fixes #12004)
1 parent 749e2a5 commit 065d190

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed
 

‎src/app/qgsattributetabledialog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
332332
bool calculationSuccess = true;
333333
QString error;
334334

335-
336335
QgsExpression exp( expression );
337336
exp.setGeomCalculator( *myDa );
338337
bool useGeometry = exp.needsGeometry();
@@ -342,6 +341,8 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
342341

343342
int rownum = 1;
344343

344+
const QgsField &fld = layer->pendingFields()[ fieldindex ];
345+
345346
//go through all the features and change the new attributes
346347
QgsFeatureIterator fit = layer->getFeatures( request );
347348
QgsFeature feature;
@@ -354,6 +355,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, QStrin
354355

355356
exp.setCurrentRowNumber( rownum );
356357
QVariant value = exp.evaluate( &feature );
358+
fld.convertCompatible( value );
357359
// Bail if we have a update error
358360
if ( exp.hasEvalError() )
359361
{

‎src/core/qgsfield.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,32 @@ bool QgsField::convertCompatible( QVariant& v ) const
137137
return true;
138138
}
139139

140+
if ( mType == QVariant::Int && v.toInt() != v.toLongLong() )
141+
{
142+
v = QVariant( mType );
143+
return false;
144+
}
145+
140146
if ( !v.convert( mType ) )
141147
{
148+
v = QVariant( mType );
142149
return false;
143150
}
144151

145152
if ( mType == QVariant::Double && mPrecision > 0 )
146153
{
147-
v = qRound64( v.toDouble() * qPow( 10, mPrecision ) ) / qPow( 10, mPrecision );
154+
double s = qPow( 10, mPrecision );
155+
double d = v.toDouble() * s;
156+
v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
148157
return true;
149158
}
150159

160+
if ( mType == QVariant::String && mLength >= 0 && v.toString().length() > mLength )
161+
{
162+
v = QVariant( mType );
163+
return false;
164+
}
165+
Code has comments. Press enter to view.
151166
return true;
152167
}
153168

‎src/gui/editorwidgets/qgstexteditwrapper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ QVariant QgsTextEditWrapper::value()
5858
if (( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
5959
v == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
6060
return QVariant( field().type() );
61+
62+
QVariant res( v );
63+
if ( field().convertCompatible( res ) )
64+
return res;
6165
else
62-
return QVariant( v );
66+
return QVariant( field().type() );
6367
}
6468

6569
QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )

0 commit comments

Comments
 (0)
Please sign in to comment.