Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix widget wrong values 0 doubles #34152

Merged
merged 1 commit into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/gui/editorwidgets/qgstexteditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ QVariant QgsTextEditWrapper::value() const
v = mLineEdit->text();
}

if ( ( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
v == QgsApplication::nullRepresentation() )
if ( ( v.isEmpty() && ( field().type() == QVariant::Int
|| field().type() == QVariant::Double
|| field().type() == QVariant::LongLong
|| field().type() == QVariant::Date ) )
|| v == QgsApplication::nullRepresentation() )
{
return QVariant( field().type() );
}

if ( !defaultValue().isNull() && v == defaultValue().toString() )
{
Expand Down Expand Up @@ -323,9 +328,14 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
v = v.remove( QLocale().groupSeparator() );
}

if ( mTextEdit )
const QVariant currentValue { value( ) };
// Note: comparing QVariants leads to funny (and wrong) results:
// QVariant(0.0) == QVariant(QVariant.Double) -> True
const bool changed { val != currentValue || val.isNull() != currentValue.isNull() };

if ( changed )
{
if ( val != value() )
if ( mTextEdit )
{
if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
{
Expand All @@ -337,18 +347,19 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
}
}
else
{
mTextEdit->setPlainText( v );
}
}
}

if ( mPlainTextEdit )
{
if ( val != value() )
else if ( mPlainTextEdit )
{
mPlainTextEdit->setPlainText( v );
}
else if ( mLineEdit )
{
mLineEdit->setText( v );
}
}

if ( mLineEdit && val != value() )
mLineEdit->setText( v );
}

void QgsTextEditWrapper::setHint( const QString &hintText )
Expand Down
16 changes: 16 additions & 0 deletions tests/src/gui/testqgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TestQgsAttributeForm : public QObject
void testDefaultValueUpdate();
void testDefaultValueUpdateRecursion();
void testSameFieldSync();
void testZeroDoubles();

private:
QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww )
Expand Down Expand Up @@ -1109,5 +1110,20 @@ void TestQgsAttributeForm::testSameFieldSync()
QCOMPARE( les[1]->cursorPosition(), 4 );
}

void TestQgsAttributeForm::testZeroDoubles()
{
// See issue GH #34118
QString def = QStringLiteral( "Point?field=col0:double" );
QgsVectorLayer layer { def, QStringLiteral( "test" ), QStringLiteral( "memory" ) };
layer.setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() ) );
QgsFeature ft( layer.dataProvider()->fields(), 1 );
ft.setAttribute( QStringLiteral( "col0" ), 0.0 );
QgsAttributeForm form( &layer );
form.setFeature( ft );
QList<QLineEdit *> les = form.findChildren<QLineEdit *>( "col0" );
QCOMPARE( les.count(), 1 );
QCOMPARE( les.at( 0 )->text(), QStringLiteral( "0" ) );
}

QGSTEST_MAIN( TestQgsAttributeForm )
#include "testqgsattributeform.moc"