Skip to content

Commit

Permalink
Fixes regressions in relation reference widget
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed May 17, 2018
1 parent 352dbcb commit eca1aef
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -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()
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.h
Expand Up @@ -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 );
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsfeaturelistcombobox.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 )
Expand Down
15 changes: 15 additions & 0 deletions src/gui/qgsfeaturelistcombobox.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit eca1aef

Please sign in to comment.