Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple lines in map tip for images #3485

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 34 additions & 0 deletions app/test/testattributepreviewcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
VitorVieiraZ marked this conversation as resolved.
Show resolved Hide resolved
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%]" );
VitorVieiraZ marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading