Skip to content

Commit

Permalink
Merge pull request #6922 from m-kuhn/relrefconditionalvaluechanged
Browse files Browse the repository at this point in the history
Relation reference widget value changed signals
  • Loading branch information
m-kuhn committed May 4, 2018
2 parents a36f6aa + 2c415e8 commit 69de345
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 20 deletions.
21 changes: 16 additions & 5 deletions src/core/qgsfeaturefiltermodel.cpp
Expand Up @@ -394,9 +394,9 @@ QSet<QString> QgsFeatureFilterModel::requestedAttributes() const
return requestedAttrs;
}

void QgsFeatureFilterModel::setExtraIdentifierValueIndex( int index )
void QgsFeatureFilterModel::setExtraIdentifierValueIndex( int index, bool force )
{
if ( mExtraIdentifierValueIndex == index )
if ( mExtraIdentifierValueIndex == index && !force )
return;

mExtraIdentifierValueIndex = index;
Expand All @@ -416,7 +416,9 @@ void QgsFeatureFilterModel::setExtraIdentifierValueUnguarded( const QVariant &ex
int index = 0;
for ( const Entry &entry : entries )
{
if ( entry.identifierValue == extraIdentifierValue && entry.identifierValue.isNull() == extraIdentifierValue.isNull() && entry.identifierValue.isValid() == extraIdentifierValue.isValid() )
if ( entry.identifierValue == extraIdentifierValue
&& entry.identifierValue.isNull() == extraIdentifierValue.isNull()
&& entry.identifierValue.isValid() == extraIdentifierValue.isValid() )
{
setExtraIdentifierValueIndex( index );
break;
Expand All @@ -434,7 +436,8 @@ void QgsFeatureFilterModel::setExtraIdentifierValueUnguarded( const QVariant &ex
else
mEntries.prepend( Entry( extraIdentifierValue, QStringLiteral( "(%1)" ).arg( extraIdentifierValue.toString() ), QgsFeature() ) );
endInsertRows();
setExtraIdentifierValueIndex( 0 );

setExtraIdentifierValueIndex( 0, true );

reloadCurrentFeature();
}
Expand Down Expand Up @@ -532,8 +535,16 @@ void QgsFeatureFilterModel::setExtraIdentifierValue( const QVariant &extraIdenti
if ( extraIdentifierValue == mExtraIdentifierValue && extraIdentifierValue.isNull() == mExtraIdentifierValue.isNull() && mExtraIdentifierValue.isValid() )
return;

setExtraIdentifierValueUnguarded( extraIdentifierValue );
if ( mIsSettingExtraIdentifierValue )
return;

mIsSettingExtraIdentifierValue = true;

mExtraIdentifierValue = extraIdentifierValue;

setExtraIdentifierValueUnguarded( extraIdentifierValue );

mIsSettingExtraIdentifierValue = false;

emit extraIdentifierValueChanged();
}
3 changes: 2 additions & 1 deletion src/core/qgsfeaturefiltermodel.h
Expand Up @@ -261,7 +261,7 @@ class CORE_EXPORT QgsFeatureFilterModel : public QAbstractItemModel

private:
QSet<QString> requestedAttributes() const;
void setExtraIdentifierValueIndex( int index );
void setExtraIdentifierValueIndex( int index, bool force = false );
void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
void reload();
void reloadCurrentFeature();
Expand Down Expand Up @@ -298,6 +298,7 @@ class CORE_EXPORT QgsFeatureFilterModel : public QAbstractItemModel
bool mShouldReloadCurrentFeature = false;
bool mExtraValueDoesNotExist = false;
bool mAllowNull = false;
bool mIsSettingExtraIdentifierValue = false;

QString mIdentifierField;

Expand Down
1 change: 0 additions & 1 deletion src/core/qgsmaplayer.cpp
Expand Up @@ -898,7 +898,6 @@ QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::Propert
{
QgsDebugMsgLevel( QString( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );

QgsDebugMsg( "loadNamedProperty" );
resultFlag = false;

QDomDocument myDocument( QStringLiteral( "qgis" ) );
Expand Down
10 changes: 5 additions & 5 deletions src/core/qgsrelation.cpp
Expand Up @@ -345,33 +345,33 @@ void QgsRelation::updateRelationStatus()
{
if ( !mReferencedLayer )
{
QgsDebugMsg( QString( "Invalid relation: referenced layer does not exist. ID: %1" ).arg( mReferencedLayerId ) );
QgsDebugMsgLevel( QStringLiteral( "Invalid relation: referenced layer does not exist. ID: %1" ).arg( mReferencedLayerId ), 4 );
mValid = false;
}
else if ( !mReferencingLayer )
{
QgsDebugMsg( QString( "Invalid relation: referencing layer does not exist. ID: %2" ).arg( mReferencingLayerId ) );
QgsDebugMsgLevel( QStringLiteral( "Invalid relation: referencing layer does not exist. ID: %2" ).arg( mReferencingLayerId ), 4 );
mValid = false;
}
else
{
if ( mFieldPairs.count() < 1 )
{
QgsDebugMsg( "Invalid relation: no pair of field is specified." );
QgsDebugMsgLevel( "Invalid relation: no pair of field is specified.", 4 );
mValid = false;
}

Q_FOREACH ( const FieldPair &fieldPair, mFieldPairs )
{
if ( -1 == mReferencingLayer->fields().lookupField( fieldPair.first ) )
{
QgsDebugMsg( QString( "Invalid relation: field %1 does not exist in referencing layer %2" ).arg( fieldPair.first, mReferencingLayer->name() ) );
QgsDebugMsg( QStringLiteral( "Invalid relation: field %1 does not exist in referencing layer %2" ).arg( fieldPair.first, mReferencingLayer->name() ) );
mValid = false;
break;
}
else if ( -1 == mReferencedLayer->fields().lookupField( fieldPair.second ) )
{
QgsDebugMsg( QString( "Invalid relation: field %1 does not exist in referencedg layer %2" ).arg( fieldPair.second, mReferencedLayer->name() ) );
QgsDebugMsg( QStringLiteral( "Invalid relation: field %1 does not exist in referencedg layer %2" ).arg( fieldPair.second, mReferencedLayer->name() ) );
mValid = false;
break;
}
Expand Down
23 changes: 16 additions & 7 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -281,7 +281,8 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant &value )
mRemoveFKButton->setEnabled( mIsEditable );
highlightFeature( mFeature ); // TODO : make this async
updateAttributeEditorFrame( mFeature );
emit foreignKeyChanged( foreignKey() );

emitForeignKeyChanged( foreignKey() );
}

void QgsRelationReferenceWidget::deleteForeignKey()
Expand All @@ -304,18 +305,16 @@ void QgsRelationReferenceWidget::deleteForeignKey()
nullText = tr( "%1 (no selection)" ).arg( nullValue );
}
mLineEdit->setText( nullText );
mForeignKey = QVariant();
mForeignKey = QVariant( QVariant::Int );
mFeature.setValid( false );
}
else
{
mComboBox->setIdentifierValue( QVariant() );


mComboBox->setIdentifierValue( QVariant( QVariant::Int ) );
}
mRemoveFKButton->setEnabled( false );
updateAttributeEditorFrame( QgsFeature() );
emit foreignKeyChanged( QVariant( QVariant::Int ) );
emitForeignKeyChanged( QVariant( QVariant::Int ) );
}

QgsFeature QgsRelationReferenceWidget::referencedFeature() const
Expand Down Expand Up @@ -667,7 +666,8 @@ void QgsRelationReferenceWidget::comboReferenceChanged( int index )
mReferencedLayer->getFeatures( mComboBox->currentFeatureRequest() ).nextFeature( mFeature );
highlightFeature( mFeature );
updateAttributeEditorFrame( mFeature );
emit foreignKeyChanged( mFeature.attribute( mReferencedFieldIdx ) );

emitForeignKeyChanged( mComboBox->identifierValue() );
}

void QgsRelationReferenceWidget::updateAttributeEditorFrame( const QgsFeature &feature )
Expand Down Expand Up @@ -900,3 +900,12 @@ void QgsRelationReferenceWidget::disableChainedComboBoxes( const QComboBox *scb
ccb = cb;
}
}

void QgsRelationReferenceWidget::emitForeignKeyChanged( const QVariant &foreignKey )
{
if ( foreignKey != mForeignKey || foreignKey.isNull() != mForeignKey.isNull() )
{
mForeignKey = foreignKey;
emit foreignKeyChanged( foreignKey );
}
}
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.h
Expand Up @@ -188,6 +188,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
void highlightFeature( QgsFeature f = QgsFeature(), CanvasExtent canvasExtent = Fixed );
void updateAttributeEditorFrame( const QgsFeature &feature );
void disableChainedComboBoxes( const QComboBox *cb );
void emitForeignKeyChanged( const QVariant &foreignKey );

// initialized
QgsAttributeEditorContext mEditorContext;
Expand Down
30 changes: 29 additions & 1 deletion tests/src/gui/testqgsrelationreferencewidget.cpp
Expand Up @@ -46,6 +46,7 @@ class TestQgsRelationReferenceWidget : public QObject
void testChainFilterRefreshed();
void testChainFilterDeleteForeignKey();
void testInvalidRelation();
void testSetGetForeignKey();

private:
std::unique_ptr<QgsVectorLayer> mLayer1;
Expand Down Expand Up @@ -129,14 +130,17 @@ void TestQgsRelationReferenceWidget::init()

void TestQgsRelationReferenceWidget::cleanup()
{
QgsProject::instance()->removeMapLayer( mLayer1.get() );
QgsProject::instance()->removeMapLayer( mLayer2.get() );
}

void TestQgsRelationReferenceWidget::testChainFilter()
{
// init a relation reference widget
QStringList filterFields = { "material", "diameter", "raccord" };

QgsRelationReferenceWidget w( new QWidget() );
QWidget parentWidget;
QgsRelationReferenceWidget w( &parentWidget );
w.setChainFilters( true );
w.setFilterFields( filterFields );
w.setRelation( *mRelation, true );
Expand Down Expand Up @@ -287,5 +291,29 @@ void TestQgsRelationReferenceWidget::testInvalidRelation()
ww.initWidget( nullptr );
}

void TestQgsRelationReferenceWidget::testSetGetForeignKey()
{
QWidget parentWidget;
QgsRelationReferenceWidget w( &parentWidget );
w.setRelation( *mRelation, true );
w.init();

QSignalSpy spy( &w, SIGNAL( foreignKeyChanged( QVariant ) ) );

w.setForeignKey( 11 );
QCOMPARE( w.foreignKey(), QVariant( 11 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(11)" ) );
QCOMPARE( spy.count(), 1 );

w.setForeignKey( 12 );
QCOMPARE( w.foreignKey(), QVariant( 12 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(12)" ) );
QCOMPARE( spy.count(), 2 );

w.setForeignKey( QVariant( QVariant::Int ) );
Q_ASSERT( w.foreignKey().isNull() );
QCOMPARE( spy.count(), 3 );
}

QGSTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"

0 comments on commit 69de345

Please sign in to comment.