diff --git a/app/attributes/attributecontroller.cpp b/app/attributes/attributecontroller.cpp index 45ebd462a..22e3b655d 100644 --- a/app/attributes/attributecontroller.cpp +++ b/app/attributes/attributecontroller.cpp @@ -813,7 +813,7 @@ void AttributeController::recalculateDerivedItems( bool isFormValueChange, bool } // Evaluate HTML and Text element expressions - recalculateRichTextWidgets(changedFormItems, expressionContext); + recalculateRichTextWidgets( changedFormItems, expressionContext ); // Evaluate tab items visiblity { @@ -934,7 +934,7 @@ void AttributeController::recalculateDerivedItems( bool isFormValueChange, bool emit formRecalculated(); } -void AttributeController::recalculateRichTextWidgets( QSet &changedFormItems, QgsExpressionContext &context) +void AttributeController::recalculateRichTextWidgets( QSet &changedFormItems, QgsExpressionContext &context ) { QMap>::iterator formItemsIterator = mFormItems.begin(); while ( formItemsIterator != mFormItems.end() ) @@ -943,9 +943,10 @@ void AttributeController::recalculateRichTextWidgets( QSet &changedFormIt if ( itemData->type() == FormItem::RichText ) { QString newValue; - QString definition = itemData->editorWidgetConfig().value(QStringLiteral("Definition")).toString(); - bool isHTML = itemData->editorWidgetConfig().value(QStringLiteral("UseHtml")).toBool(); - if (isHTML) { + QString definition = itemData->editorWidgetConfig().value( QStringLiteral( "Definition" ) ).toString(); + bool isHTML = itemData->editorWidgetConfig().value( QStringLiteral( "UseHtml" ) ).toBool(); + if ( isHTML ) + { // evaluate texts like: // QML Text does not support document.write, so just remove it @@ -954,7 +955,7 @@ void AttributeController::recalculateRichTextWidgets( QSet &changedFormIt while ( match1.hasMatch() ) { QString expression = match1.captured( 1 ); - definition = QStringLiteral( "%1" ).arg(definition.mid( 0, match1.capturedStart( 0 ) ) + expression + definition.mid( match1.capturedEnd( 0 ) )); + definition = QStringLiteral( "%1" ).arg( definition.mid( 0, match1.capturedStart( 0 ) ) + expression + definition.mid( match1.capturedEnd( 0 ) ) ); match1 = sRegEx1.match( definition ); } @@ -974,13 +975,14 @@ void AttributeController::recalculateRichTextWidgets( QSet &changedFormIt } newValue = definition; } - else { + else + { newValue = QgsExpression::replaceExpressionText( definition, &context ); } - if (itemData->rawValue() != newValue) + if ( itemData->rawValue() != newValue ) { changedFormItems.insert( itemData->id() ); - itemData->setRawValue(newValue); + itemData->setRawValue( newValue ); } } ++formItemsIterator; diff --git a/app/attributes/attributecontroller.h b/app/attributes/attributecontroller.h index 200873a4d..fd981f396 100644 --- a/app/attributes/attributecontroller.h +++ b/app/attributes/attributecontroller.h @@ -194,7 +194,7 @@ class AttributeController : public QObject */ void recalculateDerivedItems( bool isFormValueChange = false, bool isFirstUpdateOfNewFeature = false ); bool recalculateDefaultValues( QSet &changedFormItems, QgsExpressionContext &context, bool isFormValueChange = false, bool isFirstUpdateOfNewFeature = false ); - void recalculateRichTextWidgets( QSet &changedFormItems, QgsExpressionContext &context); + void recalculateRichTextWidgets( QSet &changedFormItems, QgsExpressionContext &context ); // generate tab void createTab( QgsAttributeEditorContainer *container ); diff --git a/app/test/testattributecontroller.cpp b/app/test/testattributecontroller.cpp index 9828554e4..4cbf033bf 100644 --- a/app/test/testattributecontroller.cpp +++ b/app/test/testattributecontroller.cpp @@ -656,7 +656,7 @@ void TestAttributeController::testFieldsOutsideForm() const TabItem *tab = controller.tabItem( 0 ); const QVector items = tab->formItems(); - QCOMPARE( items.size(), 3 ); + QCOMPARE( items.size(), 7 ); struct testcase { @@ -744,7 +744,6 @@ void TestAttributeController::testHtmlAndTextWidgets() QVERIFY( QgsProject::instance()->read( projectDir + "/" + projectName ) ); - /* QgsMapLayer *layer = QgsProject::instance()->mapLayersByName( QStringLiteral( "points" ) ).at( 0 ); QgsVectorLayer *surveyLayer = static_cast( layer ); @@ -762,43 +761,29 @@ void TestAttributeController::testHtmlAndTextWidgets() const TabItem *tab = controller.tabItem( 0 ); const QVector items = tab->formItems(); - QCOMPARE( items.size(), 3 ); + QCOMPARE( items.size(), 7 ); - struct testcase - { - QUuid id; - QVariant value; - QVariant expectedText; - QVariant expectedText2; - QVariant expectedNum; - QVariant expectedNum2; - }; + const auto htmlItem = controller.formItem( items.at( 3 ) ); + QCOMPARE( htmlItem->editorWidgetType(), "richtext" ); + QCOMPARE( htmlItem->editorWidgetConfig().value( "UseHtml" ).toBool(), true ); - QList testCases - { - { items.at( 0 ), QVariant( "1" ), QVariant( "1" ), QVariant( "1 on update" ), QVariant(), QVariant() }, - { items.at( 2 ), QVariant( 2 ), QVariant( "1" ), QVariant( "1 on update" ), QVariant( 2 ), QVariant( 102 ) }, - }; + const auto textItem = controller.formItem( items.at( 4 ) ); + QCOMPARE( textItem->editorWidgetType(), "richtext" ); + QCOMPARE( textItem->editorWidgetConfig().value( "UseHtml" ).toBool(), false ); - for ( const testcase &t : testCases ) - { - const FormItem *item = controller.formItem( t.id ); + const auto spacerItem = controller.formItem( items.at( 5 ) ); + QCOMPARE( spacerItem->editorWidgetType(), "spacer" ); + QCOMPARE( spacerItem->editorWidgetConfig().value( "IsHLine" ).toBool(), true ); - controller.setFormValue( t.id, t.value ); + const auto hLineItem = controller.formItem( items.at( 6 ) ); + QCOMPARE( hLineItem->editorWidgetType(), "spacer" ); + QCOMPARE( hLineItem->editorWidgetConfig().value( "IsHLine" ).toBool(), false ); - QCOMPARE( controller.featureLayerPair().feature().attribute( 1 ), t.expectedText ); - QCOMPARE( controller.featureLayerPair().feature().attribute( 2 ), t.expectedText2 ); - QCOMPARE( controller.featureLayerPair().feature().attribute( 3 ), t.expectedNum ); - QVariant v = controller.featureLayerPair().feature().attribute( 4 ); - if ( v.isNull() ) - { - QVERIFY( t.expectedNum2.isNull() ); - } - else - { - QCOMPARE( controller.featureLayerPair().feature().attribute( 4 ), t.expectedNum2 ); - } - } + // update one field on which both HTML and Text widgets depends + auto field = controller.formItem( items.at( 0 ) ); + QCOMPARE( field->name(), "text" ); + controller.setFormValue( field->id(), "my new text" ); - */ + QCOMPARE( htmlItem->rawValue(), "my new text on update" ); + QCOMPARE( textItem->rawValue(), "my new text on update" ); } diff --git a/test/test_data/expressions/project.qgz b/test/test_data/expressions/project.qgz index 15bff9d7b..7f6047415 100644 Binary files a/test/test_data/expressions/project.qgz and b/test/test_data/expressions/project.qgz differ diff --git a/test/test_data/expressions/survey.gpkg b/test/test_data/expressions/survey.gpkg index 7718f9dfc..9217eef08 100644 Binary files a/test/test_data/expressions/survey.gpkg and b/test/test_data/expressions/survey.gpkg differ