QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsvectorlayerjoininfo.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerjoininfo.cpp
3  --------------------------
4  begin : Jun 29, 2017
5  copyright : (C) 2017 by Paul Blottiere
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsvectorlayerjoininfo.h"
19 
21 {
22  QString name;
23 
24  if ( joinLayer() )
25  {
26  if ( prefix().isNull() )
27  name = joinLayer()->name() + '_';
28  else
29  name = prefix();
30 
31  name += f.name();
32  }
33 
34  return name;
35 }
36 
38 {
39  mEditable = enabled;
40 
41  if ( ! mEditable )
42  {
43  setCascadedDelete( false );
44  setUpsertOnEdit( false );
45  }
46 }
47 
49 {
50  QgsFeature joinFeature;
51 
52  if ( joinLayer() )
53  {
54  const QVariant idFieldValue = feature.attribute( targetFieldName() );
55  joinFeature.initAttributes( joinLayer()->fields().count() );
56  joinFeature.setFields( joinLayer()->fields() );
57  joinFeature.setAttribute( joinFieldName(), idFieldValue );
58 
59  const QgsFields joinFields = joinFeature.fields();
60  for ( const auto &field : joinFields )
61  {
62  const QString prefixedName = prefixedFieldName( field );
63 
64  if ( feature.fieldNameIndex( prefixedName ) != -1 )
65  joinFeature.setAttribute( field.name(), feature.attribute( prefixedName ) );
66  }
67  }
68 
69  return joinFeature;
70 }
71 
72 QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blacklisted )
73 {
74  QStringList fieldNames;
75 
76  if ( blacklisted && !info.joinFieldNamesBlackList().isEmpty() )
77  {
78  QStringList *lst = info.joinFieldNamesSubset();
79  if ( lst )
80  {
81  for ( const QString &s : qgis::as_const( *lst ) )
82  {
83  if ( !info.joinFieldNamesBlackList().contains( s ) )
84  fieldNames.append( s );
85  }
86  }
87  else
88  {
89  for ( const QgsField &f : info.joinLayer()->fields() )
90  {
91  if ( !info.joinFieldNamesBlackList().contains( f.name() )
92  && f.name() != info.joinFieldName() )
93  fieldNames.append( f.name() );
94  }
95  }
96  }
97  else
98  {
99  QStringList *lst = info.joinFieldNamesSubset();
100  if ( lst )
101  {
102  fieldNames = *lst;
103  }
104  }
105 
106  return fieldNames;
107 }
108 
109 bool QgsVectorLayerJoinInfo::hasSubset( bool blacklisted ) const
110 {
111  bool subset = joinFieldNamesSubset();
112 
113  if ( blacklisted )
114  subset |= !joinFieldNamesBlackList().isEmpty();
115 
116  return subset;
117 }
bool hasSubset(bool blacklisted=true) const
Returns true if blacklisted fields is not empty or if a subset of names has been set.
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
Definition: qgsfeature.cpp:155
QString name
Definition: qgsfield.h:57
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
Container of fields for a vector layer.
Definition: qgsfields.h:42
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
Definition: qgsfeature.cpp:204
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
QgsFields fields
Definition: qgsfeature.h:73
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
QgsVectorLayer * joinLayer() const
Returns joined layer (may be null if the reference was set by layer ID and not resolved yet) ...
QgsFields fields() const override
Returns the list of fields of this layer.
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:195
Defines left outer join from our vector layer to some other vector layer.
QString prefixedFieldName(const QgsField &field) const
Returns the prefixed name of the field.
static QStringList joinFieldNamesSubset(const QgsVectorLayerJoinInfo &info, bool blacklisted=true)
Returns the list of field names to use for joining considering blacklisted fields and subset...
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:270
QgsFeature extractJoinedFeature(const QgsFeature &feature) const
Extract the join feature from the target feature for the current join layer information.
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
QString name
Definition: qgsmaplayer.h:65
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:255
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
QStringList * joinFieldNamesSubset() const
Gets subset of fields to be used from joined layer.
QString prefix() const
Returns prefix of fields from the joined layer. If null, joined layer's name will be used...
QStringList joinFieldNamesBlackList() const
Returns the list of fields to ignore.