22#include <QDialogButtonBox>
24#include <QSortFilterProxyModel>
27#include "moc_qgsdbrelationshipwidget.cpp"
29using namespace Qt::StringLiterals;
37 , mConnection( connection )
44 const QList<Qgis::RelationshipCardinality> cardinalities = mConnection->supportedRelationshipCardinalities();
53 if ( cardinalities.contains( cardinality ) )
57 const QList<Qgis::RelationshipStrength> strengths = mConnection->supportedRelationshipStrengths();
64 if ( strengths.contains( strength ) )
71 mForwardLabelLineEdit->hide();
72 mForwardLabel->hide();
77 mBackwardLabelLineEdit->hide();
78 mReverseLabel->hide();
81 const QStringList relatedTableTypes = mConnection->relatedTableTypes();
82 mRelatedTableTypeCombo->addItems( relatedTableTypes );
84 mProxyModel =
new QSortFilterProxyModel( mTableModel );
85 mProxyModel->setSourceModel( mTableModel );
86 mProxyModel->setDynamicSortFilter(
true );
87 mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );
88 mProxyModel->setSortRole( Qt::DisplayRole );
89 mProxyModel->sort( 0 );
91 mLeftTableCombo->setModel( mProxyModel );
92 mRightTableCombo->setModel( mProxyModel );
94 connect( mNameEdit, &QLineEdit::textChanged,
this, [
this] {
97 connect( mLeftTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
98 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
101 connect( mRightTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
102 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
106 for ( QComboBox *combo :
109 qobject_cast<QComboBox *>( mLeftFieldsCombo ),
110 qobject_cast<QComboBox *>( mRightFieldsCombo ),
112 mRelatedTableTypeCombo
115 connect( combo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
120 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
121 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
127 mNameEdit->setText( mRelation.name() );
128 mNameEdit->setEnabled(
false );
129 mStrengthCombo->setCurrentIndex( mStrengthCombo->findData( QVariant::fromValue( mRelation.strength() ) ) );
132 mLeftTableCombo->setCurrentText( parts.value( u
"layerName"_s ).toString() );
134 mRightTableCombo->setCurrentText( parts.value( u
"layerName"_s ).toString() );
136 mCardinalityCombo->setCurrentIndex( mCardinalityCombo->findData( QVariant::fromValue( mRelation.cardinality() ) ) );
137 mLeftFieldsCombo->setCurrentText( mRelation.referencedLayerFields().value( 0 ) );
138 mRightFieldsCombo->setCurrentText( mRelation.referencingLayerFields().value( 0 ) );
139 mForwardLabelLineEdit->setText( mRelation.forwardPathLabel() );
140 mBackwardLabelLineEdit->setText( mRelation.backwardPathLabel() );
141 mRelatedTableTypeCombo->setCurrentText( mRelation.relatedTableType() );
146 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() );
157 if ( mRelatedTableTypeCombo->currentIndex() >= 0 )
164 if ( mNameEdit->text().trimmed().isEmpty() )
167 if ( mLeftTableCombo->currentText().isEmpty() )
170 if ( mRightTableCombo->currentText().isEmpty() )
173 if ( mLeftTableCombo->currentText() == mRightTableCombo->currentText() )
176 if ( mLeftFieldsCombo->currentText().isEmpty() )
179 if ( mRightFieldsCombo->currentText().isEmpty() )
191 : QDialog( parent, flags )
193 setObjectName( u
"QgsDbRelationDialog"_s );
195 QVBoxLayout *vLayout =
new QVBoxLayout();
197 vLayout->addWidget( mWidget, 1 );
199 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
200 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
201 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
202 vLayout->addWidget( mButtonBox );
204 setLayout( vLayout );
206 validityChanged( mWidget->isValid() );
218 return mWidget->relationship();
223 if ( !mWidget->isValid() )
229void QgsDbRelationDialog::validityChanged(
bool isValid )
231 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.