QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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 "moc_qgsdbrelationshipwidget.cpp"
18#include "qgsgui.h"
20#include "qgsproviderregistry.h"
21#include <QDialogButtonBox>
22#include <QPushButton>
23#include <QSortFilterProxyModel>
24
25//
26// QgsDbRelationWidget
27//
28
30 : QWidget( parent )
31 , mConnection( connection )
32{
33 setupUi( this );
34
35 // takes ownership of connection
36 mTableModel = new QgsDatabaseTableModel( connection, QString(), this );
37
38 const QList<Qgis::RelationshipCardinality> cardinalities = mConnection->supportedRelationshipCardinalities();
39 for ( Qgis::RelationshipCardinality cardinality :
40 {
45 } )
46 {
47 if ( cardinalities.contains( cardinality ) )
48 mCardinalityCombo->addItem( QgsRelation::cardinalityToDisplayString( cardinality ), QVariant::fromValue( cardinality ) );
49 }
50
51 const QList<Qgis::RelationshipStrength> strengths = mConnection->supportedRelationshipStrengths();
52 for ( Qgis::RelationshipStrength strength :
53 {
56 } )
57 {
58 if ( strengths.contains( strength ) )
59 mStrengthCombo->addItem( QgsRelation::strengthToDisplayString( strength ), QVariant::fromValue( strength ) );
60 }
61
62 const Qgis::RelationshipCapabilities capabilities = mConnection->supportedRelationshipCapabilities();
63 if ( !( capabilities & Qgis::RelationshipCapability::ForwardPathLabel ) )
64 {
65 mForwardLabelLineEdit->hide();
66 mForwardLabel->hide();
67 }
68
70 {
71 mBackwardLabelLineEdit->hide();
72 mReverseLabel->hide();
73 }
74
75 const QStringList relatedTableTypes = mConnection->relatedTableTypes();
76 mRelatedTableTypeCombo->addItems( relatedTableTypes );
77
78 mProxyModel = new QSortFilterProxyModel( mTableModel );
79 mProxyModel->setSourceModel( mTableModel );
80 mProxyModel->setDynamicSortFilter( true );
81 mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );
82 mProxyModel->setSortRole( Qt::DisplayRole );
83 mProxyModel->sort( 0 );
84
85 mLeftTableCombo->setModel( mProxyModel );
86 mRightTableCombo->setModel( mProxyModel );
87
88 connect( mNameEdit, &QLineEdit::textChanged, this, [=] {
89 emit validityChanged( isValid() );
90 } );
91 connect( mLeftTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, [=]( int ) {
92 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
93 emit validityChanged( isValid() );
94 } );
95 connect( mRightTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, [=]( int ) {
96 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
97 emit validityChanged( isValid() );
98 } );
99
100 for ( QComboBox *combo :
101 {
102 mCardinalityCombo,
103 qobject_cast<QComboBox *>( mLeftFieldsCombo ),
104 qobject_cast<QComboBox *>( mRightFieldsCombo ),
105 mStrengthCombo,
106 mRelatedTableTypeCombo
107 } )
108 {
109 connect( combo, qOverload<int>( &QComboBox::currentIndexChanged ), this, [=]( int ) {
110 emit validityChanged( isValid() );
111 } );
112 }
113
114 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
115 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
116}
117
119{
120 mRelation = relationship;
121 mNameEdit->setText( mRelation.name() );
122 mNameEdit->setEnabled( false );
123 mStrengthCombo->setCurrentIndex( mStrengthCombo->findData( QVariant::fromValue( mRelation.strength() ) ) );
124
125 QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( mConnection->providerKey(), mRelation.referencedLayerSource() );
126 mLeftTableCombo->setCurrentText( parts.value( QStringLiteral( "layerName" ) ).toString() );
127 parts = QgsProviderRegistry::instance()->decodeUri( mConnection->providerKey(), mRelation.referencingLayerSource() );
128 mRightTableCombo->setCurrentText( parts.value( QStringLiteral( "layerName" ) ).toString() );
129
130 mCardinalityCombo->setCurrentIndex( mCardinalityCombo->findData( QVariant::fromValue( mRelation.cardinality() ) ) );
131 mLeftFieldsCombo->setCurrentText( mRelation.referencedLayerFields().value( 0 ) );
132 mRightFieldsCombo->setCurrentText( mRelation.referencingLayerFields().value( 0 ) );
133 mForwardLabelLineEdit->setText( mRelation.forwardPathLabel() );
134 mBackwardLabelLineEdit->setText( mRelation.backwardPathLabel() );
135 mRelatedTableTypeCombo->setCurrentText( mRelation.relatedTableType() );
136}
137
139{
140 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() );
141 result.setCardinality( mCardinalityCombo->currentData().value<Qgis::RelationshipCardinality>() );
142 result.setReferencedLayerFields( { mLeftFieldsCombo->currentText() } );
143 result.setReferencingLayerFields( { mRightFieldsCombo->currentText() } );
144 result.setForwardPathLabel( mForwardLabelLineEdit->text() );
145 result.setBackwardPathLabel( mBackwardLabelLineEdit->text() );
146
147 result.setMappingTable( mRelation.mappingTable() );
148 result.setMappingReferencedLayerFields( mRelation.mappingReferencedLayerFields() );
149 result.setMappingReferencingLayerFields( mRelation.mappingReferencingLayerFields() );
150
151 if ( mRelatedTableTypeCombo->currentIndex() >= 0 )
152 result.setRelatedTableType( mRelatedTableTypeCombo->currentText() );
153 return result;
154}
155
157{
158 if ( mNameEdit->text().trimmed().isEmpty() )
159 return false;
160
161 if ( mLeftTableCombo->currentText().isEmpty() )
162 return false;
163
164 if ( mRightTableCombo->currentText().isEmpty() )
165 return false;
166
167 if ( mLeftTableCombo->currentText() == mRightTableCombo->currentText() )
168 return false;
169
170 if ( mLeftFieldsCombo->currentText().isEmpty() )
171 return false;
172
173 if ( mRightFieldsCombo->currentText().isEmpty() )
174 return false;
175
176
177 return true;
178}
179
180//
181// QgsDbRelationDialog
182//
183
184QgsDbRelationDialog::QgsDbRelationDialog( QgsAbstractDatabaseProviderConnection *connection, QWidget *parent, Qt::WindowFlags flags )
185 : QDialog( parent, flags )
186{
187 setObjectName( QStringLiteral( "QgsDbRelationDialog" ) );
188
189 QVBoxLayout *vLayout = new QVBoxLayout();
190 mWidget = new QgsDbRelationWidget( connection );
191 vLayout->addWidget( mWidget, 1 );
192
193 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
194 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
195 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
196 vLayout->addWidget( mButtonBox );
197
198 setLayout( vLayout );
199 connect( mWidget, &QgsDbRelationWidget::validityChanged, this, &QgsDbRelationDialog::validityChanged );
200 validityChanged( mWidget->isValid() );
201
203}
204
206{
207 mWidget->setRelationship( relationship );
208}
209
211{
212 return mWidget->relationship();
213}
214
216{
217 if ( !mWidget->isValid() )
218 return;
219
220 QDialog::accept();
221}
222
223void QgsDbRelationDialog::validityChanged( bool isValid )
224{
225 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
226}
RelationshipStrength
Relationship strength.
Definition qgis.h:4153
@ 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:4165
@ 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:4191
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:210
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...
QStringList mappingReferencingLayerFields() const
Returns the list of fields from the mappingTable() involved in the relationship.
QString relatedTableType() const
Returns the type string of the related table.
QString name() const
Returns the relationship's name.
QString backwardPathLabel() const
Returns the label of the backward path for 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.
QStringList referencingLayerFields() const
Returns the list of fields from the referencingLayer() involved in the relationship.
Qgis::RelationshipStrength strength() const
Returns the strength of the relation.