Skip to content

Commit

Permalink
GetLegendGraphics with no label
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
luca76 committed May 21, 2014
1 parent 9551349 commit 1487329
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 deletions src/mapserver/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
{
layerFontColor = QColor( 0, 0, 0 );
}
QMap<QString, QString>::const_iterator layerTitleIt = mParameters.find( "LAYERTITLE" );
if ( layerTitleIt != mParameters.constEnd() )
{
mDrawLegendLayerLabel = layerTitleIt.value().compare( "TRUE", Qt::CaseInsensitive );
}
else
{
mDrawLegendLayerLabel = true;
}


itemFont = mConfigParser->legendItemFont();
Expand Down Expand Up @@ -794,6 +803,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
{
itemFontColor = QColor( 0, 0, 0 );
}
QMap<QString, QString>::const_iterator itemLabelIt = mParameters.find( "RULELABEL" );
if ( itemLabelIt != mParameters.constEnd() )
{
mDrawLegendItemLabel = itemLabelIt.value().compare( "TRUE", Qt::CaseInsensitive );
}
else
{
mDrawLegendItemLabel = true;
}
}

QDomDocument QgsWMSServer::getStyle()
Expand Down Expand Up @@ -1955,28 +1973,36 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
}

QFontMetricsF layerFontMetrics( layerFont );
currentY += layerFontMetrics.ascent() / fontOversamplingFactor;
if (mDrawLegendLayerLabel == true) {
currentY += layerFontMetrics.ascent() / fontOversamplingFactor;
}

//draw layer title first
if ( p )
{
p->save();
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
p->setPen( layerFontColor );
p->setFont( layerFont );
p->drawText( boxSpace * fontOversamplingFactor, currentY * fontOversamplingFactor, item->text() );
p->restore();
if (mDrawLegendLayerLabel == true) {
p->save();
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
p->setPen( layerFontColor );
p->setFont( layerFont );
p->drawText( boxSpace * fontOversamplingFactor, currentY * fontOversamplingFactor, item->text() );
p->restore();
}
}
else
{
double layerItemWidth = layerFontMetrics.width( item->text() ) / fontOversamplingFactor + boxSpace;
if ( layerItemWidth > maxTextWidth )
{
maxTextWidth = layerItemWidth;
if ( mDrawLegendLayerLabel == true) {
maxTextWidth = layerItemWidth;
}
}
}

currentY += layerTitleSpace;
if (mDrawLegendLayerLabel == true) {
currentY += layerTitleSpace;
}

//then draw all the children
QFontMetricsF itemFontMetrics( itemFont );
Expand Down Expand Up @@ -2023,25 +2049,33 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
break;
}

//finally draw text
currentTextWidth = itemFontMetrics.width( currentComposerItem->text() ) / fontOversamplingFactor;
if ( mDrawLegendItemLabel == true ) {
//finally draw text
currentTextWidth = itemFontMetrics.width( currentComposerItem->text() ) / fontOversamplingFactor;
} else {
currentTextWidth = 0;
}
double symbolItemHeight = qMax( itemFontMetrics.ascent() / fontOversamplingFactor, currentSymbolHeight );

if ( p )
{
p->save();
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
p->setPen( itemFontColor );
p->setFont( itemFont );
p->drawText( maxSymbolWidth * fontOversamplingFactor,
( currentY + symbolItemHeight / 2.0 ) * fontOversamplingFactor + itemFontMetrics.ascent() / 2.0, currentComposerItem->text() );
p->restore();
if ( mDrawLegendItemLabel == true ) {
p->save();
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
p->setPen( itemFontColor );
p->setFont( itemFont );
p->drawText( maxSymbolWidth * fontOversamplingFactor,
( currentY + symbolItemHeight / 2.0 ) * fontOversamplingFactor + itemFontMetrics.ascent() / 2.0, currentComposerItem->text() );
p->restore();
}
}
else
{
if ( currentTextWidth > maxTextWidth )
{
maxTextWidth = currentTextWidth;
if ( mDrawLegendItemLabel == true ) {
maxTextWidth = currentTextWidth;
}
}
double symbolWidth = boxSpace + currentSymbolWidth + iconLabelSpace;
if ( symbolWidth > maxSymbolWidth )
Expand Down

0 comments on commit 1487329

Please sign in to comment.