Navigation Menu

Skip to content

Commit

Permalink
Fix #10858 - Set relative path to QLR save folder. Load relative to Q…
Browse files Browse the repository at this point in the history
…LR file
  • Loading branch information
NathanW2 committed Oct 6, 2014
1 parent 6d850ad commit e5850f3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsproject.sip
Expand Up @@ -231,7 +231,7 @@ class QgsProject : QObject

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

/** turn filename read from the project file to an absolute path
@note added in 1.3 */
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -4917,8 +4917,9 @@ void QgisApp::saveAsLayerDefinition()
if ( !path.endsWith( ".qlr" ) )
path = path.append( ".qlr" );

QDomDocument doc = QgsMapLayer::asLayerDefinition( layers );
QFile file( path );
QFileInfo fileinfo( file );
QDomDocument doc = QgsMapLayer::asLayerDefinition( layers, fileinfo.canonicalFilePath() );
if ( file.open( QFile::WriteOnly | QFile::Truncate ) )
{
QTextStream qlayerstream( &file );
Expand Down
16 changes: 9 additions & 7 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -432,7 +432,7 @@ bool QgsMapLayer::readXml( const QDomNode& layer_node )



bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& document )
bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& document, QString relativeBasePath )
{
// use scale dependent visibility flag
layerElement.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 );
Expand All @@ -456,26 +456,26 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
if ( vlayer && vlayer->providerType() == "spatialite" )
{
QgsDataSourceURI uri( src );
QString database = QgsProject::instance()->writePath( uri.database() );
QString database = QgsProject::instance()->writePath( uri.database(), relativeBasePath );
uri.setConnection( uri.host(), uri.port(), database, uri.username(), uri.password() );
src = uri.uri();
}
else if ( vlayer && vlayer->providerType() == "ogr" )
{
QStringList theURIParts = src.split( "|" );
theURIParts[0] = QgsProject::instance()->writePath( theURIParts[0] );
theURIParts[0] = QgsProject::instance()->writePath( theURIParts[0], relativeBasePath );
src = theURIParts.join( "|" );
}
else if ( vlayer && vlayer->providerType() == "delimitedtext" )
{
QUrl urlSource = QUrl::fromEncoded( src.toAscii() );
QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->writePath( urlSource.toLocalFile() ) );
QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->writePath( urlSource.toLocalFile(), relativeBasePath ) );
urlDest.setQueryItems( urlSource.queryItems() );
src = QString::fromAscii( urlDest.toEncoded() );
}
else
{
src = QgsProject::instance()->writePath( src );
src = QgsProject::instance()->writePath( src, relativeBasePath );
}

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

} // bool QgsMapLayer::writeXML

QDomDocument QgsMapLayer::asLayerDefinition( QList<QgsMapLayer *> layers )
QDomDocument QgsMapLayer::asLayerDefinition( QList<QgsMapLayer *> layers, QString relativeBasePath )
{
QDomDocument doc( "qgis-layer-definition" );
QDomElement layerselm = doc.createElement( "maplayers" );
foreach ( QgsMapLayer* layer, layers )
{
QDomElement layerelm = doc.createElement( "maplayer" );
layer->writeLayerXML( layerelm, doc );
layer->writeLayerXML( layerelm, doc, relativeBasePath );
layerelm.removeChild( layerelm.firstChildElement( "id" ) );
layerselm.appendChild( layerelm );
}
Expand Down Expand Up @@ -664,6 +664,8 @@ QList<QgsMapLayer *> QgsMapLayer::fromLayerDefinitionFile( const QString qlrfile
return QList<QgsMapLayer*>();
}

QFileInfo fileinfo( file );
QDir::setCurrent( fileinfo.absoluteDir().path() );
return QgsMapLayer::fromLayerDefinition( doc );
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -217,14 +217,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
@returns true if successful
*/
bool writeLayerXML( QDomElement& layerElement, QDomDocument& document );
bool writeLayerXML( QDomElement& layerElement, QDomDocument& document, QString relativeBasePath = QString::null );

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

/** Creates a new layer from a layer defininition document
*/
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsproject.cpp
Expand Up @@ -1561,7 +1561,7 @@ QString QgsProject::readPath( QString src ) const
}

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

if ( !relativeBasePath.isNull() )
{
projPath = relativeBasePath;
}

if ( projPath.isEmpty() )
{
return src;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsproject.h
Expand Up @@ -276,7 +276,7 @@ class CORE_EXPORT QgsProject : public QObject

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

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

0 comments on commit e5850f3

Please sign in to comment.