Skip to content
Permalink
Browse files

Notify dialog if svg accepts parameters

  • Loading branch information
mhugent committed Jun 25, 2011
1 parent 49d8704 commit bc61e9fa6ca664b468562bab9ceb6ed32a3a801f
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgssvgcache.h"
#include "qgslogger.h"
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
@@ -66,14 +67,14 @@ QgsSvgCache* QgsSvgCache::instance()
return mInstance;
}

QgsSvgCache::QgsSvgCache()
QgsSvgCache::QgsSvgCache(): mTotalSize( 0 )
{
}

QgsSvgCache::~QgsSvgCache()
{
QMap< QDateTime, QgsSvgCacheEntry* >::iterator it = mEntries.begin();
for ( ; it != mEntries.end(); ++it )
QMultiHash< QString, QgsSvgCacheEntry* >::iterator it = mEntryLookup.begin();
for( ; it != mEntryLookup.end(); ++it )
{
delete it.value();
}
@@ -92,7 +93,11 @@ const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const Q
cacheImage( currentEntry );
}

//update lastUsed with current date time
//debug: display current cache usage
QgsDebugMsg("cache size: " + QString::number( mTotalSize ) );

//set entry timestamp to current time
currentEntry->lastUsed = QDateTime::currentDateTime();

return *( currentEntry->image );
}
@@ -109,7 +114,11 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
cachePicture( currentEntry );
}

//update lastUsed with current date time
//debug: display current cache usage
QgsDebugMsg("cache size: " + QString::number( mTotalSize ) );

//set entry timestamp to current time
currentEntry->lastUsed = QDateTime::currentDateTime();

return *( currentEntry->picture );
}
@@ -122,11 +131,44 @@ QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, cons

replaceParamsAndCacheSvg( entry );

mEntries.insert( entry->lastUsed, entry );
mEntryLookup.insert( file, entry );
return entry;
}

void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, bool& hasOutlineParam, bool& hasOutlineWidthParam ) const
{
hasFillParam = false;
hasOutlineParam = false;
hasOutlineWidthParam = false;

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

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

//there are surely faster ways to get this information
QString content = svgDoc.toString();
if( content.contains("param(fill") )
{
hasFillParam = true;
}
if( content.contains("param(outline") )
{
hasOutlineParam = true;
}
if( content.contains("param(outline-width)" ) )
{
hasOutlineWidthParam = true;
}
}

void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
{
if ( !entry )
@@ -151,6 +193,7 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth );

entry->svgContent = svgDoc.toByteArray();
mTotalSize += entry->svgContent.size();
}

void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
@@ -172,6 +215,7 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
r.render( &p );

entry->image = image;
mTotalSize += (image->width() * image->height() * 32);
}

void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
@@ -195,6 +239,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
QPainter painter( picture );
renderer.render( &painter, rect );
entry->picture = picture;
mTotalSize += entry->picture->size();
}

QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
@@ -263,3 +308,9 @@ void QgsSvgCache::replaceElemParams( QDomElement& elem, const QColor& fill, cons
}
}

void QgsSvgCache::removeCacheEntry( QString s, QgsSvgCacheEntry* entry )
{
delete entry;
mEntryLookup.remove( s , entry );
}

@@ -66,6 +66,9 @@ 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;

protected:
QgsSvgCache();

@@ -83,14 +86,15 @@ class QgsSvgCache
private:
static QgsSvgCache* mInstance;

/**Entries sorted by last used time*/
QMap< QDateTime, QgsSvgCacheEntry* > mEntries;
/**Entry pointers accessible by file name*/
QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
/**Estimated total size of all images and pictures*/
double mTotalSize;
/**Estimated total size of all images, pictures and svgContent*/
long mTotalSize;
/**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 );

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

#endif // QGSSVGCACHE_H
@@ -8,6 +8,7 @@
#include "characterwidget.h"
#include "qgsdashspacedialog.h"
#include "qgssymbolv2propertiesdialog.h"
#include "qgssvgcache.h"

#include "qgsapplication.h"

@@ -595,6 +596,7 @@ void QgsSvgMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
{
selModel->select( idx, QItemSelectionModel::SelectCurrent );
selModel->setCurrentIndex( idx, QItemSelectionModel::SelectCurrent );
setName( idx );
break;
}
}
@@ -623,6 +625,14 @@ void QgsSvgMarkerSymbolLayerV2Widget::setName( const QModelIndex& idx )
QString name = idx.data( Qt::UserRole ).toString();
mLayer->setPath( name );
mFileLineEdit->setText( name );

//activate gui for svg parameters only if supported by the svg file
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
QgsSvgCache::instance()->containsParams( name, hasFillParam, hasOutlineParam, hasOutlineWidthParam );
mChangeColorButton->setEnabled( hasFillParam );
mChangeBorderColorButton->setEnabled( hasOutlineParam );
mBorderWidthSpinBox->setEnabled( hasOutlineWidthParam );

emit changed();
}

0 comments on commit bc61e9f

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