Skip to content
Permalink
Browse files
Merge branch 'wms_layer_opacity'
  • Loading branch information
mhugent committed Nov 23, 2012
2 parents e5dae02 + dd1d4b5 commit 7f8768a072c13d86bebd31579c1786c0a5ef7cfb
Showing with 130 additions and 0 deletions.
  1. +118 −0 src/mapserver/qgswmsserver.cpp
  2. +12 −0 src/mapserver/qgswmsserver.h
@@ -14,6 +14,7 @@
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgswmsserver.h"
#include "qgsconfigparser.h"
#include "qgscrscache.h"
@@ -613,13 +614,20 @@ QImage* QgsWMSServer::getMap()
QMap<QString, QString> originalLayerFilters = applyRequestedLayerFilters( layersList );
QStringList selectedLayerIdList = applyFeatureSelections( layersList );

QList< QPair< QgsVectorLayer*, QgsFeatureRendererV2*> > bkVectorRenderers;
QList< QPair< QgsRasterLayer*, QgsRasterRenderer* > > bkRasterRenderers;
QList< QPair< QgsVectorLayer*, unsigned int> > bkVectorOld;

applyOpacities( layersList, bkVectorRenderers, bkVectorOld, bkRasterRenderers );

mMapRenderer->render( &thePainter );
if ( mConfigParser )
{
//draw configuration format specific overlay items
mConfigParser->drawOverlays( &thePainter, theImage->dotsPerMeterX() / 1000.0 * 25.4, theImage->width(), theImage->height() );
}

restoreOpacities( bkVectorRenderers, bkVectorOld, bkRasterRenderers );
restoreLayerFilters( originalLayerFilters );
clearFeatureSelections( selectedLayerIdList );

@@ -1978,6 +1986,116 @@ void QgsWMSServer::clearFeatureSelections( const QStringList& layerIds ) const
return;
}

void QgsWMSServer::applyOpacities( const QStringList& layerList, QList< QPair< QgsVectorLayer*, QgsFeatureRendererV2*> >& vectorRenderers,
QList< QPair< QgsVectorLayer*, unsigned int> >& vectorOld,
QList< QPair< QgsRasterLayer*, QgsRasterRenderer* > >& rasterRenderers )
{
//get opacity list
QMap<QString, QString>::const_iterator opIt = mParameterMap.find( "OPACITIES" );
if ( opIt == mParameterMap.constEnd() )
{
return;
}
QStringList opacityList = opIt.value().split( "," );

//collect leaf layers and their opacity
QList< QPair< QgsMapLayer*, int > > layerOpacityList;
QStringList::const_iterator oIt = opacityList.constBegin();
QStringList::const_iterator lIt = layerList.constBegin();
for ( ; oIt != opacityList.constEnd(); ++oIt, ++lIt )
{
//get layer list for
int opacity = oIt->toInt();
if ( opacity < 0 || opacity > 255 )
{
continue;
}
QList<QgsMapLayer*> llist = mConfigParser->mapLayerFromStyle( *lIt, "" );
QList<QgsMapLayer*>::const_iterator lListIt = llist.constBegin();
for ( ; lListIt != llist.constEnd(); ++lListIt )
{
layerOpacityList.push_back( qMakePair( *lListIt, opacity ) );
}
}

QList< QPair< QgsMapLayer*, int > >::const_iterator lOpIt = layerOpacityList.constBegin();
for ( ; lOpIt != layerOpacityList.constEnd(); ++lOpIt )
{
//vector or raster?
QgsMapLayer* ml = lOpIt->first;
int opacity = lOpIt->second;
if ( !ml || opacity == 255 )
{
continue;
}

if ( ml->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( ml );
if ( vl && vl->isUsingRendererV2() )
{
QgsFeatureRendererV2* rendererV2 = vl->rendererV2();
//backup old renderer
vectorRenderers.push_back( qMakePair( vl, rendererV2->clone() ) );
//modify symbols of current renderer
QgsSymbolV2List symbolList = rendererV2->symbols();
QgsSymbolV2List::iterator symbolIt = symbolList.begin();
for ( ; symbolIt != symbolList.end(); ++symbolIt )
{
( *symbolIt )->setAlpha(( *symbolIt )->alpha() * opacity / 255.0 );
}
}
else //old symbology
{
vectorOld.push_back( qMakePair( vl, vl->getTransparency() ) );
vl->setTransparency( opacity );
}

}
else if ( ml->type() == QgsMapLayer::RasterLayer )
{
QgsRasterLayer* rl = qobject_cast<QgsRasterLayer*>( ml );
if ( rl )
{
QgsRasterRenderer* rasterRenderer = rl->renderer();
if ( rasterRenderer )
{
rasterRenderers.push_back( qMakePair( rl, dynamic_cast<QgsRasterRenderer*>( rasterRenderer->clone() ) ) );
rasterRenderer->setOpacity( opacity / 255.0 );
}
}
}
}
}

void QgsWMSServer::restoreOpacities( QList< QPair< QgsVectorLayer*, QgsFeatureRendererV2*> >& vectorRenderers,
QList< QPair< QgsVectorLayer*, unsigned int> >& vectorOld,
QList < QPair< QgsRasterLayer*, QgsRasterRenderer* > >& rasterRenderers )
{
if ( vectorRenderers.isEmpty() && vectorOld.isEmpty() && rasterRenderers.isEmpty() )
{
return;
}

QList< QPair< QgsVectorLayer*, QgsFeatureRendererV2*> >::iterator vIt = vectorRenderers.begin();
for ( ; vIt != vectorRenderers.end(); ++vIt )
{
( *vIt ).first->setRendererV2(( *vIt ).second );
}

QList< QPair< QgsRasterLayer*, QgsRasterRenderer* > >::iterator rIt = rasterRenderers.begin();
for ( ; rIt != rasterRenderers.end(); ++rIt )
{
( *rIt ).first->setRenderer(( *rIt ).second );
}

QList< QPair< QgsVectorLayer*, unsigned int> >::iterator oIt = vectorOld.begin();
for ( ; oIt != vectorOld.end(); ++oIt )
{
( *oIt ).first->setTransparency(( *oIt ).second );
}
}

bool QgsWMSServer::checkMaximumWidthHeight() const
{
//test if maxWidth / maxHeight set and WIDTH / HEIGHT parameter is in the range
@@ -20,6 +20,7 @@

#include <QDomDocument>
#include <QMap>
#include <QPair>
#include <QString>
#include <map>

@@ -28,10 +29,12 @@ class QgsComposerLayerItem;
class QgsComposerLegendItem;
class QgsComposition;
class QgsConfigParser;
class QgsFeatureRendererV2;
class QgsMapLayer;
class QgsMapRenderer;
class QgsPoint;
class QgsRasterLayer;
class QgsRasterRenderer;
class QgsRectangle;
class QgsRenderContext;
class QgsVectorLayer;
@@ -164,6 +167,15 @@ class QgsWMSServer
/**Clear all feature selections in the given layers*/
void clearFeatureSelections( const QStringList& layerIds ) const;

/**Applies opacity on layer/group level*/
void applyOpacities( const QStringList& layerList, QList< QPair< QgsVectorLayer*, QgsFeatureRendererV2*> >& vectorRenderers,
QList< QPair< QgsVectorLayer*, unsigned int> >& vectorOld,
QList< QPair< QgsRasterLayer*, QgsRasterRenderer* > >& rasterRenderers );

/**Restore original opacities*/
void restoreOpacities( QList< QPair <QgsVectorLayer*, QgsFeatureRendererV2*> >& vectorRenderers, QList< QPair <QgsVectorLayer*, unsigned int> >& vectorOld,
QList< QPair < QgsRasterLayer*, QgsRasterRenderer* > >& rasterRenderers );

void appendFormats( QDomDocument &doc, QDomElement &elem, const QStringList &formats );

/**Checks WIDTH/HEIGHT values agains MaxWidth and MaxHeight

0 comments on commit 7f8768a

Please sign in to comment.