QGIS API Documentation  2.99.0-Master (5b186ae)
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  private:
89 #ifdef SIP_RUN
91 #endif
92 
93 };
94 
103 class CORE_EXPORT QgsSvgCache : public QObject
104 {
105  Q_OBJECT
106 
107  public:
108 
112  QgsSvgCache( QObject *parent SIP_TRANSFERTHIS = 0 );
113 
114  ~QgsSvgCache();
115 
125  QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
126  double widthScaleFactor, bool &fitsInCache );
127 
137  QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
138  double widthScaleFactor, bool forceVectorOutput = false );
139 
150  QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
151  double widthScaleFactor );
152 
155  void containsParams( const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam,
156  double &defaultStrokeWidth ) const;
157 
179  void containsParams( const QString &path, bool &hasFillParam, bool &hasDefaultFillParam, QColor &defaultFillColor,
180  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
181  bool &hasStrokeParam, bool &hasDefaultStrokeColor, QColor &defaultStrokeColor,
182  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
183  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
184 
186  QByteArray getImageData( const QString &path ) const;
187 
189  QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
190  double widthScaleFactor );
191 
192  signals:
194  void statusChanged( const QString &statusQString );
195 
196  protected:
197 
206  QgsSvgCacheEntry *insertSvg( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
207  double widthScaleFactor );
208 
209  void replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry );
210  void cacheImage( QgsSvgCacheEntry *entry );
211  void cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput = false );
213  QgsSvgCacheEntry *cacheEntry( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
214  double widthScaleFactor );
215 
217  void trimToMaximumSize();
218 
219  //Removes entry from the ordered list (but does not delete the entry itself)
220  void takeEntryFromList( QgsSvgCacheEntry *entry );
221 
222  private slots:
223  void downloadProgress( qint64, qint64 );
224 
225  private:
227  QMultiHash< QString, QgsSvgCacheEntry * > mEntryLookup;
229  long mTotalSize;
230 
231  //The svg cache keeps the entries on a double connected list, moving the current entry to the front.
232  //That way, removing entries for more space can start with the least used objects.
233  QgsSvgCacheEntry *mLeastRecentEntry = nullptr;
234  QgsSvgCacheEntry *mMostRecentEntry = nullptr;
235 
237  static const long MAXIMUM_SIZE = 20000000;
238 
240  void replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth );
241 
242  void containsElemParams( const QDomElement &elem,
243  bool &hasFillParam, bool &hasDefaultFill, QColor &defaultFill,
244  bool &hasFillOpacityParam, bool &hasDefaultFillOpacity, double &defaultFillOpacity,
245  bool &hasStrokeParam, bool &hasDefaultStroke, QColor &defaultStroke,
246  bool &hasStrokeWidthParam, bool &hasDefaultStrokeWidth, double &defaultStrokeWidth,
247  bool &hasStrokeOpacityParam, bool &hasDefaultStrokeOpacity, double &defaultStrokeOpacity ) const SIP_PYNAME( containsParamsV3 );
248 
250  double calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const;
251 
253  void removeCacheEntry( const QString &s, QgsSvgCacheEntry *entry );
254 
256  void printEntryList();
257 
259  QByteArray mMissingSvg;
260 
262  QMutex mMutex;
263 
264 };
265 
266 #endif // QGSSVGCACHE_H
QSizeF viewboxSize
SVG viewbox size.
Definition: qgssvgcache.h:70
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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:103
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:74