From f4c0f4c4c108d8fcc7536e8aa38c69ac1758c917 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Fri, 31 May 2024 19:19:00 -0300 Subject: [PATCH 1/5] in progress - solving map tip image bug --- app/attributes/attributepreviewcontroller.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/attributes/attributepreviewcontroller.cpp b/app/attributes/attributepreviewcontroller.cpp index 912a5f08b..f7e2a6656 100644 --- a/app/attributes/attributepreviewcontroller.cpp +++ b/app/attributes/attributepreviewcontroller.cpp @@ -115,16 +115,22 @@ 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 ); + { + QString expression = lst.mid( 1 ).join( '\n' ); + return QgsExpression::replaceExpressionText( expression, &context ); + } else + { return QString(); + } } QString AttributePreviewController::mapTipHtml( ) From fbb0ed3a720ae4a50ebf2c8a986577ff0ad72d3b Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Wed, 5 Jun 2024 14:04:35 -0300 Subject: [PATCH 2/5] method change and tests implemented --- app/attributes/attributepreviewcontroller.cpp | 14 ++------ app/test/testattributepreviewcontroller.cpp | 34 +++++++++++++++++++ app/test/testattributepreviewcontroller.h | 1 + 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/attributes/attributepreviewcontroller.cpp b/app/attributes/attributepreviewcontroller.cpp index f7e2a6656..ee6c0da8c 100644 --- a/app/attributes/attributepreviewcontroller.cpp +++ b/app/attributes/attributepreviewcontroller.cpp @@ -119,18 +119,8 @@ 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 ) - { - QString expression = lst.mid( 1 ).join( '\n' ); - return QgsExpression::replaceExpressionText( expression, &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..aeab3f049 100644 --- a/app/test/testattributepreviewcontroller.cpp +++ b/app/test/testattributepreviewcontroller.cpp @@ -33,6 +33,40 @@ 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 x = 1 THEN\n 'hello.jpg'\n ELSE\n 'world.jpg'\n END\n%]" ); + QgsFeature p1( layerPhoto->dataProvider()->fields(), 5 ); + p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); + layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); + QgsProject::instance()->addMapLayer( layerPhoto ); + + // Controller setup + AttributePreviewController controller; + controller.setProject( QgsProject::instance() ); + QCOMPARE( controller.type(), AttributePreviewController::Empty ); + + // Assertion + FeatureLayerPair pair5( p1, layerPhoto ); + controller.setFeatureLayerPair( pair5 ); + QCOMPARE( controller.type(), AttributePreviewController::Photo ); + QCOMPARE( controller.photo(), "file:///my/path/to/image/[%\n CASE WHEN x = 1 THEN\n 'hello.jpg'\n ELSE\n 'world.jpg'\n END\n%]" ); + + // 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: }; From 600186a411155fd13d095b7e5d512f79f649ab01 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Mon, 10 Jun 2024 11:38:39 -0300 Subject: [PATCH 3/5] autotests enhancement --- app/test/testattributepreviewcontroller.cpp | 73 ++++++++++++--------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/app/test/testattributepreviewcontroller.cpp b/app/test/testattributepreviewcontroller.cpp index aeab3f049..97527ab8e 100644 --- a/app/test/testattributepreviewcontroller.cpp +++ b/app/test/testattributepreviewcontroller.cpp @@ -35,36 +35,49 @@ 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 x = 1 THEN\n 'hello.jpg'\n ELSE\n 'world.jpg'\n END\n%]" ); - QgsFeature p1( layerPhoto->dataProvider()->fields(), 5 ); - p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); - layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); - QgsProject::instance()->addMapLayer( layerPhoto ); - - // Controller setup - AttributePreviewController controller; - controller.setProject( QgsProject::instance() ); - QCOMPARE( controller.type(), AttributePreviewController::Empty ); - - // Assertion - FeatureLayerPair pair5( p1, layerPhoto ); - controller.setFeatureLayerPair( pair5 ); - QCOMPARE( controller.type(), AttributePreviewController::Photo ); - QCOMPARE( controller.photo(), "file:///my/path/to/image/[%\n CASE WHEN x = 1 THEN\n 'hello.jpg'\n ELSE\n 'world.jpg'\n END\n%]" ); - - // Reset - controller.reset(); - QCOMPARE( controller.type(), AttributePreviewController::Empty ); - - // Cleanup - QgsProject::instance()->removeAllMapLayers(); + // 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 set up + QgsFeature p1( layerPhoto->dataProvider()->fields() ); + p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); + layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); + + // Feature 2 set up + 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() From 8ef7b37b658b19e4b500334c13837c4c3130e5a0 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Mon, 10 Jun 2024 11:39:46 -0300 Subject: [PATCH 4/5] code layout --- app/test/testattributepreviewcontroller.cpp | 86 ++++++++++----------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/app/test/testattributepreviewcontroller.cpp b/app/test/testattributepreviewcontroller.cpp index 97527ab8e..7ea862656 100644 --- a/app/test/testattributepreviewcontroller.cpp +++ b/app/test/testattributepreviewcontroller.cpp @@ -35,49 +35,49 @@ 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 set up - QgsFeature p1( layerPhoto->dataProvider()->fields() ); - p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); - layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); - - // Feature 2 set up - 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(); + // 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 set up + QgsFeature p1( layerPhoto->dataProvider()->fields() ); + p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); + layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); + + // Feature 2 set up + 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() From 65873eef8a3fc97458d8aa7bc408a15c9c2e73e1 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Mon, 10 Jun 2024 11:40:52 -0300 Subject: [PATCH 5/5] small adjustment --- app/test/testattributepreviewcontroller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/test/testattributepreviewcontroller.cpp b/app/test/testattributepreviewcontroller.cpp index 7ea862656..90624af1c 100644 --- a/app/test/testattributepreviewcontroller.cpp +++ b/app/test/testattributepreviewcontroller.cpp @@ -44,12 +44,12 @@ void TestAttributePreviewController::testMultilineMapTips() 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 set up + // Feature 1 setup QgsFeature p1( layerPhoto->dataProvider()->fields() ); p1.setAttribute( QStringLiteral( "fldtxt" ), "myphoto" ); layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p1 ); - // Feature 2 set up + // Feature 2 setup QgsFeature p2( layerPhoto->dataProvider()->fields() ); p2.setAttribute( QStringLiteral( "fldtxt" ), "notmyphoto" ); layerPhoto->dataProvider()->addFeatures( QgsFeatureList() << p2 );