Skip to content

Commit

Permalink
Add method to build layer tree
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Apr 9, 2019
1 parent bc72ff2 commit 46746a3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
49 changes: 49 additions & 0 deletions src/server/services/wms/qgswmsgetlegendgraphics.cpp
Expand Up @@ -18,6 +18,10 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayertree.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerfeaturecounter.h"

#include "qgswmsutils.h"
#include "qgswmsserviceexception.h"
#include "qgswmsgetlegendgraphics.h"
Expand Down Expand Up @@ -124,5 +128,50 @@ namespace QgsWms
QStringLiteral( "BBOX parameter cannot be combined with RULE." ) );
}
}

QgsLayerTreeModel *legendModel( const QgsWmsRenderContext &context, QgsLayerTree &tree )
{
const QgsWmsParameters parameters = context.parameters();

std::unique_ptr<QgsLayerTreeModel> model( new QgsLayerTreeModel( &tree ) );
return model.release();
}

QgsLayerTree *layerTree( const QgsWmsRenderContext &context )
{
std::unique_ptr<QgsLayerTree> tree;

QList<QgsVectorLayerFeatureCounter *> counters;
for ( QgsMapLayer *ml : context.layersToRender() )
{
QgsLayerTreeLayer *lt = tree->addLayer( ml );

// name
if ( !ml->title().isEmpty() )
lt->setName( ml->title() );

//show feature count
const bool showFeatureCount = context.parameters().showFeatureCountAsBool();
const QString property = QStringLiteral( "showFeatureCount" );
lt->setCustomProperty( property, showFeatureCount );

if ( ml->type() != QgsMapLayerType::VectorLayer || !showFeatureCount )
continue;

QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
QgsVectorLayerFeatureCounter *counter = vl->countSymbolFeatures();
if ( !counter )
continue;

counters.append( counter );
}

for ( QgsVectorLayerFeatureCounter *counter : counters )
{
counter->waitForFinished();
}

return tree.release();
}
} // namespace QgsWms

9 changes: 6 additions & 3 deletions src/server/services/wms/qgswmsgetlegendgraphics.h
Expand Up @@ -18,6 +18,9 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayertreemodel.h"

#include "qgswmsrendercontext.h"

namespace QgsWms
{
Expand All @@ -30,8 +33,8 @@ namespace QgsWms
QgsServerResponse &response );

void checkParameters( const QgsWmsParameters &parameters );
} // namespace QgsWms



QgsLayerTreeModel *legendModel( const QgsWmsRenderContext &context, QgsLayerTree &tree );

QgsLayerTree *layerTree( const QgsWmsRenderContext &context );
} // namespace QgsWms

0 comments on commit 46746a3

Please sign in to comment.