Fixes regressions in relation reference widget

This commit is contained in:
Blottiere Paul 2018-05-17 10:34:05 +01:00
parent 352dbcb836
commit eca1aef1b5
4 changed files with 67 additions and 0 deletions

View File

@ -145,6 +145,7 @@ QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget *parent )
connect( mRemoveFKButton, &QAbstractButton::clicked, this, &QgsRelationReferenceWidget::deleteForeignKey );
connect( mAddEntryButton, &QAbstractButton::clicked, this, &QgsRelationReferenceWidget::addEntry );
connect( mComboBox, &QComboBox::editTextChanged, this, &QgsRelationReferenceWidget::updateAddEntryButton );
connect( mComboBox, &QgsFeatureListComboBox::modelUpdated, this, &QgsRelationReferenceWidget::updateIndex );
}
QgsRelationReferenceWidget::~QgsRelationReferenceWidget()
@ -155,6 +156,38 @@ QgsRelationReferenceWidget::~QgsRelationReferenceWidget()
delete mMapTool;
}
void QgsRelationReferenceWidget::updateIndex()
{
if ( mChainFilters && mComboBox->count() > 0 )
{
int index = -1;
// uninitialized filter
if ( ! mFilterComboBoxes.isEmpty()
&& mFilterComboBoxes[0]->currentIndex() == 0 && mAllowNull )
{
index = mComboBox->nullIndex();
}
else if ( mComboBox->count() > mComboBox->nullIndex() )
{
index = mComboBox->nullIndex() + 1;
}
else if ( mAllowNull )
{
index = mComboBox->nullIndex();
}
else
{
index = 0;
}
if ( mComboBox->count() > index )
{
mComboBox->setCurrentIndex( index );
}
}
}
void QgsRelationReferenceWidget::setRelation( const QgsRelation &relation, bool allowNullValue )
{
mAllowNull = allowNullValue;

View File

@ -184,6 +184,12 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
void addEntry();
void updateAddEntryButton();
/**
* Updates the FK index as soon as the underlying model is updated when
* the chainFilter option is activated.
*/
void updateIndex();
private:
void highlightFeature( QgsFeature f = QgsFeature(), CanvasExtent canvasExtent = Fixed );
void updateAttributeEditorFrame( const QgsFeature &feature );

View File

@ -46,6 +46,7 @@ QgsFeatureListComboBox::QgsFeatureListComboBox( QWidget *parent )
connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::activated ), this, &QgsFeatureListComboBox::onActivated );
connect( mModel, &QgsFeatureFilterModel::beginUpdate, this, &QgsFeatureListComboBox::storeLineEditState );
connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::restoreLineEditState );
connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::modelUpdated );
connect( mModel, &QgsFeatureFilterModel::dataChanged, this, &QgsFeatureListComboBox::onDataChanged );
connect( this, static_cast<void( QgsFeatureListComboBox::* )( int )>( &QgsFeatureListComboBox::currentIndexChanged ), this, &QgsFeatureListComboBox::onCurrentIndexChanged );
@ -136,6 +137,18 @@ void QgsFeatureListComboBox::restoreLineEditState()
mLineEditState.restore( mLineEdit );
}
int QgsFeatureListComboBox::nullIndex() const
{
int index = -1;
if ( allowNull() )
{
index = findText( tr( "NULL" ) );
}
return index;
}
void QgsFeatureListComboBox::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
{
Q_UNUSED( roles )

View File

@ -82,6 +82,14 @@ class GUI_EXPORT QgsFeatureListComboBox : public QComboBox
*/
QString filterExpression() const;
/**
* Returns the current index of the NULL value, or -1 if NULL values are
* not allowed.
*
* \since QGIS 3.2
*/
int nullIndex() const;
/**
* An additional expression to further restrict the available features.
* This can be used to integrate additional spatial or other constraints.
@ -141,6 +149,13 @@ class GUI_EXPORT QgsFeatureListComboBox : public QComboBox
signals:
/**
* The underlying model has been updated.
*
* \since QGIS 3.2
*/
void modelUpdated();
/**
* The layer from which features should be listed.
*/