Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[layout] When newline characters are entered as part of a legend item…
…'s text,

correctly respect these while rendering legend
  • Loading branch information
nyalldawson committed Apr 24, 2020
1 parent 2b7b776 commit 6caa62a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/core/qgslegendsettings.cpp
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/src/core/testqgslegendrenderer.cpp
Expand Up @@ -131,6 +131,7 @@ class TestQgsLegendRenderer : public QObject
void testModel();

void testBasic();
void testMultiline();
void testEffects();
void testBigMarker();

Expand Down Expand Up @@ -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" );
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6caa62a

Please sign in to comment.