Skip to content
Permalink
Browse files
Respect choice of relative or absolute paths for virtual layers when …
…saving project.

Fixes #29481
  • Loading branch information
jdugge authored and nyalldawson committed Aug 6, 2019
1 parent 078ab8b commit 75bfb28a1e3a9772348120ac8082a9100ba8a703
Showing with 63 additions and 0 deletions.
  1. +63 −0 src/core/qgsvectorlayer.cpp
@@ -78,6 +78,7 @@
#include "qgsvectorlayerrenderer.h"
#include "qgsvectorlayerundocommand.h"
#include "qgsvectorlayerfeaturecounter.h"
#include "qgsvirtuallayerdefinition.h"
#include "qgspoint.h"
#include "qgsrenderer.h"
#include "qgssymbollayer.h"
@@ -1833,6 +1834,37 @@ QString QgsVectorLayer::encodedSource( const QString &source, const QgsReadWrite
// Refetch the source from the provider, because adding fields actually changes the source for this provider.
src = dataProvider()->dataSourceUri();
}
else if ( providerType() == QLatin1String( "virtual" ) )
{
QUrl urlSource = QUrl::fromEncoded( src.toLatin1() );
QUrl url = QUrl::fromEncoded( src.toLatin1() );
QUrl urlSourceLayer;
QStringList theURIParts;

QUrlQuery query = QUrlQuery( urlSource.query() );
QList<QPair<QString, QString> > queryItems = query.queryItems();

for ( int i = 0; i < queryItems.size(); i++ )
{
QString key = queryItems.at( i ).first;
QString value = queryItems.at( i ).second;
if ( key == QLatin1String( "layer" ) )
{
// syntax: provider:url_encoded_source_URI(:name(:encoding)?)?
theURIParts = value.split( ':' );
theURIParts[1] = QUrl::fromPercentEncoding( theURIParts[1].toUtf8() );
theURIParts[1] = context.pathResolver().writePath( theURIParts[1] );
theURIParts[1] = QUrl::toPercentEncoding( theURIParts[1] );
queryItems[i].second = theURIParts.join( QStringLiteral( ":" ) ) ;
}
}

query.setQueryItems( queryItems );

QUrl urlDest = QUrl( urlSource );
urlDest.setQuery( query.query() );
src = QString::fromLatin1( urlDest.toEncoded() );
}
else
{
src = context.pathResolver().writePath( src );
@@ -1878,6 +1910,37 @@ QString QgsVectorLayer::decodedSource( const QString &source, const QString &pro
urlDest.setQueryItems( urlSource.queryItems() );
src = QString::fromLatin1( urlDest.toEncoded() );
}
else if ( provider == QLatin1String( "virtual" ) )
{
QUrl urlSource = QUrl::fromEncoded( src.toLatin1() );
QUrl url = QUrl::fromEncoded( src.toLatin1() );
QUrl urlSourceLayer;
QStringList theURIParts;

QUrlQuery query = QUrlQuery( urlSource.query() );
QList<QPair<QString, QString> > queryItems = query.queryItems();

for ( int i = 0; i < queryItems.size(); i++ )
{
QString key = queryItems.at( i ).first;
QString value = queryItems.at( i ).second;
if ( key == QLatin1String( "layer" ) )
{
// syntax: provider:url_encoded_source_URI(:name(:encoding)?)?
theURIParts = value.split( ':' );
theURIParts[1] = QUrl::fromPercentEncoding( theURIParts[1].toUtf8() );
theURIParts[1] = context.pathResolver().readPath( theURIParts[1] );
theURIParts[1] = QUrl::toPercentEncoding( theURIParts[1] );
queryItems[i].second = theURIParts.join( QStringLiteral( ":" ) ) ;
}
}

query.setQueryItems( queryItems );

QUrl urlDest = QUrl( urlSource );
urlDest.setQuery( query.query() );
src = QString::fromLatin1( urlDest.toEncoded() );
}
else
{
src = context.pathResolver().readPath( src );

0 comments on commit 75bfb28

Please sign in to comment.