Skip to content

Commit 1eb87e5

Browse files
committedApr 5, 2016
Cleanup ValueRelation widget
1 parent 22f1a1d commit 1eb87e5

File tree

1 file changed

+17
-67
lines changed

1 file changed

+17
-67
lines changed
 

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,7 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant& value )
175175
for ( int i = 0; i < mListWidget->count(); ++i )
176176
{
177177
QListWidgetItem* item = mListWidget->item( i );
178-
if ( config( "OrderByValue" ).toBool() )
179-
{
180-
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
181-
}
182-
else
183-
{
184-
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
185-
}
178+
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
186179
}
187180
}
188181
else if ( mComboBox )
@@ -209,70 +202,27 @@ QgsValueRelationWidgetWrapper::ValueRelationCache QgsValueRelationWidgetWrapper:
209202

210203
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( config.value( "Layer" ).toString() ) );
211204

212-
if ( layer )
213-
{
214-
int ki = layer->fieldNameIndex( config.value( "Key" ).toString() );
215-
int vi = layer->fieldNameIndex( config.value( "Value" ).toString() );
216-
217-
QgsExpressionContext context;
218-
context << QgsExpressionContextUtils::globalScope()
219-
<< QgsExpressionContextUtils::projectScope()
220-
<< QgsExpressionContextUtils::layerScope( layer );
221-
222-
QgsExpression *e = nullptr;
223-
if ( !config.value( "FilterExpression" ).toString().isEmpty() )
224-
{
225-
e = new QgsExpression( config.value( "FilterExpression" ).toString() );
226-
if ( e->hasParserError() || !e->prepare( &context ) )
227-
ki = -1;
228-
}
229-
230-
if ( ki >= 0 && vi >= 0 )
231-
{
232-
QSet<int> attributes;
233-
attributes << ki << vi;
234-
235-
QgsFeatureRequest::Flags flags = QgsFeatureRequest::NoGeometry;
205+
if ( !layer )
206+
return cache;
236207

237-
bool requiresAllAttributes = false;
238-
if ( e )
239-
{
240-
if ( e->needsGeometry() )
241-
flags = QgsFeatureRequest::NoFlags;
242-
243-
Q_FOREACH ( const QString& field, e->referencedColumns() )
244-
{
245-
if ( field == QgsFeatureRequest::AllAttributes )
246-
{
247-
requiresAllAttributes = true;
248-
break;
249-
}
250-
int idx = layer->fieldNameIndex( field );
251-
if ( idx < 0 )
252-
continue;
253-
attributes << idx;
254-
}
255-
}
208+
int ki = layer->fieldNameIndex( config.value( "Key" ).toString() );
209+
int vi = layer->fieldNameIndex( config.value( "Value" ).toString() );
256210

257-
QgsFeatureRequest fr = QgsFeatureRequest().setFlags( flags );
258-
if ( !requiresAllAttributes )
259-
{
260-
fr.setSubsetOfAttributes( attributes.toList() );
261-
}
211+
QgsFeatureRequest request;
262212

263-
QgsFeatureIterator fit = layer->getFeatures( fr );
213+
request.setFlags( QgsFeatureRequest::NoGeometry );
214+
request.setSubsetOfAttributes( QgsAttributeList() << ki << vi );
215+
if ( !config.value( "FilterExpression" ).toString().isEmpty() )
216+
{
217+
request.setFilterExpression( config.value( "FilterExpression" ).toString() );
218+
}
264219

265-
QgsFeature f;
266-
while ( fit.nextFeature( f ) )
267-
{
268-
context.setFeature( f );
269-
if ( e && !e->evaluate( &context ).toBool() )
270-
continue;
220+
QgsFeatureIterator fit = layer->getFeatures( request );
271221

272-
cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
273-
}
274-
}
275-
delete e;
222+
QgsFeature f;
223+
while ( fit.nextFeature( f ) )
224+
{
225+
cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
276226
}
277227

278228
if ( config.value( "OrderByValue" ).toBool() )

0 commit comments

Comments
 (0)
Please sign in to comment.