QGIS API Documentation  2.99.0-Master (c42dad3)
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:
41 
52  QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFactor, const QColor& fill, const QColor& outline, const QString& lookupKey = QString() );
54 
56  QString file;
58  QString lookupKey;
59  double size; //size in pixels (cast to int for QImage)
60  double outlineWidth;
63 
67  QSizeF viewboxSize;
68 
69  QColor fill;
70  QColor outline;
71  QImage* image;
72  QPicture* picture;
73  //content (with params replaced)
74  QByteArray svgContent;
75 
76  //keep entries on a least, sorted by last access
79 
81  bool operator==( const QgsSvgCacheEntry& other ) const;
83  int dataSize() const;
84 
85  private:
86 
88  QgsSvgCacheEntry& operator=( const QgsSvgCacheEntry& rh );
89 };
90 
96 class CORE_EXPORT QgsSvgCache : public QObject
97 {
98  Q_OBJECT
99 
100  public:
101 
102  static QgsSvgCache* instance();
103  ~QgsSvgCache();
104 
115  const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
116  double widthScaleFactor, double rasterScaleFactor, bool& fitsInCache );
117 
128  const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
129  double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
130 
142  QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
143  double widthScaleFactor, double rasterScaleFactor );
144 
147  void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
148  double& defaultOutlineWidth ) const;
149 
171  void containsParams( const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor,
172  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
173  bool& hasOutlineParam, bool& hasDefaultOutlineColor, QColor& defaultOutlineColor,
174  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
175  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
176 
178  QByteArray getImageData( const QString &path ) const;
179 
181  const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
182  double widthScaleFactor, double rasterScaleFactor );
183 
184  signals:
186  void statusChanged( const QString& theStatusQString );
187 
188  protected:
190  QgsSvgCache( QObject * parent = nullptr );
191 
202  QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
203  double widthScaleFactor, double rasterScaleFactor );
204 
205  void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
206  void cacheImage( QgsSvgCacheEntry* entry );
207  void cachePicture( QgsSvgCacheEntry* entry, bool forceVectorOutput = false );
209  QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
210  double widthScaleFactor, double rasterScaleFactor );
211 
213  void trimToMaximumSize();
214 
215  //Removes entry from the ordered list (but does not delete the entry itself)
216  void takeEntryFromList( QgsSvgCacheEntry* entry );
217 
218  private slots:
219  void downloadProgress( qint64, qint64 );
220 
221  private:
223  QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
225  long mTotalSize;
226 
227  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
228  //That way, removing entries for more space can start with the least used objects.
229  QgsSvgCacheEntry* mLeastRecentEntry;
230  QgsSvgCacheEntry* mMostRecentEntry;
231 
232  //Maximum cache size
233  static const long mMaximumSize = 20000000;
234 
236  void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
237 
238  void containsElemParams( const QDomElement& elem,
239  bool& hasFillParam, bool& hasDefaultFill, QColor& defaultFill,
240  bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity,
241  bool& hasOutlineParam, bool& hasDefaultOutline, QColor& defaultOutline,
242  bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth,
243  bool& hasOutlineOpacityParam, bool& hasDefaultOutlineOpacity, double& defaultOutlineOpacity ) const;
244 
246  double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const;
247 
249  void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );
250 
252  void printEntryList();
253 
255  QByteArray mMissingSvg;
256 
258  QMutex mMutex;
259 
260 };
261 
262 #endif // QGSSVGCACHE_H
QgsSvgCacheEntry * previousEntry
Definition: qgssvgcache.h:78
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:67
QImage * image
Definition: qgssvgcache.h:71
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:96
QPicture * picture
Definition: qgssvgcache.h:72
QString lookupKey
Lookup key used by QgsSvgCache&#39;s hashtable (relative or absolute path). Needed for removal from the h...
Definition: qgssvgcache.h:58
double outlineWidth
Definition: qgssvgcache.h:60
double rasterScaleFactor
Definition: qgssvgcache.h:62
double widthScaleFactor
Definition: qgssvgcache.h:61
QString file
Absolute path to SVG file.
Definition: qgssvgcache.h:56
QByteArray svgContent
Definition: qgssvgcache.h:74
QgsSvgCacheEntry * nextEntry
Definition: qgssvgcache.h:77