Skip to content

Commit

Permalink
also move the creation of weak relation to QgsWeakRelation
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 11, 2020
1 parent ad9e6a3 commit 6548dff
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 46 deletions.
49 changes: 3 additions & 46 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2129,29 +2129,8 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
for ( int i = 0; i < relationNodes.length(); ++i )
{
const QDomElement relationElement = relationNodes.at( i ).toElement();
QList<QgsRelation::FieldPair> fieldPairs;
const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral( "fieldPair" ) ) };
for ( int j = 0; j < fieldPairNodes.length(); ++j )
{
const QDomElement fieldPairElement = fieldPairNodes.at( j ).toElement();
fieldPairs.push_back( { fieldPairElement.attribute( QStringLiteral( "referencing" ) ),
fieldPairElement.attribute( QStringLiteral( "referenced" ) ) } );
}
mWeakRelations.push_back( QgsWeakRelation { relationElement.attribute( QStringLiteral( "id" ) ),
relationElement.attribute( QStringLiteral( "name" ) ),
static_cast<QgsRelation::RelationStrength>( relationElement.attribute( QStringLiteral( "strength" ) ).toInt() ),
// Referencing
id(),
name(),
resolver.writePath( publicSource() ),
providerType(),
// Referenced
relationElement.attribute( QStringLiteral( "layerId" ) ),
relationElement.attribute( QStringLiteral( "layerName" ) ),
relationElement.attribute( QStringLiteral( "dataSource" ) ),
relationElement.attribute( QStringLiteral( "providerKey" ) ),
fieldPairs
} );

mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referencing, relationElement, resolver ) );
}
}

Expand All @@ -2163,29 +2142,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
for ( int i = 0; i < relationNodes.length(); ++i )
{
const QDomElement relationElement = relationNodes.at( i ).toElement();
QList<QgsRelation::FieldPair> fieldPairs;
const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral( "fieldPair" ) ) };
for ( int j = 0; j < fieldPairNodes.length(); ++j )
{
const QDomElement fieldPairElement = fieldPairNodes.at( j ).toElement();
fieldPairs.push_back( { fieldPairElement.attribute( QStringLiteral( "referencing" ) ),
fieldPairElement.attribute( QStringLiteral( "referenced" ) ) } );
}
mWeakRelations.push_back( QgsWeakRelation { relationElement.attribute( QStringLiteral( "id" ) ),
relationElement.attribute( QStringLiteral( "name" ) ),
static_cast<QgsRelation::RelationStrength>( relationElement.attribute( QStringLiteral( "strength" ) ).toInt() ),
// Referencing
relationElement.attribute( QStringLiteral( "layerId" ) ),
relationElement.attribute( QStringLiteral( "layerName" ) ),
relationElement.attribute( QStringLiteral( "dataSource" ) ),
relationElement.attribute( QStringLiteral( "providerKey" ) ),
// Referenced
id(),
name(),
resolver.writePath( publicSource() ),
providerType(),
fieldPairs
} );
mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referenced, relationElement, resolver ) );
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions src/core/qgsweakrelation.cpp
Expand Up @@ -13,7 +13,10 @@
* (at your option) any later version. *
* *
***************************************************************************/

#include <QApplication>
#include "qgsweakrelation.h"
#include "qgslogger.h"


QgsWeakRelation::QgsWeakRelation( const QString &relationId, const QString &relationName, const QgsRelation::RelationStrength strength,
Expand Down Expand Up @@ -74,6 +77,61 @@ QList<QgsRelation::FieldPair> QgsWeakRelation::fieldPairs() const
return mFieldPairs;
}

QgsWeakRelation QgsWeakRelation::readXml( const QgsVectorLayer *layer, WeakRelationType type, const QDomNode &node, const QgsPathResolver resolver )
{
QDomElement relationElement = node.toElement();

if ( relationElement.tagName() != QLatin1String( "relation" ) )
{
QgsLogger::warning( QApplication::translate( "QgsRelation", "Cannot create relation. Unexpected tag '%1'" ).arg( relationElement.tagName() ) );
}

QList<QgsRelation::FieldPair> fieldPairs;
const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral( "fieldPair" ) ) };
for ( int j = 0; j < fieldPairNodes.length(); ++j )
{
const QDomElement fieldPairElement = fieldPairNodes.at( j ).toElement();
fieldPairs.push_back( { fieldPairElement.attribute( QStringLiteral( "referencing" ) ),
fieldPairElement.attribute( QStringLiteral( "referenced" ) ) } );
}

switch ( type )
{
case Referencing:
return QgsWeakRelation { relationElement.attribute( QStringLiteral( "id" ) ),
relationElement.attribute( QStringLiteral( "name" ) ),
static_cast<QgsRelation::RelationStrength>( relationElement.attribute( QStringLiteral( "strength" ) ).toInt() ),
// Referencing
layer->id(),
layer->name(),
resolver.writePath( layer->publicSource() ),
layer->providerType(),
// Referenced
relationElement.attribute( QStringLiteral( "layerId" ) ),
relationElement.attribute( QStringLiteral( "layerName" ) ),
relationElement.attribute( QStringLiteral( "dataSource" ) ),
relationElement.attribute( QStringLiteral( "providerKey" ) ),
fieldPairs
};
case Referenced:
return QgsWeakRelation { relationElement.attribute( QStringLiteral( "id" ) ),
relationElement.attribute( QStringLiteral( "name" ) ),
static_cast<QgsRelation::RelationStrength>( relationElement.attribute( QStringLiteral( "strength" ) ).toInt() ),
// Referencing
relationElement.attribute( QStringLiteral( "layerId" ) ),
relationElement.attribute( QStringLiteral( "layerName" ) ),
relationElement.attribute( QStringLiteral( "dataSource" ) ),
relationElement.attribute( QStringLiteral( "providerKey" ) ),
// Referenced
layer->id(),
layer->name(),
resolver.writePath( layer->publicSource() ),
layer->providerType(),
fieldPairs
};
}
}

void QgsWeakRelation::writeXml( const QgsVectorLayer *layer, const QgsRelation &relation, QDomNode &node, QDomDocument &doc )
{
if ( !layer )
Expand Down
21 changes: 21 additions & 0 deletions src/core/qgsweakrelation.h
Expand Up @@ -39,6 +39,17 @@ class CORE_EXPORT QgsWeakRelation
{
public:

/**
* Enum to distinguish if the layer is referenced or referencing
* \since QGIS 3.16
*/
enum WeakRelationType
{
Referencing, //!<
Referenced //!<
};


/**
* Creates a QgsWeakRelation
*/
Expand Down Expand Up @@ -85,6 +96,16 @@ class CORE_EXPORT QgsWeakRelation
*/
QList<QgsRelation::FieldPair> fieldPairs() const;

/**
* Returns a weak relation for the given layer
* \param layer the layer of the weak relation
* \param type determines if the layer is referencing or referenced
* \param node the QDomNode
* \param resolver the path resolver
* \since QGIS 3.16
*/
static QgsWeakRelation readXml( const QgsVectorLayer *layer, WeakRelationType type, const QDomNode &node, const QgsPathResolver resolver );

/**
* Writes a weak relation infoto an XML structure. Used for saving .qgs projects
*
Expand Down

0 comments on commit 6548dff

Please sign in to comment.