Skip to content

Commit af6a383

Browse files
committed
Respect choice of relative or absolute paths for virtual layers when saving project.
Fixes #29481
1 parent 086fc20 commit af6a383

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"
@@ -1833,6 +1834,37 @@ QString QgsVectorLayer::encodedSource( const QString &source, const QgsReadWrite
18331834
// Refetch the source from the provider, because adding fields actually changes the source for this provider.
18341835
src = dataProvider()->dataSourceUri();
18351836
}
1837+
else if ( providerType() == QLatin1String( "virtual" ) )
1838+
{
1839+
QUrl urlSource = QUrl::fromEncoded( src.toLatin1() );
1840+
QUrl url = QUrl::fromEncoded( src.toLatin1() );
1841+
QUrl urlSourceLayer;
1842+
QStringList theURIParts;
1843+
1844+
QUrlQuery query = QUrlQuery( urlSource.query() );
1845+
QList<QPair<QString, QString> > queryItems = query.queryItems();
1846+
1847+
for ( int i = 0; i < queryItems.size(); i++ )
1848+
{
1849+
QString key = queryItems.at( i ).first;
1850+
QString value = queryItems.at( i ).second;
1851+
if ( key == QLatin1String( "layer" ) )
1852+
{
1853+
// syntax: provider:url_encoded_source_URI(:name(:encoding)?)?
1854+
theURIParts = value.split( ':' );
1855+
theURIParts[1] = QUrl::fromPercentEncoding( theURIParts[1].toUtf8() );
1856+
theURIParts[1] = context.pathResolver().writePath( theURIParts[1] );
1857+
theURIParts[1] = QUrl::toPercentEncoding( theURIParts[1] );
1858+
queryItems[i].second = theURIParts.join( QStringLiteral( ":" ) ) ;
1859+
}
1860+
}
1861+
1862+
query.setQueryItems( queryItems );
1863+
1864+
QUrl urlDest = QUrl( urlSource );
1865+
urlDest.setQuery( query.query() );
1866+
src = QString::fromLatin1( urlDest.toEncoded() );
1867+
}
18361868
else
18371869
{
18381870
src = context.pathResolver().writePath( src );
@@ -1878,6 +1910,37 @@ QString QgsVectorLayer::decodedSource( const QString &source, const QString &pro
18781910
urlDest.setQueryItems( urlSource.queryItems() );
18791911
src = QString::fromLatin1( urlDest.toEncoded() );
18801912
}
1913+
else if ( provider == QLatin1String( "virtual" ) )
1914+
{
1915+
QUrl urlSource = QUrl::fromEncoded( src.toLatin1() );
1916+
QUrl url = QUrl::fromEncoded( src.toLatin1() );
1917+
QUrl urlSourceLayer;
1918+
QStringList theURIParts;
1919+
1920+
QUrlQuery query = QUrlQuery( urlSource.query() );
1921+
QList<QPair<QString, QString> > queryItems = query.queryItems();
1922+
1923+
for ( int i = 0; i < queryItems.size(); i++ )
1924+
{
1925+
QString key = queryItems.at( i ).first;
1926+
QString value = queryItems.at( i ).second;
1927+
if ( key == QLatin1String( "layer" ) )
1928+
{
1929+
// syntax: provider:url_encoded_source_URI(:name(:encoding)?)?
1930+
theURIParts = value.split( ':' );
1931+
theURIParts[1] = QUrl::fromPercentEncoding( theURIParts[1].toUtf8() );
1932+
theURIParts[1] = context.pathResolver().readPath( theURIParts[1] );
1933+
theURIParts[1] = QUrl::toPercentEncoding( theURIParts[1] );
1934+
queryItems[i].second = theURIParts.join( QStringLiteral( ":" ) ) ;
1935+
}
1936+
}
1937+
1938+
query.setQueryItems( queryItems );
1939+
1940+
QUrl urlDest = QUrl( urlSource );
1941+
urlDest.setQuery( query.query() );
1942+
src = QString::fromLatin1( urlDest.toEncoded() );
1943+
}
18811944
else
18821945
{
18831946
src = context.pathResolver().readPath( src );

0 commit comments

Comments
 (0)