Skip to content

Commit 0b951c6

Browse files
committed
Parameter for font color in GetLegendGraphic
1 parent d8bdcb2 commit 0b951c6

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/mapserver/qgswmsserver.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ QImage* QgsWMSServer::getLegendGraphics()
317317
//get icon size, spaces between legend items and font from config parser
318318
double boxSpace, layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight;
319319
QFont layerFont, itemFont;
320+
QColor layerFontColor, itemFontColor;
320321
legendParameters( mmToPixelFactor, fontOversamplingFactor, boxSpace, layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight,
321-
layerFont, itemFont );
322+
layerFont, itemFont, layerFontColor, itemFontColor );
322323

323324
//first find out image dimensions without painting
324325
QStandardItem* rootItem = legendModel.invisibleRootItem();
@@ -340,8 +341,9 @@ QImage* QgsWMSServer::getLegendGraphics()
340341
if ( layerItem )
341342
{
342343

343-
drawLegendLayerItem( layerItem, 0, maxTextWidth, maxSymbolWidth, currentY, layerFont, itemFont, boxSpace, layerSpace, symbolSpace,
344-
iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor, theImage->dotsPerMeterX() * 0.0254 );
344+
drawLegendLayerItem( layerItem, 0, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
345+
boxSpace, layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
346+
theImage->dotsPerMeterX() * 0.0254 );
345347
}
346348
}
347349
currentY += boxSpace;
@@ -359,8 +361,9 @@ QImage* QgsWMSServer::getLegendGraphics()
359361
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
360362
if ( layerItem )
361363
{
362-
drawLegendLayerItem( layerItem, &p, maxTextWidth, maxSymbolWidth, currentY, layerFont, itemFont, boxSpace, layerSpace, symbolSpace,
363-
iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor, theImage->dotsPerMeterX() * 0.0254 );
364+
drawLegendLayerItem( layerItem, &p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor, boxSpace,
365+
layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
366+
theImage->dotsPerMeterX() * 0.0254 );
364367
}
365368
}
366369

@@ -370,7 +373,7 @@ QImage* QgsWMSServer::getLegendGraphics()
370373
}
371374

372375
void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& symbolSpace, double& iconLabelSpace, double& symbolWidth, double& symbolHeight,
373-
QFont& layerFont, QFont& itemFont )
376+
QFont& layerFont, QFont& itemFont, QColor& layerFontColor, QColor& itemFontColor )
374377
{
375378
//spaces between legend elements
376379
QMap<QString, QString>::const_iterator boxSpaceIt = mParameterMap.find( "BOXSPACE" );
@@ -418,6 +421,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
418421
{
419422
layerFont.setPixelSize( layerFont.pointSizeF() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
420423
}
424+
QMap<QString, QString>::const_iterator layerFontColorIt = mParameterMap.find( "LAYERFONTCOLOR" );
425+
if ( layerFontColorIt != mParameterMap.constEnd() )
426+
{
427+
layerFontColor.setNamedColor( layerFontColorIt.value() );
428+
}
429+
else
430+
{
431+
layerFontColor = QColor( 0, 0, 0 );
432+
}
421433

422434

423435
itemFont = mConfigParser->legendItemFont();
@@ -445,6 +457,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
445457
{
446458
itemFont.setPixelSize( itemFont.pointSizeF() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
447459
}
460+
QMap<QString, QString>::const_iterator itemFontColorIt = mParameterMap.find( "ITEMFONTCOLOR" );
461+
if ( itemFontColorIt != mParameterMap.constEnd() )
462+
{
463+
itemFontColor.setNamedColor( itemFontColorIt.value() );
464+
}
465+
else
466+
{
467+
itemFontColor = QColor( 0, 0, 0 );
468+
}
448469
}
449470

450471
QDomDocument QgsWMSServer::getStyle()
@@ -1395,8 +1416,8 @@ QStringList QgsWMSServer::layerSet( const QStringList &layersList,
13951416
}
13961417

13971418
void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p, double& maxTextWidth, double& maxSymbolWidth, double& currentY, const QFont& layerFont,
1398-
const QFont& itemFont, double boxSpace, double layerSpace, double symbolSpace,
1399-
double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor,
1419+
const QColor& layerFontColor, const QFont& itemFont, const QColor& itemFontColor, double boxSpace, double layerSpace,
1420+
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor,
14001421
double dpi ) const
14011422
{
14021423
if ( !item )
@@ -1412,6 +1433,7 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
14121433
{
14131434
p->save();
14141435
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
1436+
p->setPen( layerFontColor );
14151437
p->setFont( layerFont );
14161438
p->drawText( boxSpace * fontOversamplingFactor, currentY * fontOversamplingFactor, item->text() );
14171439
p->restore();
@@ -1435,10 +1457,6 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
14351457
}
14361458

14371459
//then draw all the children
1438-
if ( p )
1439-
{
1440-
p->setFont( itemFont );
1441-
}
14421460
QFontMetricsF itemFontMetrics( itemFont );
14431461

14441462
int nChildItems = item->rowCount();
@@ -1491,6 +1509,8 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
14911509
{
14921510
p->save();
14931511
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
1512+
p->setPen( itemFontColor );
1513+
p->setFont( itemFont );
14941514
p->drawText( maxSymbolWidth * fontOversamplingFactor,
14951515
( currentY + symbolItemHeight / 2.0 ) * fontOversamplingFactor + itemFontMetrics.ascent() / 2.0, currentComposerItem->text() );
14961516
p->restore();

src/mapserver/qgswmsserver.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class QgsRasterLayer;
3535
class QgsRectangle;
3636
class QgsVectorLayer;
3737
class QgsSymbol;
38+
class QColor;
3839
class QFile;
3940
class QFont;
4041
class QImage;
@@ -127,8 +128,8 @@ class QgsWMSServer
127128
@param maxSymbolWidth Includes boxSpace and iconLabelSpace. If p==0: maximum Symbol width is calculated, if p: maxSymbolWidth is input parameter
128129
*/
129130
void drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p, double& maxTextWidth, double& maxSymbolWidth, double& currentY, const QFont& layerFont,
130-
const QFont& itemFont, double boxSpace, double layerSpace, double symbolSpace, double iconLabelSpace,
131-
double symbolWidth, double symbolHeight, double fontOversamplingFactor, double dpi ) const;
131+
const QColor& layerFontColor, const QFont& itemFont, const QColor& itemFontColor, double boxSpace, double layerSpace,
132+
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor, double dpi ) const;
132133
/**Draws a (old generation) symbol. Optionally, maxHeight is adapted (e.g. for large point markers) */
133134
void drawLegendSymbol( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double& symbolWidth, double& symbolHeight,
134135
double layerOpacity, double dpi, double yDownShift ) const;
@@ -140,7 +141,7 @@ class QgsWMSServer
140141

141142
/**Read legend parameter from the request or from the first print composer in the project*/
142143
void legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& symbolSpace, double& iconLabelSpace, double& symbolWidth, double& symbolHeight,
143-
QFont& layerFont, QFont& itemFont );
144+
QFont& layerFont, QFont& itemFont, QColor& layerFontColor, QColor& itemFontColor );
144145

145146
QImage* printCompositionToImage( QgsComposition* c ) const;
146147

0 commit comments

Comments
 (0)