QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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#include <QString>
32
33#include "moc_qgsjoindialog.cpp"
34
35using namespace Qt::StringLiterals;
36
37QgsJoinDialog::QgsJoinDialog( QgsVectorLayer *layer, QList<QgsMapLayer *> alreadyJoinedLayers, QWidget *parent, Qt::WindowFlags f )
38 : QDialog( parent, f )
39 , mLayer( layer )
40{
41 setupUi( this );
42 connect( buttonBox, &QDialogButtonBox::helpRequested, this, [] { QgsHelp::openHelp( u"working_with_vector/vector_properties.html#joins-properties"_s ); } );
43
44 if ( !mLayer )
45 {
46 return;
47 }
48 // adds self layer to the joined layer (cannot join to itself)
49 alreadyJoinedLayers.append( layer );
50
51 mTargetFieldComboBox->setLayer( mLayer );
52
53 mDynamicFormCheckBox->setToolTip( tr( "This option allows values of the joined fields to be automatically reloaded when the \"Target Field\" is changed" ) );
54
55 mEditableJoinLayer->setToolTip( tr( "This option allows values of the joined layers to be editable if they're themselves editable" ) );
56 mUpsertOnEditCheckBox->setToolTip( tr( "Automatically adds a matching row to the joined table, but if one already exists then update that matching row instead" ) );
57 mDeleteCascadeCheckBox->setToolTip( tr( "Automatically delete the corresponding feature of the linked layer if one exists" ) );
58
59 mJoinLayerComboBox->setFilters( Qgis::LayerFilter::VectorLayer );
60 mJoinLayerComboBox->setExceptedLayerList( alreadyJoinedLayers );
61 connect( mJoinLayerComboBox, &QgsMapLayerComboBox::layerChanged, mJoinFieldComboBox, &QgsFieldComboBox::setLayer );
62 connect( mJoinLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsJoinDialog::joinedLayerChanged );
63
64 mCacheInMemoryCheckBox->setChecked( true );
65 mCacheEnabled = mCacheInMemoryCheckBox->isChecked();
66
67 QgsMapLayer *joinLayer = mJoinLayerComboBox->currentLayer();
68 if ( joinLayer && joinLayer->isValid() )
69 {
70 mJoinFieldComboBox->setLayer( joinLayer );
71 joinedLayerChanged( joinLayer );
72 }
73
74 connect( mJoinLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsJoinDialog::checkDefinitionValid );
75 connect( mJoinFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsJoinDialog::checkDefinitionValid );
76 connect( mTargetFieldComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsJoinDialog::checkDefinitionValid );
77 connect( mEditableJoinLayer, &QGroupBox::toggled, this, &QgsJoinDialog::editableJoinLayerChanged );
78
79 checkDefinitionValid();
80}
81
83{
84 mJoinLayerComboBox->setLayer( joinInfo.joinLayer() );
85 mJoinFieldComboBox->setField( joinInfo.joinFieldName() );
86 mTargetFieldComboBox->setField( joinInfo.targetFieldName() );
87
88 mCacheEnabled = joinInfo.isUsingMemoryCache();
89 mCacheInMemoryCheckBox->setChecked( joinInfo.isUsingMemoryCache() );
90
91 mDynamicFormCheckBox->setChecked( joinInfo.isDynamicFormEnabled() );
92 mEditableJoinLayer->setChecked( joinInfo.isEditable() );
93 mUpsertOnEditCheckBox->setChecked( joinInfo.hasUpsertOnEdit() );
94 mDeleteCascadeCheckBox->setChecked( joinInfo.hasCascadedDelete() );
95
96 if ( joinInfo.prefix().isNull() )
97 {
98 mUseCustomPrefix->setChecked( false );
99 }
100 else
101 {
102 mUseCustomPrefix->setChecked( true );
103 mCustomPrefix->setText( joinInfo.prefix() );
104 }
105
106 QStringList *lst = joinInfo.joinFieldNamesSubset();
107 mUseJoinFieldsSubset->setChecked( lst && !lst->isEmpty() );
108 QAbstractItemModel *model = mJoinFieldsSubsetView->model();
109 if ( model )
110 {
111 for ( int i = 0; i < model->rowCount(); ++i )
112 {
113 const QModelIndex index = model->index( i, 0 );
114 if ( lst && lst->contains( model->data( index, Qt::DisplayRole ).toString() ) )
115 {
116 model->setData( index, Qt::Checked, Qt::CheckStateRole );
117 }
118 else
119 {
120 model->setData( index, Qt::Unchecked, Qt::CheckStateRole );
121 }
122 }
123 }
124
125 editableJoinLayerChanged();
126}
127
129{
131 info.setJoinLayer( qobject_cast<QgsVectorLayer *>( mJoinLayerComboBox->currentLayer() ) );
132 info.setJoinFieldName( mJoinFieldComboBox->currentField() );
133 info.setTargetFieldName( mTargetFieldComboBox->currentField() );
134 info.setUsingMemoryCache( mCacheInMemoryCheckBox->isChecked() );
135 info.setDynamicFormEnabled( mDynamicFormCheckBox->isChecked() );
136
137 info.setEditable( mEditableJoinLayer->isChecked() );
138 if ( info.isEditable() )
139 {
140 info.setUpsertOnEdit( mUpsertOnEditCheckBox->isChecked() );
141 info.setCascadedDelete( mDeleteCascadeCheckBox->isChecked() );
142 }
143
144 if ( mUseCustomPrefix->isChecked() )
145 info.setPrefix( mCustomPrefix->text() );
146 else
147 info.setPrefix( QString() );
148
149 if ( mUseJoinFieldsSubset->isChecked() )
150 {
151 QStringList lst;
152 QAbstractItemModel *model = mJoinFieldsSubsetView->model();
153 if ( model )
154 {
155 for ( int i = 0; i < model->rowCount(); ++i )
156 {
157 const QModelIndex index = model->index( i, 0 );
158 if ( model->data( index, Qt::CheckStateRole ).toInt() == Qt::Checked )
159 lst << model->data( index ).toString();
160 }
161 }
162 info.setJoinFieldNamesSubset( new QStringList( lst ) );
163 }
164
165 return info;
166}
167
169{
170 return mCreateIndexCheckBox->isChecked();
171}
172
173void QgsJoinDialog::joinedLayerChanged( QgsMapLayer *layer )
174{
175 mJoinFieldComboBox->clear();
176
177 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
178 if ( !vLayer )
179 {
180 return;
181 }
182
183 mUseJoinFieldsSubset->setChecked( false );
184 QStandardItemModel *subsetModel = new QStandardItemModel( this );
185 const QgsFields layerFields = vLayer->fields();
186 for ( const QgsField &field : layerFields )
187 {
188 QStandardItem *subsetItem = new QStandardItem( field.name() );
189 subsetItem->setCheckable( true );
190 //subsetItem->setFlags( subsetItem->flags() | Qt::ItemIsUserCheckable );
191 subsetModel->appendRow( subsetItem );
192 }
193 mJoinFieldsSubsetView->setModel( subsetModel );
194
195 QgsVectorDataProvider *dp = vLayer->dataProvider();
196 const bool canCreateAttrIndex = dp && ( dp->capabilities() & Qgis::VectorProviderCapability::CreateAttributeIndex );
197 if ( canCreateAttrIndex )
198 {
199 mCreateIndexCheckBox->setEnabled( true );
200 }
201 else
202 {
203 mCreateIndexCheckBox->setEnabled( false );
204 mCreateIndexCheckBox->setChecked( false );
205 }
206
207 if ( !mUseCustomPrefix->isChecked() )
208 {
209 mCustomPrefix->setText( layer->name() + '_' );
210 }
211}
212
213void QgsJoinDialog::checkDefinitionValid()
214{
215 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( mJoinLayerComboBox->currentIndex() != -1 && mJoinFieldComboBox->currentIndex() != -1 && mTargetFieldComboBox->currentIndex() != -1 );
216}
217
218void QgsJoinDialog::editableJoinLayerChanged()
219{
220 if ( mEditableJoinLayer->isChecked() )
221 {
222 mCacheInMemoryCheckBox->setEnabled( false );
223 mCacheInMemoryCheckBox->setToolTip( tr( "Caching can not be enabled if editable join layer is enabled" ) );
224 mCacheEnabled = mCacheInMemoryCheckBox->isChecked();
225 mCacheInMemoryCheckBox->setChecked( false );
226 }
227 else
228 {
229 mCacheInMemoryCheckBox->setEnabled( true );
230 mCacheInMemoryCheckBox->setToolTip( QString() );
231 mCacheInMemoryCheckBox->setChecked( mCacheEnabled );
232 }
233}
@ CreateAttributeIndex
Can create indexes on provider's fields.
Definition qgis.h:536
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:41
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:83
QString name
Definition qgsmaplayer.h:87
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.