19#include "moc_qgsjoindialog.cpp"
29#include <QStandardItemModel>
33 : QDialog( parent, f )
37 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, [=] {
38 QgsHelp::openHelp( QStringLiteral(
"working_with_vector/vector_properties.html#joins-properties" ) );
46 alreadyJoinedLayers.append( layer );
48 mTargetFieldComboBox->setLayer( mLayer );
50 mDynamicFormCheckBox->setToolTip( tr(
"This option allows values of the joined fields to be automatically reloaded when the \"Target Field\" is changed" ) );
52 mEditableJoinLayer->setToolTip( tr(
"This option allows values of the joined layers to be editable if they're themselves editable" ) );
53 mUpsertOnEditCheckBox->setToolTip( tr(
"Automatically adds a matching row to the joined table, but if one already exists then update that matching row instead" ) );
54 mDeleteCascadeCheckBox->setToolTip( tr(
"Automatically delete the corresponding feature of the linked layer if one exists" ) );
57 mJoinLayerComboBox->setExceptedLayerList( alreadyJoinedLayers );
61 mCacheInMemoryCheckBox->setChecked(
true );
62 mCacheEnabled = mCacheInMemoryCheckBox->isChecked();
64 QgsMapLayer *joinLayer = mJoinLayerComboBox->currentLayer();
65 if ( joinLayer && joinLayer->
isValid() )
67 mJoinFieldComboBox->setLayer( joinLayer );
68 joinedLayerChanged( joinLayer );
74 connect( mEditableJoinLayer, &QGroupBox::toggled,
this, &QgsJoinDialog::editableJoinLayerChanged );
76 checkDefinitionValid();
95 mUseCustomPrefix->setChecked(
false );
99 mUseCustomPrefix->setChecked(
true );
104 mUseJoinFieldsSubset->setChecked( lst && !lst->isEmpty() );
105 QAbstractItemModel *model = mJoinFieldsSubsetView->model();
108 for (
int i = 0; i < model->rowCount(); ++i )
110 const QModelIndex index = model->index( i, 0 );
111 if ( lst && lst->contains( model->data( index, Qt::DisplayRole ).toString() ) )
113 model->setData( index, Qt::Checked, Qt::CheckStateRole );
117 model->setData( index, Qt::Unchecked, Qt::CheckStateRole );
122 editableJoinLayerChanged();
128 info.
setJoinLayer( qobject_cast<QgsVectorLayer *>( mJoinLayerComboBox->currentLayer() ) );
134 info.
setEditable( mEditableJoinLayer->isChecked() );
141 if ( mUseCustomPrefix->isChecked() )
146 if ( mUseJoinFieldsSubset->isChecked() )
149 QAbstractItemModel *model = mJoinFieldsSubsetView->model();
152 for (
int i = 0; i < model->rowCount(); ++i )
154 const QModelIndex index = model->index( i, 0 );
155 if ( model->data( index, Qt::CheckStateRole ).toInt() == Qt::Checked )
156 lst << model->data( index ).toString();
167 return mCreateIndexCheckBox->isChecked();
170void QgsJoinDialog::joinedLayerChanged(
QgsMapLayer *layer )
172 mJoinFieldComboBox->clear();
180 mUseJoinFieldsSubset->setChecked(
false );
181 QStandardItemModel *subsetModel =
new QStandardItemModel(
this );
183 for (
const QgsField &field : layerFields )
185 QStandardItem *subsetItem =
new QStandardItem( field.name() );
186 subsetItem->setCheckable(
true );
188 subsetModel->appendRow( subsetItem );
190 mJoinFieldsSubsetView->setModel( subsetModel );
194 if ( canCreateAttrIndex )
196 mCreateIndexCheckBox->setEnabled(
true );
200 mCreateIndexCheckBox->setEnabled(
false );
201 mCreateIndexCheckBox->setChecked(
false );
204 if ( !mUseCustomPrefix->isChecked() )
206 mCustomPrefix->setText( layer->
name() +
'_' );
210void QgsJoinDialog::checkDefinitionValid()
212 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( mJoinLayerComboBox->currentIndex() != -1 && mJoinFieldComboBox->currentIndex() != -1 && mTargetFieldComboBox->currentIndex() != -1 );
215void QgsJoinDialog::editableJoinLayerChanged()
217 if ( mEditableJoinLayer->isChecked() )
219 mCacheInMemoryCheckBox->setEnabled(
false );
220 mCacheInMemoryCheckBox->setToolTip( tr(
"Caching can not be enabled if editable join layer is enabled" ) );
221 mCacheEnabled = mCacheInMemoryCheckBox->isChecked();
222 mCacheInMemoryCheckBox->setChecked(
false );
226 mCacheInMemoryCheckBox->setEnabled(
true );
227 mCacheInMemoryCheckBox->setToolTip( QString() );
228 mCacheInMemoryCheckBox->setChecked( mCacheEnabled );
@ CreateAttributeIndex
Can create indexes on provider's fields.
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.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
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.
This is the base class for vector data providers.
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...
bool hasCascadedDelete() const
Returns whether a feature deleted on the target layer has to impact the joined layer by deleting the ...
void setUsingMemoryCache(bool enabled)
Sets whether values from the joined layer should be cached in memory to speed up lookups.
bool isDynamicFormEnabled() const
Returns whether the form has to be dynamically updated with joined fields when a feature is being cre...
bool hasUpsertOnEdit() const
Returns whether a feature created on the target layer has to impact the joined layer by creating a ne...
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.
bool isUsingMemoryCache() const
Returns whether values from the joined layer should be cached in memory to speed up lookups.
QString prefix() const
Returns prefix of fields from the joined layer. If nullptr, joined layer's name will be used.
static QStringList joinFieldNamesSubset(const QgsVectorLayerJoinInfo &info, bool blocklisted=true)
Returns the list of field names to use for joining considering blocklisted fields and subset.
void setTargetFieldName(const QString &fieldName)
Sets name of the field of our layer that will be used for join.
QString joinFieldName() const
Returns name of the field of joined 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...
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
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.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet)
void setJoinFieldNamesSubset(QStringList *fieldNamesSubset)
Sets the subset of fields to be used from joined layer.
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.