Skip to content

Commit

Permalink
Respect choice of relative or absolute paths for virtual layers when …
Browse files Browse the repository at this point in the history
…saving project.

Fixes #29481
  • Loading branch information
jdugge committed Aug 5, 2019
1 parent 086fc20 commit af6a383
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit af6a383

Please sign in to comment.