Skip to content

Commit

Permalink
fixes #29667 end feature setting mode before warning attribute form i…
Browse files Browse the repository at this point in the history
…nterface
  • Loading branch information
troopa81 authored and nyalldawson committed Jul 18, 2019
1 parent 7f17345 commit 29883f1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -266,6 +266,9 @@ void QgsAttributeForm::setFeature( const QgsFeature &feature )

synchronizeEnabledState();

// Settings of feature is done when we trigger the attribute form interface
// Issue https://github.com/qgis/QGIS/issues/29667
mIsSettingFeature = false;
const auto constMInterfaces = mInterfaces;
for ( QgsAttributeFormInterface *iface : constMInterfaces )
{
Expand Down
55 changes: 55 additions & 0 deletions tests/src/gui/testqgsattributeform.cpp
Expand Up @@ -16,6 +16,7 @@

#include "qgstest.h"
#include <QPushButton>
#include <QLineEdit>

#include <editorwidgets/core/qgseditorwidgetregistry.h>
#include "qgsattributeform.h"
Expand All @@ -27,6 +28,7 @@
#include <qgsvectorlayerjoininfo.h>
#include "qgsgui.h"
#include "qgsattributeformeditorwidget.h"
#include "qgsattributeforminterface.h"

class TestQgsAttributeForm : public QObject
{
Expand All @@ -47,6 +49,7 @@ class TestQgsAttributeForm : public QObject
void testConstraintsOnJoinedFields();
void testEditableJoin();
void testUpsertOnEdit();
void testAttributeFormInterface();

private:
QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww )
Expand Down Expand Up @@ -851,6 +854,58 @@ void TestQgsAttributeForm::testUpsertOnEdit()
delete layerC;
}

void TestQgsAttributeForm::testAttributeFormInterface()
{
// Issue https://github.com/qgis/QGIS/issues/29667
// we simulate a python code execution that would be triggered
// at form opening and that would modify the value of a widget.
// We want to check that emitted signal widgetValueChanged is
// correctly emitted with correct parameters

// make a temporary vector layer
QString def = QStringLiteral( "Point?field=col0:integer" );
QgsVectorLayer *layer = new QgsVectorLayer( def, QStringLiteral( "test" ), QStringLiteral( "memory" ) );
layer->setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() ) );

// add a feature to the vector layer
QgsFeature ft( layer->dataProvider()->fields(), 1 );
ft.setAttribute( QStringLiteral( "col0" ), 10 );

class MyInterface : public QgsAttributeFormInterface
{
public:
MyInterface( QgsAttributeForm *form )
: QgsAttributeFormInterface( form ) {}

virtual void featureChanged()
{
QgsAttributeForm *f = form();
QLineEdit *le = f->findChild<QLineEdit *>( "col0" );
le->setText( "100" );
}
};

// build a form for this feature
QgsAttributeForm form( layer );
form.addInterface( new MyInterface( &form ) );

bool setted = false;
connect( &form, &QgsAttributeForm::widgetValueChanged, this,
[&setted]( const QString & attribute, const QVariant & newValue, bool attributeChanged )
{

// Check that our value setted by the QgsAttributeFormInterface has correct parameters.
// attributeChanged has to be true because it won't be taken into account by others
// (QgsValueRelationWidgetWrapper for instance)
if ( attribute == "col0" && newValue.toInt() == 100 && attributeChanged )
setted = true;
} );

form.setFeature( ft );
QVERIFY( setted );
}



QGSTEST_MAIN( TestQgsAttributeForm )
#include "testqgsattributeform.moc"

0 comments on commit 29883f1

Please sign in to comment.