QGIS API Documentation  2.99.0-Master (d55fa22)
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 "qgis.h"
23 #include <QMap>
24 #include <QMultiHash>
25 #include <QMutex>
26 #include <QString>
27 #include <QUrl>
28 #include <QObject>
29 #include <QSizeF>
30 
31 #include "qgis_core.h"
32 
33 class QDomElement;
34 class QImage;
35 class QPicture;
36 
40 class CORE_EXPORT QgsSvgCacheEntry
41 {
42  public:
44 
53  QgsSvgCacheEntry( const QString &path, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke );
55 
57  QgsSvgCacheEntry( const QgsSvgCacheEntry &rh ) = delete;
59  QgsSvgCacheEntry &operator=( const QgsSvgCacheEntry &rh ) = delete;
60 
62  QString path;
63  double size; //size in pixels (cast to int for QImage)
64  double strokeWidth;
66 
70  QSizeF viewboxSize;
71 
72  QColor fill;
73  QColor stroke;
74  QImage *image = nullptr;
75  QPicture *picture = nullptr;
76  //content (with params replaced)
77  QByteArray svgContent;
78 
79  //keep entries on a least, sorted by last access
80  QgsSvgCacheEntry *nextEntry = nullptr;
81  QgsSvgCacheEntry *previousEntry = nullptr;
82 
84  bool operator==( const QgsSvgCacheEntry &other ) const;
86  int dataSize() const;
87 
88 };
89 
98 class CORE_EXPORT QgsSvgCache : public QObject
99 {
100  Q_OBJECT
101 
102  public:
103 
107  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = 0 );
108 
109  ~QgsSvgCache();
110 
120  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
121  double widthScaleFactor, bool &fitsInCache );
122 
132  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
133  double widthScaleFactor, bool forceVectorOutput = false );
134 
145  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
146  double widthScaleFactor );
147 
150  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
151  double &defaultStrokeWidth ) const;
152 
174  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
175  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
176  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
177  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
178  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
179 
181  QByteArray getImageData( const QString &path ) const;
182 
184  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
185  double widthScaleFactor );
186 
187  signals:
189  void statusChanged( const QString &statusQString );
190 
191  protected:
192 
201  QgsSvgCacheEntry *insertSvg( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
202  double widthScaleFactor );
203 
204  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry );
205  void cacheImage( QgsSvgCacheEntry *entry );
206  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
208  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
209  double widthScaleFactor );
210 
212  void trimToMaximumSize();
213 
214  //Removes entry from the ordered list (but does not delete the entry itself)
215  void takeEntryFromList( QgsSvgCacheEntry *entry );
216 
217  private slots:
218  void downloadProgress( qint64, qint64 );
219 
220  private:
222  QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup;
224  long mTotalSize;
225 
226  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
227  //That way, removing entries for more space can start with the least used objects.
228  QgsSvgCacheEntry *mLeastRecentEntry = nullptr;
229  QgsSvgCacheEntry *mMostRecentEntry = nullptr;
230 
232  static const long MAXIMUM_SIZE = 20000000;
233 
235  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
236 
237  void containsElemParams( const QDomElement &elem,
238  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
239  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
240  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
241  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
242  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
243 
245  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
246 
248  void removeCacheEntry( const QString &s, QgsSvgCacheEntry *entry );
249 
251  void printEntryList();
252 
254  QByteArray mMissingSvg;
255 
257  QMutex mMutex;
258 
259 };
260 
261 #endif // QGSSVGCACHE_H
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:70
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double strokeWidth
Definition: qgssvgcache.h:64
A cache for images / pictures derived from svg files.
Definition: qgssvgcache.h:98
QString path
Absolute path to SVG file.
Definition: qgssvgcache.h:62
double widthScaleFactor
Definition: qgssvgcache.h:65
QByteArray svgContent
Definition: qgssvgcache.h:77
#define SIP_PYNAME(name)
Definition: qgis_sip.h:62