Skip to content
Permalink
Browse files

move the logic of writing weak relations info away from QgsVectorLaye…

…r to QgsWeakRelation

and also use QgsRelation::writeXml to avoid an incomplete info if the relation code gets other attributes
  • Loading branch information
3nids committed Sep 11, 2020
1 parent 0721910 commit ad9e6a3d82add5553e539a860e2edddf65b79b4e
Showing with 48 additions and 48 deletions.
  1. +6 −48 src/core/qgsvectorlayer.cpp
  2. +31 −0 src/core/qgsweakrelation.cpp
  3. +11 −0 src/core/qgsweakrelation.h
@@ -2627,68 +2627,26 @@ bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString
// Relation information for both referenced and referencing sides
if ( categories.testFlag( Relations ) )
{

const QgsPathResolver resolver { QgsProject::instance()->pathResolver() };

// Store referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
QDomElement referencedLayersElement = doc.createElement( QStringLiteral( "referencedLayers" ) );
node.appendChild( referencedLayersElement );

const auto constReferencingRelations { QgsProject::instance()->relationManager()->referencingRelations( this ) };
for ( const auto &rel : constReferencingRelations )
{

QDomElement relationElement = doc.createElement( QStringLiteral( "relation" ) );
referencedLayersElement.appendChild( relationElement );

relationElement.setAttribute( QStringLiteral( "id" ), rel.id() );
relationElement.setAttribute( QStringLiteral( "name" ), rel.name() );
relationElement.setAttribute( QStringLiteral( "strength" ), rel.strength() );
relationElement.setAttribute( QStringLiteral( "layerId" ), rel.referencedLayer()->id() );
relationElement.setAttribute( QStringLiteral( "layerName" ), rel.referencedLayer()->name() );
relationElement.setAttribute( QStringLiteral( "dataSource" ), resolver.writePath( rel.referencedLayer()->publicSource() ) );
relationElement.setAttribute( QStringLiteral( "providerKey" ), rel.referencedLayer()->providerType() );

const QList<QgsRelation::FieldPair> constFieldPairs { rel.fieldPairs() };
for ( const QgsRelation::FieldPair &fp : constFieldPairs )
{
QDomElement fieldPair = doc.createElement( QStringLiteral( "fieldPair" ) );
relationElement.appendChild( fieldPair );
fieldPair.setAttribute( QStringLiteral( "referenced" ), fp.referencedField() );
fieldPair.setAttribute( QStringLiteral( "referencing" ), fp.referencingField() );
}

QgsWeakRelation::writeXml( this, rel, referencedLayersElement, doc );
}

// Store referencing layers: relations where "this" is the parent layer (the referenced part where the FK points to)
// Store referencing layers: relations where "this" is the parent layer (the referenced part, that holds the FK)
QDomElement referencingLayersElement = doc.createElement( QStringLiteral( "referencingLayers" ) );
node.appendChild( referencingLayersElement );
node.appendChild( referencedLayersElement );

const auto constReferencedRelations { QgsProject::instance()->relationManager()->referencedRelations( this ) };
const auto constReferencedRelations { QgsProject::instance()->relationManager()->referencingRelations( this ) };
for ( const auto &rel : constReferencedRelations )
{

QDomElement relationElement = doc.createElement( QStringLiteral( "relation" ) );
referencingLayersElement.appendChild( relationElement );

relationElement.setAttribute( QStringLiteral( "id" ), rel.id() );
relationElement.setAttribute( QStringLiteral( "name" ), rel.name() );
relationElement.setAttribute( QStringLiteral( "strength" ), rel.strength() );
relationElement.setAttribute( QStringLiteral( "layerId" ), rel.referencingLayer()->id() );
relationElement.setAttribute( QStringLiteral( "layerName" ), rel.referencingLayer()->name() );
relationElement.setAttribute( QStringLiteral( "dataSource" ), resolver.writePath( rel.referencingLayer()->publicSource() ) );
relationElement.setAttribute( QStringLiteral( "providerKey" ), rel.referencingLayer()->providerType() );

const QList<QgsRelation::FieldPair> constFieldPairs { rel.fieldPairs() };
for ( const QgsRelation::FieldPair &fp : constFieldPairs )
{
QDomElement fieldPair = doc.createElement( QStringLiteral( "fieldPair" ) );
relationElement.appendChild( fieldPair );
fieldPair.setAttribute( QStringLiteral( "referenced" ), fp.referencedField() );
fieldPair.setAttribute( QStringLiteral( "referencing" ), fp.referencingField() );
}

QgsWeakRelation::writeXml( this, rel, referencingLayersElement, doc );
}

}

if ( categories.testFlag( Fields ) )
@@ -73,3 +73,34 @@ QList<QgsRelation::FieldPair> QgsWeakRelation::fieldPairs() const
{
return mFieldPairs;
}

void QgsWeakRelation::writeXml( const QgsVectorLayer *layer, const QgsRelation &relation, QDomNode &node, QDomDocument &doc )
{
if ( !layer )
return;

if ( layer != relation.referencingLayer() && layer != relation.referencedLayer() )
return;

const QgsPathResolver resolver { QgsProject::instance()->pathResolver() };

relation.writeXml( node, doc );
QDomElement relationElement = node.firstChildElement( QStringLiteral( "relation" ) );
Q_ASSERT( !relationElement.isNull() );
if ( layer == relation.referencingLayer() )
{
// if the layer is the referencing one, we save the referenced layer info
relationElement.setAttribute( QStringLiteral( "layerId" ), relation.referencedLayer()->id() );
relationElement.setAttribute( QStringLiteral( "layerName" ), relation.referencedLayer()->name() );
relationElement.setAttribute( QStringLiteral( "dataSource" ), resolver.writePath( relation.referencedLayer()->publicSource() ) );
relationElement.setAttribute( QStringLiteral( "providerKey" ), relation.referencedLayer()->providerType() );
}
else
{
// if the layer is the referenced one, we save the referencing layer info
relationElement.setAttribute( QStringLiteral( "layerId" ), relation.referencingLayer()->id() );
relationElement.setAttribute( QStringLiteral( "layerName" ), relation.referencingLayer()->name() );
relationElement.setAttribute( QStringLiteral( "dataSource" ), resolver.writePath( relation.referencingLayer()->publicSource() ) );
relationElement.setAttribute( QStringLiteral( "providerKey" ), relation.referencingLayer()->providerType() );
}
}
@@ -85,6 +85,17 @@ class CORE_EXPORT QgsWeakRelation
*/
QList<QgsRelation::FieldPair> fieldPairs() const;

/**
* Writes a weak relation infoto an XML structure. Used for saving .qgs projects
*
* \param layer the layer which we save the weak relation for
* \param relation the relation to save as a weak relation
* \param node The parent node in which the relation will be created
* \param doc The document in which the relation will be saved
* \since QGIS 3.16
*/
static void writeXml( const QgsVectorLayer *layer, const QgsRelation &relation, QDomNode &node, QDomDocument &doc );

private:

QgsVectorLayerRef mReferencingLayer;

0 comments on commit ad9e6a3

Please sign in to comment.
You can’t perform that action at this time.