QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsrelationreferenceconfigdlg.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationreferenceconfigdlg.cpp
3  --------------------------------------
4  Date : 21.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias at opengis dot ch
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 
18 #include "qgseditorwidgetfactory.h"
19 #include "qgsfields.h"
20 #include "qgsproject.h"
21 #include "qgsrelationmanager.h"
22 #include "qgsvectorlayer.h"
24 
26  : QgsEditorConfigWidget( vl, fieldIdx, parent )
27 
28 {
29  setupUi( this );
30  connect( mAddFilterButton, &QToolButton::clicked, this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
31  connect( mRemoveFilterButton, &QToolButton::clicked, this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
32 
33  mExpressionWidget->registerExpressionContextGenerator( vl );
34 
35  connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRelationReferenceConfigDlg::relationChanged );
36 
37  const auto constReferencingRelations = vl->referencingRelations( fieldIdx );
38  for ( const QgsRelation &relation : constReferencingRelations )
39  {
40  if ( relation.name().isEmpty() )
41  mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
42  else
43  mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
44  if ( relation.referencedLayer() )
45  {
46  mExpressionWidget->setField( relation.referencedLayer()->displayExpression() );
47  }
48  }
49 
50  connect( mCbxAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
51  connect( mCbxOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
52  connect( mCbxShowForm, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
53  connect( mCbxShowOpenFormButton, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
54  connect( mCbxMapIdentification, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
55  connect( mCbxReadOnly, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
56  connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
57  connect( mCbxAllowAddFeatures, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
58  connect( mFilterGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
59  connect( mFilterFieldsList, &QListWidget::itemChanged, this, &QgsEditorConfigWidget::changed );
60  connect( mCbxChainFilters, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
61  connect( mExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsEditorConfigWidget::changed );
62 }
63 
64 void QgsRelationReferenceConfigDlg::setConfig( const QVariantMap &config )
65 {
66  mCbxAllowNull->setChecked( config.value( QStringLiteral( "AllowNULL" ), false ).toBool() );
67  mCbxOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ), false ).toBool() );
68  mCbxShowForm->setChecked( config.value( QStringLiteral( "ShowForm" ), false ).toBool() );
69  mCbxShowOpenFormButton->setChecked( config.value( QStringLiteral( "ShowOpenFormButton" ), true ).toBool() );
70 
71  if ( config.contains( QStringLiteral( "Relation" ) ) )
72  {
73  mComboRelation->setCurrentIndex( mComboRelation->findData( config.value( QStringLiteral( "Relation" ) ).toString() ) );
74  relationChanged( mComboRelation->currentIndex() );
75  }
76 
77  mCbxMapIdentification->setChecked( config.value( QStringLiteral( "MapIdentification" ), false ).toBool() );
78  mCbxAllowAddFeatures->setChecked( config.value( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
79  mCbxReadOnly->setChecked( config.value( QStringLiteral( "ReadOnly" ), false ).toBool() );
80 
81  if ( config.contains( QStringLiteral( "FilterFields" ) ) )
82  {
83  mFilterGroupBox->setChecked( true );
84  const auto constToStringList = config.value( "FilterFields" ).toStringList();
85  for ( const QString &fld : constToStringList )
86  {
87  addFilterField( fld );
88  }
89 
90  mCbxChainFilters->setChecked( config.value( QStringLiteral( "ChainFilters" ) ).toBool() );
91  }
92 }
93 
94 void QgsRelationReferenceConfigDlg::relationChanged( int idx )
95 {
96  QString relName = mComboRelation->itemData( idx ).toString();
98 
99  mReferencedLayer = rel.referencedLayer();
100  mExpressionWidget->setLayer( mReferencedLayer ); // set even if 0
101  if ( mReferencedLayer )
102  {
103  mExpressionWidget->setField( mReferencedLayer->displayExpression() );
104  mCbxMapIdentification->setEnabled( mReferencedLayer->isSpatial() );
105  }
106 
107  loadFields();
108 }
109 
110 void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
111 {
112  const auto constSelectedItems = mAvailableFieldsList->selectedItems();
113  for ( QListWidgetItem *item : constSelectedItems )
114  {
115  addFilterField( item );
116  }
117 }
118 
119 void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
120 {
121  const auto constSelectedItems = mFilterFieldsList->selectedItems();
122  for ( QListWidgetItem *item : constSelectedItems )
123  {
124  mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
125  mAvailableFieldsList->addItem( item );
126  }
127 }
128 
130 {
131  QVariantMap myConfig;
132  myConfig.insert( QStringLiteral( "AllowNULL" ), mCbxAllowNull->isChecked() );
133  myConfig.insert( QStringLiteral( "OrderByValue" ), mCbxOrderByValue->isChecked() );
134  myConfig.insert( QStringLiteral( "ShowForm" ), mCbxShowForm->isChecked() );
135  myConfig.insert( QStringLiteral( "ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
136  myConfig.insert( QStringLiteral( "MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
137  myConfig.insert( QStringLiteral( "ReadOnly" ), mCbxReadOnly->isChecked() );
138  myConfig.insert( QStringLiteral( "Relation" ), mComboRelation->currentData() );
139  myConfig.insert( QStringLiteral( "AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
140 
141  if ( mFilterGroupBox->isChecked() )
142  {
143  QStringList filterFields;
144  filterFields.reserve( mFilterFieldsList->count() );
145  for ( int i = 0; i < mFilterFieldsList->count(); i++ )
146  {
147  filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
148  }
149  myConfig.insert( QStringLiteral( "FilterFields" ), filterFields );
150 
151  myConfig.insert( QStringLiteral( "ChainFilters" ), mCbxChainFilters->isChecked() );
152  }
153 
154  if ( mReferencedLayer )
155  {
156  // Store referenced layer data source and provider
157  myConfig.insert( QStringLiteral( "ReferencedLayerDataSource" ), mReferencedLayer->publicSource() );
158  myConfig.insert( QStringLiteral( "ReferencedLayerProviderKey" ), mReferencedLayer->providerType() );
159  myConfig.insert( QStringLiteral( "ReferencedLayerId" ), mReferencedLayer->id() );
160  myConfig.insert( QStringLiteral( "ReferencedLayerName" ), mReferencedLayer->name() );
161  mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
162  }
163 
164  return myConfig;
165 }
166 
167 void QgsRelationReferenceConfigDlg::loadFields()
168 {
169  mAvailableFieldsList->clear();
170  mFilterFieldsList->clear();
171 
172  if ( mReferencedLayer )
173  {
174  QgsVectorLayer *l = mReferencedLayer;
175  const QgsFields &flds = l->fields();
176  for ( int i = 0; i < flds.count(); i++ )
177  {
178  mAvailableFieldsList->addItem( flds.at( i ).displayName() );
179  mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
180  }
181  }
182 }
183 
184 void QgsRelationReferenceConfigDlg::addFilterField( const QString &field )
185 {
186  for ( int i = 0; i < mAvailableFieldsList->count(); i++ )
187  {
188  if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() == field )
189  {
190  addFilterField( mAvailableFieldsList->item( i ) );
191  break;
192  }
193  }
194 }
195 
196 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
197 {
198  mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
199  mFilterFieldsList->addItem( item );
200 }
201 
202 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
203 {
204  QListWidget *lw = item->listWidget();
205 
206  for ( int i = 0; i < lw->count(); i++ )
207  {
208  if ( lw->item( i ) == item )
209  return i;
210  }
211 
212  return -1;
213 }
QgsProject::relationManager
QgsRelationManager relationManager
Definition: qgsproject.h:103
qgsfields.h
QgsEditorConfigWidget
Definition: qgseditorconfigwidget.h:38
QgsFields::count
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsFields
Definition: qgsfields.h:44
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:458
QgsRelationManager::relation
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Definition: qgsrelationmanager.cpp:96
QgsVectorLayer::isSpatial
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Definition: qgsvectorlayer.cpp:3591
QgsField::name
QString name
Definition: qgsfield.h:59
QgsMapLayer::providerType
QString providerType() const
Returns the provider type (provider key) for this layer.
Definition: qgsmaplayer.cpp:1614
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsEditorConfigWidget::field
int field()
Returns the field for which this configuration widget applies.
Definition: qgseditorconfigwidget.cpp:28
QgsRelation::referencedLayer
QgsVectorLayer referencedLayer
Definition: qgsrelation.h:47
QgsFieldExpressionWidget::fieldChanged
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
QgsVectorLayer::referencingRelations
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer's relations, where the foreign key is on this layer.
Definition: qgsvectorlayer.cpp:5158
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsRelationReferenceConfigDlg::setConfig
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
Definition: qgsrelationreferenceconfigdlg.cpp:64
QgsVectorLayer::setDisplayExpression
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
Definition: qgsvectorlayer.cpp:3525
qgsrelationmanager.h
qgsvectorlayer.h
QgsMapLayer::publicSource
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
Definition: qgsmaplayer.cpp:184
QgsVectorLayer
Definition: qgsvectorlayer.h:385
qgseditorwidgetfactory.h
QgsRelationReferenceConfigDlg::config
QVariantMap config() override
Create a configuration from the current GUI state.
Definition: qgsrelationreferenceconfigdlg.cpp:129
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:85
QgsRelation
Definition: qgsrelation.h:41
QgsEditorConfigWidget::changed
void changed()
Emitted when the configuration of the widget is changed.
QgsField::displayName
QString displayName() const
Returns the name to use when displaying this field.
Definition: qgsfield.cpp:89
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsVectorLayer::displayExpression
QString displayExpression
Definition: qgsvectorlayer.h:390
QgsFieldExpressionWidget
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
Definition: qgsfieldexpressionwidget.h:46
qgsrelationreferenceconfigdlg.h
qgsproject.h
QgsRelationReferenceConfigDlg::QgsRelationReferenceConfigDlg
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
Definition: qgsrelationreferenceconfigdlg.cpp:25
qgsexpressionbuilderdialog.h