23#include <QDialogButtonBox>
25#include <QSortFilterProxyModel>
28#include "moc_qgsdbrelationshipwidget.cpp"
30using namespace Qt::StringLiterals;
38 , mConnection( connection )
45 const QList<Qgis::RelationshipCardinality> cardinalities = mConnection->supportedRelationshipCardinalities();
54 if ( cardinalities.contains( cardinality ) )
58 const QList<Qgis::RelationshipStrength> strengths = mConnection->supportedRelationshipStrengths();
65 if ( strengths.contains( strength ) )
72 mForwardLabelLineEdit->hide();
73 mForwardLabel->hide();
78 mBackwardLabelLineEdit->hide();
79 mReverseLabel->hide();
82 const QStringList relatedTableTypes = mConnection->relatedTableTypes();
83 mRelatedTableTypeCombo->addItems( relatedTableTypes );
85 mProxyModel =
new QSortFilterProxyModel( mTableModel );
86 mProxyModel->setSourceModel( mTableModel );
87 mProxyModel->setDynamicSortFilter(
true );
88 mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );
89 mProxyModel->setSortRole( Qt::DisplayRole );
90 mProxyModel->sort( 0 );
92 mLeftTableCombo->setModel( mProxyModel );
93 mRightTableCombo->setModel( mProxyModel );
95 connect( mNameEdit, &QLineEdit::textChanged,
this, [
this] {
98 connect( mLeftTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
99 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
102 connect( mRightTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
103 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
107 for ( QComboBox *combo :
110 qobject_cast<QComboBox *>( mLeftFieldsCombo ),
111 qobject_cast<QComboBox *>( mRightFieldsCombo ),
113 mRelatedTableTypeCombo
116 connect( combo, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this](
int ) {
121 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
122 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
128 mNameEdit->setText( mRelation.name() );
129 mNameEdit->setEnabled(
false );
130 mStrengthCombo->setCurrentIndex( mStrengthCombo->findData( QVariant::fromValue( mRelation.strength() ) ) );
133 mLeftTableCombo->setCurrentText( parts.value( u
"layerName"_s ).toString() );
135 mRightTableCombo->setCurrentText( parts.value( u
"layerName"_s ).toString() );
137 mCardinalityCombo->setCurrentIndex( mCardinalityCombo->findData( QVariant::fromValue( mRelation.cardinality() ) ) );
138 mLeftFieldsCombo->setCurrentText( mRelation.referencedLayerFields().value( 0 ) );
139 mRightFieldsCombo->setCurrentText( mRelation.referencingLayerFields().value( 0 ) );
140 mForwardLabelLineEdit->setText( mRelation.forwardPathLabel() );
141 mBackwardLabelLineEdit->setText( mRelation.backwardPathLabel() );
142 mRelatedTableTypeCombo->setCurrentText( mRelation.relatedTableType() );
147 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() );
158 if ( mRelatedTableTypeCombo->currentIndex() >= 0 )
165 if ( mNameEdit->text().trimmed().isEmpty() )
168 if ( mLeftTableCombo->currentText().isEmpty() )
171 if ( mRightTableCombo->currentText().isEmpty() )
174 if ( mLeftTableCombo->currentText() == mRightTableCombo->currentText() )
177 if ( mLeftFieldsCombo->currentText().isEmpty() )
180 if ( mRightFieldsCombo->currentText().isEmpty() )
192 : QDialog( parent, flags )
194 setObjectName( u
"QgsDbRelationDialog"_s );
196 QVBoxLayout *vLayout =
new QVBoxLayout();
198 vLayout->addWidget( mWidget, 1 );
200 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::StandardButton::Cancel | QDialogButtonBox::StandardButton::Help | QDialogButtonBox::StandardButton::Ok );
201 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
202 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
203 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, [] {
204 QgsHelp::openHelp( u
"working_with_vector/joins_relations.html#dataset-stored-relationships"_s );
206 vLayout->addWidget( mButtonBox );
208 setLayout( vLayout );
210 validityChanged( mWidget->isValid() );
222 return mWidget->relationship();
227 if ( !mWidget->isValid() )
233void QgsDbRelationDialog::validityChanged(
bool isValid )
235 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...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
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.