Skip to content

Commit eca1aef

Browse files
committedMay 17, 2018
Fixes regressions in relation reference widget
1 parent 352dbcb commit eca1aef

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
 

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget *parent )
145145
connect( mRemoveFKButton, &QAbstractButton::clicked, this, &QgsRelationReferenceWidget::deleteForeignKey );
146146
connect( mAddEntryButton, &QAbstractButton::clicked, this, &QgsRelationReferenceWidget::addEntry );
147147
connect( mComboBox, &QComboBox::editTextChanged, this, &QgsRelationReferenceWidget::updateAddEntryButton );
148+
connect( mComboBox, &QgsFeatureListComboBox::modelUpdated, this, &QgsRelationReferenceWidget::updateIndex );
148149
}
149150

150151
QgsRelationReferenceWidget::~QgsRelationReferenceWidget()
@@ -155,6 +156,38 @@ QgsRelationReferenceWidget::~QgsRelationReferenceWidget()
155156
delete mMapTool;
156157
}
157158

159+
void QgsRelationReferenceWidget::updateIndex()
160+
{
161+
if ( mChainFilters && mComboBox->count() > 0 )
162+
{
163+
int index = -1;
164+
165+
// uninitialized filter
166+
if ( ! mFilterComboBoxes.isEmpty()
167+
&& mFilterComboBoxes[0]->currentIndex() == 0 && mAllowNull )
168+
{
169+
index = mComboBox->nullIndex();
170+
}
171+
else if ( mComboBox->count() > mComboBox->nullIndex() )
172+
{
173+
index = mComboBox->nullIndex() + 1;
174+
}
175+
else if ( mAllowNull )
176+
{
177+
index = mComboBox->nullIndex();
178+
}
179+
else
180+
{
181+
index = 0;
182+
}
183+
184+
if ( mComboBox->count() > index )
185+
{
186+
mComboBox->setCurrentIndex( index );
187+
}
188+
}
189+
}
190+
158191
void QgsRelationReferenceWidget::setRelation( const QgsRelation &relation, bool allowNullValue )
159192
{
160193
mAllowNull = allowNullValue;

‎src/gui/editorwidgets/qgsrelationreferencewidget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
184184
void addEntry();
185185
void updateAddEntryButton();
186186

187+
/**
188+
* Updates the FK index as soon as the underlying model is updated when
189+
* the chainFilter option is activated.
190+
*/
191+
void updateIndex();
192+
187193
private:
188194
void highlightFeature( QgsFeature f = QgsFeature(), CanvasExtent canvasExtent = Fixed );
189195
void updateAttributeEditorFrame( const QgsFeature &feature );

‎src/gui/qgsfeaturelistcombobox.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ QgsFeatureListComboBox::QgsFeatureListComboBox( QWidget *parent )
4646
connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::activated ), this, &QgsFeatureListComboBox::onActivated );
4747
connect( mModel, &QgsFeatureFilterModel::beginUpdate, this, &QgsFeatureListComboBox::storeLineEditState );
4848
connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::restoreLineEditState );
49+
connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::modelUpdated );
4950
connect( mModel, &QgsFeatureFilterModel::dataChanged, this, &QgsFeatureListComboBox::onDataChanged );
5051

5152
connect( this, static_cast<void( QgsFeatureListComboBox::* )( int )>( &QgsFeatureListComboBox::currentIndexChanged ), this, &QgsFeatureListComboBox::onCurrentIndexChanged );
@@ -136,6 +137,18 @@ void QgsFeatureListComboBox::restoreLineEditState()
136137
mLineEditState.restore( mLineEdit );
137138
}
138139

140+
int QgsFeatureListComboBox::nullIndex() const
141+
{
142+
int index = -1;
143+
144+
if ( allowNull() )
145+
{
146+
index = findText( tr( "NULL" ) );
147+
}
148+
149+
return index;
150+
}
151+
139152
void QgsFeatureListComboBox::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
140153
{
141154
Q_UNUSED( roles )

‎src/gui/qgsfeaturelistcombobox.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ class GUI_EXPORT QgsFeatureListComboBox : public QComboBox
8282
*/
8383
QString filterExpression() const;
8484

85+
/**
86+
* Returns the current index of the NULL value, or -1 if NULL values are
87+
* not allowed.
88+
*
89+
* \since QGIS 3.2
90+
*/
91+
int nullIndex() const;
92+
8593
/**
8694
* An additional expression to further restrict the available features.
8795
* This can be used to integrate additional spatial or other constraints.
@@ -141,6 +149,13 @@ class GUI_EXPORT QgsFeatureListComboBox : public QComboBox
141149

142150
signals:
143151

152+
/**
153+
* The underlying model has been updated.
154+
*
155+
* \since QGIS 3.2
156+
*/
157+
void modelUpdated();
158+
144159
/**
145160
* The layer from which features should be listed.
146161
*/

0 commit comments

Comments
 (0)
Please sign in to comment.