Skip to content
Permalink
Browse files

Fixes to relative paths read/write in composer picture and arrow items

  • Loading branch information
wonder-sk committed May 13, 2017
1 parent dfcfadc commit a4717e2e6d836e1ad6d862077279c6de0587904a
@@ -439,7 +439,6 @@ QIcon QgsComposerPictureWidget::svgToIcon( const QString &filePath ) const
strokeWidth = 0.6;

bool fitsInCache; // should always fit in cache at these sizes (i.e. under 559 px ^ 2, or half cache size)
// TODO: make sure we have absolute path here (should be)
const QImage &img = QgsApplication::svgCache()->svgAsImage( filePath, 30.0, fill, stroke, strokeWidth, 3.5 /*appr. 88 dpi*/, fitsInCache );

return QIcon( QPixmap::fromImage( img ) );
@@ -256,8 +256,6 @@ void QgsComposerArrow::drawSVGMarker( QPainter *p, MarkerType type, const QStrin
if ( svgFileName.isEmpty() )
return;

// TODO: make sure svgFileName is absolute path

QSvgRenderer r;
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( svgFileName, mArrowHeadWidth, mArrowHeadFillColor, mArrowHeadStrokeColor, mArrowHeadStrokeWidth,
1.0 );
@@ -425,19 +423,27 @@ void QgsComposerArrow::setMarkerMode( MarkerMode mode )

bool QgsComposerArrow::writeXml( QDomElement &elem, QDomDocument &doc ) const
{
QgsPathResolver pathResolver;
if ( mComposition )
pathResolver = mComposition->project()->pathResolver();

QgsReadWriteContext context;
context.setPathResolver( pathResolver );

// absolute paths to relative
QString startMarkerPath = QgsSymbolLayerUtils::svgSymbolPathToName( mStartMarkerFile, pathResolver );
QString endMarkerPath = QgsSymbolLayerUtils::svgSymbolPathToName( mEndMarkerFile, pathResolver );

QDomElement composerArrowElem = doc.createElement( QStringLiteral( "ComposerArrow" ) );
composerArrowElem.setAttribute( QStringLiteral( "arrowHeadWidth" ), QString::number( mArrowHeadWidth ) );
composerArrowElem.setAttribute( QStringLiteral( "arrowHeadFillColor" ), QgsSymbolLayerUtils::encodeColor( mArrowHeadFillColor ) );
composerArrowElem.setAttribute( QStringLiteral( "arrowHeadOutlineColor" ), QgsSymbolLayerUtils::encodeColor( mArrowHeadStrokeColor ) );
composerArrowElem.setAttribute( QStringLiteral( "outlineWidth" ), QString::number( mArrowHeadStrokeWidth ) );
composerArrowElem.setAttribute( QStringLiteral( "markerMode" ), mMarkerMode );
composerArrowElem.setAttribute( QStringLiteral( "startMarkerFile" ), mStartMarkerFile );
composerArrowElem.setAttribute( QStringLiteral( "endMarkerFile" ), mEndMarkerFile );
composerArrowElem.setAttribute( QStringLiteral( "startMarkerFile" ), startMarkerPath );
composerArrowElem.setAttribute( QStringLiteral( "endMarkerFile" ), endMarkerPath );
composerArrowElem.setAttribute( QStringLiteral( "boundsBehaviorVersion" ), QString::number( mBoundsBehavior ) );

QgsReadWriteContext context;
context.setPathResolver( mComposition->project()->pathResolver() );

QDomElement styleElem = doc.createElement( QStringLiteral( "lineStyle" ) );
QDomElement lineStyleElem = QgsSymbolLayerUtils::saveSymbol( QString(), mLineSymbol, doc, context );
styleElem.appendChild( lineStyleElem );
@@ -461,19 +467,26 @@ bool QgsComposerArrow::writeXml( QDomElement &elem, QDomDocument &doc ) const

bool QgsComposerArrow::readXml( const QDomElement &itemElem, const QDomDocument &doc )
{
QgsPathResolver pathResolver;
if ( mComposition )
pathResolver = mComposition->project()->pathResolver();

QgsReadWriteContext context;
context.setPathResolver( pathResolver );

mArrowHeadWidth = itemElem.attribute( QStringLiteral( "arrowHeadWidth" ), QStringLiteral( "2.0" ) ).toDouble();
mArrowHeadFillColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "arrowHeadFillColor" ), QStringLiteral( "0,0,0,255" ) ) );
mArrowHeadStrokeColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "arrowHeadOutlineColor" ), QStringLiteral( "0,0,0,255" ) ) );
mArrowHeadStrokeWidth = itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "1.0" ) ).toDouble();
setStartMarker( itemElem.attribute( QStringLiteral( "startMarkerFile" ), QLatin1String( "" ) ) );
setEndMarker( itemElem.attribute( QStringLiteral( "endMarkerFile" ), QLatin1String( "" ) ) );
// relative paths to absolute
QString startMarkerPath = itemElem.attribute( QStringLiteral( "startMarkerFile" ), QLatin1String( "" ) );
QString endMarkerPath = itemElem.attribute( QStringLiteral( "endMarkerFile" ), QLatin1String( "" ) );
setStartMarker( QgsSymbolLayerUtils::svgSymbolNameToPath( startMarkerPath, pathResolver ) );
setEndMarker( QgsSymbolLayerUtils::svgSymbolNameToPath( endMarkerPath, pathResolver ) );
mMarkerMode = QgsComposerArrow::MarkerMode( itemElem.attribute( QStringLiteral( "markerMode" ), QStringLiteral( "0" ) ).toInt() );
//if bounds behavior version is not set, default to 2.2 behavior
mBoundsBehavior = itemElem.attribute( QStringLiteral( "boundsBehaviorVersion" ), QStringLiteral( "22" ) ).toInt();

QgsReadWriteContext context;
context.setPathResolver( mComposition->project()->pathResolver() );

//arrow style
QDomElement styleElem = itemElem.firstChildElement( QStringLiteral( "lineStyle" ) );
if ( !styleElem.isNull() )
@@ -24,6 +24,7 @@
#include "qgsexpression.h"
#include "qgsvectorlayer.h"
#include "qgsmessagelog.h"
#include "qgspathresolver.h"
#include "qgsproperty.h"
#include "qgsnetworkcontentfetcher.h"
#include "qgssymbollayerutils.h"
@@ -367,8 +368,7 @@ void QgsComposerPicture::loadLocalPicture( const QString &path )
QColor fillColor = mDataDefinedProperties.valueAsColor( QgsComposerObject::PictureSvgBackgroundColor, context, mSvgFillColor );
QColor strokeColor = mDataDefinedProperties.valueAsColor( QgsComposerObject::PictureSvgStrokeColor, context, mSvgStrokeColor );
double strokeWidth = mDataDefinedProperties.valueAsDouble( QgsComposerObject::PictureSvgStrokeWidth, context, mSvgStrokeWidth );
// TODO: check that this works
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( pic.fileName(), rect().width(), fillColor, strokeColor, strokeWidth,
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( path, rect().width(), fillColor, strokeColor, strokeWidth,
1.0 );
mSVG.load( svgContent );
if ( mSVG.isValid() )
@@ -742,7 +742,17 @@ bool QgsComposerPicture::writeXml( QDomElement &elem, QDomDocument &doc ) const
return false;
}
QDomElement composerPictureElem = doc.createElement( QStringLiteral( "ComposerPicture" ) );
composerPictureElem.setAttribute( QStringLiteral( "file" ), mComposition->project()->writePath( mSourcePath ) );
QString imagePath = mSourcePath;
if ( mComposition )
{
// convert from absolute path to relative. For SVG we also need to consider system SVG paths
QgsPathResolver pathResolver = mComposition->project()->pathResolver();
if ( imagePath.endsWith( ".svg", Qt::CaseInsensitive ) )
imagePath = QgsSymbolLayerUtils::svgSymbolPathToName( imagePath, pathResolver );
else
imagePath = pathResolver.writePath( imagePath );
}
composerPictureElem.setAttribute( QStringLiteral( "file" ), imagePath );
composerPictureElem.setAttribute( QStringLiteral( "pictureWidth" ), QString::number( mPictureWidth ) );
composerPictureElem.setAttribute( QStringLiteral( "pictureHeight" ), QString::number( mPictureHeight ) );
composerPictureElem.setAttribute( QStringLiteral( "resizeMode" ), QString::number( static_cast< int >( mResizeMode ) ) );
@@ -813,8 +823,17 @@ bool QgsComposerPicture::readXml( const QDomElement &itemElem, const QDomDocumen
mDataDefinedProperties.setProperty( QgsComposerObject::PictureSource, QgsProperty::fromExpression( sourceExpression, expressionActive ) );
}

mSourcePath = mComposition ? mComposition->project()->readPath( itemElem.attribute( QStringLiteral( "file" ) ) )
: itemElem.attribute( QStringLiteral( "file" ) );
QString imagePath = itemElem.attribute( QStringLiteral( "file" ) );
if ( mComposition )
{
// convert from relative path to absolute. For SVG we also need to consider system SVG paths
QgsPathResolver pathResolver = mComposition->project()->pathResolver();
if ( imagePath.endsWith( ".svg", Qt::CaseInsensitive ) )
imagePath = QgsSymbolLayerUtils::svgSymbolNameToPath( imagePath, pathResolver );
else
imagePath = pathResolver.readPath( imagePath );
}
mSourcePath = imagePath;

//picture rotation
if ( !qgsDoubleNear( itemElem.attribute( QStringLiteral( "pictureRotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )

0 comments on commit a4717e2

Please sign in to comment.