Skip to content
Permalink
Browse files

also move the creation of weak relation to QgsWeakRelation

  • Loading branch information
3nids committed Sep 11, 2020
1 parent ad9e6a3 commit 6548dff38dec9a28a7915cb23bc89724d0d6b0b8
Showing with 82 additions and 46 deletions.
  1. +3 −46 src/core/qgsvectorlayer.cpp
  2. +58 −0 src/core/qgsweakrelation.cpp
  3. +21 −0 src/core/qgsweakrelation.h
@@ -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 ) );
}
}

@@ -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 ) );
}
}
}
@@ -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,
@@ -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 )
@@ -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
*/
@@ -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
*

0 comments on commit 6548dff

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