QGIS API Documentation  3.0.2-Girona (307d082)
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"
20 
22  : QgsEditorConfigWidget( vl, fieldIdx, parent )
23 {
24  setupUi( this );
25  mLayerName->setFilters( QgsMapLayerProxyModel::VectorLayer );
26  connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mKeyColumn, &QgsFieldComboBox::setLayer );
27  connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mValueColumn, &QgsFieldComboBox::setLayer );
28  connect( mEditExpression, &QAbstractButton::clicked, this, &QgsValueRelationConfigDlg::editExpression );
29 
31  connect( mKeyColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
32  connect( mValueColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
33  connect( mAllowMulti, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
34  connect( mAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
35  connect( mOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
36  connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
37  connect( mUseCompleter, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
38 }
39 
41 {
42  QVariantMap cfg;
43 
44  cfg.insert( QStringLiteral( "Layer" ), mLayerName->currentLayer() ? mLayerName->currentLayer()->id() : QString() );
45  cfg.insert( QStringLiteral( "Key" ), mKeyColumn->currentField() );
46  cfg.insert( QStringLiteral( "Value" ), mValueColumn->currentField() );
47  cfg.insert( QStringLiteral( "AllowMulti" ), mAllowMulti->isChecked() );
48  cfg.insert( QStringLiteral( "AllowNull" ), mAllowNull->isChecked() );
49  cfg.insert( QStringLiteral( "OrderByValue" ), mOrderByValue->isChecked() );
50  cfg.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
51  cfg.insert( QStringLiteral( "UseCompleter" ), mUseCompleter->isChecked() );
52 
53  return cfg;
54 }
55 
56 void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
57 {
58  QgsVectorLayer *lyr = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) );
59  mLayerName->setLayer( lyr );
60  mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
61  mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
62  mAllowMulti->setChecked( config.value( QStringLiteral( "AllowMulti" ) ).toBool() );
63  mAllowNull->setChecked( config.value( QStringLiteral( "AllowNull" ) ).toBool() );
64  mOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ) ).toBool() );
65  mFilterExpression->setPlainText( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
66  mUseCompleter->setChecked( config.value( QStringLiteral( "UseCompleter" ) ).toBool() );
67 }
68 
70 {
71  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerName->currentLayer() );
72  if ( !vl )
73  return;
74 
76 
77  QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
78  dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
79 
80  if ( dlg.exec() == QDialog::Accepted )
81  {
82  mFilterExpression->setText( dlg.expressionBuilder()->expressionText() );
83  }
84 }
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.
void layerChanged(QgsMapLayer *layer)
layerChanged this signal is emitted whenever the currently selected layer changes ...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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:383
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
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.