16
16
***************************************************************************/
17
17
18
18
#include " qgssvgcache.h"
19
+ #include < QDomDocument>
20
+ #include < QFile>
21
+ #include < QImage>
22
+ #include < QPicture>
23
+
24
+ QgsSvgCacheEntry::QgsSvgCacheEntry (): file( QString() ), size( 0 ), outlineWidth( 0 ), widthScaleFactor( 1.0 ), rasterScaleFactor( 1.0 ), fill( Qt::black ),
25
+ outline( Qt::black ), image( 0 ), picture( 0 )
26
+ {
27
+ }
28
+
29
+ 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 ),
30
+ widthScaleFactor( wsf ), rasterScaleFactor( rsf ), fill( fi ), outline( ou ), image( 0 ), picture( 0 )
31
+ {
32
+ }
33
+
34
+
35
+ QgsSvgCacheEntry::~QgsSvgCacheEntry ()
36
+ {
37
+ delete image;
38
+ delete picture;
39
+ }
40
+
41
+ bool QgsSvgCacheEntry::operator ==( const QgsSvgCacheEntry& other ) const
42
+ {
43
+ return ( other.file == file && other.size == size && other.outlineWidth == outlineWidth && other.widthScaleFactor == widthScaleFactor
44
+ && other.rasterScaleFactor == rasterScaleFactor && other.fill == fill && other.outline == outline );
45
+ }
46
+
47
+ QString file;
48
+ double size;
49
+ double outlineWidth;
50
+ double widthScaleFactor;
51
+ double rasterScaleFactor;
52
+ QColor fill;
53
+ QColor outline ;
19
54
20
55
QgsSvgCache* QgsSvgCache::mInstance = 0 ;
21
56
@@ -37,23 +72,93 @@ QgsSvgCache::~QgsSvgCache()
37
72
}
38
73
39
74
40
- const QImage& QgsSvgCache::svgAsImage ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth ) const
75
+ const QImage& QgsSvgCache::svgAsImage ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth,
76
+ double widthScaleFactor, double rasterScaleFactor )
77
+ {
78
+
79
+ }
80
+
81
+ const QPicture& QgsSvgCache::svgAsPicture ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth,
82
+ double widthScaleFactor, double rasterScaleFactor )
41
83
{
84
+ // search entries in mEntryLookup
85
+ QgsSvgCacheEntry* currentEntry = 0 ;
86
+ QList<QgsSvgCacheEntry*> entries = mEntryLookup .values ( file );
87
+
88
+ QList<QgsSvgCacheEntry*>::iterator entryIt = entries.begin ();
89
+ for (; entryIt != entries.end (); ++entryIt )
90
+ {
91
+ QgsSvgCacheEntry* cacheEntry = *entryIt;
92
+ if ( cacheEntry->file == file && cacheEntry->size == size && cacheEntry->fill == fill && cacheEntry->outline == outline &&
93
+ cacheEntry->outlineWidth == outlineWidth && cacheEntry->widthScaleFactor == widthScaleFactor && cacheEntry->rasterScaleFactor == rasterScaleFactor)
94
+ {
95
+ currentEntry = cacheEntry;
96
+ break ;
97
+ }
98
+ }
99
+
100
+
101
+ // if not found: create new entry
102
+ // cache and replace params in svg content
103
+ if ( !currentEntry )
104
+ {
105
+ currentEntry = insertSVG ( file, size, fill, outline , outlineWidth, widthScaleFactor, rasterScaleFactor );
106
+ }
107
+
108
+ // if current entry image is 0: cache image for entry
109
+ // update stats for memory usage
110
+ if ( !currentEntry->picture )
111
+ {
112
+ cachePicture ( currentEntry );
113
+ }
114
+
115
+ // update lastUsed with current date time
116
+
117
+ return *( currentEntry->picture );
42
118
}
43
119
44
- const QPicture& QgsSvgCache::svgAsPicture ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth ) const
120
+ QgsSvgCacheEntry* QgsSvgCache::insertSVG ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth,
121
+ double widthScaleFactor, double rasterScaleFactor )
45
122
{
123
+ QgsSvgCacheEntry* entry = new QgsSvgCacheEntry ( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
124
+ entry->lastUsed = QDateTime::currentDateTime ();
125
+
126
+ replaceParamsAndCacheSvg ( entry );
127
+
128
+ mEntries .insert ( entry->lastUsed , entry );
129
+ mEntryLookup .insert ( file, entry );
130
+ return entry;
46
131
}
47
132
48
- void QgsSvgCache::insertSVG ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth )
133
+ void QgsSvgCache::replaceParamsAndCacheSvg ( QgsSvgCacheEntry* entry )
49
134
{
135
+ if ( !entry )
136
+ {
137
+ return ;
138
+ }
139
+
140
+ QFile svgFile ( entry->file );
141
+ if ( !svgFile.open ( QIODevice::ReadOnly ) )
142
+ {
143
+ return ;
144
+ }
145
+
146
+ QDomDocument svgDoc;
147
+ if ( !svgDoc.setContent ( &svgFile ) )
148
+ {
149
+ return ;
150
+ }
151
+
152
+ // todo: replace params here
153
+
154
+ entry->svgContent = svgDoc.toByteArray ();
50
155
}
51
156
52
- void QgsSvgCache::cacheImage ( QgsSvgCacheEntry )
157
+ void QgsSvgCache::cacheImage ( QgsSvgCacheEntry* entry )
53
158
{
54
159
}
55
160
56
- void QgsSvgCache::cachePicture ( QgsSvgCacheEntry )
161
+ void QgsSvgCache::cachePicture ( QgsSvgCacheEntry *entry )
57
162
{
58
163
}
59
164
0 commit comments