Skip to content

Commit

Permalink
Cleanup ValueRelation widget
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Apr 5, 2016
1 parent 22f1a1d commit 1eb87e5
Showing 1 changed file with 17 additions and 67 deletions.
84 changes: 17 additions & 67 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,7 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant& value )
for ( int i = 0; i < mListWidget->count(); ++i )
{
QListWidgetItem* item = mListWidget->item( i );
if ( config( "OrderByValue" ).toBool() )
{
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
}
else
{
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
}
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
}
}
else if ( mComboBox )
Expand All @@ -209,70 +202,27 @@ QgsValueRelationWidgetWrapper::ValueRelationCache QgsValueRelationWidgetWrapper:

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

if ( layer )
{
int ki = layer->fieldNameIndex( config.value( "Key" ).toString() );
int vi = layer->fieldNameIndex( config.value( "Value" ).toString() );

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( layer );

QgsExpression *e = nullptr;
if ( !config.value( "FilterExpression" ).toString().isEmpty() )
{
e = new QgsExpression( config.value( "FilterExpression" ).toString() );
if ( e->hasParserError() || !e->prepare( &context ) )
ki = -1;
}

if ( ki >= 0 && vi >= 0 )
{
QSet<int> attributes;
attributes << ki << vi;

QgsFeatureRequest::Flags flags = QgsFeatureRequest::NoGeometry;
if ( !layer )
return cache;

bool requiresAllAttributes = false;
if ( e )
{
if ( e->needsGeometry() )
flags = QgsFeatureRequest::NoFlags;

Q_FOREACH ( const QString& field, e->referencedColumns() )
{
if ( field == QgsFeatureRequest::AllAttributes )
{
requiresAllAttributes = true;
break;
}
int idx = layer->fieldNameIndex( field );
if ( idx < 0 )
continue;
attributes << idx;
}
}
int ki = layer->fieldNameIndex( config.value( "Key" ).toString() );
int vi = layer->fieldNameIndex( config.value( "Value" ).toString() );

QgsFeatureRequest fr = QgsFeatureRequest().setFlags( flags );
if ( !requiresAllAttributes )
{
fr.setSubsetOfAttributes( attributes.toList() );
}
QgsFeatureRequest request;

QgsFeatureIterator fit = layer->getFeatures( fr );
request.setFlags( QgsFeatureRequest::NoGeometry );
request.setSubsetOfAttributes( QgsAttributeList() << ki << vi );
if ( !config.value( "FilterExpression" ).toString().isEmpty() )
{
request.setFilterExpression( config.value( "FilterExpression" ).toString() );
}

QgsFeature f;
while ( fit.nextFeature( f ) )
{
context.setFeature( f );
if ( e && !e->evaluate( &context ).toBool() )
continue;
QgsFeatureIterator fit = layer->getFeatures( request );

cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
}
}
delete e;
QgsFeature f;
while ( fit.nextFeature( f ) )
{
cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
}

if ( config.value( "OrderByValue" ).toBool() )
Expand Down

0 comments on commit 1eb87e5

Please sign in to comment.