17
17
18
18
#include " qgswmsconfigparser.h"
19
19
#include " qgsmaplayer.h"
20
+ #include " qgsmapserviceexception.h"
20
21
21
22
#include " qgscomposerlabel.h"
23
+ #include " qgscomposerlegend.h"
22
24
#include " qgscomposermap.h"
23
25
#include " qgscomposerhtml.h"
24
26
#include " qgscomposerframe.h"
25
27
#include " qgscomposition.h"
26
28
29
+ #include " qgslayertreegroup.h"
30
+ #include " qgslayertreelayer.h"
31
+
27
32
QgsWMSConfigParser::QgsWMSConfigParser ()
28
33
{
29
34
@@ -37,10 +42,11 @@ QgsWMSConfigParser::~QgsWMSConfigParser()
37
42
QgsComposition* QgsWMSConfigParser::createPrintComposition ( const QString& composerTemplate, QgsMapRenderer* mapRenderer, const QMap< QString, QString >& parameterMap ) const
38
43
{
39
44
QList<QgsComposerMap*> composerMaps;
45
+ QList<QgsComposerLegend*> composerLegends;
40
46
QList<QgsComposerLabel*> composerLabels;
41
47
QList<const QgsComposerHtml*> composerHtmls;
42
48
43
- QgsComposition* c = initComposition ( composerTemplate, mapRenderer, composerMaps, composerLabels, composerHtmls );
49
+ QgsComposition* c = initComposition ( composerTemplate, mapRenderer, composerMaps, composerLegends, composerLabels, composerHtmls );
44
50
if ( !c )
45
51
{
46
52
return 0 ;
@@ -158,6 +164,66 @@ QgsComposition* QgsWMSConfigParser::createPrintComposition( const QString& compo
158
164
currentMap->setGridIntervalX ( parameterMap.value ( mapId + " :GRID_INTERVAL_X" ).toDouble () );
159
165
currentMap->setGridIntervalY ( parameterMap.value ( mapId + " :GRID_INTERVAL_Y" ).toDouble () );
160
166
}
167
+ // update legend
168
+ // if it has an auto-update model
169
+ foreach ( QgsComposerLegend* currentLegend, composerLegends )
170
+ {
171
+ if ( !currentLegend )
172
+ {
173
+ continue ;
174
+ }
175
+
176
+ if ( currentLegend->autoUpdateModel () || currentLegend->legendFilterByMapEnabled () )
177
+ {
178
+ // the legend has an auto-update model or
179
+ // has to be filter by map
180
+ // we will update it with map's layers
181
+ const QgsComposerMap* map = currentLegend->composerMap ();
182
+ if ( !map )
183
+ {
184
+ continue ;
185
+ }
186
+
187
+ // get model and layer tree root of the legend
188
+ QgsLegendModelV2* model = currentLegend->modelV2 ();
189
+ QgsLayerTreeGroup* root = model->rootGroup ();
190
+
191
+
192
+ // get layerIds find in the layer tree root
193
+ QStringList layerIds = root->findLayerIds ();
194
+ // get map layerIds
195
+ QStringList layerSet = map->layerSet ();
196
+
197
+ // get map scale
198
+ double scale = map->scale ();
199
+
200
+ // foreach layer find in the layer tree
201
+ // remove it if the layer id is not in map layerIds
202
+ foreach ( QString layerId, layerIds )
203
+ {
204
+ QgsLayerTreeLayer* nodeLayer = root->findLayer ( layerId );
205
+ if ( !nodeLayer ) {
206
+ continue ;
207
+ }
208
+ if ( !layerSet.contains ( layerId ) )
209
+ {
210
+ qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent () )->removeChildNode ( nodeLayer );
211
+ }
212
+ else
213
+ {
214
+ QgsMapLayer* layer = nodeLayer->layer ();
215
+ if ( layer->hasScaleBasedVisibility () )
216
+ {
217
+ if ( layer->minimumScale () > scale )
218
+ qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent () )->removeChildNode ( nodeLayer );
219
+ else if ( layer->maximumScale () < scale )
220
+ qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent () )->removeChildNode ( nodeLayer );
221
+ }
222
+ }
223
+ }
224
+ root->removeChildrenGroupWithoutLayers ();
225
+ }
226
+ }
161
227
162
228
// replace label text
163
229
foreach ( QgsComposerLabel *currentLabel, composerLabels )
0 commit comments