QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsvaluerelationconfigdlg.cpp"
18#include "qgsproject.h"
19#include "qgsvectorlayer.h"
23
25 : QgsEditorConfigWidget( vl, fieldIdx, parent )
26{
27 setupUi( this );
28 mLayerName->setFilters( Qgis::LayerFilter::VectorLayer );
29 mKeyColumn->setLayer( mLayerName->currentLayer() );
30 mValueColumn->setLayer( mLayerName->currentLayer() );
31 mGroupColumn->setLayer( mLayerName->currentLayer() );
32 mGroupColumn->setAllowEmptyFieldName( true );
33 mDescriptionExpression->setLayer( mLayerName->currentLayer() );
34 mOrderByFieldName->setLayer( mLayerName->currentLayer() );
35 mOrderByFieldName->setAllowEmptyFieldName( false );
36 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mKeyColumn, &QgsFieldComboBox::setLayer );
37 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mValueColumn, &QgsFieldComboBox::setLayer );
38 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mGroupColumn, &QgsFieldComboBox::setLayer );
39 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mDescriptionExpression, &QgsFieldExpressionWidget::setLayer );
40 connect( mLayerName, &QgsMapLayerComboBox::layerChanged, this, &QgsValueRelationConfigDlg::layerChanged );
41 connect( mEditExpression, &QAbstractButton::clicked, this, &QgsValueRelationConfigDlg::editExpression );
42 connect( mOrderByField, &QAbstractButton::toggled, mOrderByFieldName, [ = ]( bool enabled )
43 {
44 mOrderByFieldName->setEnabled( enabled );
45 } );
46
47 mOrderByGroupBox->setCollapsed( true );
48
49 mNofColumns->setMinimum( 1 );
50 mNofColumns->setMaximum( 10 );
51 mNofColumns->setValue( 1 );
52
54 connect( mKeyColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
55 connect( mValueColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
56 connect( mGroupColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
57 {
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, [ = ]( bool checked )
68 {
69 label_nofColumns->setEnabled( checked );
70 mNofColumns->setEnabled( checked );
71 } );
72
73 connect( mUseCompleter, &QCheckBox::stateChanged, this, [ = ]( int state )
74 {
75 mCompleterMatchFromStart->setEnabled( static_cast<Qt::CheckState>( state ) == Qt::CheckState::Checked );
76 } );
77
78 mCompleterMatchFromStart->setEnabled( mUseCompleter->isChecked() );
79
80 connect( mNofColumns, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
81
82 layerChanged();
83}
84
86{
87 QVariantMap cfg;
88
89 cfg.insert( QStringLiteral( "Layer" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->id() : QString() );
90 cfg.insert( QStringLiteral( "LayerName" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->name() : QString() );
91 cfg.insert( QStringLiteral( "LayerSource" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->publicSource() : QString() );
92 cfg.insert( QStringLiteral( "LayerProviderName" ), ( mLayerName->currentLayer() && mLayerName->currentLayer()->dataProvider() ) ?
93 mLayerName->currentLayer()->providerType() :
94 QString() );
95 cfg.insert( QStringLiteral( "Key" ), mKeyColumn->currentField() );
96 cfg.insert( QStringLiteral( "Value" ), mValueColumn->currentField() );
97 cfg.insert( QStringLiteral( "Group" ), mGroupColumn->currentField() );
98 cfg.insert( QStringLiteral( "DisplayGroupName" ), mDisplayGroupName->isChecked() );
99 cfg.insert( QStringLiteral( "Description" ), mDescriptionExpression->expression() );
100 cfg.insert( QStringLiteral( "AllowMulti" ), mAllowMulti->isChecked() );
101 cfg.insert( QStringLiteral( "NofColumns" ), mNofColumns->value() );
102 cfg.insert( QStringLiteral( "AllowNull" ), mAllowNull->isChecked() );
103 cfg.insert( QStringLiteral( "OrderByValue" ), mOrderByValue->isChecked() );
104 cfg.insert( QStringLiteral( "OrderByKey" ), mOrderByKey->isChecked() );
105 cfg.insert( QStringLiteral( "OrderByField" ), mOrderByField->isChecked() );
106 cfg.insert( QStringLiteral( "OrderByFieldName" ), mOrderByFieldName->currentField() );
107 cfg.insert( QStringLiteral( "OrderByDescending" ), mOrderByDescending->isChecked() );
108 cfg.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
109 cfg.insert( QStringLiteral( "UseCompleter" ), mUseCompleter->isChecked() );
110 const Qt::MatchFlags completerMatchFlags { mCompleterMatchFromStart->isChecked() ? Qt::MatchFlag::MatchStartsWith : Qt::MatchFlag::MatchContains };
111 cfg.insert( QStringLiteral( "CompleterMatchFlags" ), static_cast<int>( completerMatchFlags ) );
112
113 return cfg;
114}
115
116void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
117{
119 mLayerName->setLayer( lyr );
120 mOrderByFieldName->setLayer( lyr );
121 mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
122 mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
123 mGroupColumn->setField( config.value( QStringLiteral( "Group" ) ).toString() );
124 mDisplayGroupName->setChecked( config.value( QStringLiteral( "DisplayGroupName" ) ).toBool() );
125 mDescriptionExpression->setField( config.value( QStringLiteral( "Description" ) ).toString() );
126 mAllowMulti->setChecked( config.value( QStringLiteral( "AllowMulti" ) ).toBool() );
127 mNofColumns->setValue( config.value( QStringLiteral( "NofColumns" ), 1 ).toInt() );
128 if ( !mAllowMulti->isChecked() )
129 {
130 label_nofColumns->setEnabled( false );
131 mNofColumns->setEnabled( false );
132 }
133 mAllowNull->setChecked( config.value( QStringLiteral( "AllowNull" ) ).toBool() );
134 mOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ) ).toBool() );
135 mOrderByField->setChecked( config.value( QStringLiteral( "OrderByField" ) ).toBool() );
136 mOrderByKey->setChecked( config.value( QStringLiteral( "OrderByKey" ) ).toBool() );
137 mOrderByFieldName->setField( config.value( QStringLiteral( "OrderByFieldName" ) ).toString() );
138 mOrderByDescending->setChecked( config.value( QStringLiteral( "OrderByDescending" ) ).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( QStringLiteral( "FilterExpression" ) ).toString() );
152 mUseCompleter->setChecked( config.value( QStringLiteral( "UseCompleter" ) ).toBool() );
153 // Default is MatchStartsWith for backwards compatibility
154 const Qt::MatchFlags completerMatchFlags { config.contains( QStringLiteral( "CompleterMatchFlags" ) ) ? static_cast<Qt::MatchFlags>( config.value( QStringLiteral( "CompleterMatchFlags" ), 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() << QStringLiteral( "current_value" ) << QStringLiteral( "current_parent_value" ) );
176 context.setHighlightedVariables( QStringList() << QStringLiteral( "current_geometry" )
177 << QStringLiteral( "current_feature" )
178 << QStringLiteral( "form_mode" )
179 << QStringLiteral( "current_parent_geometry" )
180 << QStringLiteral( "current_parent_feature" ) );
181
182 QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
183 dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
184
185 if ( dlg.exec() == QDialog::Accepted )
186 {
187 mFilterExpression->setText( dlg.expressionBuilder()->expressionText() );
188 }
189}
This class should be subclassed for every configurable editor widget type.
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.
The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains...
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 data sets.