[layout] When newline characters are entered as part of a legend item's text,

correctly respect these while rendering legend
This commit is contained in:
Nyall Dawson 2020-04-24 12:27:40 +10:00
parent 2b7b776470
commit 6caa62a9ca
3 changed files with 33 additions and 7 deletions

View File

@ -53,15 +53,20 @@ QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsE
return splitStringForWrapping( textToRender );
}
QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplt ) const
QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplit ) const
{
QStringList list;
const QStringList lines = stringToSplit.split( '\n' );
// If the string contains nothing then just return the string without splitting.
if ( wrapChar().count() == 0 )
list << stringToSplt;
else
list = stringToSplt.split( wrapChar() );
return list;
if ( wrapChar().isEmpty() )
return lines;
QStringList res;
for ( const QString &line : lines )
{
res.append( line.split( wrapChar() ) );
}
return res;
}
#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter

View File

@ -131,6 +131,7 @@ class TestQgsLegendRenderer : public QObject
void testModel();
void testBasic();
void testMultiline();
void testEffects();
void testBigMarker();
@ -318,6 +319,26 @@ void TestQgsLegendRenderer::testBasic()
QVERIFY( _verifyImage( testName, mReport ) );
}
void TestQgsLegendRenderer::testMultiline()
{
QString testName = QStringLiteral( "legend_multiline" );
QgsLayerTreeModel legendModel( mRoot );
legendModel.findLegendNode( mVL1->id(), QString() );
QgsLayerTreeLayer *layer = legendModel.rootGroup()->findLayer( mVL1 );
layer->setCustomProperty( QStringLiteral( "legend/title-label" ), QStringLiteral( "some legend text\nwith newline\ncharacters in it" ) );
QgsLayerTreeModelLegendNode *embeddedNode = legendModel.legendNodeEmbeddedInParent( layer );
embeddedNode->setUserLabel( QString() );
QgsLegendSettings settings;
_setStandardTestFont( settings, QStringLiteral( "Bold" ) );
_renderLegend( testName, &legendModel, settings );
QVERIFY( _verifyImage( testName, mReport ) );
}
void TestQgsLegendRenderer::testEffects()
{
QString testName = QStringLiteral( "legend_effects" );

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB