Skip to content

Commit

Permalink
Merge pull request #1377 from luca76/master
Browse files Browse the repository at this point in the history
WMS GetLegendGrapics: obtain legend graphics with no labels
  • Loading branch information
mhugent committed May 21, 2014
2 parents af7af6c + b87049e commit 5417639
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
72 changes: 53 additions & 19 deletions src/mapserver/qgswmsserver.cpp
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
4 changes: 4 additions & 0 deletions src/mapserver/qgswmsserver.h
Expand Up @@ -239,6 +239,10 @@ class QgsWMSServer: public QgsOWSServer

bool mOwnsConfigParser; //delete config parser after request (e.g. sent SLD)

// speficy if layer or rule item labels should be drawn in the legend graphic with GetLegendGraphics
bool mDrawLegendLayerLabel;
bool mDrawLegendItemLabel;

QDomElement createFeatureGML(
QgsFeature* feat,
QgsVectorLayer* layer,
Expand Down

0 comments on commit 5417639

Please sign in to comment.