QGIS API Documentation  3.0.2-Girona (307d082)
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  Q_FOREACH ( const QgsRelation &relation, vl->referencingRelations( fieldIdx ) )
38  {
39  if ( relation.name().isEmpty() )
40  mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
41  else
42  mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
43  if ( relation.referencedLayer() )
44  {
45  mExpressionWidget->setField( relation.referencedLayer()->displayExpression() );
46  }
47  }
48 
49  connect( mCbxAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
50  connect( mCbxOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
51  connect( mCbxShowForm, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
52  connect( mCbxShowOpenFormButton, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
53  connect( mCbxMapIdentification, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
54  connect( mCbxReadOnly, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
55  connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
56  connect( mCbxAllowAddFeatures, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
57  connect( mFilterGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
58  connect( mFilterFieldsList, &QListWidget::itemChanged, this, &QgsEditorConfigWidget::changed );
59  connect( mCbxChainFilters, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
60  connect( mExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsEditorConfigWidget::changed );
61 }
62 
64 {
65  mCbxAllowNull->setChecked( config.value( QStringLiteral( "AllowNULL" ), false ).toBool() );
66  mCbxOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ), false ).toBool() );
67  mCbxShowForm->setChecked( config.value( QStringLiteral( "ShowForm" ), false ).toBool() );
68  mCbxShowOpenFormButton->setChecked( config.value( QStringLiteral( "ShowOpenFormButton" ), true ).toBool() );
69 
70  if ( config.contains( QStringLiteral( "Relation" ) ) )
71  {
72  mComboRelation->setCurrentIndex( mComboRelation->findData( config.value( QStringLiteral( "Relation" ) ).toString() ) );
73  relationChanged( mComboRelation->currentIndex() );
74  }
75 
76  mCbxMapIdentification->setChecked( config.value( QStringLiteral( "MapIdentification" ), false ).toBool() );
77  mCbxAllowAddFeatures->setChecked( config.value( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
78  mCbxReadOnly->setChecked( config.value( QStringLiteral( "ReadOnly" ), false ).toBool() );
79 
80  if ( config.contains( QStringLiteral( "FilterFields" ) ) )
81  {
82  mFilterGroupBox->setChecked( true );
83  Q_FOREACH ( const QString &fld, config.value( "FilterFields" ).toStringList() )
84  {
85  addFilterField( fld );
86  }
87 
88  mCbxChainFilters->setChecked( config.value( QStringLiteral( "ChainFilters" ) ).toBool() );
89  }
90 }
91 
92 void QgsRelationReferenceConfigDlg::relationChanged( int idx )
93 {
94  QString relName = mComboRelation->itemData( idx ).toString();
96 
97  mReferencedLayer = rel.referencedLayer();
98  mExpressionWidget->setLayer( mReferencedLayer ); // set even if 0
99  if ( mReferencedLayer )
100  {
101  mExpressionWidget->setField( mReferencedLayer->displayExpression() );
102  mCbxMapIdentification->setEnabled( mReferencedLayer->isSpatial() );
103  }
104 
105  loadFields();
106 }
107 
108 void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
109 {
110  Q_FOREACH ( QListWidgetItem *item, mAvailableFieldsList->selectedItems() )
111  {
112  addFilterField( item );
113  }
114 }
115 
116 void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
117 {
118  Q_FOREACH ( QListWidgetItem *item, mFilterFieldsList->selectedItems() )
119  {
120  mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
121  mAvailableFieldsList->addItem( item );
122  }
123 }
124 
126 {
127  QVariantMap myConfig;
128  myConfig.insert( QStringLiteral( "AllowNULL" ), mCbxAllowNull->isChecked() );
129  myConfig.insert( QStringLiteral( "OrderByValue" ), mCbxOrderByValue->isChecked() );
130  myConfig.insert( QStringLiteral( "ShowForm" ), mCbxShowForm->isChecked() );
131  myConfig.insert( QStringLiteral( "ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
132  myConfig.insert( QStringLiteral( "MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
133  myConfig.insert( QStringLiteral( "ReadOnly" ), mCbxReadOnly->isChecked() );
134  myConfig.insert( QStringLiteral( "Relation" ), mComboRelation->currentData() );
135  myConfig.insert( QStringLiteral( "AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
136 
137  if ( mFilterGroupBox->isChecked() )
138  {
139  QStringList filterFields;
140  filterFields.reserve( mFilterFieldsList->count() );
141  for ( int i = 0; i < mFilterFieldsList->count(); i++ )
142  {
143  filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
144  }
145  myConfig.insert( QStringLiteral( "FilterFields" ), filterFields );
146 
147  myConfig.insert( QStringLiteral( "ChainFilters" ), mCbxChainFilters->isChecked() );
148  }
149 
150  if ( mReferencedLayer )
151  {
152  mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
153  }
154 
155  return myConfig;
156 }
157 
158 void QgsRelationReferenceConfigDlg::loadFields()
159 {
160  mAvailableFieldsList->clear();
161  mFilterFieldsList->clear();
162 
163  if ( mReferencedLayer )
164  {
165  QgsVectorLayer *l = mReferencedLayer;
166  const QgsFields &flds = l->fields();
167  for ( int i = 0; i < flds.count(); i++ )
168  {
169  mAvailableFieldsList->addItem( flds.at( i ).displayName() );
170  mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
171  }
172  }
173 }
174 
175 void QgsRelationReferenceConfigDlg::addFilterField( const QString &field )
176 {
177  for ( int i = 0; i < mAvailableFieldsList->count(); i++ )
178  {
179  if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() == field )
180  {
181  addFilterField( mAvailableFieldsList->item( i ) );
182  break;
183  }
184  }
185 }
186 
187 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
188 {
189  mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
190  mFilterFieldsList->addItem( item );
191 }
192 
193 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
194 {
195  QListWidget *lw = item->listWidget();
196 
197  for ( int i = 0; i < lw->count(); i++ )
198  {
199  if ( lw->item( i ) == item )
200  return i;
201  }
202 
203  return -1;
204 }
QString name
Definition: qgsrelation.h:45
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:57
QString id
Definition: qgsrelation.h:42
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
Return number of items.
Definition: qgsfields.cpp:115
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:145
QgsFields fields() const override
Returns the list of fields of this layer.
QString displayName() const
Returns the name to use when displaying this field.
Definition: qgsfield.cpp:85
QList< QgsRelation > referencingRelations(int idx) const
Get relations, where the foreign key is on this layer.
bool isSpatial() const override
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsVectorLayer referencedLayer
Definition: qgsrelation.h:44
QgsRelationManager relationManager
Definition: qgsproject.h:92
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)
the signal is 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:383
Q_INVOKABLE QgsRelation relation(const QString &id) const
Get access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
QString referencedLayerId() const
Access the referenced (parent) layer&#39;s id.
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)