QGIS API Documentation  2.99.0-Master (314842d)
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 #include "qgis_core.h"
31 
32 class QDomElement;
33 class QImage;
34 class QPicture;
35 
39 class CORE_EXPORT QgsSvgCacheEntry
40 {
41  public:
43 
53  QgsSvgCacheEntry( const QString &file, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke, const QString &lookupKey = QString() );
55 
57  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
59  QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
60 
62  QString file;
64  QString lookupKey;
65  double size; //size in pixels (cast to int for QImage)
66  double strokeWidth;
68 
72  QSizeF viewboxSize;
73 
74  QColor fill;
75  QColor stroke;
76  QImage *image = nullptr;
77  QPicture *picture = nullptr;
78  //content (with params replaced)
79  QByteArray svgContent;
80 
81  //keep entries on a least, sorted by last access
82  QgsSvgCacheEntry *nextEntry = nullptr;
83  QgsSvgCacheEntry *previousEntry = nullptr;
84 
86  bool operator==( const QgsSvgCacheEntry &other ) const;
88  int dataSize() const;
89 
90 };
91 
100 class CORE_EXPORT QgsSvgCache : public QObject
101 {
102  Q_OBJECT
103 
104  public:
105 
109  QgsSvgCache( QObject *parent = nullptr );
110 
111  ~QgsSvgCache();
112 
122  QImage svgAsImage( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
123  double widthScaleFactor, bool &fitsInCache );
124 
134  QPicture svgAsPicture( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
135  double widthScaleFactor, bool forceVectorOutput = false );
136 
147  QSizeF svgViewboxSize( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
148  double widthScaleFactor );
149 
152  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
153  double &defaultStrokeWidth ) const;
154 
176  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
177  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
178  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
179  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
180  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const;
181 
183  QByteArray getImageData( const QString &path ) const;
184 
186  QByteArray svgContent( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
187  double widthScaleFactor );
188 
189  signals:
191  void statusChanged( const QString &statusQString );
192 
193  protected:
194 
204  QgsSvgCacheEntry *insertSVG( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
205  double widthScaleFactor );
206 
207  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry );
208  void cacheImage( QgsSvgCacheEntry *entry );
209  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
211  QgsSvgCacheEntry *cacheEntry( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
212  double widthScaleFactor );
213 
215  void trimToMaximumSize();
216 
217  //Removes entry from the ordered list (but does not delete the entry itself)
218  void takeEntryFromList( QgsSvgCacheEntry *entry );
219 
220  private slots:
221  void downloadProgress( qint64, qint64 );
222 
223  private:
225  QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup;
227  long mTotalSize;
228 
229  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
230  //That way, removing entries for more space can start with the least used objects.
231  QgsSvgCacheEntry *mLeastRecentEntry = nullptr;
232  QgsSvgCacheEntry *mMostRecentEntry = nullptr;
233 
235  static const long MAXIMUM_SIZE = 20000000;
236 
238  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
239 
240  void containsElemParams( const QDomElement &elem,
241  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
242  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
243  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
244  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
245  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const;
246 
248  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
249 
251  void removeCacheEntry( const QString &s, QgsSvgCacheEntry *entry );
252 
254  void printEntryList();
255 
257  QByteArray mMissingSvg;
258 
260  QMutex mMutex;
261 
262 };
263 
264 #endif // QGSSVGCACHE_H
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:72
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double strokeWidth
Definition: qgssvgcache.h:66
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:100
QString lookupKey
Lookup key used by QgsSvgCache&#39;s hashtable (relative or absolute path). Needed for removal from the h...
Definition: qgssvgcache.h:64
double widthScaleFactor
Definition: qgssvgcache.h:67
QString file
Absolute path to SVG file.
Definition: qgssvgcache.h:62
QByteArray svgContent
Definition: qgssvgcache.h:79