Skip to content

Commit d15355e

Browse files
author
jef
committedMay 27, 2009
allow project relative position of file data sources and svgs
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10854 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed
 

‎src/core/qgsmaplayer.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ bool QgsMapLayer::readXML( QDomNode & layer_node )
152152
QDomElement mne = mnl.toElement();
153153
mDataSource = mne.text();
154154

155+
QFileInfo fi( mDataSource );
156+
if ( !fi.exists() && fi.isRelative() )
157+
{
158+
QFileInfo pfi( QgsProject::instance()->fileName() );
159+
if ( pfi.exists() )
160+
{
161+
fi.setFile( pfi.canonicalPath() + QDir::separator() + mDataSource );
162+
163+
if ( fi.exists() )
164+
{
165+
mDataSource = fi.canonicalPath();
166+
}
167+
}
168+
}
169+
155170
// Set the CRS from project file, asking the user if necessary.
156171
// Make it the saved CRS to have WMS layer projected correctly.
157172
// We will still overwrite whatever GDAL etc picks up anyway
@@ -182,7 +197,7 @@ bool QgsMapLayer::readXML( QDomNode & layer_node )
182197
}
183198

184199
// the internal name is just the data source basename
185-
QFileInfo dataSourceFileInfo( mDataSource );
200+
//QFileInfo dataSourceFileInfo( mDataSource );
186201
//internalName = dataSourceFileInfo.baseName();
187202

188203
// set ID
@@ -263,7 +278,20 @@ bool QgsMapLayer::writeXML( QDomNode & layer_node, QDomDocument & document )
263278

264279
// data source
265280
QDomElement dataSource = document.createElement( "datasource" );
266-
QDomText dataSourceText = document.createTextNode( source() );
281+
282+
QString src = source();
283+
QFileInfo srcInfo( src );
284+
285+
if ( srcInfo.exists() )
286+
{
287+
QFileInfo pfi( QgsProject::instance()->fileName() );
288+
QgsDebugMsg( "project path: " + pfi.canonicalPath() );
289+
QgsDebugMsg( "src path: " + srcInfo.canonicalFilePath() + QDir::separator() );
290+
if ( srcInfo.canonicalFilePath().startsWith( pfi.canonicalPath() + QDir::separator() ) )
291+
src = src.mid( pfi.canonicalPath().size() + 1 );
292+
}
293+
294+
QDomText dataSourceText = document.createTextNode( src );
267295
dataSource.appendChild( dataSourceText );
268296

269297
maplayer.appendChild( dataSource );

‎src/core/symbology/qgssymbol.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsmarkercatalogue.h"
2626
#include "qgsapplication.h"
2727
#include "qgsvectorlayer.h"
28+
#include "qgsproject.h"
2829

2930
#include <QPainter>
3031
#include <QDomNode>
@@ -221,6 +222,19 @@ void QgsSymbol::setNamedPointSymbol( QString name )
221222
name = "svg:" + myLocalPath;
222223
QgsDebugMsg( "Svg found in alternative path" );
223224
}
225+
else if ( myInfo.isRelative() )
226+
{
227+
QFileInfo pfi( QgsProject::instance()->fileName() );
228+
if ( pfi.exists() && QFile( pfi.canonicalPath() + QDir::separator() + myTempName ).exists() )
229+
{
230+
name = "svg:" + pfi.canonicalPath() + QDir::separator() + myTempName;
231+
QgsDebugMsg( "Svg found in alternative path" );
232+
}
233+
else
234+
{
235+
QgsDebugMsg( "Svg not found in project path" );
236+
}
237+
}
224238
else
225239
{
226240
//couldnt find the file, no happy ending :-(
@@ -445,7 +459,29 @@ bool QgsSymbol::writeXML( QDomNode & item, QDomDocument & document, const QgsVec
445459
appendText( symbol, document, "lowervalue", mLowerValue );
446460
appendText( symbol, document, "uppervalue", mUpperValue );
447461
appendText( symbol, document, "label", mLabel );
448-
appendText( symbol, document, "pointsymbol", pointSymbolName() );
462+
463+
QString name = pointSymbolName();
464+
if ( name.startsWith( "svg:" ) )
465+
{
466+
name = name.mid( 4 );
467+
468+
QFileInfo fi( name );
469+
if ( fi.exists() )
470+
{
471+
name = fi.canonicalFilePath();
472+
473+
QString dir = QFileInfo( QgsApplication::svgPath() ).canonicalFilePath();
474+
475+
if ( !dir.isEmpty() && name.startsWith( dir ) )
476+
{
477+
name = name.mid( dir.size() );
478+
}
479+
}
480+
481+
name = "svg:" + name;
482+
}
483+
484+
appendText( symbol, document, "pointsymbol", name );
449485
appendText( symbol, document, "pointsize", QString::number( pointSize() ) );
450486

451487
if ( vl )

0 commit comments

Comments
 (0)
Please sign in to comment.