22#include <QDialogButtonBox>
24#include <QSortFilterProxyModel>
26#include "moc_qgsdbrelationshipwidget.cpp"
34 , mConnection( connection )
41 const QList<Qgis::RelationshipCardinality> cardinalities = mConnection->supportedRelationshipCardinalities();
50 if ( cardinalities.contains( cardinality ) )
54 const QList<Qgis::RelationshipStrength> strengths = mConnection->supportedRelationshipStrengths();
61 if ( strengths.contains( strength ) )
68 mForwardLabelLineEdit->hide();
69 mForwardLabel->hide();
74 mBackwardLabelLineEdit->hide();
75 mReverseLabel->hide();
78 const QStringList relatedTableTypes = mConnection->relatedTableTypes();
79 mRelatedTableTypeCombo->addItems( relatedTableTypes );
81 mProxyModel =
new QSortFilterProxyModel( mTableModel );
82 mProxyModel->setSourceModel( mTableModel );
83 mProxyModel->setDynamicSortFilter(
true );
84 mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );
85 mProxyModel->setSortRole( Qt::DisplayRole );
86 mProxyModel->sort( 0 );
88 mLeftTableCombo->setModel( mProxyModel );
89 mRightTableCombo->setModel( mProxyModel );
91 connect( mNameEdit, &QLineEdit::textChanged,
this, [
this] {
94 connect( mLeftTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
95 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
98 connect( mRightTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
99 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
103 for ( QComboBox *combo :
106 qobject_cast<QComboBox *>( mLeftFieldsCombo ),
107 qobject_cast<QComboBox *>( mRightFieldsCombo ),
109 mRelatedTableTypeCombo
112 connect( combo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
117 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
118 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
124 mNameEdit->setText( mRelation.name() );
125 mNameEdit->setEnabled(
false );
126 mStrengthCombo->setCurrentIndex( mStrengthCombo->findData( QVariant::fromValue( mRelation.strength() ) ) );
129 mLeftTableCombo->setCurrentText( parts.value( QStringLiteral(
"layerName" ) ).toString() );
131 mRightTableCombo->setCurrentText( parts.value( QStringLiteral(
"layerName" ) ).toString() );
133 mCardinalityCombo->setCurrentIndex( mCardinalityCombo->findData( QVariant::fromValue( mRelation.cardinality() ) ) );
134 mLeftFieldsCombo->setCurrentText( mRelation.referencedLayerFields().value( 0 ) );
135 mRightFieldsCombo->setCurrentText( mRelation.referencingLayerFields().value( 0 ) );
136 mForwardLabelLineEdit->setText( mRelation.forwardPathLabel() );
137 mBackwardLabelLineEdit->setText( mRelation.backwardPathLabel() );
138 mRelatedTableTypeCombo->setCurrentText( mRelation.relatedTableType() );
143 QgsWeakRelation result( mRelation.id().isEmpty() ? mNameEdit->text() : mRelation.id(), mRelation.name().isEmpty() ? mNameEdit->text() : mRelation.name(), mStrengthCombo->currentData().value<
Qgis::RelationshipStrength>(), QString(), QString(), mConnection->tableUri( QString(), mRightTableCombo->currentText() ), mConnection->providerKey(), QString(), QString(), mConnection->tableUri( QString(), mLeftTableCombo->currentText() ), mConnection->providerKey() );
154 if ( mRelatedTableTypeCombo->currentIndex() >= 0 )
161 if ( mNameEdit->text().trimmed().isEmpty() )
164 if ( mLeftTableCombo->currentText().isEmpty() )
167 if ( mRightTableCombo->currentText().isEmpty() )
170 if ( mLeftTableCombo->currentText() == mRightTableCombo->currentText() )
173 if ( mLeftFieldsCombo->currentText().isEmpty() )
176 if ( mRightFieldsCombo->currentText().isEmpty() )
188 : QDialog( parent, flags )
190 setObjectName( QStringLiteral(
"QgsDbRelationDialog" ) );
192 QVBoxLayout *vLayout =
new QVBoxLayout();
194 vLayout->addWidget( mWidget, 1 );
196 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
197 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
198 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
199 vLayout->addWidget( mButtonBox );
201 setLayout( vLayout );
203 validityChanged( mWidget->isValid() );
215 return mWidget->relationship();
220 if ( !mWidget->isValid() )
226void QgsDbRelationDialog::validityChanged(
bool isValid )
228 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
RelationshipStrength
Relationship strength.
@ Composition
Fix relation, related elements are part of the parent and a parent copy will copy any children or del...
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
@ BackwardPathLabel
Supports backward path labels.
@ ForwardPathLabel
Supports forward path labels.
RelationshipCardinality
Relationship cardinality.
@ ManyToMany
Many to many relationship.
@ ManyToOne
Many to one relationship.
@ OneToOne
One to one relationship.
@ OneToMany
One to many relationship.
QFlags< RelationshipCapability > RelationshipCapabilities
Relationship capabilities.
Provides common functionality for database based connections.
A model containing tables from a database connection.
QgsDbRelationDialog(QgsAbstractDatabaseProviderConnection *connection, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsDbRelationDialog, with the specified parent widget and window flags,...
QgsWeakRelation relationship() const
Returns the relationship as defined in the dialog.
void setRelationship(const QgsWeakRelation &relationship)
Sets the current relationship to show properties for in the dialog.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
static QString cardinalityToDisplayString(Qgis::RelationshipCardinality cardinality)
Returns a user-friendly translated string representing a relationship cardinality.
static QString strengthToDisplayString(Qgis::RelationshipStrength strength)
Returns a user-friendly translated string representing a relationship strength.
Represent a QgsRelation with possibly unresolved layers or unmatched fields.
void setMappingTable(const QgsVectorLayerRef &table)
Sets a weak reference to the mapping table, which forms the middle table in many-to-many relationship...
void setForwardPathLabel(const QString &label)
Sets the label of the forward path for the relationship.
void setMappingReferencingLayerFields(const QStringList &fields)
Sets the list of fields from the mappingTable() involved in the relationship.
void setBackwardPathLabel(const QString &label)
Sets the label of the backward path for the relationship.
void setReferencingLayerFields(const QStringList &fields)
Sets the list of fields from the referencingLayer() involved in the relationship.
void setMappingReferencedLayerFields(const QStringList &fields)
Sets the list of fields from the mappingTable() involved in the relationship.
void setCardinality(Qgis::RelationshipCardinality cardinality)
Sets the relationship's cardinality.
void setRelatedTableType(const QString &type)
Sets the type string of the related table.
void setReferencedLayerFields(const QStringList &fields)
Sets the list of fields from the referencedLayer() involved in the relationship.