QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsvaluerelationconfigdlg.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvaluerelationconfigdlg.cpp
3 --------------------------------------
4 Date : 5.1.2014
5 Copyright : (C) 2014 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
20#include "qgsproject.h"
22#include "qgsvectorlayer.h"
23
24#include <QString>
25
26#include "moc_qgsvaluerelationconfigdlg.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QgsEditorConfigWidget( vl, fieldIdx, parent )
32{
33 setupUi( this );
34 mLayerName->setFilters( Qgis::LayerFilter::VectorLayer );
35 mKeyColumn->setLayer( mLayerName->currentLayer() );
36 mValueColumn->setLayer( mLayerName->currentLayer() );
37 mGroupColumn->setLayer( mLayerName->currentLayer() );
38 mGroupColumn->setAllowEmptyFieldName( true );
39 mDescriptionExpression->setLayer( mLayerName->currentLayer() );
40 mOrderByFieldName->setLayer( mLayerName->currentLayer() );
41 mOrderByFieldName->setAllowEmptyFieldName( false );
42 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mKeyColumn, &QgsFieldComboBox::setLayer );
43 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mValueColumn, &QgsFieldComboBox::setLayer );
44 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mGroupColumn, &QgsFieldComboBox::setLayer );
45 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mDescriptionExpression, &QgsFieldExpressionWidget::setLayer );
46 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, this, &QgsValueRelationConfigDlg::layerChanged );
47 connect( mEditExpression, &QAbstractButton::clicked, this, &QgsValueRelationConfigDlg::editExpression );
48 connect( mOrderByField, &QAbstractButton::toggled, mOrderByFieldName, [this]( bool enabled ) {
49 mOrderByFieldName->setEnabled( enabled );
50 } );
51
52 mOrderByGroupBox->setCollapsed( true );
53
54 mNofColumns->setMinimum( 1 );
55 mNofColumns->setMaximum( 10 );
56 mNofColumns->setValue( 1 );
57
59 connect( mKeyColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
60 connect( mValueColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
61 connect( mGroupColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [this]( int index ) {
62 mDisplayGroupName->setEnabled( index != 0 );
63 emit changed();
64 } );
65 connect( mDescriptionExpression, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsEditorConfigWidget::changed );
66 connect( mAllowMulti, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
67 connect( mAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
68 connect( mOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
69 connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
70 connect( mUseCompleter, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
71 connect( mAllowMulti, &QAbstractButton::toggled, this, [this]( bool checked ) {
72 label_nofColumns->setEnabled( checked );
73 mNofColumns->setEnabled( checked );
74 } );
75
76 connect( mUseCompleter, &QCheckBox::stateChanged, this, [this]( int state ) {
77 mCompleterMatchFromStart->setEnabled( static_cast<Qt::CheckState>( state ) == Qt::CheckState::Checked );
78 } );
79
80 mCompleterMatchFromStart->setEnabled( mUseCompleter->isChecked() );
81
82 connect( mNofColumns, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
83
84 layerChanged();
85}
86
88{
89 QVariantMap cfg;
90
91 cfg.insert( u"Layer"_s, mLayerName->currentLayer() ? mLayerName->currentLayer()->id() : QString() );
92 cfg.insert( u"LayerName"_s, mLayerName->currentLayer() ? mLayerName->currentLayer()->name() : QString() );
93 cfg.insert( u"LayerSource"_s, mLayerName->currentLayer() ? mLayerName->currentLayer()->publicSource() : QString() );
94 cfg.insert( u"LayerProviderName"_s, ( mLayerName->currentLayer() && mLayerName->currentLayer()->dataProvider() ) ? mLayerName->currentLayer()->providerType() : QString() );
95 cfg.insert( u"Key"_s, mKeyColumn->currentField() );
96 cfg.insert( u"Value"_s, mValueColumn->currentField() );
97 cfg.insert( u"Group"_s, mGroupColumn->currentField() );
98 cfg.insert( u"DisplayGroupName"_s, mDisplayGroupName->isChecked() );
99 cfg.insert( u"Description"_s, mDescriptionExpression->expression() );
100 cfg.insert( u"AllowMulti"_s, mAllowMulti->isChecked() );
101 cfg.insert( u"NofColumns"_s, mNofColumns->value() );
102 cfg.insert( u"AllowNull"_s, mAllowNull->isChecked() );
103 cfg.insert( u"OrderByValue"_s, mOrderByValue->isChecked() );
104 cfg.insert( u"OrderByKey"_s, mOrderByKey->isChecked() );
105 cfg.insert( u"OrderByField"_s, mOrderByField->isChecked() );
106 cfg.insert( u"OrderByFieldName"_s, mOrderByFieldName->currentField() );
107 cfg.insert( u"OrderByDescending"_s, mOrderByDescending->isChecked() );
108 cfg.insert( u"FilterExpression"_s, mFilterExpression->toPlainText() );
109 cfg.insert( u"UseCompleter"_s, mUseCompleter->isChecked() );
110 const Qt::MatchFlags completerMatchFlags { mCompleterMatchFromStart->isChecked() ? Qt::MatchFlag::MatchStartsWith : Qt::MatchFlag::MatchContains };
111 cfg.insert( u"CompleterMatchFlags"_s, static_cast<int>( completerMatchFlags ) );
112
113 return cfg;
114}
115
117{
119 mLayerName->setLayer( lyr );
120 mOrderByFieldName->setLayer( lyr );
121 mKeyColumn->setField( config.value( u"Key"_s ).toString() );
122 mValueColumn->setField( config.value( u"Value"_s ).toString() );
123 mGroupColumn->setField( config.value( u"Group"_s ).toString() );
124 mDisplayGroupName->setChecked( config.value( u"DisplayGroupName"_s ).toBool() );
125 mDescriptionExpression->setField( config.value( u"Description"_s ).toString() );
126 mAllowMulti->setChecked( config.value( u"AllowMulti"_s ).toBool() );
127 mNofColumns->setValue( config.value( u"NofColumns"_s, 1 ).toInt() );
128 if ( !mAllowMulti->isChecked() )
129 {
130 label_nofColumns->setEnabled( false );
131 mNofColumns->setEnabled( false );
132 }
133 mAllowNull->setChecked( config.value( u"AllowNull"_s ).toBool() );
134 mOrderByValue->setChecked( config.value( u"OrderByValue"_s ).toBool() );
135 mOrderByField->setChecked( config.value( u"OrderByField"_s ).toBool() );
136 mOrderByKey->setChecked( config.value( u"OrderByKey"_s ).toBool() );
137 mOrderByFieldName->setField( config.value( u"OrderByFieldName"_s ).toString() );
138 mOrderByDescending->setChecked( config.value( u"OrderByDescending"_s ).toBool() );
139
140 if ( !mOrderByField->isChecked() && !mOrderByValue->isChecked() && !mOrderByKey->isChecked() )
141 {
142 mOrderByKey->setChecked( true );
143 }
144
145 // order by key is the default, if it is not checked, expand the config
146 if ( !mOrderByKey->isChecked() )
147 {
148 mOrderByGroupBox->setCollapsed( false );
149 }
150
151 mFilterExpression->setPlainText( config.value( u"FilterExpression"_s ).toString() );
152 mUseCompleter->setChecked( config.value( u"UseCompleter"_s ).toBool() );
153 // Default is MatchStartsWith for backwards compatibility
154 const Qt::MatchFlags completerMatchFlags { config.contains( u"CompleterMatchFlags"_s ) ? static_cast<Qt::MatchFlags>( config.value( u"CompleterMatchFlags"_s, Qt::MatchFlag::MatchStartsWith ).toInt() ) : Qt::MatchFlag::MatchStartsWith };
155 mCompleterMatchFromStart->setChecked( completerMatchFlags.testFlag( Qt::MatchFlag::MatchStartsWith ) );
156}
157
158void QgsValueRelationConfigDlg::layerChanged()
159{
160 mFilterExpression->setEnabled( qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() ) );
161 mEditExpression->setEnabled( qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() ) );
162 mOrderByFieldName->setLayer( mLayerName->currentLayer() );
163}
164
166{
167 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() );
168 if ( !vl )
169 return;
170
174
175 context.setHighlightedFunctions( QStringList() << u"current_value"_s << u"current_parent_value"_s );
176 context.setHighlightedVariables( QStringList() << u"current_geometry"_s << u"current_feature"_s << u"form_mode"_s << u"current_parent_geometry"_s << u"current_parent_feature"_s );
177
178 QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, u"generic"_s, context );
179 dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
180
181 if ( dlg.exec() == QDialog::Accepted )
182 {
183 mFilterExpression->setText( dlg.expressionBuilder()->expressionText() );
184 }
185}
QgsEditorConfigWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
Create a new configuration widget.
void changed()
Emitted when the configuration of the widget is changed.
A generic dialog for building expression strings.
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
QString expressionText()
Gets the expression string that has been set in the expression area.
static QgsExpressionContextScope * parentFormScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current parent attribute form/tab...
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
A widget for selection of layer fields or expression creation.
void setLayer(QgsMapLayer *layer)
Sets the layer used to display the fields and expression.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsValueRelationConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
QVariantMap config() override
Create a configuration from the current GUI state.
static QgsVectorLayer * resolveLayer(const QVariantMap &config, const QgsProject *project)
Returns the (possibly nullptr) layer from the widget's config and project.
Represents a vector layer which manages a vector based dataset.