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 setForeignKey( const QVariant &value );
void setForeignKey( const QVariant &value ) /Deprecated/;
%Docstring
this sets the related feature using from the foreign key

View File

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

View File

@ -146,7 +146,8 @@ QVariantList QgsRelationReferenceWidgetWrapper::additionalFieldValues() const
{
QVariantList values = mWidget->foreignKeys();
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() )
{

View File

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

View File

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