Navigation Menu

Skip to content

Commit

Permalink
Fixes to relative paths read/write in composer picture and arrow items
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 13, 2017
1 parent dfcfadc commit a4717e2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/app/composer/qgscomposerpicturewidget.cpp
Expand Up @@ -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 ) );
Expand Down
37 changes: 25 additions & 12 deletions src/core/composer/qgscomposerarrow.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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() )
Expand Down
29 changes: 24 additions & 5 deletions src/core/composer/qgscomposerpicture.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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 ) ) );
Expand Down Expand Up @@ -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 ) )
Expand Down

0 comments on commit a4717e2

Please sign in to comment.