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