QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #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  mMemoryCache = enabled;
41 }
42 
44 {
45  if ( mUpsertOnEdit )
46  return false;
47 
48  return mMemoryCache;
49 }
50 
52 {
53  mEditable = enabled;
54 
55  if ( ! mEditable )
56  {
57  setCascadedDelete( false );
58  setUpsertOnEdit( false );
59  }
60 }
61 
63 {
64  QgsFeature joinFeature;
65 
66  if ( joinLayer() )
67  {
68  const QVariant idFieldValue = feature.attribute( targetFieldName() );
69  joinFeature.initAttributes( joinLayer()->fields().count() );
70  joinFeature.setFields( joinLayer()->fields() );
71  joinFeature.setAttribute( joinFieldName(), idFieldValue );
72 
73  const QgsFields joinFields = joinFeature.fields();
74  for ( const auto &field : joinFields )
75  {
76  const QString prefixedName = prefixedFieldName( field );
77 
78  if ( feature.fieldNameIndex( prefixedName ) != -1 )
79  joinFeature.setAttribute( field.name(), feature.attribute( prefixedName ) );
80  }
81  }
82 
83  return joinFeature;
84 }
85 
86 QStringList QgsVectorLayerJoinInfo::joinFieldNamesSubset( const QgsVectorLayerJoinInfo &info, bool blocklisted )
87 {
88  QStringList fieldNames;
89 
90  if ( blocklisted && !info.joinFieldNamesBlockList().isEmpty() )
91  {
92  QStringList *lst = info.joinFieldNamesSubset();
93  if ( lst )
94  {
95  for ( const QString &s : qgis::as_const( *lst ) )
96  {
97  if ( !info.joinFieldNamesBlockList().contains( s ) )
98  fieldNames.append( s );
99  }
100  }
101  else
102  {
103  for ( const QgsField &f : info.joinLayer()->fields() )
104  {
105  if ( !info.joinFieldNamesBlockList().contains( f.name() )
106  && f.name() != info.joinFieldName() )
107  fieldNames.append( f.name() );
108  }
109  }
110  }
111  else
112  {
113  QStringList *lst = info.joinFieldNamesSubset();
114  if ( lst )
115  {
116  fieldNames = *lst;
117  }
118  }
119 
120  return fieldNames;
121 }
122 
123 bool QgsVectorLayerJoinInfo::hasSubset( bool blocklisted ) const
124 {
125  bool subset = joinFieldNamesSubset();
126 
127  if ( blocklisted )
128  subset |= !joinFieldNamesBlockList().isEmpty();
129 
130  return subset;
131 }
QgsVectorLayerJoinInfo::mEditable
bool mEditable
Definition: qgsvectorlayerjoininfo.h:268
QgsFeature::initAttributes
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:202
QgsVectorLayerJoinInfo::setEditable
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
Definition: qgsvectorlayerjoininfo.cpp:51
QgsFields
Definition: qgsfields.h:44
QgsVectorLayerJoinInfo::joinFieldNamesSubset
QStringList * joinFieldNamesSubset() const
Returns the subset of fields to be used from joined layer.
Definition: qgsvectorlayerjoininfo.h:236
QgsVectorLayerJoinInfo::joinFieldNamesSubset
static QStringList joinFieldNamesSubset(const QgsVectorLayerJoinInfo &info, bool blocklisted=true)
Returns the list of field names to use for joining considering blocklisted fields and subset.
Definition: qgsvectorlayerjoininfo.cpp:86
QgsVectorLayerJoinInfo::setUpsertOnEdit
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
Definition: qgsvectorlayerjoininfo.h:128
QgsVectorLayerJoinInfo::prefixedFieldName
QString prefixedFieldName(const QgsField &field) const
Returns the prefixed name of the field.
Definition: qgsvectorlayerjoininfo.cpp:21
QgsField::name
QString name
Definition: qgsfield.h:59
QgsFeature::fieldNameIndex
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:277
qgsvectorlayerjoininfo.h
QgsVectorLayerJoinInfo::targetFieldName
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
Definition: qgsvectorlayerjoininfo.h:68
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsVectorLayerJoinInfo::prefix
QString prefix() const
Returns prefix of fields from the joined layer. If nullptr, joined layer's name will be used.
Definition: qgsvectorlayerjoininfo.h:78
QgsVectorLayerJoinInfo::mUpsertOnEdit
bool mUpsertOnEdit
Definition: qgsvectorlayerjoininfo.h:270
QgsFeature::setFields
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
QgsVectorLayerJoinInfo::joinFieldName
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
Definition: qgsvectorlayerjoininfo.h:73
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
QgsVectorLayerJoinInfo
Definition: qgsvectorlayerjoininfo.h:33
QgsVectorLayerJoinInfo::setUsingMemoryCache
void setUsingMemoryCache(bool enabled)
Sets whether values from the joined layer should be cached in memory to speed up lookups.
Definition: qgsvectorlayerjoininfo.cpp:38
qgsvectorlayer.h
QgsFeature::setAttribute
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
Definition: qgsfeature.cpp:211
QgsVectorLayerJoinInfo::joinLayer
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet)
Definition: qgsvectorlayerjoininfo.h:58
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:85
QgsVectorLayerJoinInfo::joinFieldNamesBlockList
QStringList joinFieldNamesBlockList() const
Returns the list of fields to ignore.
Definition: qgsvectorlayerjoininfo.h:189
QgsVectorLayerJoinInfo::extractJoinedFeature
QgsFeature extractJoinedFeature(const QgsFeature &feature) const
Extract the join feature from the target feature for the current join layer information.
Definition: qgsvectorlayerjoininfo.cpp:62
QgsFeature::fields
QgsFields fields
Definition: qgsfeature.h:70
QgsFeature
Definition: qgsfeature.h:55
QgsVectorLayerJoinInfo::isUsingMemoryCache
bool isUsingMemoryCache() const
Returns whether values from the joined layer should be cached in memory to speed up lookups.
Definition: qgsvectorlayerjoininfo.cpp:43
QgsVectorLayerJoinInfo::mMemoryCache
bool mMemoryCache
True if the join is cached in virtual memory.
Definition: qgsvectorlayerjoininfo.h:253
QgsVectorLayerJoinInfo::hasSubset
bool hasSubset(bool blocklisted=true) const
Returns true if blocklisted fields is not empty or if a subset of names has been set.
Definition: qgsvectorlayerjoininfo.cpp:123
QgsVectorLayerJoinInfo::setCascadedDelete
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
Definition: qgsvectorlayerjoininfo.h:142
QgsField
Definition: qgsfield.h:49