Skip to content

Commit

Permalink
Fix crash in relation reference widget
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 19, 2015
1 parent 739e507 commit 2fcdef4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -59,7 +59,7 @@ QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget* parent )
, mCanvas( NULL ) , mCanvas( NULL )
, mMessageBar( NULL ) , mMessageBar( NULL )
, mForeignKey( QVariant() ) , mForeignKey( QVariant() )
, mFkeyFieldIdx( -1 ) , mReferencedFieldIdx( -1 )
, mAllowNull( true ) , mAllowNull( true )
, mHighlight( NULL ) , mHighlight( NULL )
, mMapTool( NULL ) , mMapTool( NULL )
Expand Down Expand Up @@ -199,7 +199,8 @@ void QgsRelationReferenceWidget::setRelation( const QgsRelation& relation, bool
mReferencingLayer = relation.referencingLayer(); mReferencingLayer = relation.referencingLayer();
mRelationName = relation.name(); mRelationName = relation.name();
mReferencedLayer = relation.referencedLayer(); mReferencedLayer = relation.referencedLayer();
mFkeyFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().at( 0 ).second ); mReferencedFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().at( 0 ).second );
mReferencingFieldIdx = mReferencingLayer->fieldNameIndex( relation.fieldPairs().at( 0 ).first );


QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed ); QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed );


Expand Down Expand Up @@ -260,7 +261,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
return; return;
} }


mForeignKey = mFeature.attribute( mFkeyFieldIdx ); mForeignKey = mFeature.attribute( mReferencedFieldIdx );


if ( mReadOnlySelector ) if ( mReadOnlySelector )
{ {
Expand All @@ -273,7 +274,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
QString title = expr.evaluate( &context ).toString(); QString title = expr.evaluate( &context ).toString();
if ( expr.hasEvalError() ) if ( expr.hasEvalError() )
{ {
title = mFeature.attribute( mFkeyFieldIdx ).toString(); title = mFeature.attribute( mReferencedFieldIdx ).toString();
} }
mLineEdit->setText( title ); mLineEdit->setText( title );
} }
Expand Down Expand Up @@ -355,11 +356,11 @@ QVariant QgsRelationReferenceWidget::foreignKey()
{ {
if ( !mFeature.isValid() ) if ( !mFeature.isValid() )
{ {
return QVariant( mReferencingLayer->fields().at( mFkeyFieldIdx ).type() ); return QVariant( mReferencingLayer->fields().at( mReferencingFieldIdx ).type() );
} }
else else
{ {
return mFeature.attribute( mFkeyFieldIdx ); return mFeature.attribute( mReferencedFieldIdx );
} }
} }
} }
Expand Down Expand Up @@ -675,7 +676,7 @@ void QgsRelationReferenceWidget::comboReferenceChanged( int index )
mReferencedLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ).nextFeature( mFeature ); mReferencedLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ).nextFeature( mFeature );
highlightFeature( mFeature ); highlightFeature( mFeature );
updateAttributeEditorFrame( mFeature ); updateAttributeEditorFrame( mFeature );
emit foreignKeyChanged( mFeature.attribute( mFkeyFieldIdx ) ); emit foreignKeyChanged( mFeature.attribute( mReferencedFieldIdx ) );
} }


void QgsRelationReferenceWidget::updateAttributeEditorFrame( const QgsFeature& feature ) void QgsRelationReferenceWidget::updateAttributeEditorFrame( const QgsFeature& feature )
Expand Down Expand Up @@ -703,15 +704,15 @@ void QgsRelationReferenceWidget::featureIdentified( const QgsFeature& feature )
QString title = expr.evaluate( &context ).toString(); QString title = expr.evaluate( &context ).toString();
if ( expr.hasEvalError() ) if ( expr.hasEvalError() )
{ {
title = feature.attribute( mFkeyFieldIdx ).toString(); title = feature.attribute( mReferencedFieldIdx ).toString();
} }
mLineEdit->setText( title ); mLineEdit->setText( title );
mForeignKey = feature.attribute( mFkeyFieldIdx ); mForeignKey = feature.attribute( mReferencedFieldIdx );
mFeature = feature; mFeature = feature;
} }
else else
{ {
mComboBox->setCurrentIndex( mComboBox->findData( feature.attribute( mFkeyFieldIdx ), QgsAttributeTableModel::FeatureIdRole ) ); mComboBox->setCurrentIndex( mComboBox->findData( feature.attribute( mReferencedFieldIdx ), QgsAttributeTableModel::FeatureIdRole ) );
mFeature = feature; mFeature = feature;
} }


Expand Down
3 changes: 2 additions & 1 deletion src/gui/editorwidgets/qgsrelationreferencewidget.h
Expand Up @@ -147,7 +147,8 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
QVariant mForeignKey; QVariant mForeignKey;
QgsFeature mFeature; QgsFeature mFeature;
// Index of the referenced layer key // Index of the referenced layer key
int mFkeyFieldIdx; int mReferencedFieldIdx;
int mReferencingFieldIdx;
bool mAllowNull; bool mAllowNull;
QgsHighlight* mHighlight; QgsHighlight* mHighlight;
QgsMapToolIdentifyFeature* mMapTool; QgsMapToolIdentifyFeature* mMapTool;
Expand Down

0 comments on commit 2fcdef4

Please sign in to comment.