Skip to content

Commit 382e300

Browse files
committed
Fix double layer space in GetLegendGraphic and remove code redundancy
1 parent 0c78486 commit 382e300

2 files changed

Lines changed: 41 additions & 33 deletions

File tree

‎src/mapserver/qgswmsserver.cpp‎

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ QImage* QgsWMSServer::getLegendGraphics()
343343
double mmToPixelFactor = theImage->dotsPerMeterX() / 1000.0;
344344
double maxTextWidth = 0;
345345
double maxSymbolWidth = 0;
346-
double currentY = 0;
347346
double fontOversamplingFactor = 10.0;
348347

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

363-
int numLayerItems = rootItem->rowCount();
364-
if ( numLayerItems < 1 )
365-
{
366-
return 0;
367-
}
368-
369-
currentY = boxSpace;
370-
for ( int i = 0; i < numLayerItems; ++i )
371-
{
372-
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
373-
if ( layerItem )
374-
{
375-
if ( i > 0 )
376-
{
377-
currentY += layerSpace;
378-
}
379-
drawLegendLayerItem( layerItem, 0, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
380-
boxSpace, layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
381-
theImage->dotsPerMeterX() * 0.0254 );
382-
}
383-
currentY += layerSpace;
384-
}
385-
currentY += boxSpace;
362+
double currentY = drawLegendGraphics( 0, fontOversamplingFactor, rootItem, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
363+
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor, maxTextWidth,
364+
maxSymbolWidth, theImage->dotsPerMeterX() * 0.0254 );
386365

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

394-
currentY = boxSpace;
373+
drawLegendGraphics( &p, fontOversamplingFactor, rootItem, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
374+
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor, maxTextWidth,
375+
maxSymbolWidth, theImage->dotsPerMeterX() * 0.0254 );
376+
377+
QgsMapLayerRegistry::instance()->removeAllMapLayers();
378+
delete theImage;
379+
return paintImage;
380+
}
381+
382+
double QgsWMSServer::drawLegendGraphics( QPainter* p, double fontOversamplingFactor, QStandardItem* rootItem, double boxSpace,
383+
double layerSpace, double layerTitleSpace, double symbolSpace, double iconLabelSpace,
384+
double symbolWidth, double symbolHeight, const QFont& layerFont, const QFont& itemFont,
385+
const QColor& layerFontColor, const QColor& itemFontColor, double& maxTextWidth, double& maxSymbolWidth, double dpi )
386+
{
387+
if ( !rootItem )
388+
{
389+
return 0;
390+
}
391+
392+
int numLayerItems = rootItem->rowCount();
393+
if ( numLayerItems < 1 )
394+
{
395+
return 0;
396+
}
397+
398+
double currentY = boxSpace;
395399
for ( int i = 0; i < numLayerItems; ++i )
396400
{
397401
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
@@ -401,16 +405,13 @@ QImage* QgsWMSServer::getLegendGraphics()
401405
{
402406
currentY += layerSpace;
403407
}
404-
drawLegendLayerItem( layerItem, &p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor, boxSpace,
405-
layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
406-
theImage->dotsPerMeterX() * 0.0254 );
408+
drawLegendLayerItem( layerItem, p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
409+
boxSpace, layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
410+
dpi );
407411
}
408-
currentY += layerSpace;
409412
}
410-
411-
QgsMapLayerRegistry::instance()->removeAllMapLayers();
412-
delete theImage;
413-
return paintImage;
413+
currentY += boxSpace;
414+
return currentY;
414415
}
415416

416417
void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& layerTitleSpace,

‎src/mapserver/qgswmsserver.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class QFont;
4545
class QImage;
4646
class QPaintDevice;
4747
class QPainter;
48+
class QStandardItem;
4849

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

134+
/**Draws graphics if painter != 0 and gives back y dimension, maximum symbol and text width of legend*/
135+
double drawLegendGraphics( QPainter* p, double fontOversamplingFactor, QStandardItem* rootItem, double boxSpace, double layerSpace, double layerTitleSpace,
136+
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight,
137+
const QFont& layerFont, const QFont& itemFont, const QColor& layerFontColor, const QColor& itemFontColor,
138+
double& maxTextWidth, double& maxSymbolWidth, double dpi );
139+
133140
//helper functions for GetLegendGraphics
134141
/**Draws layer item and subitems
135142
@param p painter if the item should be drawn, if 0 the size parameters are calculated only

0 commit comments

Comments
 (0)