QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsdbrelationshipwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdbrelationshipwidget.cpp
3 ------------------
4 Date : November 2022
5 Copyright : (C) 2022 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgsgui.h"
19#include "qgsproviderregistry.h"
20#include <QDialogButtonBox>
21#include <QPushButton>
22#include <QSortFilterProxyModel>
23
24//
25// QgsDbRelationWidget
26//
27
29 : QWidget( parent )
30 , mConnection( connection )
31{
32 setupUi( this );
33
34 // takes ownership of connection
35 mTableModel = new QgsDatabaseTableModel( connection, QString(), this );
36
37 const QList<Qgis::RelationshipCardinality> cardinalities = mConnection->supportedRelationshipCardinalities();
38 for ( Qgis::RelationshipCardinality cardinality :
39 {
44 } )
45 {
46 if ( cardinalities.contains( cardinality ) )
47 mCardinalityCombo->addItem( QgsRelation::cardinalityToDisplayString( cardinality ), QVariant::fromValue( cardinality ) );
48 }
49
50 const QList<Qgis::RelationshipStrength> strengths = mConnection->supportedRelationshipStrengths();
51 for ( Qgis::RelationshipStrength strength :
52 {
55 } )
56 {
57 if ( strengths.contains( strength ) )
58 mStrengthCombo->addItem( QgsRelation::strengthToDisplayString( strength ), QVariant::fromValue( strength ) );
59 }
60
61 const Qgis::RelationshipCapabilities capabilities = mConnection->supportedRelationshipCapabilities();
62 if ( !( capabilities & Qgis::RelationshipCapability::ForwardPathLabel ) )
63 {
64 mForwardLabelLineEdit->hide();
65 mForwardLabel->hide();
66 }
67
69 {
70 mBackwardLabelLineEdit->hide();
71 mReverseLabel->hide();
72 }
73
74 const QStringList relatedTableTypes = mConnection->relatedTableTypes();
75 mRelatedTableTypeCombo->addItems( relatedTableTypes );
76
77 mProxyModel = new QSortFilterProxyModel( mTableModel );
78 mProxyModel->setSourceModel( mTableModel );
79 mProxyModel->setDynamicSortFilter( true );
80 mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );
81 mProxyModel->setSortRole( Qt::DisplayRole );
82 mProxyModel->sort( 0 );
83
84 mLeftTableCombo->setModel( mProxyModel );
85 mRightTableCombo->setModel( mProxyModel );
86
87 connect( mNameEdit, &QLineEdit::textChanged, this, [ = ]
88 {
89 emit validityChanged( isValid() );
90 } );
91 connect( mLeftTableCombo, qOverload< int >( &QComboBox::currentIndexChanged ), this, [ = ]( int )
92 {
93 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
94 emit validityChanged( isValid() );
95 } );
96 connect( mRightTableCombo, qOverload< int >( &QComboBox::currentIndexChanged ), this, [ = ]( int )
97 {
98 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
99 emit validityChanged( isValid() );
100 } );
101
102 for ( QComboBox *combo :
103 {
104 mCardinalityCombo,
105 qobject_cast< QComboBox *>( mLeftFieldsCombo ),
106 qobject_cast< QComboBox *>( mRightFieldsCombo ),
107 mStrengthCombo,
108 mRelatedTableTypeCombo
109 } )
110 {
111 connect( combo, qOverload< int >( &QComboBox::currentIndexChanged ), this, [ = ]( int )
112 {
113 emit validityChanged( isValid() );
114 } );
115 }
116
117 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
118 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
119}
120
122{
123 mRelation = relationship;
124 mNameEdit->setText( mRelation.name() );
125 mNameEdit->setEnabled( false );
126 mStrengthCombo->setCurrentIndex( mStrengthCombo->findData( QVariant::fromValue( mRelation.strength() ) ) );
127
128 QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( mConnection->providerKey(), mRelation.referencedLayerSource() );
129 mLeftTableCombo->setCurrentText( parts.value( QStringLiteral( "layerName" ) ).toString() );
130 parts = QgsProviderRegistry::instance()->decodeUri( mConnection->providerKey(), mRelation.referencingLayerSource() );
131 mRightTableCombo->setCurrentText( parts.value( QStringLiteral( "layerName" ) ).toString() );
132
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() );
139}
140
142{
143 QgsWeakRelation result( mRelation.id().isEmpty() ? mNameEdit->text() : mRelation.id(),
144 mRelation.name().isEmpty() ? mNameEdit->text() : mRelation.name(),
145 mStrengthCombo->currentData().value< Qgis::RelationshipStrength >(),
146 QString(),
147 QString(),
148 mConnection->tableUri( QString(), mRightTableCombo->currentText() ),
149 mConnection->providerKey(),
150 QString(),
151 QString(),
152 mConnection->tableUri( QString(), mLeftTableCombo->currentText() ),
153 mConnection->providerKey()
154 );
155 result.setCardinality( mCardinalityCombo->currentData().value< Qgis::RelationshipCardinality >() );
156 result.setReferencedLayerFields( { mLeftFieldsCombo->currentText() } );
157 result.setReferencingLayerFields( { mRightFieldsCombo->currentText() } );
158 result.setForwardPathLabel( mForwardLabelLineEdit->text() );
159 result.setBackwardPathLabel( mBackwardLabelLineEdit->text() );
160
161 result.setMappingTable( mRelation.mappingTable() );
164
165 if ( mRelatedTableTypeCombo->currentIndex() >= 0 )
166 result.setRelatedTableType( mRelatedTableTypeCombo->currentText() );
167 return result;
168}
169
171{
172 if ( mNameEdit->text().trimmed().isEmpty() )
173 return false;
174
175 if ( mLeftTableCombo->currentText().isEmpty() )
176 return false;
177
178 if ( mRightTableCombo->currentText().isEmpty() )
179 return false;
180
181 if ( mLeftTableCombo->currentText() == mRightTableCombo->currentText() )
182 return false;
183
184 if ( mLeftFieldsCombo->currentText().isEmpty() )
185 return false;
186
187 if ( mRightFieldsCombo->currentText().isEmpty() )
188 return false;
189
190
191 return true;
192}
193
194//
195// QgsDbRelationDialog
196//
197
198QgsDbRelationDialog::QgsDbRelationDialog( QgsAbstractDatabaseProviderConnection *connection, QWidget *parent, Qt::WindowFlags flags )
199 : QDialog( parent, flags )
200{
201 setObjectName( QStringLiteral( "QgsDbRelationDialog" ) );
202
203 QVBoxLayout *vLayout = new QVBoxLayout();
204 mWidget = new QgsDbRelationWidget( connection );
205 vLayout->addWidget( mWidget, 1 );
206
207 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
208 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
209 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
210 vLayout->addWidget( mButtonBox );
211
212 setLayout( vLayout );
213 connect( mWidget, &QgsDbRelationWidget::validityChanged, this, &QgsDbRelationDialog::validityChanged );
214 validityChanged( mWidget->isValid() );
215
217}
218
220{
221 mWidget->setRelationship( relationship );
222}
223
225{
226 return mWidget->relationship();
227}
228
230{
231 if ( !mWidget->isValid() )
232 return;
233
234 QDialog::accept();
235}
236
237void QgsDbRelationDialog::validityChanged( bool isValid )
238{
239 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
240}
RelationshipStrength
Relationship strength.
Definition: qgis.h:3645
@ 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.
Definition: qgis.h:3657
@ 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.
Definition: qgis.h:3683
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
QString providerKey() const
Returns the provider key.
virtual QString tableUri(const QString &schema, const QString &name) const
Returns the URI string for the given table and schema.
virtual QStringList relatedTableTypes() const
Returns a list of the related table types supported by the database format.
virtual Qgis::RelationshipCapabilities supportedRelationshipCapabilities() const
Returns the relationship capabilities supported by the provider.
virtual QList< Qgis::RelationshipCardinality > supportedRelationshipCardinalities() const
Returns a list of relationship cardinalities which are supported by the provider.
virtual QList< Qgis::RelationshipStrength > supportedRelationshipStrengths() const
Returns a list of relationship strengths which are supported by the provider.
virtual QgsFields fields(const QString &schema, const QString &table, QgsFeedback *feedback=nullptr) const
Returns the fields of a table and schema.
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.
A widget for configuration of the properties of a relationship in a database.
void setRelationship(const QgsWeakRelation &relationship)
Sets the current relationship to show properties for in the widget.
QgsDbRelationWidget(QgsAbstractDatabaseProviderConnection *connection, QWidget *parent=nullptr)
Constructor for QgsDbRelationWidget with the specified parent widget, for the specified connection.
void validityChanged(bool isValid)
Emitted whenever the validity of the relationship configuration in the widget changes.
bool isValid() const
Returns true if the widget currently represents a valid relationship configuration.
QgsWeakRelation relationship() const
Returns the relationship as defined in the widget.
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...
Definition: qgsgui.cpp:194
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.
The QgsWeakRelation class 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...
QStringList mappingReferencingLayerFields() const
Returns the list of fields from the mappingTable() involved in the 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.
QString relatedTableType() const
Returns the type string of the related table.
void setReferencingLayerFields(const QStringList &fields)
Sets the list of fields from the referencingLayer() involved in the relationship.
QString name() const
Returns the relationship's name.
QString backwardPathLabel() const
Returns the label of the backward path for the relationship.
void setMappingReferencedLayerFields(const QStringList &fields)
Sets the list of fields from the mappingTable() involved in the relationship.
QString referencedLayerSource() const
Returns the source URI for the referenced (or "parent" / "left") layer.
QString referencingLayerSource() const
Returns the source URI for the referencing (or "child" / "right") layer.
QString id() const
Returns the relationship's ID.
QString forwardPathLabel() const
Returns the label of the forward path for the relationship.
Qgis::RelationshipCardinality cardinality() const
Returns the relationship's cardinality.
QgsVectorLayerRef mappingTable() const
Returns a weak reference to the mapping table, which forms the middle table in many-to-many relations...
void setCardinality(Qgis::RelationshipCardinality cardinality)
Sets the relationship's cardinality.
QStringList referencedLayerFields() const
Returns the list of fields from the referencedLayer() involved in the relationship.
QStringList mappingReferencedLayerFields() const
Returns the list of fields from the mappingTable() involved in the relationship.
void setRelatedTableType(const QString &type)
Sets the type string of the related table.
QStringList referencingLayerFields() const
Returns the list of fields from the referencingLayer() involved in the relationship.
void setReferencedLayerFields(const QStringList &fields)
Sets the list of fields from the referencedLayer() involved in the relationship.
Qgis::RelationshipStrength strength() const
Returns the strength of the relation.