QGIS API Documentation  2.14.0-Essen
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 
34 class CORE_EXPORT QgsSvgCacheEntry
35 {
36  public:
48  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline, const QString& lookupKey = QString() );
50 
55  double size; //size in pixels (cast to int for QImage)
56  double outlineWidth;
59 
64 
69  //content (with params replaced)
71 
72  //keep entries on a least, sorted by last access
75 
77  bool operator==( const QgsSvgCacheEntry& other ) const;
79  int dataSize() const;
80 
81  private:
82 
84  QgsSvgCacheEntry& operator=( const QgsSvgCacheEntry& rh );
85 };
86 
90 class CORE_EXPORT QgsSvgCache : public QObject
91 {
92  Q_OBJECT
93 
94  public:
95 
96  static QgsSvgCache* instance();
97  ~QgsSvgCache();
98 
109  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
110  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
121  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
122  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
123 
135  QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
136  double widthScaleFactor, double rasterScaleFactor );
137 
140  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
141  double& defaultOutlineWidth ) const;
142 
159  Q_DECL_DEPRECATED void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
160  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
161  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth ) const;
162 
184  void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
185  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
186  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
187  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
188  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
189 
191  QByteArray getImageData( const QString &path ) const;
192 
194  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
195  double widthScaleFactor, double rasterScaleFactor );
196 
197  signals:
199  void statusChanged( const QString& theStatusQString );
200 
201  protected:
203  QgsSvgCache( QObject * parent = nullptr );
204 
215  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
216  double widthScaleFactor, double rasterScaleFactor );
217 
218  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
219  void cacheImage( QgsSvgCacheEntry* entry );
220  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
222  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
223  double widthScaleFactor, double rasterScaleFactor );
224 
226  void trimToMaximumSize();
227 
228  //Removes entry from the ordered list (but does not delete the entry itself)
229  void takeEntryFromList( QgsSvgCacheEntry* entry );
230 
231  private slots:
232  void downloadProgress( qint64, qint64 );
233 
234  private:
238  long mTotalSize;
239 
240  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
241  //That way, removing entries for more space can start with the least used objects.
242  QgsSvgCacheEntry* mLeastRecentEntry;
243  QgsSvgCacheEntry* mMostRecentEntry;
244 
245  //Maximum cache size
246  static const long mMaximumSize = 20000000;
247 
249  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
250 
251  void containsElemParams( const QDomElement& elem,
252  bool& hasFillParam, bool& hasDefaultFill, QColor& defaultFill,
253  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
254  bool& hasOutlineParam, bool& hasDefaultOutline, QColor& defaultOutline,
255  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
256  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
257 
259  double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const;
260 
262  void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );
263 
265  void printEntryList();
266 
268  QByteArray mMissingSvg;
269 
271  QMutex mMutex;
272 
273 };
274 
275 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:74
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:63
QImage * image
Definition: qgssvgcache.h:67
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:90
QPicture * picture
Definition: qgssvgcache.h:68
QString lookupKey
Lookup key used by QgsSvgCache&#39;s hashtable (relative or absolute path). Needed for removal from the h...
Definition: qgssvgcache.h:54
double outlineWidth
Definition: qgssvgcache.h:56
double rasterScaleFactor
Definition: qgssvgcache.h:58
double widthScaleFactor
Definition: qgssvgcache.h:57
QString file
Absolute path to SVG file.
Definition: qgssvgcache.h:52
QByteArray svgContent
Definition: qgssvgcache.h:70
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:73