diff --git a/app/attributes/attributepreviewcontroller.cpp b/app/attributes/attributepreviewcontroller.cpp index 912a5f08b..ee6c0da8c 100644 --- a/app/attributes/attributepreviewcontroller.cpp +++ b/app/attributes/attributepreviewcontroller.cpp @@ -115,16 +115,12 @@ QVector> AttributePreviewController::mapTipFields( ) return lst; } -QString AttributePreviewController::mapTipImage( ) +QString AttributePreviewController::mapTipImage() { QgsExpressionContext context( globalProjectLayerScopes( mFeatureLayerPair.layer() ) ); context.setFeature( mFeatureLayerPair.feature() ); - QString mapTip = mFeatureLayerPair.layer()->mapTipTemplate(); - QStringList lst = mapTip.split( '\n' ); // first line is "# image" - if ( lst.count() >= 2 ) - return QgsExpression::replaceExpressionText( lst[1], &context ); - else - return QString(); + QString mapTip = mFeatureLayerPair.layer()->mapTipTemplate().remove( "# image\n" ); // first line is "# image" + return QgsExpression::replaceExpressionText( mapTip, &context ); } QString AttributePreviewController::mapTipHtml( ) diff --git a/app/test/testattributepreviewcontroller.cpp b/app/test/testattributepreviewcontroller.cpp index 5b9e7129f..90624af1c 100644 --- a/app/test/testattributepreviewcontroller.cpp +++ b/app/test/testattributepreviewcontroller.cpp @@ -33,6 +33,53 @@ void TestAttributePreviewController::cleanupTestCase() } +void TestAttributePreviewController::testMultilineMapTips() +{ + // Layer creation + QgsVectorLayer *layerPhoto = + new QgsVectorLayer( QStringLiteral( "Point?field=fldtxt:string" ), + QStringLiteral( "layer" ), + QStringLiteral( "memory" ) + ); + QVERIFY( layerPhoto && layerPhoto->isValid() ); + layerPhoto->setMapTipTemplate( "# image\nfile:///my/path/to/image/[%\n CASE WHEN fldtxt = 'myphoto' THEN\n 'hello.jpg'\n ELSE\n 'world.jpg'\n END\n%]" ); + + // Feature 1 setup + QgsFeature p1( layerPhoto->dataProvider()->fields() ); + p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); + layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); + + // Feature 2 setup + QgsFeature p2( layerPhoto->dataProvider()->fields() ); + p2.setAttribute( QStringLiteral( "fldtxt" ), "notmyphoto" ); + layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p2 ); + QgsProject::instance()->addMapLayer( layerPhoto ); + + // Controller setup + AttributePreviewController controller; + controller.setProject( QgsProject::instance() ); + QCOMPARE( controller.type(), AttributePreviewController::Empty ); + + // Assertion for matching feature + FeatureLayerPair pair1( p1, layerPhoto ); + controller.setFeatureLayerPair( pair1 ); + QCOMPARE( controller.type(), AttributePreviewController::Photo ); + QCOMPARE( controller.photo(), "file:///my/path/to/image/hello.jpg" ); + + // Assertion for non-matching feature + FeatureLayerPair pair2( p2, layerPhoto ); + controller.setFeatureLayerPair( pair2 ); + QCOMPARE( controller.type(), AttributePreviewController::Photo ); + QCOMPARE( controller.photo(), "file:///my/path/to/image/world.jpg" ); + + // Reset + controller.reset(); + QCOMPARE( controller.type(), AttributePreviewController::Empty ); + + // Cleanup + QgsProject::instance()->removeAllMapLayers(); +} + void TestAttributePreviewController::testPreviewForms() { // Prepare Layers! diff --git a/app/test/testattributepreviewcontroller.h b/app/test/testattributepreviewcontroller.h index afab75339..30022a9fe 100644 --- a/app/test/testattributepreviewcontroller.h +++ b/app/test/testattributepreviewcontroller.h @@ -29,6 +29,7 @@ class TestAttributePreviewController: public QObject void cleanupTestCase(); void testPreviewForms(); + void testMultilineMapTips(); private: };