24 : mReferencingLayer( NULL )
25 , mReferencedLayer( NULL )
32 QDomElement elem = node.toElement();
34 if ( elem.tagName() !=
"relation" )
36 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
43 QString
id = elem.attribute(
"id" );
44 QString
name = elem.attribute(
"name" );
51 if ( NULL == referencingLayer )
53 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Relation defined for layer '%1' which does not exist." ).arg( referencingLayerId ) );
57 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Relation defined for layer '%1' which is not of type VectorLayer." ).arg( referencingLayerId ) );
60 if ( NULL == referencedLayer )
62 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Relation defined for layer '%1' which does not exist." ).arg( referencedLayerId ) );
66 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Relation defined for layer '%1' which is not of type VectorLayer." ).arg( referencedLayerId ) );
73 relation.mRelationId =
id;
74 relation.mRelationName =
name;
76 QDomNodeList references = elem.elementsByTagName(
"fieldRef" );
77 for (
int i = 0; i < references.size(); ++i )
79 QDomElement refEl = references.at( i ).toElement();
81 QString referencingField = refEl.attribute(
"referencingField" );
82 QString referencedField = refEl.attribute(
"referencedField" );
84 relation.
addFieldPair( referencingField, referencedField );
94 QDomElement elem = doc.createElement(
"relation" );
95 elem.setAttribute(
"id", mRelationId );
96 elem.setAttribute(
"name", mRelationName );
97 elem.setAttribute(
"referencingLayer", mReferencingLayerId );
98 elem.setAttribute(
"referencedLayer", mReferencedLayerId );
100 foreach (
FieldPair fields, mFieldPairs )
102 QDomElement referenceElem = doc.createElement(
"fieldRef" );
103 referenceElem.setAttribute(
"referencingField", fields.first );
104 referenceElem.setAttribute(
"referencedField", fields.second );
105 elem.appendChild( referenceElem );
108 node.appendChild( elem );
118 mRelationName =
name;
123 mReferencingLayerId =
id;
130 mReferencedLayerId =
id;
137 mFieldPairs <<
FieldPair( referencingField, referencedField );
143 mFieldPairs << fieldPair;
154 QStringList conditions;
161 switch ( referencingField.
type() )
163 case QVariant::String:
177 QgsDebugMsg( QString(
"Filter conditions: '%1'" ).arg( conditions.join(
" AND " ) ) );
186 return mRelationName;
196 return mReferencingLayerId;
201 return mReferencingLayer;
206 return mReferencedLayerId;
211 return mReferencedLayer;
228 mReferencingLayer = qobject_cast<
QgsVectorLayer*>( mapLayers[mReferencingLayerId] );
229 mReferencedLayer = qobject_cast<
QgsVectorLayer*>( mapLayers[mReferencedLayerId] );
233 if ( !mReferencedLayer || !mReferencingLayer )
239 if ( mFieldPairs.count() < 1 )
244 Q_FOREACH (
const FieldPair& fieldPair, mFieldPairs )
247 || -1 == mReferencedLayer->fieldNameIndex( fieldPair.second ) )
void setRelationId(QString id)
Set a name for this relation.
Wrapper for iterator of features from vector data provider or vector layer.
bool isValid() const
Returns the validity of this relation.
const QString name() const
Base class for all map layer types.
QgsMapLayer::LayerType type() const
Get the type of the layer.
void setRelationName(QString name)
Set a name for this relation.
static void warning(const QString &msg)
Goes to qWarning.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
QgsRelation()
Default constructor.
static QgsMapLayerRegistry * instance()
QgsVectorLayer * referencedLayer() const
Access the referenced (parent) layer.
static QgsRelation createFromXML(const QDomNode &node)
Creates a relation from an XML structure.
void setReferencingLayer(QString id)
Set the referencing layer id.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QString referencingLayerId() const
Access the referencing (child) layer's id This is the layer which has the field(s) which point to ano...
void writeXML(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set filter expression. {.
void setReferencedLayer(QString id)
Set the referenced layer id.
Defines a relation between matchin fields of the two involved tables of a relation.
const QString & id() const
The id.
QgsVectorLayer * referencingLayer() const
Access the referencing (child) layer This is the layer which has the field(s) which point to another ...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Encapsulate a field in an attribute table or data source.
QgsFeatureRequest getRelatedFeaturesRequest(const QgsFeature &feature) const
Creates a request to return all the features on the referencing (child) layer which have a foreign ke...
QString referencedLayerId() const
Access the referenced (parent) layer's id.
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
const QString & referencingField() const
Get the name of the referencing field.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QgsFeatureIterator getRelatedFeatures(const QgsFeature &feature) const
Creates an iterator which returns all the features on the referencing (child) layer which have a fore...
const QMap< QString, QgsMapLayer * > & mapLayers()
Retrieve the mapLayers collection (mainly intended for use by projection)
void addFieldPair(QString referencingField, QString referencedField)
Add a field pairs which is part of this relation The first element of each pair are the field names f...
QList< FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names f...
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
const QString & referencedField() const
Get the name of the referenced field.
void updateRelationStatus()
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.