QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
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  mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
157  }
158 
159  return myConfig;
160 }
161 
162 void QgsRelationReferenceConfigDlg::loadFields()
163 {
164  mAvailableFieldsList->clear();
165  mFilterFieldsList->clear();
166 
167  if ( mReferencedLayer )
168  {
169  QgsVectorLayer *l = mReferencedLayer;
170  const QgsFields &flds = l->fields();
171  for ( int i = 0; i < flds.count(); i++ )
172  {
173  mAvailableFieldsList->addItem( flds.at( i ).displayName() );
174  mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
175  }
176  }
177 }
178 
179 void QgsRelationReferenceConfigDlg::addFilterField( const QString &field )
180 {
181  for ( int i = 0; i < mAvailableFieldsList->count(); i++ )
182  {
183  if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() == field )
184  {
185  addFilterField( mAvailableFieldsList->item( i ) );
186  break;
187  }
188  }
189 }
190 
191 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
192 {
193  mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
194  mFilterFieldsList->addItem( item );
195 }
196 
197 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
198 {
199  QListWidget *lw = item->listWidget();
200 
201  for ( int i = 0; i < lw->count(); i++ )
202  {
203  if ( lw->item( i ) == item )
204  return i;
205  }
206 
207  return -1;
208 }
int field()
Returns the field for which this configuration widget applies.
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
This class should be subclassed for every configurable editor widget type.
QString name
Definition: qgsfield.h:58
Container of fields for a vector layer.
Definition: qgsfields.h:42
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString displayName() const
Returns the name to use when displaying this field.
Definition: qgsfield.cpp:87
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer&#39;s relations, where the foreign key is on this layer.
QgsVectorLayer referencedLayer
Definition: qgsrelation.h:47
QgsRelationManager relationManager
Definition: qgsproject.h:100
QString displayExpression
QVariantMap config() override
Create a configuration from the current GUI state.
void changed()
Emitted when the configuration of the widget is changed.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:438
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)