Skip to content
Permalink
Browse files

Fix #10858 - Set relative path to QLR save folder. Load relative to Q…

…LR file
  • Loading branch information
NathanW2 committed Oct 6, 2014
1 parent 6d850ad commit e5850f38b152e9072c5ae26ae5c5966846b7be3a
Showing with 21 additions and 13 deletions.
  1. +1 −1 python/core/qgsproject.sip
  2. +2 −1 src/app/qgisapp.cpp
  3. +9 −7 src/core/qgsmaplayer.cpp
  4. +2 −2 src/core/qgsmaplayer.h
  5. +6 −1 src/core/qgsproject.cpp
  6. +1 −1 src/core/qgsproject.h
@@ -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 */
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
}
@@ -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 );
}

@@ -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
*/
@@ -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() )
{
@@ -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;
@@ -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 */

0 comments on commit e5850f3

Please sign in to comment.
You can’t perform that action at this time.