QGIS API Documentation  2.13.0-Master
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 
163 
164 
186  void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
187  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
188  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
189  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
190  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
191 
193  QByteArray getImageData( const QString &path ) const;
194 
196  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
197  double widthScaleFactor, double rasterScaleFactor );
198 
199  signals:
201  void statusChanged( const QString& theStatusQString );
202 
203  protected:
205  QgsSvgCache( QObject * parent = nullptr );
206 
217  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
218  double widthScaleFactor, double rasterScaleFactor );
219 
220  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
221  void cacheImage( QgsSvgCacheEntry* entry );
222  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
224  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
225  double widthScaleFactor, double rasterScaleFactor );
226 
228  void trimToMaximumSize();
229 
230  //Removes entry from the ordered list (but does not delete the entry itself)
231  void takeEntryFromList( QgsSvgCacheEntry* entry );
232 
233  private slots:
234  void downloadProgress( qint64, qint64 );
235 
236  private:
240  long mTotalSize;
241 
242  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
243  //That way, removing entries for more space can start with the least used objects.
244  QgsSvgCacheEntry* mLeastRecentEntry;
245  QgsSvgCacheEntry* mMostRecentEntry;
246 
247  //Maximum cache size
248  static const long mMaximumSize = 20000000;
249 
251  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
252 
253  void containsElemParams( const QDomElement& elem,
254  bool& hasFillParam, bool& hasDefaultFill, QColor& defaultFill,
255  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
256  bool& hasOutlineParam, bool& hasDefaultOutline, QColor& defaultOutline,
257  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
258  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
259 
261  double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const;
262 
264  void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );
265 
267  void printEntryList();
268 
270  QByteArray mMissingSvg;
271 
273  QMutex mMutex;
274 
275 };
276 
277 #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