Skip to content

Commit 1a51a75

Browse files
authoredOct 19, 2016
Merge pull request #3631 from nirvn/legend_vertical_alignment_2
[legend] insure multi-line labels are vertically centered with tall symbols
2 parents 4bcd970 + 4558742 commit 1a51a75

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
@@ -108,9 +108,9 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings& set
108108

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

116116
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
@@ -114,6 +114,7 @@ class TestQgsLegendRenderer : public QObject
114114
void testBasic();
115115
void testBigMarker();
116116
void testMapUnits();
117+
void testTallSymbol();
117118
void testLongSymbolText();
118119
void testThreeColumns();
119120
void testFilterByMap();
@@ -338,6 +339,28 @@ void TestQgsLegendRenderer::testMapUnits()
338339
QVERIFY( _verifyImage( testName, mReport ) );
339340
}
340341

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

0 commit comments

Comments
 (0)
Please sign in to comment.