QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
19#include "qgsgui.h"
20#include "qgsproviderregistry.h"
21
22#include <QDialogButtonBox>
23#include <QPushButton>
24#include <QSortFilterProxyModel>
25
26#include "moc_qgsdbrelationshipwidget.cpp"
27
28//
29// QgsDbRelationWidget
30//
31
33 : QWidget( parent )
34 , mConnection( connection )
35{
36 setupUi( this );
37
38 // takes ownership of connection
39 mTableModel = new QgsDatabaseTableModel( connection, QString(), this );
40
41 const QList<Qgis::RelationshipCardinality> cardinalities = mConnection->supportedRelationshipCardinalities();
42 for ( Qgis::RelationshipCardinality cardinality :
43 {
48 } )
49 {
50 if ( cardinalities.contains( cardinality ) )
51 mCardinalityCombo->addItem( QgsRelation::cardinalityToDisplayString( cardinality ), QVariant::fromValue( cardinality ) );
52 }
53
54 const QList<Qgis::RelationshipStrength> strengths = mConnection->supportedRelationshipStrengths();
55 for ( Qgis::RelationshipStrength strength :
56 {
59 } )
60 {
61 if ( strengths.contains( strength ) )
62 mStrengthCombo->addItem( QgsRelation::strengthToDisplayString( strength ), QVariant::fromValue( strength ) );
63 }
64
65 const Qgis::RelationshipCapabilities capabilities = mConnection->supportedRelationshipCapabilities();
66 if ( !( capabilities & Qgis::RelationshipCapability::ForwardPathLabel ) )
67 {
68 mForwardLabelLineEdit->hide();
69 mForwardLabel->hide();
70 }
71
73 {
74 mBackwardLabelLineEdit->hide();
75 mReverseLabel->hide();
76 }
77
78 const QStringList relatedTableTypes = mConnection->relatedTableTypes();
79 mRelatedTableTypeCombo->addItems( relatedTableTypes );
80
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 );
87
88 mLeftTableCombo->setModel( mProxyModel );
89 mRightTableCombo->setModel( mProxyModel );
90
91 connect( mNameEdit, &QLineEdit::textChanged, this, [this] {
92 emit validityChanged( isValid() );
93 } );
94 connect( mLeftTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
95 mLeftFieldsCombo->setFields( mConnection->fields( QString(), mLeftTableCombo->currentText() ) );
96 emit validityChanged( isValid() );
97 } );
98 connect( mRightTableCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
99 mRightFieldsCombo->setFields( mConnection->fields( QString(), mRightTableCombo->currentText() ) );
100 emit validityChanged( isValid() );
101 } );
102
103 for ( QComboBox *combo :
104 {
105 mCardinalityCombo,
106 qobject_cast<QComboBox *>( mLeftFieldsCombo ),
107 qobject_cast<QComboBox *>( mRightFieldsCombo ),
108 mStrengthCombo,
109 mRelatedTableTypeCombo
110 } )
111 {
112 connect( combo, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
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(), 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() );
144 result.setCardinality( mCardinalityCombo->currentData().value<Qgis::RelationshipCardinality>() );
145 result.setReferencedLayerFields( { mLeftFieldsCombo->currentText() } );
146 result.setReferencingLayerFields( { mRightFieldsCombo->currentText() } );
147 result.setForwardPathLabel( mForwardLabelLineEdit->text() );
148 result.setBackwardPathLabel( mBackwardLabelLineEdit->text() );
149
150 result.setMappingTable( mRelation.mappingTable() );
151 result.setMappingReferencedLayerFields( mRelation.mappingReferencedLayerFields() );
152 result.setMappingReferencingLayerFields( mRelation.mappingReferencingLayerFields() );
153
154 if ( mRelatedTableTypeCombo->currentIndex() >= 0 )
155 result.setRelatedTableType( mRelatedTableTypeCombo->currentText() );
156 return result;
157}
158
160{
161 if ( mNameEdit->text().trimmed().isEmpty() )
162 return false;
163
164 if ( mLeftTableCombo->currentText().isEmpty() )
165 return false;
166
167 if ( mRightTableCombo->currentText().isEmpty() )
168 return false;
169
170 if ( mLeftTableCombo->currentText() == mRightTableCombo->currentText() )
171 return false;
172
173 if ( mLeftFieldsCombo->currentText().isEmpty() )
174 return false;
175
176 if ( mRightFieldsCombo->currentText().isEmpty() )
177 return false;
178
179
180 return true;
181}
182
183//
184// QgsDbRelationDialog
185//
186
187QgsDbRelationDialog::QgsDbRelationDialog( QgsAbstractDatabaseProviderConnection *connection, QWidget *parent, Qt::WindowFlags flags )
188 : QDialog( parent, flags )
189{
190 setObjectName( QStringLiteral( "QgsDbRelationDialog" ) );
191
192 QVBoxLayout *vLayout = new QVBoxLayout();
193 mWidget = new QgsDbRelationWidget( connection );
194 vLayout->addWidget( mWidget, 1 );
195
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 );
200
201 setLayout( vLayout );
202 connect( mWidget, &QgsDbRelationWidget::validityChanged, this, &QgsDbRelationDialog::validityChanged );
203 validityChanged( mWidget->isValid() );
204
206}
207
209{
210 mWidget->setRelationship( relationship );
211}
212
214{
215 return mWidget->relationship();
216}
217
219{
220 if ( !mWidget->isValid() )
221 return;
222
223 QDialog::accept();
224}
225
226void QgsDbRelationDialog::validityChanged( bool isValid )
227{
228 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
229}
RelationshipStrength
Relationship strength.
Definition qgis.h:4405
@ Composition
Fix relation, related elements are part of the parent and a parent copy will copy any children or del...
Definition qgis.h:4407
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition qgis.h:4406
@ BackwardPathLabel
Supports backward path labels.
Definition qgis.h:4434
@ ForwardPathLabel
Supports forward path labels.
Definition qgis.h:4433
RelationshipCardinality
Relationship cardinality.
Definition qgis.h:4417
@ ManyToMany
Many to many relationship.
Definition qgis.h:4421
@ ManyToOne
Many to one relationship.
Definition qgis.h:4420
@ OneToOne
One to one relationship.
Definition qgis.h:4418
@ OneToMany
One to many relationship.
Definition qgis.h:4419
QFlags< RelationshipCapability > RelationshipCapabilities
Relationship capabilities.
Definition qgis.h:4443
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.
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:221
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.