3636
3737QgsLegendModel::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
131130void 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
150203QStandardItem* QgsLegendModel::addGroup ( QString text, int position, QStandardItem* parentItem )
@@ -256,14 +309,17 @@ void QgsLegendModel::removeLayer( const QString& layerId )
256309
257310void 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
0 commit comments