Skip to content

Commit

Permalink
Merge pull request #3485 from MerginMaps/fix/mapTipImage
Browse files Browse the repository at this point in the history
Support for multiple lines in map tip for images
  • Loading branch information
tomasMizera authored Jun 14, 2024
2 parents af743e8 + 65873ee commit 1159b7c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
10 changes: 3 additions & 7 deletions app/attributes/attributepreviewcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,12 @@ QVector<QPair<QString, QString>> 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( )
Expand Down
47 changes: 47 additions & 0 deletions app/test/testattributepreviewcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
1 change: 1 addition & 0 deletions app/test/testattributepreviewcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TestAttributePreviewController: public QObject
void cleanupTestCase();

void testPreviewForms();
void testMultilineMapTips();

private:
};
Expand Down

1 comment on commit 1159b7c

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 24.6.633911 just submitted!

Please sign in to comment.