QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsjoindialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsjoindialog.cpp
3 --------------------
4 begin : July 10, 2010
5 copyright : (C) 2010 by Marco Hugentobler
6 email : marco dot hugentobler at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsjoindialog.h"
19
20#include "qgsfieldcombobox.h"
21#include "qgshelp.h"
22#include "qgsmaplayer.h"
23#include "qgsmaplayercombobox.h"
24#include "qgsproject.h"
26#include "qgsvectorlayer.h"
28
29#include <QPushButton>
30#include <QStandardItemModel>
31
32#include "moc_qgsjoindialog.cpp"
33
34QgsJoinDialog::QgsJoinDialog( QgsVectorLayer *layer, QList<QgsMapLayer *> alreadyJoinedLayers, QWidget *parent, Qt::WindowFlags f )
35 : QDialog( parent, f )
36 , mLayer( layer )
37{
38 setupUi( this );
39 connect( buttonBox, &QDialogButtonBox::helpRequested, this, [] {
40 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#joins-properties" ) );
41 } );
42
43 if ( !mLayer )
44 {
45 return;
46 }
47 // adds self layer to the joined layer (cannot join to itself)
48 alreadyJoinedLayers.append( layer );
49
50 mTargetFieldComboBox->setLayer( mLayer );
51
52 mDynamicFormCheckBox->setToolTip( tr( "This option allows values of the joined fields to be automatically reloaded when the \"Target Field\" is changed" ) );
53
54 mEditableJoinLayer->setToolTip( tr( "This option allows values of the joined layers to be editable if they're themselves editable" ) );
55 mUpsertOnEditCheckBox->setToolTip( tr( "Automatically adds a matching row to the joined table, but if one already exists then update that matching row instead" ) );
56 mDeleteCascadeCheckBox->setToolTip( tr( "Automatically delete the corresponding feature of the linked layer if one exists" ) );
57
58 mJoinLayerComboBox->setFilters( Qgis::LayerFilter::VectorLayer );
59 mJoinLayerComboBox->setExceptedLayerList( alreadyJoinedLayers );
60 connect( mJoinLayerComboBox, &QgsMapLayerComboBox::layerChanged, mJoinFieldComboBox, &QgsFieldComboBox::setLayer );
61 connect( mJoinLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsJoinDialog::joinedLayerChanged );
62
63 mCacheInMemoryCheckBox->setChecked( true );
64 mCacheEnabled = mCacheInMemoryCheckBox->isChecked();
65
66 QgsMapLayer *joinLayer = mJoinLayerComboBox->currentLayer();
67 if ( joinLayer && joinLayer->isValid() )
68 {
69 mJoinFieldComboBox->setLayer( joinLayer );
70 joinedLayerChanged( joinLayer );
71 }
72
73 connect( mJoinLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsJoinDialog::checkDefinitionValid );
74 connect( mJoinFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsJoinDialog::checkDefinitionValid );
75 connect( mTargetFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsJoinDialog::checkDefinitionValid );
76 connect( mEditableJoinLayer, &QGroupBox::toggled, this, &QgsJoinDialog::editableJoinLayerChanged );
77
78 checkDefinitionValid();
79}
80
82{
83 mJoinLayerComboBox->setLayer( joinInfo.joinLayer() );
84 mJoinFieldComboBox->setField( joinInfo.joinFieldName() );
85 mTargetFieldComboBox->setField( joinInfo.targetFieldName() );
86
87 mCacheEnabled = joinInfo.isUsingMemoryCache();
88 mCacheInMemoryCheckBox->setChecked( joinInfo.isUsingMemoryCache() );
89
90 mDynamicFormCheckBox->setChecked( joinInfo.isDynamicFormEnabled() );
91 mEditableJoinLayer->setChecked( joinInfo.isEditable() );
92 mUpsertOnEditCheckBox->setChecked( joinInfo.hasUpsertOnEdit() );
93 mDeleteCascadeCheckBox->setChecked( joinInfo.hasCascadedDelete() );
94
95 if ( joinInfo.prefix().isNull() )
96 {
97 mUseCustomPrefix->setChecked( false );
98 }
99 else
100 {
101 mUseCustomPrefix->setChecked( true );
102 mCustomPrefix->setText( joinInfo.prefix() );
103 }
104
105 QStringList *lst = joinInfo.joinFieldNamesSubset();
106 mUseJoinFieldsSubset->setChecked( lst && !lst->isEmpty() );
107 QAbstractItemModel *model = mJoinFieldsSubsetView->model();
108 if ( model )
109 {
110 for ( int i = 0; i < model->rowCount(); ++i )
111 {
112 const QModelIndex index = model->index( i, 0 );
113 if ( lst && lst->contains( model->data( index, Qt::DisplayRole ).toString() ) )
114 {
115 model->setData( index, Qt::Checked, Qt::CheckStateRole );
116 }
117 else
118 {
119 model->setData( index, Qt::Unchecked, Qt::CheckStateRole );
120 }
121 }
122 }
123
124 editableJoinLayerChanged();
125}
126
128{
130 info.setJoinLayer( qobject_cast<QgsVectorLayer *>( mJoinLayerComboBox->currentLayer() ) );
131 info.setJoinFieldName( mJoinFieldComboBox->currentField() );
132 info.setTargetFieldName( mTargetFieldComboBox->currentField() );
133 info.setUsingMemoryCache( mCacheInMemoryCheckBox->isChecked() );
134 info.setDynamicFormEnabled( mDynamicFormCheckBox->isChecked() );
135
136 info.setEditable( mEditableJoinLayer->isChecked() );
137 if ( info.isEditable() )
138 {
139 info.setUpsertOnEdit( mUpsertOnEditCheckBox->isChecked() );
140 info.setCascadedDelete( mDeleteCascadeCheckBox->isChecked() );
141 }
142
143 if ( mUseCustomPrefix->isChecked() )
144 info.setPrefix( mCustomPrefix->text() );
145 else
146 info.setPrefix( QString() );
147
148 if ( mUseJoinFieldsSubset->isChecked() )
149 {
150 QStringList lst;
151 QAbstractItemModel *model = mJoinFieldsSubsetView->model();
152 if ( model )
153 {
154 for ( int i = 0; i < model->rowCount(); ++i )
155 {
156 const QModelIndex index = model->index( i, 0 );
157 if ( model->data( index, Qt::CheckStateRole ).toInt() == Qt::Checked )
158 lst << model->data( index ).toString();
159 }
160 }
161 info.setJoinFieldNamesSubset( new QStringList( lst ) );
162 }
163
164 return info;
165}
166
168{
169 return mCreateIndexCheckBox->isChecked();
170}
171
172void QgsJoinDialog::joinedLayerChanged( QgsMapLayer *layer )
173{
174 mJoinFieldComboBox->clear();
175
176 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
177 if ( !vLayer )
178 {
179 return;
180 }
181
182 mUseJoinFieldsSubset->setChecked( false );
183 QStandardItemModel *subsetModel = new QStandardItemModel( this );
184 const QgsFields layerFields = vLayer->fields();
185 for ( const QgsField &field : layerFields )
186 {
187 QStandardItem *subsetItem = new QStandardItem( field.name() );
188 subsetItem->setCheckable( true );
189 //subsetItem->setFlags( subsetItem->flags() | Qt::ItemIsUserCheckable );
190 subsetModel->appendRow( subsetItem );
191 }
192 mJoinFieldsSubsetView->setModel( subsetModel );
193
194 QgsVectorDataProvider *dp = vLayer->dataProvider();
195 const bool canCreateAttrIndex = dp && ( dp->capabilities() & Qgis::VectorProviderCapability::CreateAttributeIndex );
196 if ( canCreateAttrIndex )
197 {
198 mCreateIndexCheckBox->setEnabled( true );
199 }
200 else
201 {
202 mCreateIndexCheckBox->setEnabled( false );
203 mCreateIndexCheckBox->setChecked( false );
204 }
205
206 if ( !mUseCustomPrefix->isChecked() )
207 {
208 mCustomPrefix->setText( layer->name() + '_' );
209 }
210}
211
212void QgsJoinDialog::checkDefinitionValid()
213{
214 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( mJoinLayerComboBox->currentIndex() != -1 && mJoinFieldComboBox->currentIndex() != -1 && mTargetFieldComboBox->currentIndex() != -1 );
215}
216
217void QgsJoinDialog::editableJoinLayerChanged()
218{
219 if ( mEditableJoinLayer->isChecked() )
220 {
221 mCacheInMemoryCheckBox->setEnabled( false );
222 mCacheInMemoryCheckBox->setToolTip( tr( "Caching can not be enabled if editable join layer is enabled" ) );
223 mCacheEnabled = mCacheInMemoryCheckBox->isChecked();
224 mCacheInMemoryCheckBox->setChecked( false );
225 }
226 else
227 {
228 mCacheInMemoryCheckBox->setEnabled( true );
229 mCacheInMemoryCheckBox->setToolTip( QString() );
230 mCacheInMemoryCheckBox->setChecked( mCacheEnabled );
231 }
232}
@ CreateAttributeIndex
Can create indexes on provider's fields.
Definition qgis.h:510
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
QgsJoinDialog(QgsVectorLayer *layer, QList< QgsMapLayer * > alreadyJoinedLayers, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
QgsVectorLayerJoinInfo joinInfo() const
Returns the join info.
bool createAttributeIndex() const
Returns true if user wants to create an attribute index on the join field.
void setJoinInfo(const QgsVectorLayerJoinInfo &joinInfo)
Configure the dialog for an existing join.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
Base class for all map layer types.
Definition qgsmaplayer.h:80
QString name
Definition qgsmaplayer.h:84
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
Defines left outer join from our vector layer to some other vector layer.
void setDynamicFormEnabled(bool enabled)
Sets whether the form has to be dynamically updated with joined fields when a feature is being create...
void setUsingMemoryCache(bool enabled)
Sets whether values from the joined layer should be cached in memory to speed up lookups.
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
bool isEditable() const
Returns whether joined fields may be edited through the form of the target layer.
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
void setJoinFieldName(const QString &fieldName)
Sets name of the field of joined layer that will be used for join.
void setTargetFieldName(const QString &fieldName)
Sets name of the field of our layer that will be used for join.
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
void setPrefix(const QString &prefix)
Sets prefix of fields from the joined layer. If nullptr, joined layer's name will be used.
void setJoinLayer(QgsVectorLayer *layer)
Sets weak reference to the joined layer.
void setJoinFieldNamesSubset(QStringList *fieldNamesSubset)
Sets the subset of fields to be used from joined layer.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.