Skip to content

Commit

Permalink
Notify dialog if svg accepts parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 25, 2011
1 parent 49d8704 commit bc61e9f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
63 changes: 57 additions & 6 deletions src/core/symbology-ng/qgssvgcache.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgssvgcache.h"
#include "qgslogger.h"
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
Expand Down Expand Up @@ -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();
}
Expand All @@ -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 );
}
Expand All @@ -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 );
}
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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,
Expand Down Expand Up @@ -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 );
}

12 changes: 8 additions & 4 deletions src/core/symbology-ng/qgssvgcache.h
Expand Up @@ -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();

Expand All @@ -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
10 changes: 10 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Expand Up @@ -8,6 +8,7 @@
#include "characterwidget.h"
#include "qgsdashspacedialog.h"
#include "qgssymbolv2propertiesdialog.h"
#include "qgssvgcache.h"

#include "qgsapplication.h"

Expand Down Expand Up @@ -595,6 +596,7 @@ void QgsSvgMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
{
selModel->select( idx, QItemSelectionModel::SelectCurrent );
selModel->setCurrentIndex( idx, QItemSelectionModel::SelectCurrent );
setName( idx );
break;
}
}
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit bc61e9f

Please sign in to comment.