QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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( QgsMapLayerProxyModel::VectorLayer );
28  connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mKeyColumn, &QgsFieldComboBox::setLayer );
29  connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mValueColumn, &QgsFieldComboBox::setLayer );
30  connect( mEditExpression, &QAbstractButton::clicked, this, &QgsValueRelationConfigDlg::editExpression );
31 
32  mNofColumns->setMinimum( 1 );
33  mNofColumns->setMaximum( 10 );
34  mNofColumns->setValue( 1 );
35 
37  connect( mKeyColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
38  connect( mValueColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
39  connect( mAllowMulti, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
40  connect( mAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
41  connect( mOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
42  connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
43  connect( mUseCompleter, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
44  connect( mAllowMulti, &QAbstractButton::toggled, this, [ = ]( bool checked )
45  {
46  label_nofColumns->setEnabled( checked );
47  mNofColumns->setEnabled( checked );
48  }
49  );
50 
51  connect( mNofColumns, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
52 }
53 
55 {
56  QVariantMap cfg;
57 
58  cfg.insert( QStringLiteral( "Layer" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->id() : QString() );
59  cfg.insert( QStringLiteral( "LayerName" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->name() : QString() );
60  cfg.insert( QStringLiteral( "LayerSource" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->publicSource() : QString() );
61  cfg.insert( QStringLiteral( "LayerProviderName" ), ( mLayerName->currentLayer() && mLayerName->currentLayer()->dataProvider() ) ?
62  mLayerName->currentLayer()->dataProvider()->name() :
63  QString() );
64  cfg.insert( QStringLiteral( "Key" ), mKeyColumn->currentField() );
65  cfg.insert( QStringLiteral( "Value" ), mValueColumn->currentField() );
66  cfg.insert( QStringLiteral( "AllowMulti" ), mAllowMulti->isChecked() );
67  cfg.insert( QStringLiteral( "NofColumns" ), mNofColumns->value() );
68  cfg.insert( QStringLiteral( "AllowNull" ), mAllowNull->isChecked() );
69  cfg.insert( QStringLiteral( "OrderByValue" ), mOrderByValue->isChecked() );
70  cfg.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
71  cfg.insert( QStringLiteral( "UseCompleter" ), mUseCompleter->isChecked() );
72 
73  return cfg;
74 }
75 
76 void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
77 {
79  mLayerName->setLayer( lyr );
80  mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
81  mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
82  mAllowMulti->setChecked( config.value( QStringLiteral( "AllowMulti" ) ).toBool() );
83  mNofColumns->setValue( config.value( QStringLiteral( "NofColumns" ), 1 ).toInt() );
84  if ( !mAllowMulti->isChecked() )
85  {
86  label_nofColumns->setEnabled( false );
87  mNofColumns->setEnabled( false );
88  }
89  mAllowNull->setChecked( config.value( QStringLiteral( "AllowNull" ) ).toBool() );
90  mOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ) ).toBool() );
91  mFilterExpression->setPlainText( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
92  mUseCompleter->setChecked( config.value( QStringLiteral( "UseCompleter" ) ).toBool() );
93 }
94 
96 {
97  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() );
98  if ( !vl )
99  return;
100 
103 
104  context.setHighlightedFunctions( QStringList() << QStringLiteral( "current_value" ) );
105  context.setHighlightedVariables( QStringList() << QStringLiteral( "current_geometry" )
106  << QStringLiteral( "current_feature" )
107  << QStringLiteral( "form_mode" ) );
108 
109  QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
110  dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
111 
112  if ( dlg.exec() == QDialog::Accepted )
113  {
114  mFilterExpression->setText( dlg.expressionBuilder()->expressionText() );
115  }
116 }
This class should be subclassed for every configurable editor widget type.
QVariantMap config() override
Create a configuration from the current GUI state.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
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 feat...
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QgsVectorLayer * resolveLayer(const QVariantMap &config, const QgsProject *project)
Returns the (possibly NULL) layer from the widget&#39;s config and project.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer&#39;s project and layer.
void changed()
Emitted when the configuration of the widget is changed.
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:438
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
QgsValueRelationConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Represents a vector layer which manages a vector based data sets.
A generic dialog for building expression strings.