Skip to content

Commit 6d92ed4

Browse files
nirvnnyalldawson
authored andcommitted
[legend] insure multi-line labels are vertically centered with tall symbols
(cherry-picked from 4558742)
1 parent a3e22f6 commit 6d92ed4

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/core/layertree/qgslayertreemodellegendnode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings& set
109109

110110
// Vertical alignment of label with symbol
111111
if ( labelSize.height() < symbolSize.height() )
112-
labelY += symbolSize.height() / 2 + textHeight / 2; // label centered with symbol
113-
else
114-
labelY += textHeight; // label starts at top and runs under symbol
112+
labelY += symbolSize.height() / 2 - labelSize.height() / 2; // label centered with symbol
113+
114+
labelY += textHeight;
115115
}
116116

117117
for ( QStringList::Iterator itemPart = lines.begin(); itemPart != lines.end(); ++itemPart )

tests/src/core/testqgslegendrenderer.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class TestQgsLegendRenderer : public QObject
115115
void testBasic();
116116
void testBigMarker();
117117
void testMapUnits();
118+
void testTallSymbol();
118119
void testLongSymbolText();
119120
void testThreeColumns();
120121
void testFilterByMap();
@@ -336,6 +337,28 @@ void TestQgsLegendRenderer::testMapUnits()
336337
QVERIFY( _verifyImage( testName, mReport ) );
337338
}
338339

340+
void TestQgsLegendRenderer::testTallSymbol()
341+
{
342+
QString testName = "legend_tall_symbol";
343+
344+
QgsCategorizedSymbolRenderer* catRenderer = dynamic_cast<QgsCategorizedSymbolRenderer*>( mVL3->renderer() );
345+
QVERIFY( catRenderer );
346+
catRenderer->updateCategoryLabel( 1, "This is\nthree lines\nlong label" );
347+
348+
mVL2->setName( "This is a two lines\nlong label" );
349+
350+
QgsLayerTreeModel legendModel( mRoot );
351+
352+
QgsLegendSettings settings;
353+
settings.setWrapChar( "\n" );
354+
settings.setSymbolSize( QSizeF( 10.0, 10.0 ) );
355+
_setStandardTestFont( settings );
356+
_renderLegend( testName, &legendModel, settings );
357+
QVERIFY( _verifyImage( testName, mReport ) );
358+
359+
mVL2->setName( "Polygon Layer" );
360+
}
361+
339362
void TestQgsLegendRenderer::testLongSymbolText()
340363
{
341364
QString testName = "legend_long_symbol_text";

0 commit comments

Comments
 (0)