Skip to content

Commit e5850f3

Browse files
committed
Fix #10858 - Set relative path to QLR save folder. Load relative to QLR file
1 parent 6d850ad commit e5850f3

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

python/core/qgsproject.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class QgsProject : QObject
231231

232232
/** prepare a filename to save it to the project file
233233
@note added in 1.3 */
234-
QString writePath( QString filename ) const;
234+
QString writePath( QString filename, QString relativeBasePath = QString::null ) const;
235235

236236
/** turn filename read from the project file to an absolute path
237237
@note added in 1.3 */

src/app/qgisapp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4917,8 +4917,9 @@ void QgisApp::saveAsLayerDefinition()
49174917
if ( !path.endsWith( ".qlr" ) )
49184918
path = path.append( ".qlr" );
49194919

4920-
QDomDocument doc = QgsMapLayer::asLayerDefinition( layers );
49214920
QFile file( path );
4921+
QFileInfo fileinfo( file );
4922+
QDomDocument doc = QgsMapLayer::asLayerDefinition( layers, fileinfo.canonicalFilePath() );
49224923
if ( file.open( QFile::WriteOnly | QFile::Truncate ) )
49234924
{
49244925
QTextStream qlayerstream( &file );

src/core/qgsmaplayer.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ bool QgsMapLayer::readXml( const QDomNode& layer_node )
432432

433433

434434

435-
bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& document )
435+
bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& document, QString relativeBasePath )
436436
{
437437
// use scale dependent visibility flag
438438
layerElement.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 );
@@ -456,26 +456,26 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
456456
if ( vlayer && vlayer->providerType() == "spatialite" )
457457
{
458458
QgsDataSourceURI uri( src );
459-
QString database = QgsProject::instance()->writePath( uri.database() );
459+
QString database = QgsProject::instance()->writePath( uri.database(), relativeBasePath );
460460
uri.setConnection( uri.host(), uri.port(), database, uri.username(), uri.password() );
461461
src = uri.uri();
462462
}
463463
else if ( vlayer && vlayer->providerType() == "ogr" )
464464
{
465465
QStringList theURIParts = src.split( "|" );
466-
theURIParts[0] = QgsProject::instance()->writePath( theURIParts[0] );
466+
theURIParts[0] = QgsProject::instance()->writePath( theURIParts[0], relativeBasePath );
467467
src = theURIParts.join( "|" );
468468
}
469469
else if ( vlayer && vlayer->providerType() == "delimitedtext" )
470470
{
471471
QUrl urlSource = QUrl::fromEncoded( src.toAscii() );
472-
QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->writePath( urlSource.toLocalFile() ) );
472+
QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->writePath( urlSource.toLocalFile(), relativeBasePath ) );
473473
urlDest.setQueryItems( urlSource.queryItems() );
474474
src = QString::fromAscii( urlDest.toEncoded() );
475475
}
476476
else
477477
{
478-
src = QgsProject::instance()->writePath( src );
478+
src = QgsProject::instance()->writePath( src, relativeBasePath );
479479
}
480480

481481
QDomText dataSourceText = document.createTextNode( src );
@@ -599,14 +599,14 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
599599

600600
} // bool QgsMapLayer::writeXML
601601

602-
QDomDocument QgsMapLayer::asLayerDefinition( QList<QgsMapLayer *> layers )
602+
QDomDocument QgsMapLayer::asLayerDefinition( QList<QgsMapLayer *> layers, QString relativeBasePath )
603603
{
604604
QDomDocument doc( "qgis-layer-definition" );
605605
QDomElement layerselm = doc.createElement( "maplayers" );
606606
foreach ( QgsMapLayer* layer, layers )
607607
{
608608
QDomElement layerelm = doc.createElement( "maplayer" );
609-
layer->writeLayerXML( layerelm, doc );
609+
layer->writeLayerXML( layerelm, doc, relativeBasePath );
610610
layerelm.removeChild( layerelm.firstChildElement( "id" ) );
611611
layerselm.appendChild( layerelm );
612612
}
@@ -664,6 +664,8 @@ QList<QgsMapLayer *> QgsMapLayer::fromLayerDefinitionFile( const QString qlrfile
664664
return QList<QgsMapLayer*>();
665665
}
666666

667+
QFileInfo fileinfo( file );
668+
QDir::setCurrent( fileinfo.absoluteDir().path() );
667669
return QgsMapLayer::fromLayerDefinition( doc );
668670
}
669671

src/core/qgsmaplayer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
217217
218218
@returns true if successful
219219
*/
220-
bool writeLayerXML( QDomElement& layerElement, QDomDocument& document );
220+
bool writeLayerXML( QDomElement& layerElement, QDomDocument& document, QString relativeBasePath = QString::null );
221221

222222
/** Returns the given layer as a layer definition document
223223
Layer definitions store the data source as well as styling and custom properties.
224224
225225
Layer definitions can be used to load a layer and styling all from a single file.
226226
*/
227-
static QDomDocument asLayerDefinition( QList<QgsMapLayer*> layers );
227+
static QDomDocument asLayerDefinition( QList<QgsMapLayer*> layers, QString relativeBasePath = QString::null );
228228

229229
/** Creates a new layer from a layer defininition document
230230
*/

src/core/qgsproject.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ QString QgsProject::readPath( QString src ) const
15611561
}
15621562

15631563
// return the absolute or relative path to write it to the project file
1564-
QString QgsProject::writePath( QString src ) const
1564+
QString QgsProject::writePath( QString src, QString relativeBasePath ) const
15651565
{
15661566
if ( readBoolEntry( "Paths", "/Absolute", false ) || src.isEmpty() )
15671567
{
@@ -1573,6 +1573,11 @@ QString QgsProject::writePath( QString src ) const
15731573
QString srcPath = srcFileInfo.canonicalFilePath();
15741574
QString projPath = projFileInfo.canonicalFilePath();
15751575

1576+
if ( !relativeBasePath.isNull() )
1577+
{
1578+
projPath = relativeBasePath;
1579+
}
1580+
15761581
if ( projPath.isEmpty() )
15771582
{
15781583
return src;

src/core/qgsproject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class CORE_EXPORT QgsProject : public QObject
276276

277277
/** prepare a filename to save it to the project file
278278
@note added in 1.3 */
279-
QString writePath( QString filename ) const;
279+
QString writePath( QString filename, QString relativeBasePath = QString::null ) const;
280280

281281
/** turn filename read from the project file to an absolute path
282282
@note added in 1.3 */

0 commit comments

Comments
 (0)