Skip to content

Commit ad48ab0

Browse files
jduggenyalldawson
authored andcommitted
Respect choice of relative or absolute paths for virtual layers when saving project.
Fixes #29481
1 parent 66972c7 commit ad48ab0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/core/qgsvectorlayer.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "qgsvectorlayerrenderer.h"
7979
#include "qgsvectorlayerundocommand.h"
8080
#include "qgsvectorlayerfeaturecounter.h"
81+
#include "qgsvirtuallayerdefinition.h"
8182
#include "qgspoint.h"
8283
#include "qgsrenderer.h"
8384
#include "qgssymbollayer.h"
@@ -1817,6 +1818,37 @@ QString QgsVectorLayer::encodedSource( const QString &source, const QgsReadWrite
18171818
// Refetch the source from the provider, because adding fields actually changes the source for this provider.
18181819
src = dataProvider()->dataSourceUri();
18191820
}
1821+
else if ( providerType() == QLatin1String( "virtual" ) )
1822+
{
1823+
QUrl urlSource = QUrl::fromEncoded( src.toLatin1() );
1824+
QUrl url = QUrl::fromEncoded( src.toLatin1() );
1825+
QUrl urlSourceLayer;
1826+
QStringList theURIParts;
1827+
1828+
QUrlQuery query = QUrlQuery( urlSource.query() );
1829+
QList<QPair<QString, QString> > queryItems = query.queryItems();
1830+
1831+
for ( int i = 0; i < queryItems.size(); i++ )
1832+
{
1833+
QString key = queryItems.at( i ).first;
1834+
QString value = queryItems.at( i ).second;
1835+
if ( key == QLatin1String( "layer" ) )
1836+
{
1837+
// syntax: provider:url_encoded_source_URI(:name(:encoding)?)?
1838+
theURIParts = value.split( ':' );
1839+
theURIParts[1] = QUrl::fromPercentEncoding( theURIParts[1].toUtf8() );
1840+
theURIParts[1] = context.pathResolver().writePath( theURIParts[1] );
1841+
theURIParts[1] = QUrl::toPercentEncoding( theURIParts[1] );
1842+
queryItems[i].second = theURIParts.join( QStringLiteral( ":" ) ) ;
1843+
}
1844+
}
1845+
1846+
query.setQueryItems( queryItems );
1847+
1848+
QUrl urlDest = QUrl( urlSource );
1849+
urlDest.setQuery( query.query() );
1850+
src = QString::fromLatin1( urlDest.toEncoded() );
1851+
}
18201852
else
18211853
{
18221854
src = context.pathResolver().writePath( src );
@@ -1862,6 +1894,37 @@ QString QgsVectorLayer::decodedSource( const QString &source, const QString &pro
18621894
urlDest.setQueryItems( urlSource.queryItems() );
18631895
src = QString::fromLatin1( urlDest.toEncoded() );
18641896
}
1897+
else if ( provider == QLatin1String( "virtual" ) )
1898+
{
1899+
QUrl urlSource = QUrl::fromEncoded( src.toLatin1() );
1900+
QUrl url = QUrl::fromEncoded( src.toLatin1() );
1901+
QUrl urlSourceLayer;
1902+
QStringList theURIParts;
1903+
1904+
QUrlQuery query = QUrlQuery( urlSource.query() );
1905+
QList<QPair<QString, QString> > queryItems = query.queryItems();
1906+
1907+
for ( int i = 0; i < queryItems.size(); i++ )
1908+
{
1909+
QString key = queryItems.at( i ).first;
1910+
QString value = queryItems.at( i ).second;
1911+
if ( key == QLatin1String( "layer" ) )
1912+
{
1913+
// syntax: provider:url_encoded_source_URI(:name(:encoding)?)?
1914+
theURIParts = value.split( ':' );
1915+
theURIParts[1] = QUrl::fromPercentEncoding( theURIParts[1].toUtf8() );
1916+
theURIParts[1] = context.pathResolver().readPath( theURIParts[1] );
1917+
theURIParts[1] = QUrl::toPercentEncoding( theURIParts[1] );
1918+
queryItems[i].second = theURIParts.join( QStringLiteral( ":" ) ) ;
1919+
}
1920+
}
1921+
1922+
query.setQueryItems( queryItems );
1923+
1924+
QUrl urlDest = QUrl( urlSource );
1925+
urlDest.setQuery( query.query() );
1926+
src = QString::fromLatin1( urlDest.toEncoded() );
1927+
}
18651928
else
18661929
{
18671930
src = context.pathResolver().readPath( src );

0 commit comments

Comments
 (0)