QGIS API Documentation  2.8.6-Wien
qgsrelationreferenceconfigdlg.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationreferenceconfigdlg.cpp
3  --------------------------------------
4  Date : 21.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias dot kuhn at gmx 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 
18 #include "qgseditorwidgetfactory.h"
19 #include "qgsfield.h"
20 #include "qgsproject.h"
21 #include "qgsrelationmanager.h"
22 #include "qgsvectorlayer.h"
24 
26  : QgsEditorConfigWidget( vl, fieldIdx, parent )
27 {
28  setupUi( this );
29  connect( mComboRelation, SIGNAL( currentIndexChanged( int ) ), this, SLOT( relationChanged( int ) ) );
30 
31  foreach ( const QgsRelation& relation, vl->referencingRelations( fieldIdx ) )
32  {
33  mComboRelation->addItem( QString( "%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
34  if ( relation.referencedLayer() )
35  {
36  mExpressionWidget->setField( relation.referencedLayer()->displayExpression() );
37  }
38  }
39 }
40 
41 void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& config )
42 {
43  if ( config.contains( "AllowNULL" ) )
44  {
45  mCbxAllowNull->setChecked( config[ "AllowNULL" ].toBool() );
46  }
47 
48  if ( config.contains( "OrderByValue" ) )
49  {
50  mCbxOrderByValue->setChecked( config[ "OrderByValue" ].toBool() );
51  }
52 
53  if ( config.contains( "ShowForm" ) )
54  {
55  mCbxShowForm->setChecked( config[ "ShowForm" ].toBool() );
56  }
57 
58  if ( config.contains( "Relation" ) )
59  {
60  mComboRelation->setCurrentIndex( mComboRelation->findData( config[ "Relation" ].toString() ) );
61  }
62 
63  if ( config.contains( "MapIdentification" ) )
64  {
65  mCbxMapIdentification->setChecked( config[ "MapIdentification"].toBool() );
66  }
67 
68  if ( config.contains( "ReadOnly" ) )
69  {
70  mCbxReadOnly->setChecked( config[ "ReadOnly"].toBool() );
71  }
72 }
73 
74 void QgsRelationReferenceConfigDlg::relationChanged( int idx )
75 {
76  QString relName = mComboRelation->itemData( idx ).toString();
78 
79  QgsVectorLayer* referencedLayer = rel.referencedLayer();
80  mExpressionWidget->setLayer( referencedLayer ); // set even if 0
81  if ( referencedLayer )
82  {
83  mExpressionWidget->setField( referencedLayer->displayExpression() );
84  mCbxMapIdentification->setEnabled( referencedLayer->hasGeometryType() );
85  }
86 }
87 
89 {
90  QgsEditorWidgetConfig myConfig;
91  myConfig.insert( "AllowNULL", mCbxAllowNull->isChecked() );
92  myConfig.insert( "OrderByValue", mCbxOrderByValue->isChecked() );
93  myConfig.insert( "ShowForm", mCbxShowForm->isChecked() );
94  myConfig.insert( "MapIdentification", mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
95  myConfig.insert( "ReadOnly", mCbxReadOnly->isChecked() );
96  myConfig.insert( "Relation", mComboRelation->itemData( mComboRelation->currentIndex() ) );
97 
98  QString relName = mComboRelation->itemData( mComboRelation->currentIndex() ).toString();
99  QgsRelation relation = QgsProject::instance()->relationManager()->relation( relName );
100 
101  if ( relation.isValid() )
102  {
103  relation.referencedLayer()->setDisplayExpression( mExpressionWidget->currentField() );
104  }
105 
106  return myConfig;
107 }
108 
virtual QgsEditorWidgetConfig config() override
Create a configuration from the current GUI state.
bool isValid() const
Returns the validity of this relation.
This class should be subclassed for every configurable editor widget type.
QgsVectorLayer * referencedLayer() const
Access the referenced (parent) layer.
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
QList< QgsRelation > referencingRelations(int idx)
Get relations, where the foreign key is on this layer.
const QString displayExpression()
Get the preview expression, used to create a human readable preview string.
virtual void setConfig(const QgsEditorWidgetConfig &config) override
Update the configuration widget to represent the given configuration.
QgsRelation relation(const QString &id) const
Get access to a relation by its id.
const QString & id() const
The id.
QString referencedLayerId() const
Access the referenced (parent) layer&#39;s id.
bool hasGeometryType() const
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:351
QMap< QString, QVariant > QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
Represents a vector layer which manages a vector based data sets.
QgsRelationManager * relationManager() const
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)