Skip to content

Commit

Permalink
Change color through svg marker dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 25, 2011
1 parent e112204 commit 49d8704
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 35 deletions.
25 changes: 20 additions & 5 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -450,6 +450,9 @@ QgsSvgMarkerSymbolLayerV2::QgsSvgMarkerSymbolLayerV2( QString name, double size,
mSize = size;
mAngle = angle;
mOffset = QPointF( 0, 0 );
mOutlineWidth = 1.0;
mFillColor = QColor( Qt::black );
mOutlineColor = QColor( Qt::black );
}


Expand All @@ -469,6 +472,12 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::create( const QgsStringMap& props )
QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2( name, size, angle );
if ( props.contains( "offset" ) )
m->setOffset( QgsSymbolLayerV2Utils::decodePoint( props["offset"] ) );
if ( props.contains( "fill" ) )
m->setFillColor( QColor( props["fill"] ) );
if ( props.contains( "outline" ) )
m->setOutlineColor( QColor( props["outline"] ) );
if ( props.contains( "outline-width" ) )
m->setOutlineWidth( props["outline-width"].toDouble() );
return m;
}

Expand Down Expand Up @@ -534,16 +543,16 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
if ( mAngle != 0 )
p->rotate( mAngle );

if( doubleNear( context.renderContext().rasterScaleFactor(), 1.0 ) )
if ( doubleNear( context.renderContext().rasterScaleFactor(), 1.0, 0.1 ) )
{
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, mSize, mFillColor, mOutlineColor, mOutlineWidth,
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawImage( -img.width() / 2.0, -img.width() / 2.0, img );
}
else
{
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, mFillColor, mOutlineColor, mOutlineWidth,
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawPicture( 0, 0, pct );
}

Expand All @@ -558,12 +567,18 @@ QgsStringMap QgsSvgMarkerSymbolLayerV2::properties() const
map["size"] = QString::number( mSize );
map["angle"] = QString::number( mAngle );
map["offset"] = QgsSymbolLayerV2Utils::encodePoint( mOffset );
map["fill"] = mFillColor.name();
map["outline"] = mOutlineColor.name();
map["outline-width"] = QString::number( mOutlineWidth );
return map;
}

QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::clone() const
{
QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2( mPath, mSize, mAngle );
m->setFillColor( mFillColor );
m->setOutlineColor( mOutlineColor );
m->setOutlineWidth( 1.0 );
m->setOffset( mOffset );
return m;
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.h
Expand Up @@ -114,11 +114,28 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
QString path() const { return mPath; }
void setPath( QString path ) { mPath = path; }

QColor fillColor() const { return mFillColor; }
void setFillColor( const QColor& c ) { mFillColor = c; }

QColor outlineColor() const { return mOutlineColor; }
void setOutlineColor( const QColor& c ) { mOutlineColor = c; }

double outlineWidth() const { return mOutlineWidth; }
void setOutlineWidth( double w ) { mOutlineWidth = w; }

protected:

void loadSvg();

QString mPath;

//param(fill), param(outline), param(outline-width) are going
//to be replaced in memory
QColor mFillColor;
QColor mOutlineColor;
double mOutlineWidth;


QPicture mPicture;
QPicture mSelPicture;
double mOrigSize;
Expand Down
50 changes: 25 additions & 25 deletions src/core/symbology-ng/qgssvgcache.cpp
Expand Up @@ -25,12 +25,12 @@
#include <QSvgRenderer>

QgsSvgCacheEntry::QgsSvgCacheEntry(): file( QString() ), size( 0 ), outlineWidth( 0 ), widthScaleFactor( 1.0 ), rasterScaleFactor( 1.0 ), fill( Qt::black ),
outline( Qt::black ), image( 0 ), picture( 0 )
outline( Qt::black ), image( 0 ), picture( 0 )
{
}

QgsSvgCacheEntry::QgsSvgCacheEntry( const QString& f, double s, double ow, double wsf, double rsf, const QColor& fi, const QColor& ou ): file( f ), size( s ), outlineWidth( ow ),
widthScaleFactor( wsf ), rasterScaleFactor( rsf ), fill( fi ), outline( ou ), image( 0 ), picture( 0 )
widthScaleFactor( wsf ), rasterScaleFactor( rsf ), fill( fi ), outline( ou ), image( 0 ), picture( 0 )
{
}

Expand Down Expand Up @@ -73,7 +73,7 @@ QgsSvgCache::QgsSvgCache()
QgsSvgCache::~QgsSvgCache()
{
QMap< QDateTime, QgsSvgCacheEntry* >::iterator it = mEntries.begin();
for(; it != mEntries.end(); ++it )
for ( ; it != mEntries.end(); ++it )
{
delete it.value();
}
Expand All @@ -83,11 +83,11 @@ QgsSvgCache::~QgsSvgCache()
const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* currentEntry = this->cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
QgsSvgCacheEntry* currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );

//if current entry image is 0: cache image for entry
//update stats for memory usage
if( !currentEntry->image )
if ( !currentEntry->image )
{
cacheImage( currentEntry );
}
Expand All @@ -98,13 +98,13 @@ const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const Q
}

const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* currentEntry = this->cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
QgsSvgCacheEntry* currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );

//if current entry image is 0: cache image for entry
//update stats for memory usage
if( !currentEntry->picture )
if ( !currentEntry->picture )
{
cachePicture( currentEntry );
}
Expand All @@ -115,7 +115,7 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
}

QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* entry = new QgsSvgCacheEntry( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
entry->lastUsed = QDateTime::currentDateTime();
Expand All @@ -129,19 +129,19 @@ QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, cons

void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
{
if( !entry )
if ( !entry )
{
return;
}

QFile svgFile( entry->file );
if( !svgFile.open( QIODevice::ReadOnly ) )
if ( !svgFile.open( QIODevice::ReadOnly ) )
{
return;
}

QDomDocument svgDoc;
if( !svgDoc.setContent( &svgFile ) )
if ( !svgDoc.setContent( &svgFile ) )
{
return;
}
Expand All @@ -155,7 +155,7 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )

void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
{
if( !entry )
if ( !entry )
{
return;
}
Expand All @@ -176,7 +176,7 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )

void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
{
if( !entry )
if ( !entry )
{
return;
}
Expand All @@ -198,18 +198,18 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
}

QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
double widthScaleFactor, double rasterScaleFactor )
{
//search entries in mEntryLookup
QgsSvgCacheEntry* currentEntry = 0;
QList<QgsSvgCacheEntry*> entries = mEntryLookup.values( file );

QList<QgsSvgCacheEntry*>::iterator entryIt = entries.begin();
for(; entryIt != entries.end(); ++entryIt )
for ( ; entryIt != entries.end(); ++entryIt )
{
QgsSvgCacheEntry* cacheEntry = *entryIt;
if( cacheEntry->file == file && cacheEntry->size == size && cacheEntry->fill == fill && cacheEntry->outline == outline &&
cacheEntry->outlineWidth == outlineWidth && cacheEntry->widthScaleFactor == widthScaleFactor && cacheEntry->rasterScaleFactor == rasterScaleFactor)
if ( cacheEntry->file == file && cacheEntry->size == size && cacheEntry->fill == fill && cacheEntry->outline == outline &&
cacheEntry->outlineWidth == outlineWidth && cacheEntry->widthScaleFactor == widthScaleFactor && cacheEntry->rasterScaleFactor == rasterScaleFactor )
{
currentEntry = cacheEntry;
break;
Expand All @@ -219,7 +219,7 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con

//if not found: create new entry
//cache and replace params in svg content
if( !currentEntry )
if ( !currentEntry )
{
currentEntry = insertSVG( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
}
Expand All @@ -228,35 +228,35 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con

void QgsSvgCache::replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth )
{
if( elem.isNull() )
if ( elem.isNull() )
{
return;
}

//go through attributes
QDomNamedNodeMap attributes = elem.attributes();
int nAttributes = attributes.count();
for( int i = 0; i < nAttributes; ++i )
for ( int i = 0; i < nAttributes; ++i )
{
QDomAttr attribute = attributes.item( i ).toAttr();
QString value = attribute.value();
if( value.startsWith("params(fill)") )
if ( value.startsWith( "param(fill)" ) )
{
elem.setAttribute( attribute.name(), fill.name() );
}
else if( value.startsWith("params(outline)") )
else if ( value.startsWith( "param(outline)" ) )
{
elem.setAttribute( attribute.name(), outline.name() );
}
else if( value.startsWith("params(outline-width)") )
else if ( value.startsWith( "param(outline-width)" ) )
{
elem.setAttribute( attribute.name(), QString::number( outlineWidth ) );
}
}

QDomNodeList childList = elem.childNodes();
int nChildren = childList.count();
for( int i = 0; i < nChildren; ++i )
for ( int i = 0; i < nChildren; ++i )
{
QDomElement childElem = childList.at( i ).toElement();
replaceElemParams( childElem, fill, outline, outlineWidth );
Expand Down
34 changes: 34 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Expand Up @@ -669,6 +669,40 @@ void QgsSvgMarkerSymbolLayerV2Widget::on_mFileLineEdit_textEdited( const QString
emit changed();
}

void QgsSvgMarkerSymbolLayerV2Widget::on_mChangeColorButton_clicked()
{
if ( !mLayer )
{
return;
}
QColor c = QColorDialog::getColor( mLayer->fillColor() );
if ( c.isValid() )
{
mLayer->setFillColor( c );
}
}

void QgsSvgMarkerSymbolLayerV2Widget::on_mChangeBorderColorButton_clicked()
{
if ( !mLayer )
{
return;
}
QColor c = QColorDialog::getColor( mLayer->outlineColor() );
if ( c.isValid() )
{
mLayer->setOutlineColor( c );
}
}

void QgsSvgMarkerSymbolLayerV2Widget::on_mBorderWidthSpinBox_valueChanged( double d )
{
if ( mLayer )
{
mLayer->setOutlineWidth( d );
}
}

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

QgsLineDecorationSymbolLayerV2Widget::QgsLineDecorationSymbolLayerV2Widget( QWidget* parent )
Expand Down
3 changes: 3 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.h
Expand Up @@ -181,6 +181,9 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerV2Widget : public QgsSymbolLayerV2Widget
void setOffset();
void on_mFileToolButton_clicked();
void on_mFileLineEdit_textEdited( const QString& text );
void on_mChangeColorButton_clicked();
void on_mChangeBorderColorButton_clicked();
void on_mBorderWidthSpinBox_valueChanged( double d );

protected:

Expand Down

0 comments on commit 49d8704

Please sign in to comment.