QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "moc_qgsvaluerelationconfigdlg.cpp"
25
27 : QgsEditorConfigWidget( vl, fieldIdx, parent )
28{
29 setupUi( this );
30 mLayerName->setFilters( Qgis::LayerFilter::VectorLayer );
31 mKeyColumn->setLayer( mLayerName->currentLayer() );
32 mValueColumn->setLayer( mLayerName->currentLayer() );
33 mGroupColumn->setLayer( mLayerName->currentLayer() );
34 mGroupColumn->setAllowEmptyFieldName( true );
35 mDescriptionExpression->setLayer( mLayerName->currentLayer() );
36 mOrderByFieldName->setLayer( mLayerName->currentLayer() );
37 mOrderByFieldName->setAllowEmptyFieldName( false );
38 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mKeyColumn, &QgsFieldComboBox::setLayer );
39 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mValueColumn, &QgsFieldComboBox::setLayer );
40 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mGroupColumn, &QgsFieldComboBox::setLayer );
41 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mDescriptionExpression, &QgsFieldExpressionWidget::setLayer );
42 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, this, &QgsValueRelationConfigDlg::layerChanged );
43 connect( mEditExpression, &QAbstractButton::clicked, this, &QgsValueRelationConfigDlg::editExpression );
44 connect( mOrderByField, &QAbstractButton::toggled, mOrderByFieldName, [this]( bool enabled ) {
45 mOrderByFieldName->setEnabled( enabled );
46 } );
47
48 mOrderByGroupBox->setCollapsed( true );
49
50 mNofColumns->setMinimum( 1 );
51 mNofColumns->setMaximum( 10 );
52 mNofColumns->setValue( 1 );
53
55 connect( mKeyColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
56 connect( mValueColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
57 connect( mGroupColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [this]( int index ) {
58 mDisplayGroupName->setEnabled( index != 0 );
59 emit changed();
60 } );
61 connect( mDescriptionExpression, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsEditorConfigWidget::changed );
62 connect( mAllowMulti, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
63 connect( mAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
64 connect( mOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
65 connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
66 connect( mUseCompleter, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
67 connect( mAllowMulti, &QAbstractButton::toggled, this, [this]( bool checked ) {
68 label_nofColumns->setEnabled( checked );
69 mNofColumns->setEnabled( checked );
70 } );
71
72 connect( mUseCompleter, &QCheckBox::stateChanged, this, [this]( int state ) {
73 mCompleterMatchFromStart->setEnabled( static_cast<Qt::CheckState>( state ) == Qt::CheckState::Checked );
74 } );
75
76 mCompleterMatchFromStart->setEnabled( mUseCompleter->isChecked() );
77
78 connect( mNofColumns, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
79
80 layerChanged();
81}
82
84{
85 QVariantMap cfg;
86
87 cfg.insert( QStringLiteral( "Layer" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->id() : QString() );
88 cfg.insert( QStringLiteral( "LayerName" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->name() : QString() );
89 cfg.insert( QStringLiteral( "LayerSource" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->publicSource() : QString() );
90 cfg.insert( QStringLiteral( "LayerProviderName" ), ( mLayerName->currentLayer() && mLayerName->currentLayer()->dataProvider() ) ? mLayerName->currentLayer()->providerType() : QString() );
91 cfg.insert( QStringLiteral( "Key" ), mKeyColumn->currentField() );
92 cfg.insert( QStringLiteral( "Value" ), mValueColumn->currentField() );
93 cfg.insert( QStringLiteral( "Group" ), mGroupColumn->currentField() );
94 cfg.insert( QStringLiteral( "DisplayGroupName" ), mDisplayGroupName->isChecked() );
95 cfg.insert( QStringLiteral( "Description" ), mDescriptionExpression->expression() );
96 cfg.insert( QStringLiteral( "AllowMulti" ), mAllowMulti->isChecked() );
97 cfg.insert( QStringLiteral( "NofColumns" ), mNofColumns->value() );
98 cfg.insert( QStringLiteral( "AllowNull" ), mAllowNull->isChecked() );
99 cfg.insert( QStringLiteral( "OrderByValue" ), mOrderByValue->isChecked() );
100 cfg.insert( QStringLiteral( "OrderByKey" ), mOrderByKey->isChecked() );
101 cfg.insert( QStringLiteral( "OrderByField" ), mOrderByField->isChecked() );
102 cfg.insert( QStringLiteral( "OrderByFieldName" ), mOrderByFieldName->currentField() );
103 cfg.insert( QStringLiteral( "OrderByDescending" ), mOrderByDescending->isChecked() );
104 cfg.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
105 cfg.insert( QStringLiteral( "UseCompleter" ), mUseCompleter->isChecked() );
106 const Qt::MatchFlags completerMatchFlags { mCompleterMatchFromStart->isChecked() ? Qt::MatchFlag::MatchStartsWith : Qt::MatchFlag::MatchContains };
107 cfg.insert( QStringLiteral( "CompleterMatchFlags" ), static_cast<int>( completerMatchFlags ) );
108
109 return cfg;
110}
111
113{
115 mLayerName->setLayer( lyr );
116 mOrderByFieldName->setLayer( lyr );
117 mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
118 mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
119 mGroupColumn->setField( config.value( QStringLiteral( "Group" ) ).toString() );
120 mDisplayGroupName->setChecked( config.value( QStringLiteral( "DisplayGroupName" ) ).toBool() );
121 mDescriptionExpression->setField( config.value( QStringLiteral( "Description" ) ).toString() );
122 mAllowMulti->setChecked( config.value( QStringLiteral( "AllowMulti" ) ).toBool() );
123 mNofColumns->setValue( config.value( QStringLiteral( "NofColumns" ), 1 ).toInt() );
124 if ( !mAllowMulti->isChecked() )
125 {
126 label_nofColumns->setEnabled( false );
127 mNofColumns->setEnabled( false );
128 }
129 mAllowNull->setChecked( config.value( QStringLiteral( "AllowNull" ) ).toBool() );
130 mOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ) ).toBool() );
131 mOrderByField->setChecked( config.value( QStringLiteral( "OrderByField" ) ).toBool() );
132 mOrderByKey->setChecked( config.value( QStringLiteral( "OrderByKey" ) ).toBool() );
133 mOrderByFieldName->setField( config.value( QStringLiteral( "OrderByFieldName" ) ).toString() );
134 mOrderByDescending->setChecked( config.value( QStringLiteral( "OrderByDescending" ) ).toBool() );
135
136 if ( !mOrderByField->isChecked() && !mOrderByValue->isChecked() && !mOrderByKey->isChecked() )
137 {
138 mOrderByKey->setChecked( true );
139 }
140
141 // order by key is the default, if it is not checked, expand the config
142 if ( !mOrderByKey->isChecked() )
143 {
144 mOrderByGroupBox->setCollapsed( false );
145 }
146
147 mFilterExpression->setPlainText( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
148 mUseCompleter->setChecked( config.value( QStringLiteral( "UseCompleter" ) ).toBool() );
149 // Default is MatchStartsWith for backwards compatibility
150 const Qt::MatchFlags completerMatchFlags { config.contains( QStringLiteral( "CompleterMatchFlags" ) ) ? static_cast<Qt::MatchFlags>( config.value( QStringLiteral( "CompleterMatchFlags" ), Qt::MatchFlag::MatchStartsWith ).toInt() ) : Qt::MatchFlag::MatchStartsWith };
151 mCompleterMatchFromStart->setChecked( completerMatchFlags.testFlag( Qt::MatchFlag::MatchStartsWith ) );
152}
153
154void QgsValueRelationConfigDlg::layerChanged()
155{
156 mFilterExpression->setEnabled( qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() ) );
157 mEditExpression->setEnabled( qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() ) );
158 mOrderByFieldName->setLayer( mLayerName->currentLayer() );
159}
160
162{
163 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() );
164 if ( !vl )
165 return;
166
170
171 context.setHighlightedFunctions( QStringList() << QStringLiteral( "current_value" ) << QStringLiteral( "current_parent_value" ) );
172 context.setHighlightedVariables( QStringList() << QStringLiteral( "current_geometry" ) << QStringLiteral( "current_feature" ) << QStringLiteral( "form_mode" ) << QStringLiteral( "current_parent_geometry" ) << QStringLiteral( "current_parent_feature" ) );
173
174 QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
175 dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
176
177 if ( dlg.exec() == QDialog::Accepted )
178 {
179 mFilterExpression->setText( dlg.expressionBuilder()->expressionText() );
180 }
181}
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.