Skip to content
Permalink
Browse files
Merge pull request #43488 from 3nids/fix41959
offer a restriced set of fields as referenced fiels for polymorphic relations
  • Loading branch information
3nids committed Jun 2, 2021
2 parents 082aa7b + ac8cb99 commit a636d805419f44ae98a711822473eee6ffb7045a
@@ -143,7 +143,7 @@ set(QGIS_APP_SRCS
qgsrastercalcdialog.cpp
qgsrelationmanagerdialog.cpp
qgsrelationadddlg.cpp
qgsrelationaddpolymorphicdlg.cpp
qgsrelationaddpolymorphicdialog.cpp
qgsselectbyformdialog.cpp
qgsstatisticalsummarydockwidget.cpp
qgstextannotationdialog.cpp
@@ -22,7 +22,7 @@
#include <QHBoxLayout>
#include <QVBoxLayout>

#include "qgsrelationaddpolymorphicdlg.h"
#include "qgsrelationaddpolymorphicdialog.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayercombobox.h"
#include "qgsfieldcombobox.h"
@@ -33,7 +33,7 @@
#include "qgsrelationmanager.h"
#include "qgsfieldexpressionwidget.h"

QgsRelationAddPolymorphicDlg::QgsRelationAddPolymorphicDlg( bool isEditDialog, QWidget *parent )
QgsRelationAddPolymorphicDialog::QgsRelationAddPolymorphicDialog( bool isEditDialog, QWidget *parent )
: QDialog( parent )
, Ui::QgsRelationManagerAddPolymorphicDialogBase()
, mIsEditDialog( isEditDialog )
@@ -45,8 +45,8 @@ QgsRelationAddPolymorphicDlg::QgsRelationAddPolymorphicDlg( bool isEditDialog, Q
: tr( "Add Polymorphic Relation" ) );

mButtonBox->setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsRelationAddPolymorphicDlg::accept );
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsRelationAddPolymorphicDlg::reject );
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsRelationAddPolymorphicDialog::accept );
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsRelationAddPolymorphicDialog::reject );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [ = ]
{
QgsHelp::openHelp( QStringLiteral( "working_with_vector/attribute_table.html#defining-polymorphic-relations" ) );
@@ -67,23 +67,25 @@ QgsRelationAddPolymorphicDlg::QgsRelationAddPolymorphicDlg( bool isEditDialog, Q
"Duplications are made by the feature duplication action.\n"
"The default actions are activated in the Action section of the layer properties." ) );

mFieldsMappingWidget->setEnabled( false );
addFieldsRow();
updateTypeConfigWidget();
updateDialogButtons();
updateReferencedLayerFieldComboBox();

connect( mFieldsMappingTable, &QTableWidget::itemSelectionChanged, this, &QgsRelationAddPolymorphicDlg::updateFieldsMappingButtons );
connect( mFieldsMappingAddButton, &QToolButton::clicked, this, &QgsRelationAddPolymorphicDlg::addFieldsRow );
connect( mFieldsMappingRemoveButton, &QToolButton::clicked, this, &QgsRelationAddPolymorphicDlg::removeFieldsRow );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDlg::updateDialogButtons );
connect( mFieldsMappingTable, &QTableWidget::itemSelectionChanged, this, &QgsRelationAddPolymorphicDialog::updateFieldsMappingButtons );
connect( mFieldsMappingAddButton, &QToolButton::clicked, this, &QgsRelationAddPolymorphicDialog::addFieldsRow );
connect( mFieldsMappingRemoveButton, &QToolButton::clicked, this, &QgsRelationAddPolymorphicDialog::removeFieldsRow );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDialog::updateDialogButtons );
connect( mRelationStrengthComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int index ) { Q_UNUSED( index ); updateDialogButtons(); } );
connect( mReferencedLayerExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsRelationAddPolymorphicDlg::updateDialogButtons );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDlg::updateChildRelationsComboBox );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDlg::updateReferencingFieldsComboBoxes );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDlg::updateReferencedLayerFieldComboBox );
connect( mReferencedLayerExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsRelationAddPolymorphicDialog::updateDialogButtons );
connect( mReferencedLayersComboBox, &QgsCheckableComboBox::checkedItemsChanged, this, &QgsRelationAddPolymorphicDialog::referencedLayersChanged );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDialog::updateChildRelationsComboBox );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDialog::updateReferencingFieldsComboBoxes );
connect( mReferencingLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddPolymorphicDialog::updateReferencedLayerFieldComboBox );
}

void QgsRelationAddPolymorphicDlg::setPolymorphicRelation( const QgsPolymorphicRelation polyRel )
void QgsRelationAddPolymorphicDialog::setPolymorphicRelation( const QgsPolymorphicRelation polyRel )
{
mIdLineEdit->setText( polyRel.id() );
mReferencingLayerComboBox->setLayer( polyRel.referencingLayer() );
@@ -106,15 +108,15 @@ void QgsRelationAddPolymorphicDlg::setPolymorphicRelation( const QgsPolymorphicR
mReferencedLayersComboBox->setItemCheckState( mReferencedLayersComboBox->findData( layerId ), Qt::Checked );
}

void QgsRelationAddPolymorphicDlg::updateTypeConfigWidget()
void QgsRelationAddPolymorphicDialog::updateTypeConfigWidget()
{
updateDialogButtons();
}

void QgsRelationAddPolymorphicDlg::addFieldsRow()
void QgsRelationAddPolymorphicDialog::addFieldsRow()
{
QgsFieldComboBox *referencingField = new QgsFieldComboBox( this );
QLineEdit *referencedPolymorphicField = new QLineEdit( this );
QComboBox *referencedPolymorphicField = new QComboBox( this );
int index = mFieldsMappingTable->rowCount();

referencingField->setLayer( mReferencingLayerComboBox->currentLayer() );
@@ -128,7 +130,7 @@ void QgsRelationAddPolymorphicDlg::addFieldsRow()
updateDialogButtons();
}

void QgsRelationAddPolymorphicDlg::removeFieldsRow()
void QgsRelationAddPolymorphicDialog::removeFieldsRow()
{
if ( mFieldsMappingTable->selectionModel()->hasSelection() )
{
@@ -151,7 +153,7 @@ void QgsRelationAddPolymorphicDlg::removeFieldsRow()
updateDialogButtons();
}

void QgsRelationAddPolymorphicDlg::updateFieldsMappingButtons()
void QgsRelationAddPolymorphicDialog::updateFieldsMappingButtons()
{
int rowsCount = mFieldsMappingTable->rowCount();
int selectedRowsCount = mFieldsMappingTable->selectionModel()->selectedRows().count();
@@ -160,7 +162,7 @@ void QgsRelationAddPolymorphicDlg::updateFieldsMappingButtons()
mFieldsMappingRemoveButton->setEnabled( isRemoveButtonEnabled );
}

void QgsRelationAddPolymorphicDlg::updateFieldsMappingHeaders()
void QgsRelationAddPolymorphicDialog::updateFieldsMappingHeaders()
{
int rowsCount = mFieldsMappingTable->rowCount();
QStringList verticalHeaderLabels;
@@ -171,61 +173,64 @@ void QgsRelationAddPolymorphicDlg::updateFieldsMappingHeaders()
mFieldsMappingTable->setVerticalHeaderLabels( verticalHeaderLabels );
}

QString QgsRelationAddPolymorphicDlg::referencingLayerId()
QString QgsRelationAddPolymorphicDialog::referencingLayerId()
{
return mReferencingLayerComboBox->currentLayer()->id();
}

QString QgsRelationAddPolymorphicDlg::referencedLayerField()
QString QgsRelationAddPolymorphicDialog::referencedLayerField()
{
return mReferencedLayerFieldComboBox->currentField();
}

QString QgsRelationAddPolymorphicDlg::referencedLayerExpression()
QString QgsRelationAddPolymorphicDialog::referencedLayerExpression()
{
return mReferencedLayerExpressionWidget->expression();
}

QStringList QgsRelationAddPolymorphicDlg::referencedLayerIds()
QStringList QgsRelationAddPolymorphicDialog::referencedLayerIds()
{
return QVariant( mReferencedLayersComboBox->checkedItemsData() ).toStringList();
}

QList< QPair< QString, QString > > QgsRelationAddPolymorphicDlg::fieldPairs()
QList< QPair< QString, QString > > QgsRelationAddPolymorphicDialog::fieldPairs()
{
QList< QPair< QString, QString > > references;
for ( int i = 0, l = mFieldsMappingTable->rowCount(); i < l; i++ )
{
QString referencedField = static_cast<QLineEdit *>( mFieldsMappingTable->cellWidget( i, 0 ) )->text();
QComboBox *referencedFieldComboBox = static_cast<QComboBox *>( mFieldsMappingTable->cellWidget( i, 0 ) );
if ( referencedFieldComboBox->currentData().toInt() == -1 )
continue;
QString referencedField = referencedFieldComboBox->currentText();
QString referencingField = static_cast<QgsFieldComboBox *>( mFieldsMappingTable->cellWidget( i, 1 ) )->currentField();
references << qMakePair( referencingField, referencedField );
}

return references;
}

QString QgsRelationAddPolymorphicDlg::relationId()
QString QgsRelationAddPolymorphicDialog::relationId()
{
return mIdLineEdit->text();
}

QString QgsRelationAddPolymorphicDlg::relationName()
QString QgsRelationAddPolymorphicDialog::relationName()
{
QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( mReferencingLayerComboBox->currentLayer() );
return tr( "Polymorphic relations for \"%1\"" ).arg( vl ? vl->name() : QStringLiteral( "<NO LAYER>" ) );
}

QgsRelation::RelationStrength QgsRelationAddPolymorphicDlg::relationStrength()
QgsRelation::RelationStrength QgsRelationAddPolymorphicDialog::relationStrength()
{
return mRelationStrengthComboBox->currentData().value<QgsRelation::RelationStrength>();
}

void QgsRelationAddPolymorphicDlg::updateDialogButtons()
void QgsRelationAddPolymorphicDialog::updateDialogButtons()
{
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isDefinitionValid() );
}

bool QgsRelationAddPolymorphicDlg::isDefinitionValid()
bool QgsRelationAddPolymorphicDialog::isDefinitionValid()
{
bool isValid = true;
return isValid;
@@ -241,7 +246,7 @@ bool QgsRelationAddPolymorphicDlg::isDefinitionValid()
return isValid;
}

void QgsRelationAddPolymorphicDlg::updateChildRelationsComboBox()
void QgsRelationAddPolymorphicDialog::updateChildRelationsComboBox()
{
QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( mReferencingLayerComboBox->currentLayer() );
if ( !vl || !vl->isValid() )
@@ -262,7 +267,7 @@ void QgsRelationAddPolymorphicDlg::updateChildRelationsComboBox()
}
}

void QgsRelationAddPolymorphicDlg::updateReferencingFieldsComboBoxes()
void QgsRelationAddPolymorphicDialog::updateReferencingFieldsComboBoxes()
{
QgsMapLayer *vl = mReferencingLayerComboBox->currentLayer();
if ( !vl || !vl->isValid() )
@@ -275,7 +280,59 @@ void QgsRelationAddPolymorphicDlg::updateReferencingFieldsComboBoxes()
}
}

void QgsRelationAddPolymorphicDlg::updateReferencedLayerFieldComboBox()
void QgsRelationAddPolymorphicDialog::updateReferencedLayerFieldComboBox()
{
mReferencedLayerFieldComboBox->setLayer( mReferencingLayerComboBox->currentLayer() );
}

void QgsRelationAddPolymorphicDialog::referencedLayersChanged()
{
const QStringList &layerIds = referencedLayerIds();
mFieldsMappingWidget->setEnabled( layerIds.count() > 0 );

bool firstLayer = true;
QSet<QString> fields;
for ( const QString &layerId : layerIds )
{
QgsVectorLayer *vl = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
if ( vl && vl->isValid() )
{
const QSet layerFields = qgis::listToSet( vl->fields().names() );
if ( firstLayer )
{
fields = layerFields;
firstLayer = false;
}
else
{
fields.intersect( layerFields );
}
}
}

for ( int i = 0, l = mFieldsMappingTable->rowCount(); i < l; i++ )
{
QComboBox *cb = static_cast<QComboBox *>( mFieldsMappingTable->cellWidget( i, 0 ) );
const QString currentField = cb->currentText();
cb->clear();
if ( fields.count() > 0 )
{
const QSet<QString> constFields = fields;
for ( const QString &field : constFields )
{
cb->addItem( field );
if ( field == currentField )
cb->setCurrentText( field );
}
}
else
{
cb->addItem( tr( "None" ), -1 );
cb->addItem( tr( "the referenced layers have no common fields." ), -1 );
QStandardItem *item = qobject_cast<QStandardItemModel *>( cb->model() )->item( 1 );
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
}
}

updateDialogButtons();
}
@@ -12,8 +12,8 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSRELATIONADDPOLYMORPHICDLG_H
#define QGSRELATIONADDPOLYMORPHICDLG_H
#ifndef QGSRELATIONADDPOLYMORPHICDIALOG_H
#define QGSRELATIONADDPOLYMORPHICDIALOG_H

#include <QDialog>
#include "qgis_app.h"
@@ -36,12 +36,12 @@ class QgsMapLayerComboBox;
* QgsRelationAddDlg allows configuring a new relation.
* Multiple field pairs can be set.
*/
class APP_EXPORT QgsRelationAddPolymorphicDlg : public QDialog, private Ui::QgsRelationManagerAddPolymorphicDialogBase
class APP_EXPORT QgsRelationAddPolymorphicDialog : public QDialog, private Ui::QgsRelationManagerAddPolymorphicDialogBase
{
Q_OBJECT

public:
explicit QgsRelationAddPolymorphicDlg( bool isEditDialog, QWidget *parent = nullptr );
explicit QgsRelationAddPolymorphicDialog( bool isEditDialog, QWidget *parent = nullptr );

/**
* Returns the id of the referencing layer
@@ -98,6 +98,7 @@ class APP_EXPORT QgsRelationAddPolymorphicDlg : public QDialog, private Ui::QgsR
void updateChildRelationsComboBox();
void updateReferencingFieldsComboBoxes();
void updateReferencedLayerFieldComboBox();
void referencedLayersChanged();

private:
bool isDefinitionValid();
@@ -107,4 +108,4 @@ class APP_EXPORT QgsRelationAddPolymorphicDlg : public QDialog, private Ui::QgsR

};

#endif // QGSRELATIONADDPOLYMORPHICDLG_H
#endif // QGSRELATIONADDPOLYMORPHICDIALOG_H
@@ -15,7 +15,7 @@

#include "qgsdiscoverrelationsdialog.h"
#include "qgsrelationadddlg.h"
#include "qgsrelationaddpolymorphicdlg.h"
#include "qgsrelationaddpolymorphicdialog.h"
#include "qgsrelationmanagerdialog.h"
#include "qgsrelationmanager.h"
#include "qgspolymorphicrelation.h"
@@ -240,7 +240,7 @@ void QgsRelationManagerDialog::mBtnAddRelation_clicked()

void QgsRelationManagerDialog::mActionAddPolymorphicRelation_triggered()
{
QgsRelationAddPolymorphicDlg addDlg( false );
QgsRelationAddPolymorphicDialog addDlg( false );

if ( addDlg.exec() )
{
@@ -268,7 +268,7 @@ void QgsRelationManagerDialog::mActionAddPolymorphicRelation_triggered()

void QgsRelationManagerDialog::mActionEditPolymorphicRelation_triggered()
{
QgsRelationAddPolymorphicDlg addDlg( true );
QgsRelationAddPolymorphicDialog addDlg( true );
const QModelIndexList rows = mRelationsTree->selectionModel()->selectedRows();

if ( rows.size() != 1 )

0 comments on commit a636d80

Please sign in to comment.