const correctness, fix deprecated methods,

qVariantListIsNull returns true for an empty list
This commit is contained in:
Denis Rouzaud 2019-08-27 15:35:57 +02:00
parent 93a82f883a
commit ee6f8c580c
6 changed files with 26 additions and 24 deletions

View File

@ -46,7 +46,7 @@ class QgsRelationReferenceWidget : QWidget
void setRelationEditable( bool editable ); void setRelationEditable( bool editable );
void setForeignKey( const QVariant &value ); void setForeignKey( const QVariant &value ) /Deprecated/;
%Docstring %Docstring
this sets the related feature using from the foreign key this sets the related feature using from the foreign key

View File

@ -48,7 +48,7 @@
bool qVariantListIsNull( const QVariantList &list ) bool qVariantListIsNull( const QVariantList &list )
{ {
if ( list.isEmpty() ) if ( list.isEmpty() )
return false; return true;
for ( int i = 0; i < list.size(); ++i ) for ( int i = 0; i < list.size(); ++i )
{ {
@ -271,7 +271,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant &value )
void QgsRelationReferenceWidget::setForeignKeys( const QVariantList &values ) void QgsRelationReferenceWidget::setForeignKeys( const QVariantList &values )
{ {
if ( !values.isEmpty() ) if ( values.isEmpty() )
{ {
return; return;
} }
@ -291,7 +291,8 @@ void QgsRelationReferenceWidget::setForeignKeys( const QVariantList &values )
// Set the value on the foreign key fields of the referencing record // Set the value on the foreign key fields of the referencing record
const QList<QgsRelation::FieldPair> fieldPairs = mRelation.fieldPairs(); const QList<QgsRelation::FieldPair> fieldPairs = mRelation.fieldPairs();
for ( int i = 0; i < fieldPairs.count(); i++ ) int fieldCount = std::min( fieldPairs.count(), values.count() );
for ( int i = 0; i < fieldCount; i++ )
{ {
int idx = mReferencingLayer->fields().lookupField( fieldPairs.at( i ).referencingField() ); int idx = mReferencingLayer->fields().lookupField( fieldPairs.at( i ).referencingField() );
attrs[idx] = values.at( i ); attrs[idx] = values.at( i );
@ -307,7 +308,7 @@ void QgsRelationReferenceWidget::setForeignKeys( const QVariantList &values )
} }
mForeignKeys.clear(); mForeignKeys.clear();
for ( const QString &fieldName : mReferencedFields ) for ( const QString &fieldName : qgis::as_const( mReferencedFields ) )
mForeignKeys << mFeature.attribute( fieldName ); mForeignKeys << mFeature.attribute( fieldName );
QgsExpression expr( mReferencedLayer->displayExpression() ); QgsExpression expr( mReferencedLayer->displayExpression() );
@ -317,7 +318,7 @@ void QgsRelationReferenceWidget::setForeignKeys( const QVariantList &values )
if ( expr.hasEvalError() ) if ( expr.hasEvalError() )
{ {
QStringList titleFields; QStringList titleFields;
for ( const QString &fieldName : mReferencedFields ) for ( const QString &fieldName : qgis::as_const( mReferencedFields ) )
titleFields << mFeature.attribute( fieldName ).toString(); titleFields << mFeature.attribute( fieldName ).toString();
title = titleFields.join( QStringLiteral( " " ) ); title = titleFields.join( QStringLiteral( " " ) );
} }
@ -373,7 +374,7 @@ void QgsRelationReferenceWidget::deleteForeignKeys()
} }
mLineEdit->setText( nullText ); mLineEdit->setText( nullText );
QVariantList nullAttributes; QVariantList nullAttributes;
for ( const QString &fieldName : mReferencedFields ) for ( const QString &fieldName : qgis::as_const( mReferencedFields ) )
{ {
Q_UNUSED( fieldName ); Q_UNUSED( fieldName );
nullAttributes << QVariant( QVariant::Int ); nullAttributes << QVariant( QVariant::Int );
@ -784,13 +785,13 @@ void QgsRelationReferenceWidget::featureIdentified( const QgsFeature &feature )
if ( expr.hasEvalError() ) if ( expr.hasEvalError() )
{ {
QStringList titleFields; QStringList titleFields;
for ( const QString &fieldName : mReferencedFields ) for ( const QString &fieldName : qgis::as_const( mReferencedFields ) )
titleFields << mFeature.attribute( fieldName ).toString(); titleFields << mFeature.attribute( fieldName ).toString();
title = titleFields.join( QStringLiteral( " " ) ); title = titleFields.join( QStringLiteral( " " ) );
} }
mLineEdit->setText( title ); mLineEdit->setText( title );
mForeignKeys.clear(); mForeignKeys.clear();
for ( const QString &fieldName : mReferencedFields ) for ( const QString &fieldName : qgis::as_const( mReferencedFields ) )
mForeignKeys << mFeature.attribute( fieldName ); mForeignKeys << mFeature.attribute( fieldName );
mFeature = feature; mFeature = feature;
} }
@ -956,8 +957,8 @@ void QgsRelationReferenceWidget::addEntry()
if ( mEditorContext.vectorLayerTools()->addFeature( mReferencedLayer, attributes, QgsGeometry(), &f ) ) if ( mEditorContext.vectorLayerTools()->addFeature( mReferencedLayer, attributes, QgsGeometry(), &f ) )
{ {
QVariantList attrs; QVariantList attrs;
for ( const QString &fieldName : mReferencedFields ) for ( const QString &fieldName : qgis::as_const( mReferencedFields ) )
attrs << mFeature.attribute( fieldName ); attrs << f.attribute( fieldName );
mComboBox->setIdentifierValues( attrs ); mComboBox->setIdentifierValues( attrs );

View File

@ -87,7 +87,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
* this sets the related feature using from the foreign key * this sets the related feature using from the foreign key
* \deprecated since QGIS 3.10 use setForeignKeys * \deprecated since QGIS 3.10 use setForeignKeys
*/ */
void setForeignKey( const QVariant &value ); Q_DECL_DEPRECATED void setForeignKey( const QVariant &value ) SIP_DEPRECATED;
/** /**
* Sets the related feature using the foreign keys * Sets the related feature using the foreign keys

View File

@ -146,7 +146,8 @@ QVariantList QgsRelationReferenceWidgetWrapper::additionalFieldValues() const
{ {
QVariantList values = mWidget->foreignKeys(); QVariantList values = mWidget->foreignKeys();
const QList<QgsRelation::FieldPair> fieldPairs = mWidget->relation().fieldPairs(); const QList<QgsRelation::FieldPair> fieldPairs = mWidget->relation().fieldPairs();
for ( int i = 0; i < fieldPairs.count(); i++ ) int fieldCount = std::min( fieldPairs.count(), values.count() );
for ( int i = 0; i < fieldCount; i++ )
{ {
if ( fieldPairs.at( i ).referencingField() == field().name() ) if ( fieldPairs.at( i ).referencingField() == field().name() )
{ {

View File

@ -82,7 +82,7 @@ void QgsFeatureListComboBox::setCurrentFeature( const QgsFeature &feature )
{ {
values << feature.attribute( field ); values << feature.attribute( field );
} }
setCurrentIndex( findData( values, QgsFeatureFilterModel::Role::IdentifierValueRole ) ); setIdentifierValues( values );
} }
QString QgsFeatureListComboBox::displayExpression() const QString QgsFeatureListComboBox::displayExpression() const

View File

@ -249,17 +249,17 @@ void TestQgsRelationReferenceWidget::testChainFilterRefreshed()
QCOMPARE( cbs[2]->currentText(), QString( "raccord" ) ); QCOMPARE( cbs[2]->currentText(), QString( "raccord" ) );
// update foreign key // update foreign key
w.setForeignKey( QVariant( 12 ) ); w.setForeignKeys( QVariantList() << QVariant( 12 ) );
QCOMPARE( cbs[0]->currentText(), QString( "steel" ) ); QCOMPARE( cbs[0]->currentText(), QString( "steel" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) ); QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
QCOMPARE( cbs[2]->currentText(), QString( "collar" ) ); QCOMPARE( cbs[2]->currentText(), QString( "collar" ) );
w.setForeignKey( QVariant( 10 ) ); w.setForeignKeys( QVariantList() << QVariant( 10 ) );
QCOMPARE( cbs[0]->currentText(), QString( "iron" ) ); QCOMPARE( cbs[0]->currentText(), QString( "iron" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) ); QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
QCOMPARE( cbs[2]->currentText(), QString( "brides" ) ); QCOMPARE( cbs[2]->currentText(), QString( "brides" ) );
w.setForeignKey( QVariant( 11 ) ); w.setForeignKeys( QVariantList() << QVariant( 11 ) );
QCOMPARE( cbs[0]->currentText(), QString( "iron" ) ); QCOMPARE( cbs[0]->currentText(), QString( "iron" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) ); QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
QCOMPARE( cbs[2]->currentText(), QString( "sleeve" ) ); QCOMPARE( cbs[2]->currentText(), QString( "sleeve" ) );
@ -289,7 +289,7 @@ void TestQgsRelationReferenceWidget::testChainFilterDeleteForeignKey()
QCOMPARE( cbs[2]->isEnabled(), false ); QCOMPARE( cbs[2]->isEnabled(), false );
// set a foreign key // set a foreign key
w.setForeignKey( QVariant( 11 ) ); w.setForeignKeys( QVariantList() << QVariant( 11 ) );
QCOMPARE( cbs[0]->currentText(), QString( "iron" ) ); QCOMPARE( cbs[0]->currentText(), QString( "iron" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) ); QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
@ -328,18 +328,18 @@ void TestQgsRelationReferenceWidget::testSetGetForeignKey()
QSignalSpy spy( &w, SIGNAL( foreignKeyChanged( QVariant ) ) ); QSignalSpy spy( &w, SIGNAL( foreignKeyChanged( QVariant ) ) );
w.setForeignKey( 11 ); w.setForeignKeys( QVariantList() << 11 );
QCOMPARE( w.foreignKey(), QVariant( 11 ) ); QCOMPARE( w.foreignKeys().at( 0 ), QVariant( 11 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(11)" ) ); QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(11)" ) );
QCOMPARE( spy.count(), 1 ); QCOMPARE( spy.count(), 1 );
w.setForeignKey( 12 ); w.setForeignKeys( QVariantList() << 12 );
QCOMPARE( w.foreignKey(), QVariant( 12 ) ); QCOMPARE( w.foreignKeys().at( 0 ), QVariant( 12 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(12)" ) ); QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(12)" ) );
QCOMPARE( spy.count(), 2 ); QCOMPARE( spy.count(), 2 );
w.setForeignKey( QVariant( QVariant::Int ) ); w.setForeignKeys( QVariantList() << QVariant( QVariant::Int ) );
Q_ASSERT( w.foreignKey().isNull() ); Q_ASSERT( w.foreignKeys().at( 0 ).isNull() );
QCOMPARE( spy.count(), 3 ); QCOMPARE( spy.count(), 3 );
} }