QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsmaplayerregistry.h"
18 #include "qgsvectorlayer.h"
20 
22  : QgsEditorConfigWidget( vl, fieldIdx, parent )
23 {
24  setupUi( this );
25  mLayerName->setFilters( QgsMapLayerProxyModel::VectorLayer );
26  connect( mLayerName, SIGNAL( layerChanged( QgsMapLayer* ) ), mKeyColumn, SLOT( setLayer( QgsMapLayer* ) ) );
27  connect( mLayerName, SIGNAL( layerChanged( QgsMapLayer* ) ), mValueColumn, SLOT( setLayer( QgsMapLayer* ) ) );
28  connect( mEditExpression, SIGNAL( clicked() ), this, SLOT( editExpression() ) );
29 
30  connect( mLayerName, SIGNAL( layerChanged( QgsMapLayer* ) ), this, SIGNAL( changed() ) );
31  connect( mKeyColumn, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
32  connect( mValueColumn, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
33  connect( mAllowMulti, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
34  connect( mAllowNull, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
35  connect( mOrderByValue, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
36  connect( mFilterExpression, SIGNAL( textChanged() ), this, SIGNAL( changed() ) );
37  connect( mUseCompleter, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
38 }
39 
41 {
43 
44  cfg.insert( "Layer", mLayerName->currentLayer() ? mLayerName->currentLayer()->id() : QString() );
45  cfg.insert( "Key", mKeyColumn->currentField() );
46  cfg.insert( "Value", mValueColumn->currentField() );
47  cfg.insert( "AllowMulti", mAllowMulti->isChecked() );
48  cfg.insert( "AllowNull", mAllowNull->isChecked() );
49  cfg.insert( "OrderByValue", mOrderByValue->isChecked() );
50  cfg.insert( "FilterExpression", mFilterExpression->toPlainText() );
51  cfg.insert( "UseCompleter", mUseCompleter->isChecked() );
52 
53  return cfg;
54 }
55 
57 {
58  QgsVectorLayer* lyr = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( config.value( "Layer" ).toString() ) );
59  mLayerName->setLayer( lyr );
60  mKeyColumn->setField( config.value( "Key" ).toString() );
61  mValueColumn->setField( config.value( "Value" ).toString() );
62  mAllowMulti->setChecked( config.value( "AllowMulti" ).toBool() );
63  mAllowNull->setChecked( config.value( "AllowNull" ).toBool() );
64  mOrderByValue->setChecked( config.value( "OrderByValue" ).toBool() );
65  mFilterExpression->setPlainText( config.value( "FilterExpression" ).toString() );
66  mUseCompleter->setChecked( config.value( "UseCompleter" ).toBool() );
67 }
68 
70 {
71  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer*>( mLayerName->currentLayer() );
72  if ( !vl )
73  return;
74 
75  QgsExpressionContext context;
79 
80  QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, "generic", context );
81  dlg.setWindowTitle( tr( "Edit filter expression" ) );
82 
83  if ( dlg.exec() == QDialog::Accepted )
84  {
85  mFilterExpression->setText( dlg.expressionBuilder()->expressionText() );
86  }
87 }
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setupUi(QWidget *widget)
This class should be subclassed for every configurable editor widget type.
QgsEditorWidgetConfig config() override
Create a configuration from the current GUI state.
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
QString tr(const char *sourceText, const char *disambiguation, int n)
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void changed()
Emitted when the configuration of the widget is changed.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void setWindowTitle(const QString &)
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QgsValueRelationConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
A generic dialog for building expression strings.
void setConfig(const QgsEditorWidgetConfig &config) override
Update the configuration widget to represent the given configuration.