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; }
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; }
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 QgsRelationWidgetWrapper::initWidget( QWidget* editor ) { QgsRelationEditorWidget* w = dynamic_cast<QgsRelationEditorWidget*>( editor ); // if the editor cannot be cast to relation editor, insert a new one if ( !w ) { w = new QgsRelationEditorWidget( editor ); w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); if ( ! editor->layout() ) { editor->setLayout( new QGridLayout() ); } editor->layout()->addWidget( w ); } QgsAttributeEditorContext myContext( QgsAttributeEditorContext( context(), mRelation, QgsAttributeEditorContext::Multiple, QgsAttributeEditorContext::Embed ) ); w->setEditorContext( myContext ); QgsRelation nmrel = QgsProject::instance()->relationManager()->relation( config( "nm-rel" ).toString() ); // If this widget is already embedded by the same relation, reduce functionality const QgsAttributeEditorContext* ctx = &context(); do { if (( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed ) || ( nmrel.isValid() && ctx->relation().name() == nmrel.name() ) ) { w->setVisible( false ); break; } ctx = ctx->parentContext(); } while ( ctx ); w->setRelations( mRelation, nmrel ); mWidget = w; }