QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgssvgcache.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssvgcache.h
3  ------------------------------
4  begin : 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSSVGCACHE_H
19 #define QGSSVGCACHE_H
20 
21 #include <QColor>
22 #include <QMap>
23 #include <QMultiHash>
24 #include <QMutex>
25 #include <QString>
26 #include <QUrl>
27 #include <QObject>
28 #include <QSizeF>
29 
30 class QDomElement;
31 class QImage;
32 class QPicture;
33 
37 class CORE_EXPORT QgsSvgCacheEntry
38 {
39  public:
51  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline, const QString& lookupKey = QString() );
53 
58  double size; //size in pixels (cast to int for QImage)
59  double outlineWidth;
62 
67 
72  //content (with params replaced)
74 
75  //keep entries on a least, sorted by last access
78 
80  bool operator==( const QgsSvgCacheEntry& other ) const;
82  int dataSize() const;
83 
84  private:
85 
87  QgsSvgCacheEntry& operator=( const QgsSvgCacheEntry& rh );
88 };
89 
95 class CORE_EXPORT QgsSvgCache : public QObject
96 {
97  Q_OBJECT
98 
99  public:
100 
101  static QgsSvgCache* instance();
102  ~QgsSvgCache();
103 
114  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
115  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
126  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
127  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
128 
140  QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
141  double widthScaleFactor, double rasterScaleFactor );
142 
145  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
146  double& defaultOutlineWidth ) const;
147 
164  Q_DECL_DEPRECATED void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
165  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
166  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth ) const;
167 
189  void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
190  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
191  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
192  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
193  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
194 
196  QByteArray getImageData( const QString &path ) const;
197 
199  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
200  double widthScaleFactor, double rasterScaleFactor );
201 
202  signals:
204  void statusChanged( const QString& theStatusQString );
205 
206  protected:
208  QgsSvgCache( QObject * parent = nullptr );
209 
220  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
221  double widthScaleFactor, double rasterScaleFactor );
222 
223  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
224  void cacheImage( QgsSvgCacheEntry* entry );
225  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
227  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
228  double widthScaleFactor, double rasterScaleFactor );
229 
231  void trimToMaximumSize();
232 
233  //Removes entry from the ordered list (but does not delete the entry itself)
234  void takeEntryFromList( QgsSvgCacheEntry* entry );
235 
236  private slots:
237  void downloadProgress( qint64, qint64 );
238 
239  private:
243  long mTotalSize;
244 
245  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
246  //That way, removing entries for more space can start with the least used objects.
247  QgsSvgCacheEntry* mLeastRecentEntry;
248  QgsSvgCacheEntry* mMostRecentEntry;
249 
250  //Maximum cache size
251  static const long mMaximumSize = 20000000;
252 
254  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
255 
256  void containsElemParams( const QDomElement& elem,
257  bool& hasFillParam, bool& hasDefaultFill, QColor& defaultFill,
258  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
259  bool& hasOutlineParam, bool& hasDefaultOutline, QColor& defaultOutline,
260  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
261  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
262 
264  double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const;
265 
267  void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );
268 
270  void printEntryList();
271 
273  QByteArray mMissingSvg;
274 
276  QMutex mMutex;
277 
278 };
279 
280 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:77
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:66
QImage * image
Definition: qgssvgcache.h:70
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:95
QPicture * picture
Definition: qgssvgcache.h:71
QString lookupKey
Lookup key used by QgsSvgCache&#39;s hashtable (relative or absolute path). Needed for removal from the h...
Definition: qgssvgcache.h:57
double outlineWidth
Definition: qgssvgcache.h:59
double rasterScaleFactor
Definition: qgssvgcache.h:61
double widthScaleFactor
Definition: qgssvgcache.h:60
QString file
Absolute path to SVG file.
Definition: qgssvgcache.h:55
QByteArray svgContent
Definition: qgssvgcache.h:73
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:76