Skip to content

Commit

Permalink
Fix double layer space in GetLegendGraphic and remove code redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 24, 2013
1 parent 0c78486 commit 382e300
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/mapserver/qgswmsserver.cpp
Expand Up @@ -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
Expand All @@ -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 ) );
Expand All @@ -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 ) );
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/mapserver/qgswmsserver.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 382e300

Please sign in to comment.