Skip to content

Commit 1487329

Browse files
committed
GetLegendGraphics with no label
With this commit, you can call GetLegendGraphics with two parameters (LAYERTITLE=false / RULELABEL=false) to disable labels in the resulting legend. Useful for Qgis Web Client, LizMap-Web-Client and Qgis2Leaf.
1 parent 9551349 commit 1487329

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

src/mapserver/qgswmsserver.cpp

+53-19
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
758758
{
759759
layerFontColor = QColor( 0, 0, 0 );
760760
}
761+
QMap<QString, QString>::const_iterator layerTitleIt = mParameters.find( "LAYERTITLE" );
762+
if ( layerTitleIt != mParameters.constEnd() )
763+
{
764+
mDrawLegendLayerLabel = layerTitleIt.value().compare( "TRUE", Qt::CaseInsensitive );
765+
}
766+
else
767+
{
768+
mDrawLegendLayerLabel = true;
769+
}
761770

762771

763772
itemFont = mConfigParser->legendItemFont();
@@ -794,6 +803,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
794803
{
795804
itemFontColor = QColor( 0, 0, 0 );
796805
}
806+
QMap<QString, QString>::const_iterator itemLabelIt = mParameters.find( "RULELABEL" );
807+
if ( itemLabelIt != mParameters.constEnd() )
808+
{
809+
mDrawLegendItemLabel = itemLabelIt.value().compare( "TRUE", Qt::CaseInsensitive );
810+
}
811+
else
812+
{
813+
mDrawLegendItemLabel = true;
814+
}
797815
}
798816

799817
QDomDocument QgsWMSServer::getStyle()
@@ -1955,28 +1973,36 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
19551973
}
19561974

19571975
QFontMetricsF layerFontMetrics( layerFont );
1958-
currentY += layerFontMetrics.ascent() / fontOversamplingFactor;
1976+
if (mDrawLegendLayerLabel == true) {
1977+
currentY += layerFontMetrics.ascent() / fontOversamplingFactor;
1978+
}
19591979

19601980
//draw layer title first
19611981
if ( p )
19621982
{
1963-
p->save();
1964-
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
1965-
p->setPen( layerFontColor );
1966-
p->setFont( layerFont );
1967-
p->drawText( boxSpace * fontOversamplingFactor, currentY * fontOversamplingFactor, item->text() );
1968-
p->restore();
1983+
if (mDrawLegendLayerLabel == true) {
1984+
p->save();
1985+
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
1986+
p->setPen( layerFontColor );
1987+
p->setFont( layerFont );
1988+
p->drawText( boxSpace * fontOversamplingFactor, currentY * fontOversamplingFactor, item->text() );
1989+
p->restore();
1990+
}
19691991
}
19701992
else
19711993
{
19721994
double layerItemWidth = layerFontMetrics.width( item->text() ) / fontOversamplingFactor + boxSpace;
19731995
if ( layerItemWidth > maxTextWidth )
19741996
{
1975-
maxTextWidth = layerItemWidth;
1997+
if ( mDrawLegendLayerLabel == true) {
1998+
maxTextWidth = layerItemWidth;
1999+
}
19762000
}
19772001
}
19782002

1979-
currentY += layerTitleSpace;
2003+
if (mDrawLegendLayerLabel == true) {
2004+
currentY += layerTitleSpace;
2005+
}
19802006

19812007
//then draw all the children
19822008
QFontMetricsF itemFontMetrics( itemFont );
@@ -2023,25 +2049,33 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
20232049
break;
20242050
}
20252051

2026-
//finally draw text
2027-
currentTextWidth = itemFontMetrics.width( currentComposerItem->text() ) / fontOversamplingFactor;
2052+
if ( mDrawLegendItemLabel == true ) {
2053+
//finally draw text
2054+
currentTextWidth = itemFontMetrics.width( currentComposerItem->text() ) / fontOversamplingFactor;
2055+
} else {
2056+
currentTextWidth = 0;
2057+
}
20282058
double symbolItemHeight = qMax( itemFontMetrics.ascent() / fontOversamplingFactor, currentSymbolHeight );
20292059

20302060
if ( p )
20312061
{
2032-
p->save();
2033-
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
2034-
p->setPen( itemFontColor );
2035-
p->setFont( itemFont );
2036-
p->drawText( maxSymbolWidth * fontOversamplingFactor,
2037-
( currentY + symbolItemHeight / 2.0 ) * fontOversamplingFactor + itemFontMetrics.ascent() / 2.0, currentComposerItem->text() );
2038-
p->restore();
2062+
if ( mDrawLegendItemLabel == true ) {
2063+
p->save();
2064+
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
2065+
p->setPen( itemFontColor );
2066+
p->setFont( itemFont );
2067+
p->drawText( maxSymbolWidth * fontOversamplingFactor,
2068+
( currentY + symbolItemHeight / 2.0 ) * fontOversamplingFactor + itemFontMetrics.ascent() / 2.0, currentComposerItem->text() );
2069+
p->restore();
2070+
}
20392071
}
20402072
else
20412073
{
20422074
if ( currentTextWidth > maxTextWidth )
20432075
{
2044-
maxTextWidth = currentTextWidth;
2076+
if ( mDrawLegendItemLabel == true ) {
2077+
maxTextWidth = currentTextWidth;
2078+
}
20452079
}
20462080
double symbolWidth = boxSpace + currentSymbolWidth + iconLabelSpace;
20472081
if ( symbolWidth > maxSymbolWidth )

0 commit comments

Comments
 (0)