Skip to content

Commit

Permalink
Parameter for font color in GetLegendGraphic
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jan 16, 2012
1 parent d8bdcb2 commit 0b951c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
44 changes: 32 additions & 12 deletions src/mapserver/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ QImage* QgsWMSServer::getLegendGraphics()
//get icon size, spaces between legend items and font from config parser
double boxSpace, layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight;
QFont layerFont, itemFont;
QColor layerFontColor, itemFontColor;
legendParameters( mmToPixelFactor, fontOversamplingFactor, boxSpace, layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight,
layerFont, itemFont );
layerFont, itemFont, layerFontColor, itemFontColor );

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

drawLegendLayerItem( layerItem, 0, maxTextWidth, maxSymbolWidth, currentY, layerFont, itemFont, boxSpace, layerSpace, symbolSpace,
iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor, theImage->dotsPerMeterX() * 0.0254 );
drawLegendLayerItem( layerItem, 0, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
boxSpace, layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
theImage->dotsPerMeterX() * 0.0254 );
}
}
currentY += boxSpace;
Expand All @@ -359,8 +361,9 @@ QImage* QgsWMSServer::getLegendGraphics()
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
if ( layerItem )
{
drawLegendLayerItem( layerItem, &p, maxTextWidth, maxSymbolWidth, currentY, layerFont, itemFont, boxSpace, layerSpace, symbolSpace,
iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor, theImage->dotsPerMeterX() * 0.0254 );
drawLegendLayerItem( layerItem, &p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor, boxSpace,
layerSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor,
theImage->dotsPerMeterX() * 0.0254 );
}
}

Expand All @@ -370,7 +373,7 @@ QImage* QgsWMSServer::getLegendGraphics()
}

void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& symbolSpace, double& iconLabelSpace, double& symbolWidth, double& symbolHeight,
QFont& layerFont, QFont& itemFont )
QFont& layerFont, QFont& itemFont, QColor& layerFontColor, QColor& itemFontColor )
{
//spaces between legend elements
QMap<QString, QString>::const_iterator boxSpaceIt = mParameterMap.find( "BOXSPACE" );
Expand Down Expand Up @@ -418,6 +421,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
{
layerFont.setPixelSize( layerFont.pointSizeF() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
}
QMap<QString, QString>::const_iterator layerFontColorIt = mParameterMap.find( "LAYERFONTCOLOR" );
if ( layerFontColorIt != mParameterMap.constEnd() )
{
layerFontColor.setNamedColor( layerFontColorIt.value() );
}
else
{
layerFontColor = QColor( 0, 0, 0 );
}


itemFont = mConfigParser->legendItemFont();
Expand Down Expand Up @@ -445,6 +457,15 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
{
itemFont.setPixelSize( itemFont.pointSizeF() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
}
QMap<QString, QString>::const_iterator itemFontColorIt = mParameterMap.find( "ITEMFONTCOLOR" );
if ( itemFontColorIt != mParameterMap.constEnd() )
{
itemFontColor.setNamedColor( itemFontColorIt.value() );
}
else
{
itemFontColor = QColor( 0, 0, 0 );
}
}

QDomDocument QgsWMSServer::getStyle()
Expand Down Expand Up @@ -1395,8 +1416,8 @@ QStringList QgsWMSServer::layerSet( const QStringList &layersList,
}

void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p, double& maxTextWidth, double& maxSymbolWidth, double& currentY, const QFont& layerFont,
const QFont& itemFont, double boxSpace, double layerSpace, double symbolSpace,
double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor,
const QColor& layerFontColor, const QFont& itemFont, const QColor& itemFontColor, double boxSpace, double layerSpace,
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor,
double dpi ) const
{
if ( !item )
Expand All @@ -1412,6 +1433,7 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* 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();
Expand All @@ -1435,10 +1457,6 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p,
}

//then draw all the children
if ( p )
{
p->setFont( itemFont );
}
QFontMetricsF itemFontMetrics( itemFont );

int nChildItems = item->rowCount();
Expand Down Expand Up @@ -1491,6 +1509,8 @@ void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* 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();
Expand Down
7 changes: 4 additions & 3 deletions src/mapserver/qgswmsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class QgsRasterLayer;
class QgsRectangle;
class QgsVectorLayer;
class QgsSymbol;
class QColor;
class QFile;
class QFont;
class QImage;
Expand Down Expand Up @@ -127,8 +128,8 @@ class QgsWMSServer
@param maxSymbolWidth Includes boxSpace and iconLabelSpace. If p==0: maximum Symbol width is calculated, if p: maxSymbolWidth is input parameter
*/
void drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p, double& maxTextWidth, double& maxSymbolWidth, double& currentY, const QFont& layerFont,
const QFont& itemFont, double boxSpace, double layerSpace, double symbolSpace, double iconLabelSpace,
double symbolWidth, double symbolHeight, double fontOversamplingFactor, double dpi ) const;
const QColor& layerFontColor, const QFont& itemFont, const QColor& itemFontColor, double boxSpace, double layerSpace,
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor, double dpi ) const;
/**Draws a (old generation) symbol. Optionally, maxHeight is adapted (e.g. for large point markers) */
void drawLegendSymbol( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double& symbolWidth, double& symbolHeight,
double layerOpacity, double dpi, double yDownShift ) const;
Expand All @@ -140,7 +141,7 @@ class QgsWMSServer

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

QImage* printCompositionToImage( QgsComposition* c ) const;

Expand Down

0 comments on commit 0b951c6

Please sign in to comment.