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