Skip to content

Commit f96484f

Browse files
committedSep 1, 2014
Make WMS server use QgsLegendRenderer
1 parent 4d0c043 commit f96484f

File tree

6 files changed

+137
-317
lines changed

6 files changed

+137
-317
lines changed
 

‎src/core/composer/qgscomposerlegenditem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem
167167
//! @note added in 2.6
168168
QString ruleKey() const { return mItem.key; }
169169

170+
//! @note added in 2.6
171+
const QgsLegendSymbolItemV2& itemData() const { return mItem; }
172+
170173
//! @note added in 2.6
171174
static QgsComposerSymbolV2Item* findItemByRuleKey( QgsComposerLayerItem* parentLayerItem, QString ruleKey );
172175

‎src/core/composer/qgslegendmodel.cpp

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
QgsLegendModel::QgsLegendModel()
3838
: QStandardItemModel()
39-
, mScaleDenominator( -1 )
4039
, mAutoUpdate( true )
4140
{
4241
setColumnCount( 2 );
@@ -131,8 +130,6 @@ void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QL
131130
void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenominator, QString rule )
132131
{
133132
mLayerIds = layerIds;
134-
mScaleDenominator = scaleDenominator;
135-
mRule = rule;
136133

137134
//for now clear the model and add the new entries
138135
clear();
@@ -145,6 +142,62 @@ void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenom
145142
currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *idIter );
146143
addLayer( currentLayer, scaleDenominator, rule );
147144
}
145+
146+
// filter out items where the rule is not matching - used by WMS to get symbol icon for a particular rule
147+
if ( !rule.isEmpty() )
148+
{
149+
for ( int i = rowCount() - 1 ; i >= 0; --i )
150+
{
151+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( invisibleRootItem()->child( i ) );
152+
if ( !lItem )
153+
continue;
154+
155+
// remove rules that do not match
156+
bool gotMatchingRule = false;
157+
for ( int j = 0; j < lItem->rowCount(); ++j )
158+
{
159+
QgsComposerSymbolV2Item* sItem = dynamic_cast<QgsComposerSymbolV2Item*>( lItem->child( j ) );
160+
if ( !sItem )
161+
continue;
162+
163+
if ( sItem->itemData().label == rule )
164+
{
165+
QStandardItem* takenSItem = lItem->takeChild( j );
166+
lItem->removeRows( 0, lItem->rowCount() );
167+
lItem->setChild( 0, takenSItem );
168+
gotMatchingRule = true;
169+
break;
170+
}
171+
}
172+
173+
if ( !gotMatchingRule )
174+
removeRow( i );
175+
}
176+
}
177+
178+
if ( scaleDenominator != -1 )
179+
{
180+
for ( int i = 0; i < rowCount(); ++i )
181+
{
182+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( invisibleRootItem()->child( i ) );
183+
if ( !lItem )
184+
continue;
185+
186+
for ( int j = lItem->rowCount() - 1; j >= 0; --j )
187+
{
188+
QgsComposerSymbolV2Item* sItem = dynamic_cast<QgsComposerSymbolV2Item*>( lItem->child( j ) );
189+
if ( !sItem )
190+
continue;
191+
192+
if ( sItem->itemData().scaleDenomMin > 0 && sItem->itemData().scaleDenomMax > 0 &&
193+
( sItem->itemData().scaleDenomMin > scaleDenominator || sItem->itemData().scaleDenomMax < scaleDenominator ) )
194+
{
195+
lItem->removeRow( j );
196+
}
197+
}
198+
199+
}
200+
}
148201
}
149202

150203
QStandardItem* QgsLegendModel::addGroup( QString text, int position, QStandardItem* parentItem )
@@ -256,14 +309,17 @@ void QgsLegendModel::removeLayer( const QString& layerId )
256309

257310
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator, QString rule, QStandardItem* parentItem )
258311
{
259-
Q_UNUSED( scaleDenominator );
260312
Q_UNUSED( rule );
261313

262314
if ( !theMapLayer )
263315
{
264316
return;
265317
}
266318

319+
if ( scaleDenominator != -1 && theMapLayer->hasScaleBasedVisibility() &&
320+
( theMapLayer->minimumScale() > scaleDenominator || theMapLayer->maximumScale() < scaleDenominator ) )
321+
return;
322+
267323
if ( !parentItem )
268324
parentItem = invisibleRootItem();
269325

‎src/core/composer/qgslegendmodel.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
120120

121121
protected:
122122
QStringList mLayerIds;
123-
double mScaleDenominator;
124-
QString mRule;
125123
/**True if this application has toplevel windows (normally true). If this is false, this means that the application
126124
might not have a running x-server on unix systems and so QPixmap and QIcon cannot be used*/
127125
bool mHasTopLevelWindow;

‎src/core/symbology-ng/qgslegendsymbolitemv2.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class QgsSymbolV2;
77

8-
class QgsLegendSymbolItemV2
8+
class CORE_EXPORT QgsLegendSymbolItemV2
99
{
1010
public:
1111
QgsLegendSymbolItemV2();
@@ -23,7 +23,6 @@ class QgsLegendSymbolItemV2
2323

2424
int scaleDenomMin;
2525
int scaleDenomMax;
26-
// TODO: QString rule;
2726
};
2827

2928

‎src/mapserver/qgswmsserver.cpp

Lines changed: 72 additions & 289 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgscrscache.h"
2121
#include "qgsfield.h"
2222
#include "qgsgeometry.h"
23+
#include "qgslegendrenderer.h"
2324
#include "qgsmaplayer.h"
2425
#include "qgsmaplayerregistry.h"
2526
#include "qgsmaprenderer.h"
@@ -557,25 +558,15 @@ QImage* QgsWMSServer::getLegendGraphics()
557558
return 0;
558559
}
559560

560-
//create first image (to find out dpi)
561-
QImage* theImage = createImage( 10, 10 );
562-
if ( !theImage )
563-
{
564-
return 0;
565-
}
566-
double mmToPixelFactor = theImage->dotsPerMeterX() / 1000.0;
567-
double maxTextWidth = 0;
568-
double maxSymbolWidth = 0;
569-
double fontOversamplingFactor = 10.0;
570-
571561
//get icon size, spaces between legend items and font from config parser
572562
double boxSpace, layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight;
573563
QFont layerFont, itemFont;
574564
QColor layerFontColor, itemFontColor;
575-
legendParameters( mmToPixelFactor, fontOversamplingFactor, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
565+
legendParameters( boxSpace, layerSpace, layerTitleSpace, symbolSpace,
576566
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor );
577567

578568
QString rule;
569+
int ruleSymbolWidth = 0, ruleSymbolHeight = 0;
579570
QMap<QString, QString>::const_iterator ruleIt = mParameters.find( "RULE" );
580571
if ( ruleIt != mParameters.constEnd() )
581572
{
@@ -588,7 +579,7 @@ QImage* QgsWMSServer::getLegendGraphics()
588579
double width = widthIt.value().toDouble( &conversionSuccess );
589580
if ( conversionSuccess )
590581
{
591-
symbolWidth = width;
582+
ruleSymbolWidth = width;
592583
}
593584
}
594585

@@ -599,7 +590,7 @@ QImage* QgsWMSServer::getLegendGraphics()
599590
double width = heightIt.value().toDouble( &conversionSuccess );
600591
if ( conversionSuccess )
601592
{
602-
symbolHeight = width;
593+
ruleSymbolHeight = width;
603594
}
604595
}
605596
}
@@ -614,124 +605,110 @@ QImage* QgsWMSServer::getLegendGraphics()
614605
return 0;
615606
}
616607

608+
// find out DPI
609+
QImage* tmpImage = createImage( 1, 1 );
610+
if ( !tmpImage )
611+
return 0;
612+
qreal dpmm = tmpImage->dotsPerMeterX() / 1000.0;
613+
delete tmpImage;
614+
615+
// setup legend configuration
616+
QgsLegendSettings legendSettings;
617+
legendSettings.setTitle( QString() );
618+
legendSettings.setBoxSpace( boxSpace );
619+
legendSettings.rstyle( QgsComposerLegendStyle::Subgroup ).setMargin( QgsComposerLegendStyle::Top, layerSpace );
620+
// TODO: not available: layer title space
621+
legendSettings.rstyle( QgsComposerLegendStyle::Symbol ).setMargin( QgsComposerLegendStyle::Top, symbolSpace );
622+
legendSettings.rstyle( QgsComposerLegendStyle::SymbolLabel ).setMargin( QgsComposerLegendStyle::Left, iconLabelSpace );
623+
legendSettings.setSymbolSize( QSizeF( symbolWidth, symbolHeight ) );
624+
legendSettings.rstyle( QgsComposerLegendStyle::Subgroup ).setFont( layerFont );
625+
legendSettings.rstyle( QgsComposerLegendStyle::SymbolLabel ).setFont( itemFont );
626+
// TODO: not available: layer font color
627+
legendSettings.setFontColor( itemFontColor );
628+
617629
if ( !rule.isEmpty() )
618630
{
619631
//create second image with the right dimensions
620-
QImage* paintImage = createImage( symbolWidth, symbolHeight );
632+
QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight );
621633

622634
//go through the items a second time for painting
623635
QPainter p( paintImage );
624636
p.setRenderHint( QPainter::Antialiasing, true );
637+
p.scale( dpmm, dpmm );
625638

626-
QgsComposerLegendItem* currentComposerItem = dynamic_cast<QgsComposerLegendItem*>( rootItem->child( 0 )->child( 0 ) );
639+
QgsComposerBaseSymbolItem* currentComposerItem = dynamic_cast<QgsComposerBaseSymbolItem*>( rootItem->child( 0 )->child( 0 ) );
627640
if ( currentComposerItem != NULL )
628641
{
629-
QgsComposerLegendItem::ItemType type = currentComposerItem->itemType();
630-
switch ( type )
631-
{
632-
case QgsComposerLegendItem::SymbologyV2Item:
633-
drawLegendSymbolV2( currentComposerItem, &p, 0., 0., symbolWidth, symbolHeight, 0. );
634-
break;
635-
case QgsComposerLegendItem::RasterSymbolItem:
636-
drawRasterSymbol( currentComposerItem, &p, 0., 0., symbolWidth, symbolHeight, 0. );
637-
break;
638-
case QgsComposerLegendItem::GroupItem:
639-
//QgsDebugMsg( "GroupItem not handled" );
640-
break;
641-
case QgsComposerLegendItem::LayerItem:
642-
//QgsDebugMsg( "GroupItem not handled" );
643-
break;
644-
case QgsComposerLegendItem::StyleItem:
645-
//QgsDebugMsg( "StyleItem not handled" );
646-
break;
647-
}
642+
QgsComposerBaseSymbolItem::ItemContext ctx;
643+
ctx.painter = &p;
644+
ctx.labelXOffset = 0;
645+
ctx.point = QPointF();
646+
double itemHeight = ruleSymbolHeight / dpmm;
647+
currentComposerItem->drawSymbol( legendSettings, &ctx, itemHeight );
648648
}
649649

650650
QgsMapLayerRegistry::instance()->removeAllMapLayers();
651-
delete theImage;
652651
return paintImage;
653652
}
654653

655-
double currentY = drawLegendGraphics( 0, fontOversamplingFactor, rootItem, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
656-
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor, maxTextWidth,
657-
maxSymbolWidth );
654+
for ( int i = 0; i < rootItem->rowCount(); ++i )
655+
{
656+
if ( QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) ) )
657+
{
658+
// layer titles - hidden or not
659+
lItem->setStyle( mDrawLegendLayerLabel ? QgsComposerLegendStyle::Subgroup : QgsComposerLegendStyle::Hidden );
658660

659-
//create second image with the right dimensions
660-
QImage* paintImage = createImage( maxTextWidth + maxSymbolWidth, ceil( currentY ) );
661+
// rule item titles
662+
if ( !mDrawLegendItemLabel )
663+
{
664+
for ( int j = 0; j < lItem->rowCount(); ++j )
665+
{
666+
if ( QgsComposerBaseSymbolItem* sItem = dynamic_cast<QgsComposerBaseSymbolItem*>( lItem->child( j ) ) )
667+
sItem->setUserText( " " ); // empty string = no override, so let's use one space
668+
}
669+
}
670+
}
671+
}
672+
673+
QgsLegendRenderer legendRenderer( &legendModel, legendSettings );
674+
QSizeF minSize = legendRenderer.minimumSize();
675+
QSize s( minSize.width() * dpmm, minSize.height() * dpmm );
676+
677+
QImage* paintImage = createImage( s.width(), s.height() );
661678

662-
//go through the items a second time for painting
663679
QPainter p( paintImage );
664680
p.setRenderHint( QPainter::Antialiasing, true );
681+
p.scale( dpmm, dpmm );
665682

666-
drawLegendGraphics( &p, fontOversamplingFactor, rootItem, boxSpace, layerSpace, layerTitleSpace, symbolSpace,
667-
iconLabelSpace, symbolWidth, symbolHeight, layerFont, itemFont, layerFontColor, itemFontColor, maxTextWidth,
668-
maxSymbolWidth );
683+
legendRenderer.drawLegend( &p );
684+
685+
p.end();
669686

670687
QgsMapLayerRegistry::instance()->removeAllMapLayers();
671-
delete theImage;
672688
return paintImage;
673689
}
674690

675-
double QgsWMSServer::drawLegendGraphics( QPainter* p, double fontOversamplingFactor, QStandardItem* rootItem, double boxSpace,
676-
double layerSpace, double layerTitleSpace, double symbolSpace, double iconLabelSpace,
677-
double symbolWidth, double symbolHeight, const QFont& layerFont, const QFont& itemFont,
678-
const QColor& layerFontColor, const QColor& itemFontColor, double& maxTextWidth, double& maxSymbolWidth )
679-
{
680-
if ( !rootItem )
681-
{
682-
return 0;
683-
}
684-
685-
int numLayerItems = rootItem->rowCount();
686-
if ( numLayerItems < 1 )
687-
{
688-
return 0;
689-
}
690691

691-
double currentY = boxSpace;
692-
for ( int i = 0; i < numLayerItems; ++i )
693-
{
694-
QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( rootItem->child( i ) );
695-
if ( layerItem )
696-
{
697-
if ( i > 0 )
698-
{
699-
currentY += layerSpace;
700-
}
701-
drawLegendLayerItem( layerItem, p, maxTextWidth, maxSymbolWidth, currentY, layerFont, layerFontColor, itemFont, itemFontColor,
702-
boxSpace, layerSpace, layerTitleSpace, symbolSpace, iconLabelSpace, symbolWidth, symbolHeight, fontOversamplingFactor );
703-
}
704-
}
705-
currentY += boxSpace;
706-
return currentY;
707-
}
708692

709-
void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& layerTitleSpace,
693+
void QgsWMSServer::legendParameters( double& boxSpace, double& layerSpace, double& layerTitleSpace,
710694
double& symbolSpace, double& iconLabelSpace, double& symbolWidth, double& symbolHeight,
711695
QFont& layerFont, QFont& itemFont, QColor& layerFontColor, QColor& itemFontColor )
712696
{
713697
//spaces between legend elements
714698
QMap<QString, QString>::const_iterator boxSpaceIt = mParameters.find( "BOXSPACE" );
715-
boxSpace = ( boxSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendBoxSpace() * mmToPixelFactor :
716-
boxSpaceIt.value().toDouble() * mmToPixelFactor;
699+
boxSpace = ( boxSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendBoxSpace() : boxSpaceIt.value().toDouble();
717700
QMap<QString, QString>::const_iterator layerSpaceIt = mParameters.find( "LAYERSPACE" );
718-
layerSpace = ( layerSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendLayerSpace() * mmToPixelFactor :
719-
layerSpaceIt.value().toDouble() * mmToPixelFactor;
701+
layerSpace = ( layerSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendLayerSpace() : layerSpaceIt.value().toDouble();
720702
QMap<QString, QString>::const_iterator layerTitleSpaceIt = mParameters.find( "LAYERTITLESPACE" );
721-
layerTitleSpace = ( layerTitleSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendLayerTitleSpace() * mmToPixelFactor :
722-
layerTitleSpaceIt.value().toDouble() * mmToPixelFactor;
703+
layerTitleSpace = ( layerTitleSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendLayerTitleSpace() : layerTitleSpaceIt.value().toDouble();
723704
QMap<QString, QString>::const_iterator symbolSpaceIt = mParameters.find( "SYMBOLSPACE" );
724-
symbolSpace = ( symbolSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendSymbolSpace() * mmToPixelFactor :
725-
symbolSpaceIt.value().toDouble() * mmToPixelFactor;
705+
symbolSpace = ( symbolSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendSymbolSpace() : symbolSpaceIt.value().toDouble();
726706
QMap<QString, QString>::const_iterator iconLabelSpaceIt = mParameters.find( "ICONLABELSPACE" );
727-
iconLabelSpace = ( iconLabelSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendIconLabelSpace() * mmToPixelFactor :
728-
iconLabelSpaceIt.value().toDouble() * mmToPixelFactor;
707+
iconLabelSpace = ( iconLabelSpaceIt == mParameters.constEnd() ) ? mConfigParser->legendIconLabelSpace() : iconLabelSpaceIt.value().toDouble();
729708
QMap<QString, QString>::const_iterator symbolWidthIt = mParameters.find( "SYMBOLWIDTH" );
730-
symbolWidth = ( symbolWidthIt == mParameters.constEnd() ) ? mConfigParser->legendSymbolWidth() * mmToPixelFactor :
731-
symbolWidthIt.value().toDouble() * mmToPixelFactor;
709+
symbolWidth = ( symbolWidthIt == mParameters.constEnd() ) ? mConfigParser->legendSymbolWidth() : symbolWidthIt.value().toDouble();
732710
QMap<QString, QString>::const_iterator symbolHeightIt = mParameters.find( "SYMBOLHEIGHT" );
733-
symbolHeight = ( symbolHeightIt == mParameters.constEnd() ) ? mConfigParser->legendSymbolHeight() * mmToPixelFactor :
734-
symbolHeightIt.value().toDouble() * mmToPixelFactor;
711+
symbolHeight = ( symbolHeightIt == mParameters.constEnd() ) ? mConfigParser->legendSymbolHeight() : symbolHeightIt.value().toDouble();
735712

736713
//font properties
737714
layerFont = mConfigParser->legendLayerFont();
@@ -751,14 +728,7 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
751728
layerFont.setItalic( layerFontItalicIt.value().compare( "TRUE", Qt::CaseInsensitive ) == 0 );
752729
}
753730
QMap<QString, QString>::const_iterator layerFontSizeIt = mParameters.find( "LAYERFONTSIZE" );
754-
if ( layerFontSizeIt != mParameters.constEnd() )
755-
{
756-
layerFont.setPixelSize( layerFontSizeIt.value().toDouble() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
757-
}
758-
else
759-
{
760-
layerFont.setPixelSize( layerFont.pointSizeF() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
761-
}
731+
layerFont.setPointSizeF( layerFontSizeIt != mParameters.constEnd() ? layerFontSizeIt.value().toDouble() : layerFont.pointSizeF() );
762732
QMap<QString, QString>::const_iterator layerFontColorIt = mParameters.find( "LAYERFONTCOLOR" );
763733
if ( layerFontColorIt != mParameters.constEnd() )
764734
{
@@ -796,14 +766,7 @@ void QgsWMSServer::legendParameters( double mmToPixelFactor, double fontOversamp
796766
itemFont.setItalic( itemFontItalicIt.value().compare( "TRUE", Qt::CaseInsensitive ) == 0 );
797767
}
798768
QMap<QString, QString>::const_iterator itemFontSizeIt = mParameters.find( "ITEMFONTSIZE" );
799-
if ( itemFontSizeIt != mParameters.constEnd() )
800-
{
801-
itemFont.setPixelSize( itemFontSizeIt.value().toDouble() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
802-
}
803-
else
804-
{
805-
itemFont.setPixelSize( itemFont.pointSizeF() * 0.3528 * mmToPixelFactor * fontOversamplingFactor );
806-
}
769+
itemFont.setPointSizeF( itemFontSizeIt != mParameters.constEnd() ? itemFontSizeIt.value().toDouble() : itemFont.pointSizeF() );
807770
QMap<QString, QString>::const_iterator itemFontColorIt = mParameters.find( "ITEMFONTCOLOR" );
808771
if ( itemFontColorIt != mParameters.constEnd() )
809772
{
@@ -1984,186 +1947,6 @@ QStringList QgsWMSServer::layerSet( const QStringList &layersList,
19841947
return layerKeys;
19851948
}
19861949

1987-
void QgsWMSServer::drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p, double& maxTextWidth, double& maxSymbolWidth, double& currentY, const QFont& layerFont,
1988-
const QColor& layerFontColor, const QFont& itemFont, const QColor& itemFontColor, double boxSpace, double layerSpace,
1989-
double layerTitleSpace, double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor ) const
1990-
{
1991-
Q_UNUSED( layerSpace );
1992-
if ( !item )
1993-
{
1994-
return;
1995-
}
1996-
1997-
QFontMetricsF layerFontMetrics( layerFont );
1998-
if ( mDrawLegendLayerLabel )
1999-
{
2000-
currentY += layerFontMetrics.ascent() / fontOversamplingFactor;
2001-
}
2002-
2003-
//draw layer title first
2004-
if ( p )
2005-
{
2006-
if ( mDrawLegendLayerLabel )
2007-
{
2008-
p->save();
2009-
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
2010-
p->setPen( layerFontColor );
2011-
p->setFont( layerFont );
2012-
p->drawText( boxSpace * fontOversamplingFactor, currentY * fontOversamplingFactor, item->text() );
2013-
p->restore();
2014-
}
2015-
}
2016-
else
2017-
{
2018-
double layerItemWidth = layerFontMetrics.width( item->text() ) / fontOversamplingFactor + boxSpace;
2019-
if ( layerItemWidth > maxTextWidth )
2020-
{
2021-
if ( mDrawLegendLayerLabel )
2022-
{
2023-
maxTextWidth = layerItemWidth;
2024-
}
2025-
}
2026-
}
2027-
2028-
if ( mDrawLegendLayerLabel )
2029-
{
2030-
currentY += layerTitleSpace;
2031-
}
2032-
2033-
//then draw all the children
2034-
QFontMetricsF itemFontMetrics( itemFont );
2035-
2036-
int nChildItems = item->rowCount();
2037-
QgsComposerLegendItem* currentComposerItem = 0;
2038-
2039-
for ( int i = 0; i < nChildItems; ++i )
2040-
{
2041-
currentComposerItem = dynamic_cast<QgsComposerLegendItem*>( item->child( i ) );
2042-
if ( !currentComposerItem )
2043-
{
2044-
continue;
2045-
}
2046-
2047-
double currentSymbolHeight = symbolHeight;
2048-
double currentSymbolWidth = symbolWidth; //symbol width (without box space and icon/label space
2049-
double currentTextWidth = 0;
2050-
2051-
//if the font is larger than the standard symbol size, try to draw the symbol centered (shifting towards the bottom)
2052-
double symbolDownShift = ( itemFontMetrics.ascent() / fontOversamplingFactor - symbolHeight ) / 2.0;
2053-
if ( symbolDownShift < 0 )
2054-
{
2055-
symbolDownShift = 0;
2056-
}
2057-
2058-
QgsComposerLegendItem::ItemType type = currentComposerItem->itemType();
2059-
switch ( type )
2060-
{
2061-
case QgsComposerLegendItem::SymbologyV2Item:
2062-
drawLegendSymbolV2( currentComposerItem, p, boxSpace, currentY, currentSymbolWidth, currentSymbolHeight, symbolDownShift );
2063-
break;
2064-
case QgsComposerLegendItem::RasterSymbolItem:
2065-
drawRasterSymbol( currentComposerItem, p, boxSpace, currentY, currentSymbolWidth, currentSymbolHeight, symbolDownShift );
2066-
break;
2067-
case QgsComposerLegendItem::GroupItem:
2068-
//QgsDebugMsg( "GroupItem not handled" );
2069-
break;
2070-
case QgsComposerLegendItem::LayerItem:
2071-
//QgsDebugMsg( "GroupItem not handled" );
2072-
break;
2073-
case QgsComposerLegendItem::StyleItem:
2074-
//QgsDebugMsg( "StyleItem not handled" );
2075-
break;
2076-
}
2077-
2078-
if ( mDrawLegendItemLabel )
2079-
{
2080-
//finally draw text
2081-
currentTextWidth = itemFontMetrics.width( currentComposerItem->text() ) / fontOversamplingFactor;
2082-
}
2083-
else
2084-
{
2085-
currentTextWidth = 0;
2086-
}
2087-
double symbolItemHeight = qMax( itemFontMetrics.ascent() / fontOversamplingFactor, currentSymbolHeight );
2088-
2089-
if ( p )
2090-
{
2091-
if ( mDrawLegendItemLabel )
2092-
{
2093-
p->save();
2094-
p->scale( 1.0 / fontOversamplingFactor, 1.0 / fontOversamplingFactor );
2095-
p->setPen( itemFontColor );
2096-
p->setFont( itemFont );
2097-
p->drawText( maxSymbolWidth * fontOversamplingFactor,
2098-
( currentY + symbolItemHeight / 2.0 ) * fontOversamplingFactor + itemFontMetrics.ascent() / 2.0, currentComposerItem->text() );
2099-
p->restore();
2100-
}
2101-
}
2102-
else
2103-
{
2104-
if ( currentTextWidth > maxTextWidth )
2105-
{
2106-
if ( mDrawLegendItemLabel )
2107-
{
2108-
maxTextWidth = currentTextWidth;
2109-
}
2110-
}
2111-
double symbolWidth = boxSpace + currentSymbolWidth + iconLabelSpace;
2112-
if ( symbolWidth > maxSymbolWidth )
2113-
{
2114-
maxSymbolWidth = symbolWidth;
2115-
}
2116-
}
2117-
2118-
currentY += symbolItemHeight;
2119-
if ( i < ( nChildItems - 1 ) )
2120-
{
2121-
currentY += symbolSpace;
2122-
}
2123-
}
2124-
}
2125-
2126-
2127-
void QgsWMSServer::drawLegendSymbolV2( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double& symbolWidth,
2128-
double& symbolHeight, double yDownShift ) const
2129-
{
2130-
QgsComposerSymbolV2Item* symbolItem = dynamic_cast< QgsComposerSymbolV2Item* >( item );
2131-
if ( !symbolItem )
2132-
{
2133-
return;
2134-
}
2135-
QgsSymbolV2* symbol = symbolItem->symbolV2();
2136-
if ( !symbol )
2137-
{
2138-
return;
2139-
}
2140-
2141-
if ( p )
2142-
{
2143-
p->save();
2144-
p->translate( boxSpace, currentY + yDownShift );
2145-
p->scale( 1.0, 1.0 );
2146-
symbol->drawPreviewIcon( p, QSize( symbolWidth, symbolHeight ) );
2147-
p->restore();
2148-
}
2149-
}
2150-
2151-
void QgsWMSServer::drawRasterSymbol( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double symbolWidth, double symbolHeight, double yDownShift ) const
2152-
{
2153-
if ( !item || ! p )
2154-
{
2155-
return;
2156-
}
2157-
2158-
QgsComposerRasterSymbolItem* rasterItem = dynamic_cast< QgsComposerRasterSymbolItem* >( item );
2159-
if ( !rasterItem )
2160-
{
2161-
return;
2162-
}
2163-
2164-
p->setBrush( QBrush( rasterItem->color() ) );
2165-
p->drawRect( QRectF( boxSpace, currentY + yDownShift, symbolWidth, symbolHeight ) );
2166-
}
21671950

21681951
QMap<QString, QString> QgsWMSServer::applyRequestedLayerFilters( const QStringList& layerList ) const
21691952
{

‎src/mapserver/qgswmsserver.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,8 @@ class QgsWMSServer: public QgsOWSServer
155155
@param scaleDenominator Filter out layer if scale based visibility does not match (or use -1 if no scale restriction)*/
156156
QStringList layerSet( const QStringList& layersList, const QStringList& stylesList, const QgsCoordinateReferenceSystem& destCRS, double scaleDenominator = -1 ) const;
157157

158-
/**Draws graphics if painter != 0 and gives back y dimension, maximum symbol and text width of legend*/
159-
double drawLegendGraphics( QPainter* p, double fontOversamplingFactor, QStandardItem* rootItem, double boxSpace, double layerSpace, double layerTitleSpace,
160-
double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight,
161-
const QFont& layerFont, const QFont& itemFont, const QColor& layerFontColor, const QColor& itemFontColor,
162-
double& maxTextWidth, double& maxSymbolWidth );
163-
164-
//helper functions for GetLegendGraphics
165-
/**Draws layer item and subitems
166-
@param p painter if the item should be drawn, if 0 the size parameters are calculated only
167-
@param maxTextWidth Includes boxSpace (on the right side). If p==0: maximumTextWidth is calculated, if p: maxTextWidth parameter is used for rendering
168-
@param maxSymbolWidth Includes boxSpace and iconLabelSpace. If p==0: maximum Symbol width is calculated, if p: maxSymbolWidth is input parameter
169-
*/
170-
void drawLegendLayerItem( QgsComposerLayerItem* item, QPainter* p, double& maxTextWidth, double& maxSymbolWidth, double& currentY, const QFont& layerFont,
171-
const QColor& layerFontColor, const QFont& itemFont, const QColor& itemFontColor, double boxSpace, double layerSpace,
172-
double layerTitleSpace, double symbolSpace, double iconLabelSpace, double symbolWidth, double symbolHeight, double fontOversamplingFactor ) const;
173-
/**Draws a symbol. Optionally, maxHeight is adapted (e.g. for large point markers) */
174-
void drawLegendSymbolV2( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double& symbolWidth, double& symbolHeight, double yDownShift ) const;
175-
void drawRasterSymbol( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double symbolWidth, double symbolHeight, double yDownShift ) const;
176-
177158
/**Read legend parameter from the request or from the first print composer in the project*/
178-
void legendParameters( double mmToPixelFactor, double fontOversamplingFactor, double& boxSpace, double& layerSpace, double& layerTitleSpace,
159+
void legendParameters( double& boxSpace, double& layerSpace, double& layerTitleSpace,
179160
double& symbolSpace, double& iconLabelSpace, double& symbolWidth, double& symbolHeight, QFont& layerFont, QFont& itemFont, QColor& layerFontColor, QColor& itemFontColor );
180161

181162
#if 0

0 commit comments

Comments
 (0)
Please sign in to comment.