Skip to content

Commit bc61e9f

Browse files
committed
Notify dialog if svg accepts parameters
1 parent 49d8704 commit bc61e9f

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

src/core/symbology-ng/qgssvgcache.cpp

+57-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgssvgcache.h"
19+
#include "qgslogger.h"
1920
#include <QDomDocument>
2021
#include <QDomElement>
2122
#include <QFile>
@@ -66,14 +67,14 @@ QgsSvgCache* QgsSvgCache::instance()
6667
return mInstance;
6768
}
6869

69-
QgsSvgCache::QgsSvgCache()
70+
QgsSvgCache::QgsSvgCache(): mTotalSize( 0 )
7071
{
7172
}
7273

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

95-
//update lastUsed with current date time
96+
//debug: display current cache usage
97+
QgsDebugMsg("cache size: " + QString::number( mTotalSize ) );
98+
99+
//set entry timestamp to current time
100+
currentEntry->lastUsed = QDateTime::currentDateTime();
96101

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

112-
//update lastUsed with current date time
117+
//debug: display current cache usage
118+
QgsDebugMsg("cache size: " + QString::number( mTotalSize ) );
119+
120+
//set entry timestamp to current time
121+
currentEntry->lastUsed = QDateTime::currentDateTime();
113122

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

123132
replaceParamsAndCacheSvg( entry );
124133

125-
mEntries.insert( entry->lastUsed, entry );
126134
mEntryLookup.insert( file, entry );
127135
return entry;
128136
}
129137

138+
void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, bool& hasOutlineParam, bool& hasOutlineWidthParam ) const
139+
{
140+
hasFillParam = false;
141+
hasOutlineParam = false;
142+
hasOutlineWidthParam = false;
143+
144+
QFile svgFile( path );
145+
if ( !svgFile.open( QIODevice::ReadOnly ) )
146+
{
147+
return;
148+
}
149+
150+
QDomDocument svgDoc;
151+
if ( !svgDoc.setContent( &svgFile ) )
152+
{
153+
return;
154+
}
155+
156+
//there are surely faster ways to get this information
157+
QString content = svgDoc.toString();
158+
if( content.contains("param(fill") )
159+
{
160+
hasFillParam = true;
161+
}
162+
if( content.contains("param(outline") )
163+
{
164+
hasOutlineParam = true;
165+
}
166+
if( content.contains("param(outline-width)" ) )
167+
{
168+
hasOutlineWidthParam = true;
169+
}
170+
}
171+
130172
void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
131173
{
132174
if ( !entry )
@@ -151,6 +193,7 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
151193
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth );
152194

153195
entry->svgContent = svgDoc.toByteArray();
196+
mTotalSize += entry->svgContent.size();
154197
}
155198

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

174217
entry->image = image;
218+
mTotalSize += (image->width() * image->height() * 32);
175219
}
176220

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

200245
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
263308
}
264309
}
265310

311+
void QgsSvgCache::removeCacheEntry( QString s, QgsSvgCacheEntry* entry )
312+
{
313+
delete entry;
314+
mEntryLookup.remove( s , entry );
315+
}
316+

src/core/symbology-ng/qgssvgcache.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class QgsSvgCache
6666
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
6767
double widthScaleFactor, double rasterScaleFactor );
6868

69+
/**Tests if an svg file contains parameters for fill, outline color, outline width*/
70+
void containsParams( const QString& path, bool& hasFillParam, bool& hasOutlineParam, bool& hasOutlineWidthParam ) const;
71+
6972
protected:
7073
QgsSvgCache();
7174

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

86-
/**Entries sorted by last used time*/
87-
QMap< QDateTime, QgsSvgCacheEntry* > mEntries;
8889
/**Entry pointers accessible by file name*/
8990
QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
90-
/**Estimated total size of all images and pictures*/
91-
double mTotalSize;
91+
/**Estimated total size of all images, pictures and svgContent*/
92+
long mTotalSize;
9293
/**Replaces parameters in elements of a dom node and calls method for all child nodes*/
9394
void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
95+
96+
/**Release memory and remove cache entry from mEntryLookup*/
97+
void removeCacheEntry( QString s, QgsSvgCacheEntry* entry );
9498
};
9599

96100
#endif // QGSSVGCACHE_H

src/gui/symbology-ng/qgssymbollayerv2widget.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "characterwidget.h"
99
#include "qgsdashspacedialog.h"
1010
#include "qgssymbolv2propertiesdialog.h"
11+
#include "qgssvgcache.h"
1112

1213
#include "qgsapplication.h"
1314

@@ -595,6 +596,7 @@ void QgsSvgMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
595596
{
596597
selModel->select( idx, QItemSelectionModel::SelectCurrent );
597598
selModel->setCurrentIndex( idx, QItemSelectionModel::SelectCurrent );
599+
setName( idx );
598600
break;
599601
}
600602
}
@@ -623,6 +625,14 @@ void QgsSvgMarkerSymbolLayerV2Widget::setName( const QModelIndex& idx )
623625
QString name = idx.data( Qt::UserRole ).toString();
624626
mLayer->setPath( name );
625627
mFileLineEdit->setText( name );
628+
629+
//activate gui for svg parameters only if supported by the svg file
630+
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
631+
QgsSvgCache::instance()->containsParams( name, hasFillParam, hasOutlineParam, hasOutlineWidthParam );
632+
mChangeColorButton->setEnabled( hasFillParam );
633+
mChangeBorderColorButton->setEnabled( hasOutlineParam );
634+
mBorderWidthSpinBox->setEnabled( hasOutlineWidthParam );
635+
626636
emit changed();
627637
}
628638

0 commit comments

Comments
 (0)