QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 : paul.blottiere@oslandia.com
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 #include "qgsvectorlayer.h"
20 
22 {
23  QString name;
24 
25  if ( joinLayer() )
26  {
27  if ( prefix().isNull() )
28  name = joinLayer()->name() + '_';
29  else
30  name = prefix();
31 
32  name += f.name();
33  }
34 
35  return name;
36 }
37 
39 {
40  mEditable = enabled;
41 
42  if ( ! mEditable )
43  {
44  setCascadedDelete( false );
45  setUpsertOnEdit( false );
46  }
47 }
48 
50 {
51  QgsFeature joinFeature;
52 
53  if ( joinLayer() )
54  {
55  const QVariant idFieldValue = feature.attribute( targetFieldName() );
56  joinFeature.initAttributes( joinLayer()->fields().count() );
57  joinFeature.setFields( joinLayer()->fields() );
58  joinFeature.setAttribute( joinFieldName(), idFieldValue );
59 
60  const QgsFields joinFields = joinFeature.fields();
61  for ( const auto &field : joinFields )
62  {
63  const QString prefixedName = prefixedFieldName( field );
64 
65  if ( feature.fieldNameIndex( prefixedName ) != -1 )
66  joinFeature.setAttribute( field.name(), feature.attribute( prefixedName ) );
67  }
68  }
69 
70  return joinFeature;
71 }
72 
73 QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blacklisted )
74 {
75  QStringList fieldNames;
76 
77  if ( blacklisted && !info.joinFieldNamesBlackList().isEmpty() )
78  {
79  QStringList *lst = info.joinFieldNamesSubset();
80  if ( lst )
81  {
82  for ( const QString &s : qgis::as_const( *lst ) )
83  {
84  if ( !info.joinFieldNamesBlackList().contains( s ) )
85  fieldNames.append( s );
86  }
87  }
88  else
89  {
90  for ( const QgsField &f : info.joinLayer()->fields() )
91  {
92  if ( !info.joinFieldNamesBlackList().contains( f.name() )
93  && f.name() != info.joinFieldName() )
94  fieldNames.append( f.name() );
95  }
96  }
97  }
98  else
99  {
100  QStringList *lst = info.joinFieldNamesSubset();
101  if ( lst )
102  {
103  fieldNames = *lst;
104  }
105  }
106 
107  return fieldNames;
108 }
109 
110 bool QgsVectorLayerJoinInfo::hasSubset( bool blacklisted ) const
111 {
112  bool subset = joinFieldNamesSubset();
113 
114  if ( blacklisted )
115  subset |= !joinFieldNamesBlackList().isEmpty();
116 
117  return subset;
118 }
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:162
QString name
Definition: qgsfield.h:58
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:211
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QgsFields fields
Definition: qgsfeature.h:66
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet) ...
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:202
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:277
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:82
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
QStringList * joinFieldNamesSubset() const
Returns the subset of fields to be used from joined layer.
QString prefix() const
Returns prefix of fields from the joined layer. If nullptr, joined layer's name will be used...
QStringList joinFieldNamesBlackList() const
Returns the list of fields to ignore.