Skip to content
Permalink
Browse files

Consider svg parameters for svg fill

  • Loading branch information
mhugent committed Feb 6, 2012
1 parent dd60292 commit af71f5fac9d59ac0478cebd884762a7c1b1a3d24
@@ -206,6 +206,7 @@ QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QString& svgFilePath, double
setSvgFilePath( svgFilePath );
mOutlineWidth = 0.3;
mAngle = angle;
setDefaultSvgParams();
}

QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QByteArray& svgData, double width, double angle ): QgsImageFillSymbolLayer(), mPatternWidth( width ),
@@ -215,6 +216,7 @@ QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QByteArray& svgData, double
mOutlineWidth = 0.3;
mAngle = angle;
setSubSymbol( new QgsLineSymbolV2() );
setDefaultSvgParams();
}

QgsSVGFillSymbolLayer::~QgsSVGFillSymbolLayer()
@@ -228,9 +230,11 @@ void QgsSVGFillSymbolLayer::setSvgFilePath( const QString& svgPath )
if ( svgFile.open( QFile::ReadOnly ) )
{
mSvgData = svgFile.readAll();

storeViewBox();
}
mSvgFilePath = svgPath;
setDefaultSvgParams();
}

QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties )
@@ -240,7 +244,6 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
QString svgFilePath;
double angle = 0.0;


if ( properties.contains( "width" ) )
{
width = properties["width"].toDouble();
@@ -256,19 +259,36 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
angle = properties["angle"].toDouble();
}

QgsSVGFillSymbolLayer* symbolLayer = 0;
if ( !svgFilePath.isEmpty() )
{
return new QgsSVGFillSymbolLayer( svgFilePath, width, angle );
symbolLayer = new QgsSVGFillSymbolLayer( svgFilePath, width, angle );
}
else
{
if ( properties.contains( "data" ) )
{
data = QByteArray::fromHex( properties["data"].toLocal8Bit() );
}
symbolLayer = new QgsSVGFillSymbolLayer( data, width, angle );
}

return new QgsSVGFillSymbolLayer( data, width, angle );
//svg parameters
if ( properties.contains( "svgFillColor" ) )
{
symbolLayer->setSvgFillColor( QColor( properties["svgFillColor"] ) );
}
if ( properties.contains( "svgOutlineColor" ) )
{
symbolLayer->setSvgOutlineColor( QColor( properties["svgOutlineColor"] ) );
}
if ( properties.contains( "svgOutlineWidth" ) )
{
symbolLayer->setSvgOutlineWidth( properties["svgOutlineWidth"].toDouble() );
}


return symbolLayer;
}

QString QgsSVGFillSymbolLayer::layerType() const
@@ -283,11 +303,8 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
return;
}

QColor fillColor( Qt::black );
QColor outlineColor( Qt::black );
double outlineWidth = 1;
int size = context.outputPixelSize( mPatternWidth );
const QImage& patternImage = QgsSvgCache::instance()->svgAsImage( mSvgFilePath, size, fillColor, outlineColor, outlineWidth,
const QImage& patternImage = QgsSvgCache::instance()->svgAsImage( mSvgFilePath, size, mSvgFillColor, mSvgOutlineColor, mSvgOutlineWidth,
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
QTransform brushTransform;
brushTransform.scale( 1.0 / context.renderContext().rasterScaleFactor(), 1.0 / context.renderContext().rasterScaleFactor() );
@@ -331,6 +348,12 @@ QgsStringMap QgsSVGFillSymbolLayer::properties() const

map.insert( "width", QString::number( mPatternWidth ) );
map.insert( "angle", QString::number( mAngle ) );

//svg parameters
map.insert( "svgFillColor", mSvgFillColor.name() );
map.insert( "svgOutlineColor", mSvgOutlineColor.name() );
map.insert( "svgOutlineWidth", QString::number( mSvgOutlineWidth ) );

return map;
}

@@ -340,6 +363,10 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
if ( !mSvgFilePath.isEmpty() )
{
clonedLayer = new QgsSVGFillSymbolLayer( mSvgFilePath, mPatternWidth, mAngle );
QgsSVGFillSymbolLayer* sl = static_cast<QgsSVGFillSymbolLayer*>( clonedLayer );
sl->setSvgFillColor( mSvgFillColor );
sl->setSvgOutlineColor( mSvgOutlineColor );
sl->setSvgOutlineWidth( mSvgOutlineWidth );
}
else
{
@@ -369,6 +396,38 @@ void QgsSVGFillSymbolLayer::storeViewBox()
return;
}

void QgsSVGFillSymbolLayer::setDefaultSvgParams()
{
//default values
mSvgFillColor = QColor( 0, 0, 0 );
mSvgOutlineColor = QColor( 0, 0, 0 );
mSvgOutlineWidth = 0.3;

if ( mSvgFilePath.isEmpty() )
{
return;
}

bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
QColor defaultFillColor, defaultOutlineColor;
double defaultOutlineWidth;
QgsSvgCache::instance()->containsParams( mSvgFilePath, hasFillParam, defaultFillColor, hasOutlineParam, defaultOutlineColor, hasOutlineWidthParam,
defaultOutlineWidth );

if ( hasFillParam )
{
mSvgFillColor = defaultFillColor;
}
if ( hasOutlineParam )
{
mSvgOutlineColor = defaultOutlineColor;
}
if ( hasOutlineWidthParam )
{
mSvgOutlineWidth = defaultOutlineWidth;
}
}

QgsLinePatternFillSymbolLayer::QgsLinePatternFillSymbolLayer(): QgsImageFillSymbolLayer()
{
}
@@ -115,6 +115,13 @@ class CORE_EXPORT QgsSVGFillSymbolLayer: public QgsImageFillSymbolLayer
void setPatternWidth( double width ) { mPatternWidth = width;}
double patternWidth() const { return mPatternWidth; }

void setSvgFillColor( const QColor& c ) { mSvgFillColor = c; }
QColor svgFillColor() const { return mSvgFillColor; }
void setSvgOutlineColor( const QColor& c ) { mSvgOutlineColor = c; }
QColor svgOutlineColor() const { return mSvgOutlineColor; }
void setSvgOutlineWidth( double w ) { mSvgOutlineWidth = w; }
double svgOutlineWidth() const { return mSvgOutlineWidth; }

protected:
/**Width of the pattern (in QgsSymbolV2 output units)*/
double mPatternWidth;
@@ -125,9 +132,16 @@ class CORE_EXPORT QgsSVGFillSymbolLayer: public QgsImageFillSymbolLayer
/**SVG view box (to keep the aspect ratio */
QRectF mSvgViewBox;

//param(fill), param(outline), param(outline-width) are going
//to be replaced in memory
QColor mSvgFillColor;
QColor mSvgOutlineColor;
double mSvgOutlineWidth;

private:
/**Helper function that gets the view box from the byte array*/
void storeViewBox();
void setDefaultSvgParams(); //fills mSvgFillColor, mSvgOutlineColor, mSvgOutlineWidth with default values for mSvgFilePath
};

class CORE_EXPORT QgsLinePatternFillSymbolLayer: public QgsImageFillSymbolLayer
@@ -828,6 +828,7 @@ void QgsSVGFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayerV2* layer )
mRotationSpinBox->setValue( mLayer->angle() );
}
updateOutlineIcon();
updateParamGui();
}

QgsSymbolLayerV2* QgsSVGFillSymbolLayerWidget::symbolLayer()
@@ -868,11 +869,13 @@ void QgsSVGFillSymbolLayerWidget::on_mSVGLineEdit_textChanged( const QString & t
}
mLayer->setSvgFilePath( text );
emit changed();
updateParamGui();
}

void QgsSVGFillSymbolLayerWidget::setFile( const QModelIndex& item )
{
mSVGLineEdit->setText( item.data( Qt::UserRole ).toString() );
updateParamGui();
}

void QgsSVGFillSymbolLayerWidget::insertIcons()
@@ -911,6 +914,55 @@ void QgsSVGFillSymbolLayerWidget::updateOutlineIcon()
}
}

void QgsSVGFillSymbolLayerWidget::updateParamGui()
{
//activate gui for svg parameters only if supported by the svg file
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
QColor defaultFill, defaultOutline;
double defaultOutlineWidth;
QgsSvgCache::instance()->containsParams( mSVGLineEdit->text(), hasFillParam, defaultFill, hasOutlineParam, defaultOutline, hasOutlineWidthParam, defaultOutlineWidth );
mChangeColorButton->setEnabled( hasFillParam );
mChangeBorderColorButton->setEnabled( hasOutlineParam );
mBorderWidthSpinBox->setEnabled( hasOutlineWidthParam );
}

void QgsSVGFillSymbolLayerWidget::on_mChangeColorButton_clicked()
{
if ( !mLayer )
{
return;
}
QColor c = QColorDialog::getColor( mLayer->svgFillColor() );
if ( c.isValid() )
{
mLayer->setSvgFillColor( c );
emit changed();
}
}

void QgsSVGFillSymbolLayerWidget::on_mChangeBorderColorButton_clicked()
{
if ( !mLayer )
{
return;
}
QColor c = QColorDialog::getColor( mLayer->svgOutlineColor() );
if ( c.isValid() )
{
mLayer->setSvgOutlineColor( c );
emit changed();
}
}

void QgsSVGFillSymbolLayerWidget::on_mBorderWidthSpinBox_valueChanged( double d )
{
if ( mLayer )
{
mLayer->setSvgOutlineWidth( d );
emit changed();
}
}

/////////////

QgsLinePatternFillSymbolLayerWidget::QgsLinePatternFillSymbolLayerWidget( const QgsVectorLayer* vl, QWidget* parent ):
@@ -251,6 +251,7 @@ class GUI_EXPORT QgsSVGFillSymbolLayerWidget : public QgsSymbolLayerV2Widget, pr
void setOutputUnit();
void insertIcons();
void updateOutlineIcon();
void updateParamGui();

private slots:
void on_mBrowseToolButton_clicked();
@@ -259,6 +260,9 @@ class GUI_EXPORT QgsSVGFillSymbolLayerWidget : public QgsSymbolLayerV2Widget, pr
void setFile( const QModelIndex& item );
void on_mChangeOutlinePushButton_clicked();
void on_mRotationSpinBox_valueChanged( double d );
void on_mChangeColorButton_clicked();
void on_mChangeBorderColorButton_clicked();
void on_mBorderWidthSpinBox_valueChanged( double d );
};

//////////

0 comments on commit af71f5f

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