void QgsRelationReferenceWidget::deleteForeignKey() { QVariant nullValue = QSettings().value( "qgis/nullValue", "NULL" ); if ( mReadOnlySelector ) { QString nullText = ""; if ( mAllowNull ) { nullText = tr( "%1 (no selection)" ).arg( nullValue.toString() ); } mLineEdit->setText( nullText ); mForeignKey = QVariant(); mFeature.setValid( false ); } else { if ( mAllowNull ) { mComboBox->setCurrentIndex( 0 ); } else { mComboBox->setCurrentIndex( -1 ); } } mRemoveFKButton->setEnabled( false ); updateAttributeEditorFrame( QgsFeature() ); emit foreignKeyChanged( QVariant( QVariant::Int ) ); }
void QgsRelationReferenceWidget::featureIdentified( const QgsFeature &feature ) { if ( mReadOnlySelector ) { QgsExpression expr( mReferencedLayer->displayExpression() ); QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mReferencedLayer ) ); context.setFeature( feature ); QString title = expr.evaluate( &context ).toString(); if ( expr.hasEvalError() ) { title = feature.attribute( mReferencedFieldIdx ).toString(); } mLineEdit->setText( title ); mForeignKey = feature.attribute( mReferencedFieldIdx ); mFeature = feature; } else { mComboBox->setCurrentIndex( mComboBox->findData( feature.attribute( mReferencedFieldIdx ), QgsFeatureFilterModel::Role::IdentifierValueRole ) ); mFeature = feature; } mRemoveFKButton->setEnabled( mIsEditable ); highlightFeature( feature ); updateAttributeEditorFrame( feature ); emit foreignKeyChanged( foreignKey() ); unsetMapTool(); }
void QgsRelationReferenceWidget::deleteForeignKey() { // deactivate filter comboboxes if ( mChainFilters && !mFilterComboBoxes.isEmpty() ) { QComboBox *cb = mFilterComboBoxes.first(); cb->setCurrentIndex( 0 ); disableChainedComboBoxes( cb ); } if ( mReadOnlySelector ) { const QString nullValue = QgsApplication::nullRepresentation(); QString nullText; if ( mAllowNull ) { nullText = tr( "%1 (no selection)" ).arg( nullValue ); } mLineEdit->setText( nullText ); mForeignKey = QVariant( QVariant::Int ); mFeature.setValid( false ); } else { mComboBox->setIdentifierValue( QVariant( QVariant::Int ) ); } mRemoveFKButton->setEnabled( false ); updateAttributeEditorFrame( QgsFeature() ); emitForeignKeyChanged( QVariant( QVariant::Int ) ); }
void QgsRelationReferenceWidget::setForeignKey( const QVariant& value ) { if ( !value.isValid() || value.isNull() ) { deleteForeignKey(); return; } if ( !mReferencedLayer ) return; QgsAttributes attrs = QgsAttributes( mReferencingLayer->fields().count() ); attrs[mFkeyFieldIdx] = value; QgsFeatureRequest request = mRelation.getReferencedFeatureRequest( attrs ); mReferencedLayer->getFeatures( request ).nextFeature( mFeature ); if ( !mFeature.isValid() ) { deleteForeignKey(); return; } mForeignKey = mFeature.attribute( mFkeyFieldIdx ); if ( mReadOnlySelector ) { QgsExpression expr( mReferencedLayer->displayExpression() ); QgsExpressionContext context; context << QgsExpressionContextUtils::globalScope() << QgsExpressionContextUtils::projectScope() << QgsExpressionContextUtils::layerScope( mReferencedLayer ); context.setFeature( mFeature ); QString title = expr.evaluate( &context ).toString(); if ( expr.hasEvalError() ) { title = mFeature.attribute( mFkeyFieldIdx ).toString(); } mLineEdit->setText( title ); } else { int i = mComboBox->findData( mFeature.id(), QgsAttributeTableModel::FeatureIdRole ); if ( i == -1 && mAllowNull ) { mComboBox->setCurrentIndex( 0 ); } else { mComboBox->setCurrentIndex( i ); } } mRemoveFKButton->setEnabled( mIsEditable ); highlightFeature( mFeature ); updateAttributeEditorFrame( mFeature ); emit foreignKeyChanged( foreignKey() ); }
void QgsRelationReferenceWidget::setForeignKey( const QVariant& value ) { if ( !value.isValid() || value.isNull() ) { deleteForeignKey(); return; } QgsFeature f; if ( !mReferencedLayer ) return; // TODO: Rewrite using expression QgsFeatureIterator fit = mReferencedLayer->getFeatures( QgsFeatureRequest() ); while ( fit.nextFeature( f ) ) { if ( f.attribute( mFkeyFieldIdx ) == value ) { break; } } if ( !f.isValid() ) { deleteForeignKey(); return; } mForeignKey = f.attribute( mFkeyFieldIdx ); if ( mReadOnlySelector ) { QgsExpression expr( mReferencedLayer->displayExpression() ); QString title = expr.evaluate( &f ).toString(); if ( expr.hasEvalError() ) { title = f.attribute( mFkeyFieldIdx ).toString(); } mLineEdit->setText( title ); mFeatureId = f.id(); } else { int i = mComboBox->findData( value ); if ( i == -1 && mAllowNull ) { mComboBox->setCurrentIndex( 0 ); } else { mComboBox->setCurrentIndex( i ); } } mRemoveFKButton->setEnabled( mIsEditable ); highlightFeature( f ); updateAttributeEditorFrame( f ); emit foreignKeyChanged( foreignKey() ); }
void QgsRelationReferenceWidget::comboReferenceChanged( int index ) { Q_UNUSED( index ) mReferencedLayer->getFeatures( mComboBox->currentFeatureRequest() ).nextFeature( mFeature ); highlightFeature( mFeature ); updateAttributeEditorFrame( mFeature ); emitForeignKeyChanged( mComboBox->identifierValue() ); }
void QgsRelationReferenceWidget::showIndeterminateState() { if ( mReadOnlySelector ) { whileBlocking( mLineEdit )->setText( QString() ); } else { whileBlocking( mComboBox )->setCurrentIndex( -1 ); } mRemoveFKButton->setEnabled( false ); updateAttributeEditorFrame( QgsFeature() ); }
void QgsRelationReferenceWidget::setForeignKey( const QVariant &value ) { if ( !value.isValid() ) { return; } if ( value.isNull() ) { deleteForeignKey(); return; } if ( !mReferencedLayer ) return; if ( mReadOnlySelector ) { // Attributes from the referencing layer QgsAttributes attrs = QgsAttributes( mReferencingLayer->fields().count() ); // Set the value on the foreign key field of the referencing record attrs[ mReferencingLayer->fields().lookupField( mRelation.fieldPairs().at( 0 ).first )] = value; QgsFeatureRequest request = mRelation.getReferencedFeatureRequest( attrs ); mReferencedLayer->getFeatures( request ).nextFeature( mFeature ); if ( !mFeature.isValid() ) { return; } mForeignKey = mFeature.attribute( mReferencedFieldIdx ); QgsExpression expr( mReferencedLayer->displayExpression() ); QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mReferencedLayer ) ); context.setFeature( mFeature ); QString title = expr.evaluate( &context ).toString(); if ( expr.hasEvalError() ) { title = mFeature.attribute( mReferencedFieldIdx ).toString(); } mLineEdit->setText( title ); } else { mComboBox->setIdentifierValue( value ); if ( mChainFilters ) { QVariant nullValue = QgsApplication::nullRepresentation(); QgsFeatureRequest request = mComboBox->currentFeatureRequest(); mReferencedLayer->getFeatures( request ).nextFeature( mFeature ); const int count = std::min( mFilterComboBoxes.size(), mFilterFields.size() ); for ( int i = 0; i < count; i++ ) { QVariant v = mFeature.attribute( mFilterFields[i] ); QString f = v.isNull() ? nullValue.toString() : v.toString(); mFilterComboBoxes.at( i )->setCurrentIndex( mFilterComboBoxes.at( i )->findText( f ) ); } } } mRemoveFKButton->setEnabled( mIsEditable ); highlightFeature( mFeature ); // TODO : make this async updateAttributeEditorFrame( mFeature ); emitForeignKeyChanged( foreignKey() ); }