Skip to content
Permalink
Browse files

Try to consider default parameters

  • Loading branch information
mhugent committed Jun 29, 2011
1 parent 05b4183 commit 66c009fca756a05b726a1703ae930a85a52a1ded
@@ -470,6 +470,28 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::create( const QgsStringMap& props )
angle = props["angle"].toDouble();

QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2( name, size, angle );

//we only check the svg default parameters if necessary, since it could be expensive
if( !props.contains("fill") && !props.contains("outline") && !props.contains("outline-width") )
{
QColor fillColor, outlineColor;
double outlineWidth;
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
QgsSvgCache::instance()->containsParams( name, hasFillParam, fillColor, hasOutlineParam, outlineColor, hasOutlineWidthParam, outlineWidth );
if( hasFillParam )
{
m->setFillColor( fillColor );
}
if( hasOutlineParam )
{
m->setOutlineColor( outlineColor );
}
if( hasOutlineWidthParam )
{
m->setOutlineWidth( outlineWidth );
}
}

if ( props.contains( "offset" ) )
m->setOffset( QgsSymbolLayerV2Utils::decodePoint( props["offset"] ) );
if ( props.contains( "fill" ) )
@@ -481,6 +503,27 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::create( const QgsStringMap& props )
return m;
}

void QgsSvgMarkerSymbolLayerV2::setPath( QString path )
{
mPath = path;
QColor fillColor, outlineColor;
double outlineWidth;
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
QgsSvgCache::instance()->containsParams( path, hasFillParam, fillColor, hasOutlineParam, outlineColor, hasOutlineWidthParam, outlineWidth );
if( hasFillParam )
{
setFillColor( fillColor );
}
if( hasOutlineParam )
{
setOutlineColor( outlineColor );
}
if( hasOutlineWidthParam )
{
setOutlineWidth( outlineWidth );
}
}


QString QgsSvgMarkerSymbolLayerV2::layerType() const
{
@@ -112,7 +112,7 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
QgsSymbolLayerV2* clone() const;

QString path() const { return mPath; }
void setPath( QString path ) { mPath = path; }
void setPath( QString path );

QColor fillColor() const { return mFillColor; }
void setFillColor( const QColor& c ) { mFillColor = c; }
@@ -156,9 +156,10 @@ QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, cons
return entry;
}

void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, bool& hasOutlineParam, bool& hasOutlineWidthParam ) const
void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor,
bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const
{
hasFillParam = false;
/*hasFillParam = false;
hasOutlineParam = false;
hasOutlineWidthParam = false;
@@ -187,7 +188,26 @@ void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, bool&
if ( content.contains( "param(outline-width)" ) )
{
hasOutlineWidthParam = true;
}*/

defaultFillColor = QColor( Qt::black );
defaultOutlineColor= QColor( Qt::black );
defaultOutlineWidth = 1.0;

QFile svgFile( path );
if ( !svgFile.open( QIODevice::ReadOnly ) )
{
return;
}

QDomDocument svgDoc;
if ( !svgDoc.setContent( &svgFile ) )
{
return;
}

QDomElement docElem = svgDoc.documentElement();
containsElemParams( docElem, hasFillParam, defaultFillColor, hasOutlineParam, defaultOutlineColor, hasOutlineWidthParam, defaultOutlineWidth );
}

void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
@@ -377,6 +397,103 @@ void QgsSvgCache::replaceElemParams( QDomElement& elem, const QColor& fill, cons
}
}

void QgsSvgCache::containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const
{
if( elem.isNull() )
{
return;
}

//we already have all the information, no need to go deeper
if( hasFillParam && hasOutlineParam && hasOutlineWidthParam )
{
return;
}

//check this elements attribute
QDomNamedNodeMap attributes = elem.attributes();
int nAttributes = attributes.count();

QStringList valueSplit;
for ( int i = 0; i < nAttributes; ++i )
{
QDomAttr attribute = attributes.item( i ).toAttr();
if ( attribute.name().compare( "style", Qt::CaseInsensitive ) == 0 )
{
//entries separated by ';'
QStringList entryList = attribute.value().split( ';' );
QStringList::const_iterator entryIt = entryList.constBegin();
for ( ; entryIt != entryList.constEnd(); ++entryIt )
{
valueSplit = entryIt->split(" ");
if( !hasFillParam && entryIt->startsWith( "param(fill)" ) )
{
hasFillParam = true;
if( valueSplit.size() > 1 )
{
defaultFill = QColor( valueSplit.at( 1 ) );
}
}
else if( !hasOutlineParam && entryIt->startsWith( "param(outline)" ) )
{
hasOutlineParam = true;
if( valueSplit.size() > 1 )
{
defaultOutline = QColor( valueSplit.at( 1 ) );
}
}
else if( !hasOutlineWidthParam && entryIt->startsWith( "param(outlineWidth)" ) )
{
hasOutlineWidthParam = true;
if( valueSplit.size() > 1 )
{
defaultOutlineWidth = valueSplit.at( 1 ).toDouble();
}
}
}
}
else
{
QString value = attribute.value();
valueSplit = value.split(" ");
if ( !hasFillParam && value.startsWith( "param(fill)" ) )
{
hasFillParam = true;
if( valueSplit.size() > 1 )
{
defaultFill = QColor( valueSplit.at( 1 ) );
}
}
else if( !hasOutlineParam && value.startsWith( "param(outline)" ) )
{
hasOutlineParam = true;
if( valueSplit.size() > 1 )
{
defaultOutline = QColor( valueSplit.at( 1 ) );
}
}
else if( !hasOutlineWidthParam && value.startsWith( "param(outlineWidth)" ) )
{
hasOutlineWidthParam = true;
if( valueSplit.size() > 1 )
{
defaultOutlineWidth = valueSplit.at( 1 ).toDouble();
}
}
}
}

//pass it further to child items
QDomNodeList childList = elem.childNodes();
int nChildren = childList.count();
for ( int i = 0; i < nChildren; ++i )
{
QDomElement childElem = childList.at( i ).toElement();
containsElemParams( childElem, hasFillParam, defaultFill, hasOutlineParam, defaultOutline, hasOutlineWidthParam, defaultOutlineWidth );
}
}

void QgsSvgCache::removeCacheEntry( QString s, QgsSvgCacheEntry* entry )
{
delete entry;
@@ -70,8 +70,10 @@ class QgsSvgCache
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

/**Tests if an svg file contains parameters for fill, outline color, outline width*/
void containsParams( const QString& path, bool& hasFillParam, bool& hasOutlineParam, bool& hasOutlineWidthParam ) const;
/**Tests if an svg file contains parameters for fill, outline color, outline width. If yes, possible default values are returned. If there are several
default values in the svg file, only the first one is considered*/
void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
double& defaultOutlineWidth ) const;

protected:
QgsSvgCache();
@@ -112,6 +114,9 @@ class QgsSvgCache
/**Replaces parameters in elements of a dom node and calls method for all child nodes*/
void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );

void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;

/**Release memory and remove cache entry from mEntryLookup*/
void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );

@@ -576,18 +576,29 @@ void QgsSvgMarkerSymbolLayerV2Widget::populateList()
viewImages->setModel( m );
}

void QgsSvgMarkerSymbolLayerV2Widget::setGuiForSvg( const QString& svgPath )
void QgsSvgMarkerSymbolLayerV2Widget::setGuiForSvg( const QgsSvgMarkerSymbolLayerV2* layer )
{
if( !layer )
{
return;
}

//activate gui for svg parameters only if supported by the svg file
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
QgsSvgCache::instance()->containsParams( svgPath, hasFillParam, hasOutlineParam, hasOutlineWidthParam );
QColor defaultFill, defaultOutline;
double defaultOutlineWidth;
QgsSvgCache::instance()->containsParams( layer->path(), hasFillParam, defaultFill, hasOutlineParam, defaultOutline, hasOutlineWidthParam, defaultOutlineWidth );
mChangeColorButton->setEnabled( hasFillParam );
mChangeBorderColorButton->setEnabled( hasOutlineParam );
mBorderWidthSpinBox->setEnabled( hasOutlineWidthParam );

mFileLineEdit->blockSignals( true );
mFileLineEdit->setText( svgPath );
mFileLineEdit->setText( layer->path() );
mFileLineEdit->blockSignals( false );

mBorderWidthSpinBox->blockSignals( true );
mBorderWidthSpinBox->setValue( layer->outlineWidth() );
mBorderWidthSpinBox->blockSignals( false );
}


@@ -628,7 +639,8 @@ void QgsSvgMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
spinOffsetY->setValue( mLayer->offset().y() );
spinOffsetY->blockSignals( false );

setGuiForSvg( mLayer->path() );
setGuiForSvg( mLayer );

}

QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2Widget::symbolLayer()
@@ -642,7 +654,7 @@ void QgsSvgMarkerSymbolLayerV2Widget::setName( const QModelIndex& idx )
mLayer->setPath( name );
mFileLineEdit->setText( name );

setGuiForSvg( name );
setGuiForSvg( mLayer );
emit changed();
}

@@ -189,7 +189,7 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerV2Widget : public QgsSymbolLayerV2Widget

void populateList();
//update gui for svg file (insert new path, update activation of gui elements for svg params)
void setGuiForSvg( const QString& svgPath );
void setGuiForSvg( const QgsSvgMarkerSymbolLayerV2* layer );

QgsSvgMarkerSymbolLayerV2* mLayer;
};

0 comments on commit 66c009f

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