Skip to content
Permalink
Browse files
Fix double layer space in GetLegendGraphic and remove code redundancy
  • Loading branch information
mhugent committed Jul 24, 2013
1 parent 0c78486 commit 382e300c22b56f57a2c61ae5f5b202e3a6dc5bf3
Showing with 41 additions and 33 deletions.
  1. +34 −33 src/mapserver/qgswmsserver.cpp
  2. +7 −0 src/mapserver/qgswmsserver.h
@@ -343,7 +343,6 @@ QImage* QgsWMSServer::getLegendGraphics()
double mmToPixelFactor = theImage->dotsPerMeterX() / 1000.0;
double maxTextWidth = 0;
double maxSymbolWidth = 0;
double currentY = 0;
double fontOversamplingFactor = 10.0;

//get icon size, spaces between legend items and font from config parser
@@ -360,29 +359,9 @@ QImage* QgsWMSServer::getLegendGraphics()
return 0;
}

int numLayerItems = rootItem->rowCount();
if ( numLayerItems < 1 )
{
return 0;
}

currentY = boxSpace;
for ( int i = 0; i < numLayerItems; ++i )
{
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
if ( layerItem )
{
if ( i > 0 )
{
currentY += layerSpace;
}
drawLegendLayerItem( layerItem, 0, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
boxSpace, layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
theImage->dotsPerMeterX() * 0.0254 );
}
currentY += layerSpace;
}
currentY += boxSpace;
double currentY = drawLegendGraphics( 0, fontOversamplingFactor, rootItem, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor, maxTextWidth,
maxSymbolWidth, theImage->dotsPerMeterX() * 0.0254 );

//create second image with the right dimensions
QImage* paintImage = createImage( maxTextWidth + maxSymbolWidth, ceil( currentY ) );
@@ -391,7 +370,32 @@ QImage* QgsWMSServer::getLegendGraphics()
QPainter p( paintImage );
p.setRenderHint( QPainter::Antialiasing, true );

currentY = boxSpace;
drawLegendGraphics( &p, fontOversamplingFactor, rootItem, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor, maxTextWidth,
maxSymbolWidth, theImage->dotsPerMeterX() * 0.0254 );

QgsMapLayerRegistry::instance()->removeAllMapLayers();
delete theImage;
return paintImage;
}

double QgsWMSServer::drawLegendGraphics( QPainter* p, double fontOversamplingFactor, QStandardItem* rootItem, double boxSpace,
double layerSpace, double layerTitleSpace, double symbolSpace, double iconLabelSpace,
double symbolWidth, double symbolHeight, const QFont& layerFont, const QFont& itemFont,
const QColor& layerFontColor, const QColor& itemFontColor, double& maxTextWidth, double& maxSymbolWidth, double dpi )
{
if ( !rootItem )
{
return 0;
}

int numLayerItems = rootItem->rowCount();
if ( numLayerItems < 1 )
{
return 0;
}

double currentY = boxSpace;
for ( int i = 0; i < numLayerItems; ++i )
{
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
@@ -401,16 +405,13 @@ QImage* QgsWMSServer::getLegendGraphics()
{
currentY += layerSpace;
}
drawLegendLayerItem( layerItem, &p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor, boxSpace,
layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
theImage->dotsPerMeterX() * 0.0254 );
drawLegendLayerItem( layerItem, p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
boxSpace, layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
dpi );
}
currentY += layerSpace;
}

QgsMapLayerRegistry::instance()->removeAllMapLayers();
delete theImage;
return paintImage;
currentY += boxSpace;
return currentY;
}

void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& layerTitleSpace,
@@ -45,6 +45,7 @@ class QFont;
class QImage;
class QPaintDevice;
class QPainter;
class QStandardItem;

/**This class handles all the wms server requests. The parameters and values have to be passed in the form of
a map<QString, QString>. This map is usually generated by a subclass of QgsWMSRequestHandler, which makes QgsWMSServer
@@ -130,6 +131,12 @@ class QgsWMSServer
@param scaleDenominator Filter out layer if scale based visibility does not match (or use -1 if no scale restriction)*/
QStringList layerSet( const QStringList& layersList, const QStringList& stylesList, const QgsCoordinateReferenceSystem& destCRS, double scaleDenominator = -1 ) const;

/**Draws graphics if painter != 0 and gives back y dimension, maximum symbol and text width of legend*/
double drawLegendGraphics( QPainter* p, double fontOversamplingFactor, QStandardItem* rootItem, double boxSpace, double layerSpace, double layerTitleSpace,
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight,
const QFont& layerFont, const QFont& itemFont, const QColor& layerFontColor, const QColor& itemFontColor,
double& maxTextWidth, double& maxSymbolWidth, double dpi );

//helper functions for GetLegendGraphics
/**Draws layer item and subitems
@param p painter if the item should be drawn, if 0 the size parameters are calculated only

0 comments on commit 382e300

Please sign in to comment.