void QgsRelationReferenceWidget::setRelation( const QgsRelation& relation, bool allowNullValue ) { mAllowNull = allowNullValue; mRemoveFKButton->setVisible( allowNullValue && mReadOnlySelector ); if ( relation.isValid() ) { mInvalidLabel->hide(); mRelation = relation; mReferencingLayer = relation.referencingLayer(); mRelationName = relation.name(); mReferencedLayer = relation.referencedLayer(); mFkeyFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().first().second ); QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed ); if ( mEmbedForm ) { mAttributeEditorFrame->setTitle( mReferencedLayer->name() ); mReferencedAttributeForm = new QgsAttributeForm( relation.referencedLayer(), QgsFeature(), context, this ); mReferencedAttributeForm->hideButtonBox(); mAttributeEditorLayout->addWidget( mReferencedAttributeForm ); } } else { mInvalidLabel->show(); } if ( mShown && isVisible() ) { init(); } }
QString QgsRelationReferenceFieldFormatter::representValue( QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const { Q_UNUSED( cache ); // Some sanity checks if ( !config.contains( QStringLiteral( "Relation" ) ) ) { QgsMessageLog::logMessage( "Missing Relation in configuration" ); return value.toString(); } QgsRelation relation = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ); if ( !relation.isValid() ) { QgsMessageLog::logMessage( "Invalid relation" ); return value.toString(); } QgsVectorLayer* referencingLayer = relation.referencingLayer(); if ( layer != referencingLayer ) { QgsMessageLog::logMessage( "representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ); return value.toString(); } int referencingFieldIdx = referencingLayer->fields().lookupField( relation.fieldPairs().at( 0 ).first ); if ( referencingFieldIdx != fieldIndex ) { QgsMessageLog::logMessage( "representValue() with inconsistent fieldIndex parameter w.r.t relation referencingFieldIdx" ); return value.toString(); } QgsVectorLayer* referencedLayer = relation.referencedLayer(); if ( !referencedLayer ) { QgsMessageLog::logMessage( "Cannot find referenced layer" ); return value.toString(); } // Attributes from the referencing layer QgsAttributes attrs = QgsAttributes( layer->fields().count() ); // Set the value on the foreign key field of the referencing record attrs[ referencingFieldIdx ] = value; QgsFeatureRequest request = relation.getReferencedFeatureRequest( attrs ); QgsFeature feature; referencedLayer->getFeatures( request ).nextFeature( feature ); if ( !feature.isValid() ) return value.toString(); QgsExpression expr( referencedLayer->displayExpression() ); QgsExpressionContext context; context << QgsExpressionContextUtils::globalScope() << QgsExpressionContextUtils::projectScope() << QgsExpressionContextUtils::layerScope( referencedLayer ); context.setFeature( feature ); QString title = expr.evaluate( &context ).toString(); if ( expr.hasEvalError() ) { int referencedFieldIdx = referencedLayer->fields().lookupField( relation.fieldPairs().at( 0 ).second ); title = feature.attribute( referencedFieldIdx ).toString(); } return title; }
void QgsRelationReferenceWidget::setRelation( const QgsRelation &relation, bool allowNullValue ) { mAllowNull = allowNullValue; mRemoveFKButton->setVisible( allowNullValue && mReadOnlySelector ); if ( relation.isValid() ) { mInvalidLabel->hide(); mRelation = relation; mReferencingLayer = relation.referencingLayer(); mRelationName = relation.name(); mReferencedLayer = relation.referencedLayer(); mReferencedField = relation.fieldPairs().at( 0 ).second; if ( mComboBox ) mComboBox->setIdentifierField( mReferencedField ); mReferencedFieldIdx = mReferencedLayer->fields().lookupField( relation.fieldPairs().at( 0 ).second ); mReferencingFieldIdx = mReferencingLayer->fields().lookupField( relation.fieldPairs().at( 0 ).first ); mAttributeEditorFrame->setObjectName( QStringLiteral( "referencing/" ) + relation.name() ); if ( mEmbedForm ) { QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed ); mAttributeEditorFrame->setTitle( mReferencedLayer->name() ); mReferencedAttributeForm = new QgsAttributeForm( relation.referencedLayer(), QgsFeature(), context, this ); mAttributeEditorLayout->addWidget( mReferencedAttributeForm ); } connect( mReferencedLayer, &QgsVectorLayer::editingStarted, this, &QgsRelationReferenceWidget::updateAddEntryButton ); connect( mReferencedLayer, &QgsVectorLayer::editingStopped, this, &QgsRelationReferenceWidget::updateAddEntryButton ); updateAddEntryButton(); } else { mInvalidLabel->show(); } if ( mShown && isVisible() ) { init(); } }
void QgsRelReferenceConfigDlg::on_mComboRelation_indexChanged( int idx ) { QString relName = mComboRelation->itemData( idx ).toString(); QgsRelation rel = QgsProject::instance()->relationManager()->relation( relName ); QgsVectorLayer* referencedLayer = rel.referencedLayer(); if ( referencedLayer ) { mTxtDisplayExpression->setText( referencedLayer->displayExpression() ); } }
void QgsRelReferenceConfigDlg::previewExpressionBuilder() { QString relName = mComboRelation->itemData( mComboRelation->currentIndex() ).toString(); QgsRelation relation = QgsProject::instance()->relationManager()->relation( relName ); // Show expression builder QgsExpressionBuilderDialog dlg( relation.referencedLayer(), mTxtDisplayExpression->text() , this ); dlg.setWindowTitle( tr( "Preview Expression" ) ); if ( dlg.exec() == QDialog::Accepted ) { mTxtDisplayExpression->setText( dlg.expressionText() ); } }
QgsEditorWidgetConfig QgsRelReferenceConfigDlg::config() { QgsEditorWidgetConfig myConfig; myConfig.insert( "AllowNULL", mCbxAllowNull->isChecked() ); myConfig.insert( "ShowForm", mCbxShowForm->isChecked() ); myConfig.insert( "Relation", mComboRelation->itemData( mComboRelation->currentIndex() ) ); QString relName = mComboRelation->itemData( mComboRelation->currentIndex() ).toString(); QgsRelation relation = QgsProject::instance()->relationManager()->relation( relName ); if ( relation.isValid() ) { relation.referencedLayer()->setDisplayExpression( mTxtDisplayExpression->text() ); } return myConfig; }