Skip to content
Permalink
Browse files

Cleanup ValueRelation widget

  • Loading branch information
m-kuhn committed Apr 5, 2016
1 parent 22f1a1d commit 1eb87e5f6ffc70b97d0cc7615c7735b6da4a9007
Showing with 17 additions and 67 deletions.
  1. +17 −67 src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
@@ -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 )
@@ -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() )

0 comments on commit 1eb87e5

Please sign in to comment.
You can’t perform that action at this time.