Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsvectorlayerjoinbuffer.h 3 : : --------------------------- 4 : : begin : Feb 09, 2011 5 : : copyright : (C) 2011 by Marco Hugentobler 6 : : email : marco dot hugentobler at sourcepole dot ch 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 : : #ifndef QGSVECTORLAYERJOINBUFFER_H 19 : : #define QGSVECTORLAYERJOINBUFFER_H 20 : : 21 : : #include "qgis_core.h" 22 : : #include "qgis_sip.h" 23 : : #include "qgsvectorlayerjoininfo.h" 24 : : #include "qgsfeaturesink.h" 25 : : 26 : : #include <QHash> 27 : : #include <QString> 28 : : 29 : : 30 : : typedef QList< QgsVectorLayerJoinInfo > QgsVectorJoinList; 31 : : 32 : : 33 : : /** 34 : : * \ingroup core 35 : : * \brief Manages joined fields for a vector layer. 36 : : */ 37 : : class CORE_EXPORT QgsVectorLayerJoinBuffer : public QObject, public QgsFeatureSink 38 : : { 39 : : Q_OBJECT 40 : : public: 41 : : QgsVectorLayerJoinBuffer( QgsVectorLayer *layer = nullptr ); 42 : : 43 : : /** 44 : : * Joins another vector layer to this layer 45 : : * \param joinInfo join object containing join layer id, target and source field 46 : : * \returns (since 2.6) whether the join was successfully added 47 : : */ 48 : : bool addJoin( const QgsVectorLayerJoinInfo &joinInfo ); 49 : : 50 : : /** 51 : : * Removes a vector layer join 52 : : * \returns TRUE if join was found and successfully removed 53 : : */ 54 : : bool removeJoin( const QString &joinLayerId ); 55 : : 56 : : /** 57 : : * Updates field map with joined attributes 58 : : * \param fields map to append joined attributes 59 : : */ 60 : : void updateFields( QgsFields &fields ); 61 : : 62 : : //! Calls cacheJoinLayer() for all vector joins 63 : : void createJoinCaches(); 64 : : 65 : : //! Saves mVectorJoins to xml under the layer node 66 : : void writeXml( QDomNode &layer_node, QDomDocument &document ) const; 67 : : 68 : : /** 69 : : * Reads joins from project file. 70 : : * Does not resolve layer IDs to layers - call resolveReferences() afterwards 71 : : */ 72 : : void readXml( const QDomNode &layer_node ); 73 : : 74 : : /** 75 : : * Resolves layer IDs of joined layers using given project's available layers 76 : : * \since QGIS 3.0 77 : : */ 78 : : void resolveReferences( QgsProject *project ); 79 : : 80 : : //! Quick way to test if there is any join at all 81 : 279 : bool containsJoins() const { return !mVectorJoins.isEmpty(); } 82 : : 83 : 0 : const QgsVectorJoinList &vectorJoins() const { return mVectorJoins; } 84 : : 85 : : /** 86 : : * Finds the vector join for a layer field index. 87 : : * \param index this layers attribute index 88 : : * \param fields fields of the vector layer (including joined fields) 89 : : * \param sourceFieldIndex Output: field's index in source layer 90 : : * \returns the vector layer join info 91 : : */ 92 : : const QgsVectorLayerJoinInfo *joinForFieldIndex( int index, const QgsFields &fields, int &sourceFieldIndex SIP_OUT ) const; 93 : : 94 : : /** 95 : : * Find out what is the first index of the join within fields. Returns -1 if join is not present 96 : : * \since QGIS 2.6 97 : : */ 98 : : int joinedFieldsOffset( const QgsVectorLayerJoinInfo *info, const QgsFields &fields ); 99 : : 100 : : /** 101 : : * Returns a vector of indices for use in join based on field names from the layer 102 : : * \since QGIS 2.6 103 : : */ 104 : : static QVector<int> joinSubsetIndices( QgsVectorLayer *joinLayer, const QStringList &joinFieldsSubset ); 105 : : 106 : : /** 107 : : * Returns joins where the field of a target layer is considered as an id. 108 : : * \param field the field of a target layer 109 : : * \returns a list of vector joins 110 : : * \since QGIS 3.0 111 : : */ 112 : : QList<const QgsVectorLayerJoinInfo *> joinsWhereFieldIsId( const QgsField &field ) const; 113 : : 114 : : /** 115 : : * Returns the joined feature corresponding to the feature. 116 : : * \param info the vector join information 117 : : * \param feature the feature of the target layer 118 : : * \since QGIS 3.0 119 : : */ 120 : : QgsFeature joinedFeatureOf( const QgsVectorLayerJoinInfo *info, const QgsFeature &feature ) const; 121 : : 122 : : /** 123 : : * Returns the targeted feature corresponding to the joined feature. 124 : : * \param info the vector join information 125 : : * \param feature the feature of the joined layer 126 : : * \since QGIS 3.0 127 : : */ 128 : : QgsFeature targetedFeatureOf( const QgsVectorLayerJoinInfo *info, const QgsFeature &feature ) const; 129 : : 130 : : /** 131 : : * Returns TRUE if the join information is about auxiliary layer, FALSE otherwise 132 : : * 133 : : * \param info The join information 134 : : * 135 : : * \returns TRUE if the join information is about auxiliary layer, FALSE otherwise 136 : : * 137 : : * \since QGIS 3.0 138 : : */ 139 : : bool isAuxiliaryJoin( const QgsVectorLayerJoinInfo &info ) const; 140 : : 141 : : /** 142 : : * Create a copy of the join buffer 143 : : * \since QGIS 2.6 144 : : */ 145 : : QgsVectorLayerJoinBuffer *clone() const SIP_FACTORY; 146 : : 147 : : /** 148 : : * Adds a list of features in joined layers. Features given in parameter 149 : : * are those added in target layer. If a corresponding joined feature yet 150 : : * exists in a joined layer, then this feature is just updated. Note that 151 : : * if a corresponding joined feature has only empty fields, then it's not 152 : : * created nor added. 153 : : * 154 : : * \param features The list of features added in the target layer 155 : : * \param flags Unused parameter 156 : : * 157 : : * \returns FALSE if an error happened, TRUE otherwise 158 : : * 159 : : * \since QGIS 3.0 160 : : */ 161 : : bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override; 162 : : 163 : : /** 164 : : * Changes attribute value in joined layers. The feature id given in 165 : : * parameter is the one added in target layer. If the corresponding joined 166 : : * feature does not exist in a joined layer, then it's automatically 167 : : * created if its fields are not empty. 168 : : * 169 : : * \param fid The feature id 170 : : * \param field The field to update 171 : : * \param newValue The new value of the attribute 172 : : * \param oldValue The old value of the attribute 173 : : * 174 : : * \returns FALSE if an error happened, TRUE otherwise 175 : : * 176 : : * \since QGIS 3.0 177 : : */ 178 : : bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() ); 179 : : 180 : : /** 181 : : * Changes attributes' values in joined layers. The feature id given in 182 : : * parameter is the one added in target layer. If the corresponding joined 183 : : * feature does not exist in a joined layer, then it's automatically 184 : : * created if its fields are not empty. 185 : : * 186 : : * \param fid The feature id 187 : : * \param newValues The new values for attributes 188 : : * \param oldValues The old values for attributes 189 : : * 190 : : * \returns FALSE if an error happened, TRUE otherwise 191 : : * 192 : : * \since QGIS 3.0 193 : : */ 194 : : bool changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues = QgsAttributeMap() ); 195 : : 196 : : /** 197 : : * Deletes a feature from joined layers. The feature id given in 198 : : * parameter is the one coming from the target layer. 199 : : * 200 : : * \param fid The feature id from the target layer to delete 201 : : * \param context The chain of features which will be deleted for feedback and to avoid infinite recursions. Can be NULLPTR. 202 : : * 203 : : * \returns FALSE if an error happened, TRUE otherwise 204 : : * 205 : : * \since QGIS 3.0 206 : : */ 207 : : bool deleteFeature( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context = nullptr ) const; 208 : : 209 : : /** 210 : : * Deletes a list of features from joined layers. Feature ids given 211 : : * in a parameter are those coming from the target layer. 212 : : * 213 : : * \param fids Feature ids from the target layer to delete 214 : : * \param context The chain of features who will be deleted for feedback and to avoid infinite recursions. Can be NULLPTR. 215 : : * 216 : : * \returns FALSE if an error happened, TRUE otherwise 217 : : * 218 : : * \since QGIS 3.0 219 : : */ 220 : : bool deleteFeatures( const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context = nullptr ) const; 221 : : 222 : : signals: 223 : : 224 : : /** 225 : : * Emitted whenever the list of joined fields changes (e.g. added join or joined layer's fields change) 226 : : * \since QGIS 2.6 227 : : */ 228 : : void joinedFieldsChanged(); 229 : : 230 : : private slots: 231 : : void joinedLayerUpdatedFields(); 232 : : 233 : : void joinedLayerModified(); 234 : : 235 : : void joinedLayerWillBeDeleted(); 236 : : 237 : : private: 238 : : void connectJoinedLayer( QgsVectorLayer *vl ); 239 : : 240 : : private: 241 : : 242 : : QgsVectorLayer *mLayer = nullptr; 243 : : 244 : : //! Joined vector layers 245 : : QgsVectorJoinList mVectorJoins; 246 : : 247 : : //! Caches attributes of join layer in memory if QgsVectorJoinInfo.memoryCache is TRUE (and the cache is not already there) 248 : : void cacheJoinLayer( QgsVectorLayerJoinInfo &joinInfo ); 249 : : 250 : : //! Main mutex to protect most data members that can be modified concurrently 251 : : QMutex mMutex; 252 : : }; 253 : : 254 : : #endif // QGSVECTORLAYERJOINBUFFER_H