Skip to content

Commit

Permalink
QgsLayerTreeLayer and QgsMapLayer XML serialization/deserialization: …
Browse files Browse the repository at this point in the history
…use QgsProviderRegistry relativeToAbsoluteUri/absoluteToRelativeUri()

Fixes #55975
  • Loading branch information
rouault authored and github-actions[bot] committed Jan 25, 2024
1 parent 4920744 commit cbd7ee3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/core/layertree/qgslayertreelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include "qgslayertreeutils.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgsproviderregistry.h"
#include "qgssymbollayerutils.h"


QgsLayerTreeLayer::QgsLayerTreeLayer( QgsMapLayer *layer )
: QgsLayerTreeNode( NodeLayer, true )
, mRef( layer )
Expand Down Expand Up @@ -110,7 +110,8 @@ QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsRe
const QString layerName = element.attribute( QStringLiteral( "name" ) );

const QString providerKey = element.attribute( QStringLiteral( "providerKey" ) );
const QString source = context.pathResolver().readPath( element.attribute( QStringLiteral( "source" ) ) );
const QString sourceRaw = element.attribute( QStringLiteral( "source" ) );
const QString source = providerKey.isEmpty() ? sourceRaw : QgsProviderRegistry::instance()->relativeToAbsoluteUri( providerKey, sourceRaw, context );

const Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) );
const bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
Expand Down Expand Up @@ -157,8 +158,10 @@ void QgsLayerTreeLayer::writeXml( QDomElement &parentElement, const QgsReadWrite

if ( mRef )
{
elem.setAttribute( QStringLiteral( "source" ), context.pathResolver().writePath( mRef->publicSource() ) );
elem.setAttribute( QStringLiteral( "providerKey" ), mRef->dataProvider() ? mRef->dataProvider()->name() : QString() );
const QString providerKey = mRef->dataProvider() ? mRef->dataProvider()->name() : QString();
const QString source = providerKey.isEmpty() ? mRef->publicSource() : QgsProviderRegistry::instance()->absoluteToRelativeUri( providerKey, mRef->publicSource(), context );
elem.setAttribute( QStringLiteral( "source" ), source );
elem.setAttribute( QStringLiteral( "providerKey" ), providerKey );
}

elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCon
// set data source
mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
mne = mnl.toElement();
mDataSource = context.pathResolver().readPath( mne.text() );
const QString dataSourceRaw = mne.text();
mDataSource = provider.isEmpty() ? dataSourceRaw : QgsProviderRegistry::instance()->relativeToAbsoluteUri( provider, dataSourceRaw, context );

// if the layer needs authentication, ensure the master password is set
const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
Expand Down Expand Up @@ -643,7 +644,10 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume

// data source
QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
const QString src = context.pathResolver().writePath( encodedSource( source(), context ) );
const QgsDataProvider *provider = dataProvider();
const QString providerKey = provider ? provider->name() : QString();
const QString srcRaw = encodedSource( source(), context );
const QString src = providerKey.isEmpty() ? srcRaw : QgsProviderRegistry::instance()->absoluteToRelativeUri( providerKey, srcRaw, context );
const QDomText dataSourceText = document.createTextNode( src );
dataSource.appendChild( dataSourceText );
layerElement.appendChild( dataSource );
Expand Down
8 changes: 4 additions & 4 deletions tests/src/python/test_qgspathresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ def testPathWriter(self):
readerId = QgsPathResolver.setPathPreprocessor(self.__test_path_reader)
writerId = QgsPathResolver.setPathWriter(self.__test__path_writer)

lines_shp_path = os.path.join(TEST_DATA_DIR, 'lines.shp')
uri = os.path.join(TEST_DATA_DIR, 'points_gpkg.gpkg') + "|layername=points_gpkg|subset=1=1 /* foo */"

lines_layer = QgsVectorLayer(lines_shp_path, 'Lines', 'ogr')
lines_layer = QgsVectorLayer(uri, 'Points', 'ogr')
self.assertTrue(lines_layer.isValid())
p = QgsProject()
p.addMapLayer(lines_layer)
Expand All @@ -188,9 +188,9 @@ def testPathWriter(self):

p2 = QgsProject()
self.assertTrue(p2.read(temp_project_path))
l = p2.mapLayersByName('Lines')[0]
l = p2.mapLayersByName('Points')[0]
self.assertEqual(l.isValid(), True)
self.assertEqual(l.source(), lines_shp_path)
self.assertEqual(l.source(), uri)

QgsPathResolver.removePathPreprocessor(readerId)
QgsPathResolver.removePathWriter(writerId)
Expand Down

0 comments on commit cbd7ee3

Please sign in to comment.